From 5bd79bf8f905ff63a2e6e4f60c3aab0bf0cce34c Mon Sep 17 00:00:00 2001 From: = Date: Tue, 30 Nov 2021 10:21:39 +0800 Subject: [PATCH 01/17] add dynamically adjust log level --- .../examples/{log => telemetry}/DemoLog.java | 70 +- pom.xml | 826 +++++++++--------- sdk-component/pom.xml | 7 + .../log/agent/CapaLog4jAppenderAgent.java | 1 + .../log/agent/CapaLogbackAppenderAgent.java | 157 ++-- .../telemetry/log/manager/CapaLogLevel.java | 30 + .../telemetry/log/manager/LogsManager.java | 9 + 7 files changed, 575 insertions(+), 525 deletions(-) rename examples/src/main/java/group/rxcloud/capa/examples/{log => telemetry}/DemoLog.java (95%) create mode 100644 sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/log/manager/CapaLogLevel.java create mode 100644 sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/log/manager/LogsManager.java diff --git a/examples/src/main/java/group/rxcloud/capa/examples/log/DemoLog.java b/examples/src/main/java/group/rxcloud/capa/examples/telemetry/DemoLog.java similarity index 95% rename from examples/src/main/java/group/rxcloud/capa/examples/log/DemoLog.java rename to examples/src/main/java/group/rxcloud/capa/examples/telemetry/DemoLog.java index 20ac7e2..e75463b 100644 --- a/examples/src/main/java/group/rxcloud/capa/examples/log/DemoLog.java +++ b/examples/src/main/java/group/rxcloud/capa/examples/telemetry/DemoLog.java @@ -1,35 +1,35 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package group.rxcloud.capa.examples.log; - -import lombok.extern.slf4j.Slf4j; - -/** - * An application cannot use log4j and logback configuration to print logs at the same time. - * So if you want to test the log4j2 configuration to print logs, then you need to copy the resources/xml/log4j2.xml file to the resources directory, and then add log4j-slf4j-impl dependency to the pom file. - * Else if you want to use logback configuration to print logs, then the resources/xml/logback.xml file needs to be copied to the resources path, and the logback-classic dependency needs to be added to the pom file. - * Notice: - * 1. Resources cannot contain log4j2.xml and logback.xml files at the same time, - * 2. log4j-slf4j-impl and logback-classic cannot exist at the same time. - */ -@Slf4j -public class DemoLog { - - public static void main(String[] args) { - log.info("test"); - } -} +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package group.rxcloud.capa.examples.telemetry; + +import lombok.extern.slf4j.Slf4j; + +/** + * An application cannot use log4j and logback configuration to print logs at the same time. + * So if you want to test the log4j2 configuration to print logs, then you need to copy the resources/xml/log4j2.xml file to the resources directory, and then add log4j-slf4j-impl dependency to the pom file. + * Else if you want to use logback configuration to print logs, then the resources/xml/logback.xml file needs to be copied to the resources path, and the logback-classic dependency needs to be added to the pom file. + * Notice: + * 1. Resources cannot contain log4j2.xml and logback.xml files at the same time, + * 2. log4j-slf4j-impl and logback-classic cannot exist at the same time. + */ +@Slf4j +public class DemoLog { + + public static void main(String[] args) { + log.info("test"); + } +} diff --git a/pom.xml b/pom.xml index c45dd65..546f707 100644 --- a/pom.xml +++ b/pom.xml @@ -1,414 +1,414 @@ - - - - 4.0.0 - - group.rxcloud - capa-parent - pom - 1.0.7-beta-2 - capa-sdk-parent - SDK for Capa. - https://github.com/reactivegroup - - - - The Apache Software License, Version 2.0 - http://www.apache.org/licenses/LICENSE-2.0.txt - - - - - - kevinten10 - 596823919@qq.com - - - - - scm:git:git@github.com:reactivegroup/capa.git - scm:git:git@github.com:reactivegroup/capa.git - git@github.com:reactivegroup/capa.git - - - - - ossrh - https://s01.oss.sonatype.org/content/repositories/snapshots - - - ossrh - https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/ - - - - - examples - sdk-springboot - sdk-spi-demo - sdk-spi - sdk - sdk-component - sdk-infrastructure - - - - 8 - UTF-8 - 3.8.1 - 1.0.8.RELEASE - 3.3.22.RELEASE - 1.7.21 - 1.9.0 - 1.9.0-alpha - - 5.3.1 - 3.6.0 - 3.1.2 - 0.13 - 2.7 - 3.2.0 - 3.0.0-M1 - 0.8.6 - true - true - true - - - - - - - group.rxcloud - capa-sdk-spi-demo - ${project.version} - - - group.rxcloud - capa-sdk-spi - ${project.version} - - - group.rxcloud - capa-sdk - ${project.version} - - - group.rxcloud - capa-sdk-component - ${project.version} - - - group.rxcloud - capa-sdk-infrastructure - ${project.version} - - - - - group.rxcloud - cloud-runtimes-api - ${cloud-runtimes.version} - - - - - io.projectreactor - reactor-core - ${reactor-core.version} - true - - - - - org.slf4j - slf4j-api - ${slf4j.version} - - - - - io.opentelemetry - opentelemetry-api - ${open.telemetry.version} - - - io.opentelemetry - opentelemetry-api-metrics - ${open.telemetry.version.alpha} - - - - - org.junit.jupiter - junit-jupiter-engine - ${junit.version} - test - - - org.mockito - mockito-core - ${mockito-core.version} - test - - - - - - - - jacoco - - false - - - - - rat - - false - - - - - checkstyle - - false - - - - - - - - org.apache.maven.plugins - maven-compiler-plugin - ${maven.version} - - ${java.version} - ${java.version} - ${file.encoding} - - - - org.apache.maven.plugins - maven-resources-plugin - ${maven.resources.version} - - UTF-8 - - - - copy-checkstyle - validate - - copy-resources - - - ${basedir}/target/codestyle - - - ${basedir}/codestyle - - * - - - - - - - - - org.apache.maven.plugins - maven-source-plugin - 3.2.1 - - - attach-sources - - jar-no-fork - - - - - - org.apache.maven.plugins - maven-checkstyle-plugin - ${checkstyle.version} - - - com.puppycrawl.tools - checkstyle - 8.41 - - - - - checkstyle-validation - validate - - codestyle/checkstyle.xml - codestyle/checkstyle-suppressions.xml - UTF-8 - true - true - ${checkstyle.skip} - - **/generated/**/*, - **/target/**/*, - **/*.json - **/*.sh - - - - check - - - - - - org.apache.rat - apache-rat-plugin - ${apache.rat.version} - - - rat-validate - validate - - check - - - - - - **/*.versionsBackup - **/.idea/ - **/*.iml - **/*.txt - **/*.load - **/*.flex - **/*.fc - **/*.properties - **/*.sh - **/*.bat - **/*.md - .git/ - .gitignore - - .repository/ - **/.settings/* - **/.classpath - **/.project - **/target/** - **/*.log - .codecov.yml - .travis.yml - PULL_REQUEST_TEMPLATE.md - CONTRIBUTING.md - README.md - Jenkinsfile - **/codestyle/* - **/resources/META-INF/** - .github/** - codestyle/** - docs/** - spec/** - **/generated/** - **/*.json - - - - - org.codehaus.mojo - cobertura-maven-plugin - ${cobertura.maven.version} - - - html - xml - - - - - - org.jacoco - jacoco-maven-plugin - ${maven.jacoco.version} - - - - prepare-agent - - prepare-agent - - - - - generate-report - - report - - - - - - org.apache.maven.plugins - maven-javadoc-plugin - 3.2.0 - - - attach-javadocs - - jar - - - - - - org.apache.maven.plugins - maven-gpg-plugin - 1.6 - - - sign-artifacts - verify - - sign - - - - - - org.sonatype.plugins - nexus-staging-maven-plugin - 1.6.7 - true - - ossrh - https://s01.oss.sonatype.org/ - true - - - - - + + + + 4.0.0 + + group.rxcloud + capa-parent + pom + 1.0.7-beta-3 + capa-sdk-parent + SDK for Capa. + https://github.com/reactivegroup + + + + The Apache Software License, Version 2.0 + http://www.apache.org/licenses/LICENSE-2.0.txt + + + + + + kevinten10 + 596823919@qq.com + + + + + scm:git:git@github.com:reactivegroup/capa.git + scm:git:git@github.com:reactivegroup/capa.git + git@github.com:reactivegroup/capa.git + + + + + ossrh + https://s01.oss.sonatype.org/content/repositories/snapshots + + + ossrh + https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/ + + + + + examples + sdk-springboot + sdk-spi-demo + sdk-spi + sdk + sdk-component + sdk-infrastructure + + + + 8 + UTF-8 + 3.8.1 + 1.0.8.RELEASE + 3.3.22.RELEASE + 1.7.21 + 1.9.0 + 1.9.0-alpha + + 5.3.1 + 3.6.0 + 3.1.2 + 0.13 + 2.7 + 3.2.0 + 3.0.0-M1 + 0.8.6 + true + true + true + + + + + + + group.rxcloud + capa-sdk-spi-demo + ${project.version} + + + group.rxcloud + capa-sdk-spi + ${project.version} + + + group.rxcloud + capa-sdk + ${project.version} + + + group.rxcloud + capa-sdk-component + ${project.version} + + + group.rxcloud + capa-sdk-infrastructure + ${project.version} + + + + + group.rxcloud + cloud-runtimes-api + ${cloud-runtimes.version} + + + + + io.projectreactor + reactor-core + ${reactor-core.version} + true + + + + + org.slf4j + slf4j-api + ${slf4j.version} + + + + + io.opentelemetry + opentelemetry-api + ${open.telemetry.version} + + + io.opentelemetry + opentelemetry-api-metrics + ${open.telemetry.version.alpha} + + + + + org.junit.jupiter + junit-jupiter-engine + ${junit.version} + test + + + org.mockito + mockito-core + ${mockito-core.version} + test + + + + + + + + jacoco + + false + + + + + rat + + false + + + + + checkstyle + + false + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven.version} + + ${java.version} + ${java.version} + ${file.encoding} + + + + org.apache.maven.plugins + maven-resources-plugin + ${maven.resources.version} + + UTF-8 + + + + copy-checkstyle + validate + + copy-resources + + + ${basedir}/target/codestyle + + + ${basedir}/codestyle + + * + + + + + + + + + org.apache.maven.plugins + maven-source-plugin + 3.2.1 + + + attach-sources + + jar-no-fork + + + + + + org.apache.maven.plugins + maven-checkstyle-plugin + ${checkstyle.version} + + + com.puppycrawl.tools + checkstyle + 8.41 + + + + + checkstyle-validation + validate + + codestyle/checkstyle.xml + codestyle/checkstyle-suppressions.xml + UTF-8 + true + true + ${checkstyle.skip} + + **/generated/**/*, + **/target/**/*, + **/*.json + **/*.sh + + + + check + + + + + + org.apache.rat + apache-rat-plugin + ${apache.rat.version} + + + rat-validate + validate + + check + + + + + + **/*.versionsBackup + **/.idea/ + **/*.iml + **/*.txt + **/*.load + **/*.flex + **/*.fc + **/*.properties + **/*.sh + **/*.bat + **/*.md + .git/ + .gitignore + + .repository/ + **/.settings/* + **/.classpath + **/.project + **/target/** + **/*.log + .codecov.yml + .travis.yml + PULL_REQUEST_TEMPLATE.md + CONTRIBUTING.md + README.md + Jenkinsfile + **/codestyle/* + **/resources/META-INF/** + .github/** + codestyle/** + docs/** + spec/** + **/generated/** + **/*.json + + + + + org.codehaus.mojo + cobertura-maven-plugin + ${cobertura.maven.version} + + + html + xml + + + + + + org.jacoco + jacoco-maven-plugin + ${maven.jacoco.version} + + + + prepare-agent + + prepare-agent + + + + + generate-report + + report + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 3.2.0 + + + attach-javadocs + + jar + + + + + + org.apache.maven.plugins + maven-gpg-plugin + 1.6 + + + sign-artifacts + verify + + sign + + + + + + org.sonatype.plugins + nexus-staging-maven-plugin + 1.6.7 + true + + ossrh + https://s01.oss.sonatype.org/ + true + + + + + \ No newline at end of file diff --git a/sdk-component/pom.xml b/sdk-component/pom.xml index 714aee9..895a561 100644 --- a/sdk-component/pom.xml +++ b/sdk-component/pom.xml @@ -98,6 +98,13 @@ true ${logback.version} + + ch.qos.logback + logback-classic + + true + ${logback.version} + org.junit.jupiter diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/log/agent/CapaLog4jAppenderAgent.java b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/log/agent/CapaLog4jAppenderAgent.java index 4cdf775..74ff0f3 100644 --- a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/log/agent/CapaLog4jAppenderAgent.java +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/log/agent/CapaLog4jAppenderAgent.java @@ -97,6 +97,7 @@ public static CapaLog4jAppender buildCapaLog4jAppender() { @Override public void append(LogEvent event) { + event.getLevel(); logAppender.appendLog(event); } diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/log/agent/CapaLogbackAppenderAgent.java b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/log/agent/CapaLogbackAppenderAgent.java index 31c7423..3c84ae7 100644 --- a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/log/agent/CapaLogbackAppenderAgent.java +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/log/agent/CapaLogbackAppenderAgent.java @@ -1,77 +1,80 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package group.rxcloud.capa.component.telemetry.log.agent; - -import ch.qos.logback.core.UnsynchronizedAppenderBase; -import group.rxcloud.capa.infrastructure.CapaClassLoader; - -/** - * The agent of the logback impl. - */ -public class CapaLogbackAppenderAgent extends UnsynchronizedAppenderBase { - - /** - * The log component type. - */ - private static final String LOG_COMPONENT_TYPE = "log"; - /** - * Capa logback appender instance. - */ - private static final CapaLogbackAppender logbackAppender; - - /** - * Init the logbackAppender impl. - */ - static { - logbackAppender = buildCapaLogbackAppender(); - } - - /** - * Build the logback appender impl. - * - * @return CapaLogbackAppender instance. - */ - public static CapaLogbackAppender buildCapaLogbackAppender() { - // load spi capa logback appender impl - return CapaClassLoader.loadComponentClassObj( - LOG_COMPONENT_TYPE, - CapaLogbackAppender.class); - } - - /** - * Deal with the log. - * - * @param event The log event. - */ - @Override - protected void append(EVENT event) { - logbackAppender.appendLog(event); - } - - /** - * The abstract api of the logback appender impl.Implement this and provide your specific impl. - */ - public interface CapaLogbackAppender { - - /** - * Deal with the log. - * - * @param event The log event. - */ - void appendLog(EVENT event); - } -} +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package group.rxcloud.capa.component.telemetry.log.agent; + +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.UnsynchronizedAppenderBase; +import group.rxcloud.capa.infrastructure.CapaClassLoader; +import jdk.jfr.internal.LogLevel; + +/** + * The agent of the logback impl. + */ +public class CapaLogbackAppenderAgent extends UnsynchronizedAppenderBase { + + /** + * The log component type. + */ + private static final String LOG_COMPONENT_TYPE = "log"; + /** + * Capa logback appender instance. + */ + private static final CapaLogbackAppender logbackAppender; + + /** + * Init the logbackAppender impl. + */ + static { + logbackAppender = buildCapaLogbackAppender(); + } + + /** + * Build the logback appender impl. + * + * @return CapaLogbackAppender instance. + */ + public static CapaLogbackAppender buildCapaLogbackAppender() { + // load spi capa logback appender impl + return CapaClassLoader.loadComponentClassObj( + LOG_COMPONENT_TYPE, + CapaLogbackAppender.class); + } + + /** + * Deal with the log. + * + * @param event The log event. + */ + @Override + protected void append(ILoggingEvent event) { + event.getLevel(); + logbackAppender.appendLog(event); + } + + /** + * The abstract api of the logback appender impl.Implement this and provide your specific impl. + */ + public interface CapaLogbackAppender { + + /** + * Deal with the log. + * + * @param event The log event. + */ + void appendLog(ILoggingEvent event); + } +} diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/log/manager/CapaLogLevel.java b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/log/manager/CapaLogLevel.java new file mode 100644 index 0000000..fed2c89 --- /dev/null +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/log/manager/CapaLogLevel.java @@ -0,0 +1,30 @@ +package group.rxcloud.capa.component.telemetry.log.manager; + +import java.util.Arrays; + +public enum CapaLogLevel { + OFF(1, "OFF"), + FATAL(2, "FATAL"), + TRACE(3, "TRACE"), + DEBUG(4, "DEBUG"), + INFO(5, "INFO"), + WARN(6, "WARN"), + ERROR(7, "ERROR"), + ALL(8, "ALL"); + + final int level; + final String levelName; + + + CapaLogLevel(int level, String levelName) { + this.level = level; + this.levelName = levelName; + } + + public CapaLogLevel toCapaLogLevel(String logLevelArg) { + return Arrays.stream(CapaLogLevel.values()) + .filter(logLevel -> logLevel.levelName.equalsIgnoreCase(logLevelArg)) + .findAny() + .orElseThrow(() -> new IllegalArgumentException("Level " + logLevelArg + " is unknown.")); + } +} diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/log/manager/LogsManager.java b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/log/manager/LogsManager.java new file mode 100644 index 0000000..1c43b28 --- /dev/null +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/log/manager/LogsManager.java @@ -0,0 +1,9 @@ +package group.rxcloud.capa.component.telemetry.log.manager; + + +public class LogsManager { + + public static boolean isLogLevelClosed(CapaLogLevel logLevel) { + return false; + } +} From 1964c222be6773dac5d08f9746c20dd4a8450775 Mon Sep 17 00:00:00 2001 From: chenyijiang Date: Tue, 30 Nov 2021 19:35:44 +0800 Subject: [PATCH 02/17] Fix Histogram Metrics. Add sample config. --- examples/pom.xml | 2 +- pom.xml | 2 +- sdk-component/pom.xml | 2 +- .../component/telemetry/SamplerConfig.java | 87 +++++++++++-------- .../metrics/CapaDoubleHistogram.java | 47 ++++++++++ .../metrics/CapaDoubleHistogramBuilder.java | 76 ++++++++++++++++ .../telemetry/metrics/CapaLongHistogram.java | 45 ++++++++++ .../metrics/CapaLongHistogramBuilder.java | 75 ++++++++++++++++ .../telemetry/metrics/CapaMeter.java | 72 +++++++++++++++ .../telemetry/metrics/CapaMeterBuilder.java | 56 ++++++++++++ .../telemetry/metrics/CapaMeterProvider.java | 46 ++++++++++ .../metrics/CapaMeterProviderBuilder.java | 26 +++--- .../metrics/CapaMeterProviderSettings.java | 4 +- .../telemetry/metrics/CapaMeterWrapper.java | 65 ++++++++++++++ .../metrics/CapaMetricsExporter.java | 66 ++++++++++++++ .../telemetry/metrics/CapaMetricsSampler.java | 59 ++++--------- .../telemetry/trace/CapaSpanBuilder.java | 2 +- .../telemetry/trace/CapaTraceSampler.java | 34 +++----- .../component/telemetry/trace/CapaTracer.java | 2 +- .../telemetry/trace/CapaTracerBuilder.java | 2 +- .../telemetry/trace/CapaTracerProvider.java | 2 +- .../trace/CapaTracerProviderBuilder.java | 14 +-- .../trace/CapaTracerProviderSettings.java | 4 +- ...apaWrapper.java => CapaTracerWrapper.java} | 4 +- .../metrics/CapaMeterProviderBuilderTest.java | 12 +-- .../metrics/CapaMetricsSamplerTest.java | 39 +++------ .../metrics/TestMetricsExporter.java | 14 ++- .../telemetry/trace/CapaTraceSamplerTest.java | 35 ++++---- .../trace/CapaTracerProviderBuilderTest.java | 2 +- .../src/test/resources/capa-sample.properties | 2 - sdk-infrastructure/pom.xml | 2 +- sdk-spi-demo/pom.xml | 2 +- sdk-spi/pom.xml | 2 +- .../spi/telemetry/CapaDoubleHistogramSpi.java | 28 ++++++ .../spi/telemetry/CapaLongHistogramSpi.java | 29 +++++++ .../spi/telemetry/CapaMetricsExporterSpi.java | 33 +++++++ sdk-springboot/pom.xml | 2 +- sdk/pom.xml | 2 +- .../telemetry/CapaTelemetryClientBuilder.java | 4 +- .../CapaTelemetryClientBuilderTest.java | 2 +- .../capa/telemetry/MetricTestExporter.java | 15 +++- 41 files changed, 810 insertions(+), 209 deletions(-) create mode 100644 sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaDoubleHistogram.java create mode 100644 sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaDoubleHistogramBuilder.java create mode 100644 sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaLongHistogram.java create mode 100644 sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaLongHistogramBuilder.java create mode 100644 sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMeter.java create mode 100644 sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMeterBuilder.java create mode 100644 sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMeterProvider.java create mode 100644 sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMeterWrapper.java create mode 100644 sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMetricsExporter.java rename sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/trace/{CapaWrapper.java => CapaTracerWrapper.java} (98%) delete mode 100644 sdk-component/src/test/resources/capa-sample.properties create mode 100644 sdk-spi/src/main/java/group/rxcloud/capa/spi/telemetry/CapaDoubleHistogramSpi.java create mode 100644 sdk-spi/src/main/java/group/rxcloud/capa/spi/telemetry/CapaLongHistogramSpi.java create mode 100644 sdk-spi/src/main/java/group/rxcloud/capa/spi/telemetry/CapaMetricsExporterSpi.java diff --git a/examples/pom.xml b/examples/pom.xml index 739a0b7..40f3206 100644 --- a/examples/pom.xml +++ b/examples/pom.xml @@ -23,7 +23,7 @@ capa-parent group.rxcloud - 1.0.7.RELEASE + 1.0.8-alpha-1 capa-examples diff --git a/pom.xml b/pom.xml index b8f7b9d..5fb5bf2 100644 --- a/pom.xml +++ b/pom.xml @@ -23,7 +23,7 @@ group.rxcloud capa-parent pom - 1.0.7.RELEASE + 1.0.8-alpha-1 capa-sdk-parent SDK for Capa. https://github.com/reactivegroup diff --git a/sdk-component/pom.xml b/sdk-component/pom.xml index 22941a3..a669e53 100644 --- a/sdk-component/pom.xml +++ b/sdk-component/pom.xml @@ -23,7 +23,7 @@ group.rxcloud capa-parent - 1.0.7.RELEASE + 1.0.8-alpha-1 capa-sdk-component diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/SamplerConfig.java b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/SamplerConfig.java index 702962e..f53b99b 100644 --- a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/SamplerConfig.java +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/SamplerConfig.java @@ -16,66 +16,81 @@ */ package group.rxcloud.capa.component.telemetry; -import group.rxcloud.capa.infrastructure.utils.SpiUtils; +import group.rxcloud.capa.component.telemetry.metrics.CapaMeterProviderBuilder; +import group.rxcloud.capa.infrastructure.CapaProperties; +import group.rxcloud.capa.infrastructure.hook.ConfigurationHooks; +import group.rxcloud.capa.infrastructure.hook.Mixer; +import group.rxcloud.cloudruntimes.domain.core.configuration.ConfigurationItem; +import group.rxcloud.cloudruntimes.utils.TypeRef; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.Serializable; -import java.util.Properties; +import java.util.Collections; +import java.util.List; +import java.util.Optional; +import java.util.function.Supplier; /** * Sampler config. */ public class SamplerConfig implements Serializable { - public static final String FILE_PATH = "/capa-sample.properties"; + public static final transient String FILE_PATH = "capa-sample.properties"; /** * Sample all data as default. */ public static final transient SamplerConfig DEFAULT_CONFIG = new SamplerConfig(); - private static final long serialVersionUID = -2113523925814197551L; - - private boolean metricsSample = true; + private static final transient Logger log = LoggerFactory.getLogger(CapaMeterProviderBuilder.class); + + public static final transient Supplier DEFAULT_SUPPLIER = () -> { + try { + String storeName = Optional.ofNullable(CapaProperties.COMPONENT_PROPERTIES_SUPPLIER.apply("configuration") + .getProperty( + "CONFIGURATION_COMPONENT_STORE_NAME")) + .orElse("UN_CONFIGURED_STORE_CONFIG_NAME"); + Optional hooksOptional = Mixer.configurationHooksNullable(); + if (hooksOptional.isPresent()) { + List> config = hooksOptional.get().getConfiguration(storeName, + null, + Collections.singletonList(FILE_PATH), + null, + "", + "", + TypeRef.get(SamplerConfig.class)).block(); + if (!config.isEmpty()) { + SamplerConfig item = config.get(0).getContent(); + return item == null ? DEFAULT_CONFIG : item; + } + } + } catch (Throwable throwable) { + log.warn("Fail to load config item. Dynamic config is disabled for capa telemetry.", throwable); + } - private boolean traceSample = true; + return DEFAULT_CONFIG; + }; - private boolean logSample = true; + private static final long serialVersionUID = -2113523925814197551L; - public boolean isMetricsSample() { - return metricsSample; - } + private boolean metricsEnable = true; - public void setMetricsSample(boolean metricsSample) { - this.metricsSample = metricsSample; - } + private boolean traceEnable = true; - public boolean isTraceSample() { - return traceSample; + public boolean isMetricsEnable() { + return metricsEnable; } - public void setTraceSample(boolean traceSample) { - this.traceSample = traceSample; + public void setMetricsEnable(boolean metricsEnable) { + this.metricsEnable = metricsEnable; } - public boolean isLogSample() { - return logSample; + public boolean isTraceEnable() { + return traceEnable; } - public void setLogSample(boolean logSample) { - this.logSample = logSample; - } - - public static SamplerConfig loadOrDefault() { - Properties properties = SpiUtils.loadPropertiesNullable(FILE_PATH); - if (properties == null) { - return DEFAULT_CONFIG; - } - - SamplerConfig result = new SamplerConfig(); - result.setMetricsSample(Boolean.valueOf(properties.getProperty("metricsSample", Boolean.TRUE.toString()))); - result.setTraceSample(Boolean.valueOf(properties.getProperty("traceSample", Boolean.TRUE.toString()))); - result.setLogSample(Boolean.valueOf(properties.getProperty("logSample", Boolean.TRUE.toString()))); - - return result; + public void setTraceEnable(boolean traceEnable) { + this.traceEnable = traceEnable; } } diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaDoubleHistogram.java b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaDoubleHistogram.java new file mode 100644 index 0000000..4828001 --- /dev/null +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaDoubleHistogram.java @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package group.rxcloud.capa.component.telemetry.metrics; + +import io.opentelemetry.api.metrics.DoubleHistogram; + +/** + */ +public abstract class CapaDoubleHistogram implements DoubleHistogram { + + protected final String meterName; + + protected final String schemaUrl; + + protected final String version; + + protected final String name; + + protected String description; + + protected String unit; + + public CapaDoubleHistogram(String meterName, String schemaUrl, String version, String name, + String description, + String unit) { + this.meterName = meterName; + this.schemaUrl = schemaUrl; + this.version = version; + this.name = name; + this.description = description; + this.unit = unit; + } +} diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaDoubleHistogramBuilder.java b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaDoubleHistogramBuilder.java new file mode 100644 index 0000000..6fafd87 --- /dev/null +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaDoubleHistogramBuilder.java @@ -0,0 +1,76 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package group.rxcloud.capa.component.telemetry.metrics; + +import io.opentelemetry.api.metrics.DoubleHistogram; +import io.opentelemetry.api.metrics.DoubleHistogramBuilder; +import io.opentelemetry.api.metrics.LongHistogramBuilder; +import io.opentelemetry.api.metrics.internal.NoopMeter; + +/** + * + */ +public class CapaDoubleHistogramBuilder implements DoubleHistogramBuilder { + + protected final String meterName; + + protected final String schemaUrl; + + protected final String version; + + private final String name; + + private String description; + + private String unit; + + public CapaDoubleHistogramBuilder(String meterName, String schemaUrl, String version, String name) { + this.meterName = meterName; + this.schemaUrl = schemaUrl; + this.version = version; + this.name = name; + } + + @Override + public DoubleHistogramBuilder setDescription(String description) { + this.description = description; + return this; + } + + @Override + public DoubleHistogramBuilder setUnit(String unit) { + this.unit = unit; + return this; + } + + @Override + public LongHistogramBuilder ofLongs() { + return new CapaLongHistogramBuilder(meterName, schemaUrl, version, name).setDescription(description) + .setUnit(unit); + } + + @Override + public DoubleHistogram build() { + DoubleHistogram histogram = CapaMeterWrapper + .loadHistogramNullable(meterName, schemaUrl, version, name, description, unit, + CapaDoubleHistogram.class); + if (histogram == null) { + return NoopMeter.getInstance().histogramBuilder(name).build(); + } + return histogram; + } +} diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaLongHistogram.java b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaLongHistogram.java new file mode 100644 index 0000000..a3d750d --- /dev/null +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaLongHistogram.java @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package group.rxcloud.capa.component.telemetry.metrics; + +import io.opentelemetry.api.metrics.LongHistogram; + +/** + */ +public abstract class CapaLongHistogram implements LongHistogram { + + protected final String meterName; + + protected final String schemaUrl; + + protected final String version; + + protected final String name; + + protected String description; + + protected String unit; + + public CapaLongHistogram(String meterName, String schemaUrl, String version, String name, String description, String unit) { + this.meterName = meterName; + this.schemaUrl = schemaUrl; + this.version = version; + this.name = name; + this.description = description; + this.unit = unit; + } +} diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaLongHistogramBuilder.java b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaLongHistogramBuilder.java new file mode 100644 index 0000000..508c943 --- /dev/null +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaLongHistogramBuilder.java @@ -0,0 +1,75 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package group.rxcloud.capa.component.telemetry.metrics; + +import io.opentelemetry.api.metrics.DoubleHistogramBuilder; +import io.opentelemetry.api.metrics.LongHistogram; +import io.opentelemetry.api.metrics.LongHistogramBuilder; +import io.opentelemetry.api.metrics.internal.NoopMeter; + +/** + * + */ +public class CapaLongHistogramBuilder implements LongHistogramBuilder { + + private final String meterName; + + private final String schemaUrl; + + private final String version; + + private final String name; + + private String description; + + private String unit; + + public CapaLongHistogramBuilder(String meterName, String schemaUrl, String version, String name) { + this.meterName = meterName; + this.schemaUrl = schemaUrl; + this.version = version; + this.name = name; + } + + @Override + public LongHistogramBuilder setDescription(String description) { + this.description = description; + return this; + } + + @Override + public LongHistogramBuilder setUnit(String unit) { + this.unit = unit; + return this; + } + + @Override + public DoubleHistogramBuilder ofDoubles() { + return new CapaDoubleHistogramBuilder(meterName, schemaUrl, version, name).setDescription(description) + .setUnit(unit); + } + + @Override + public LongHistogram build() { + LongHistogram histogram = CapaMeterWrapper + .loadHistogramNullable(meterName, schemaUrl, version, name, description, unit, CapaLongHistogram.class); + if (histogram == null) { + return NoopMeter.getInstance().histogramBuilder(name).ofLongs().build(); + } + return histogram; + } +} diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMeter.java b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMeter.java new file mode 100644 index 0000000..050ba10 --- /dev/null +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMeter.java @@ -0,0 +1,72 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package group.rxcloud.capa.component.telemetry.metrics; + +import io.opentelemetry.api.metrics.DoubleGaugeBuilder; +import io.opentelemetry.api.metrics.DoubleHistogramBuilder; +import io.opentelemetry.api.metrics.LongCounterBuilder; +import io.opentelemetry.api.metrics.LongUpDownCounterBuilder; +import io.opentelemetry.api.metrics.Meter; + +import javax.annotation.concurrent.NotThreadSafe; + +/** + */ +@NotThreadSafe +public class CapaMeter implements Meter { + + private final String meterName; + + private final String schemaUrl; + + private final String version; + + private final Meter meter; + + public CapaMeter(String meterName, String schemaUrl, String version, Meter meter) { + this.meterName = meterName; + this.schemaUrl = schemaUrl; + this.version = version; + this.meter = meter; + } + + @Override + public LongCounterBuilder counterBuilder(String name) { + return meter.counterBuilder(name); + } + + @Override + public LongUpDownCounterBuilder upDownCounterBuilder(String name) { + return meter.upDownCounterBuilder(name); + } + + /** + * Only histogram need self inplementation now. + * + * @param name metric name. + * @return CapaDoubleHistogramBuilder + */ + @Override + public DoubleHistogramBuilder histogramBuilder(String name) { + return new CapaDoubleHistogramBuilder(meterName, schemaUrl, version, name); + } + + @Override + public DoubleGaugeBuilder gaugeBuilder(String name) { + return meter.gaugeBuilder(name); + } +} diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMeterBuilder.java b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMeterBuilder.java new file mode 100644 index 0000000..330a827 --- /dev/null +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMeterBuilder.java @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package group.rxcloud.capa.component.telemetry.metrics; + +import io.opentelemetry.api.metrics.Meter; +import io.opentelemetry.api.metrics.MeterBuilder; + +/** + * Builder for capa meter. + */ +public class CapaMeterBuilder implements MeterBuilder { + + private final MeterBuilder meterBuilder; + + private final String name; + + private String schemaUrl; + + private String version; + + public CapaMeterBuilder(String name, MeterBuilder builder) { + this.name = name; + meterBuilder = builder; + } + + @Override + public MeterBuilder setSchemaUrl(String schemaUrl) { + this.schemaUrl = schemaUrl; + return meterBuilder.setSchemaUrl(schemaUrl); + } + + @Override + public MeterBuilder setInstrumentationVersion(String instrumentationVersion) { + version = instrumentationVersion; + return meterBuilder.setInstrumentationVersion(instrumentationVersion); + } + + @Override + public Meter build() { + return CapaMeterWrapper.wrap(name, schemaUrl, version, meterBuilder.build()); + } +} diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMeterProvider.java b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMeterProvider.java new file mode 100644 index 0000000..38957b5 --- /dev/null +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMeterProvider.java @@ -0,0 +1,46 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package group.rxcloud.capa.component.telemetry.metrics; + +import io.opentelemetry.api.metrics.Meter; +import io.opentelemetry.api.metrics.MeterBuilder; +import io.opentelemetry.api.metrics.MeterProvider; + +import javax.annotation.concurrent.NotThreadSafe; + +/** + * Builder for capa meter provider. + */ +@NotThreadSafe +public class CapaMeterProvider implements MeterProvider { + + private final MeterProvider meterProvider; + + public CapaMeterProvider(MeterProvider meterProvider) { + this.meterProvider = meterProvider; + } + + @Override + public Meter get(String instrumentationName) { + return CapaMeterWrapper.wrap(instrumentationName, null, null, meterProvider.get(instrumentationName)); + } + + @Override + public MeterBuilder meterBuilder(String instrumentationName) { + return CapaMeterWrapper.wrap(instrumentationName, meterProvider.meterBuilder(instrumentationName)); + } +} diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMeterProviderBuilder.java b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMeterProviderBuilder.java index 044eb92..160bd28 100644 --- a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMeterProviderBuilder.java +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMeterProviderBuilder.java @@ -32,6 +32,7 @@ import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.ThreadFactory; import java.util.concurrent.TimeUnit; +import java.util.function.Supplier; /** * Builder for capa metric provider. @@ -47,7 +48,7 @@ public class CapaMeterProviderBuilder implements CapaMeterProviderSettings { /** * Sampler config. */ - private SamplerConfig samplerConfig; + private Supplier samplerConfig = SamplerConfig.DEFAULT_SUPPLIER; /** * Readers manually set. @@ -61,11 +62,13 @@ public class CapaMeterProviderBuilder implements CapaMeterProviderSettings { * @param readerConfigs metrics reader configs. * @return metrics reader factories. */ - private static List bulidReaderFactories(List readerConfigs) { + private static List bulidReaderFactories(List readerConfigs, + Supplier samplerConfig) { List factories = new ArrayList<>(); for (MetricsReaderConfig config : readerConfigs) { MetricExporter exporter = SpiUtils - .newInstanceWithConstructorCache(config.getExporterType(), MetricExporter.class); + .newInstance(config.getExporterType(), CapaMetricsExporter.class, new Class[]{Supplier.class}, + new Object[]{samplerConfig}, false); if (exporter == null) { throw new IllegalArgumentException( "Metric Exporter is not configured. readerName = " + config.getName() + '.'); @@ -91,7 +94,7 @@ public Thread newThread(@NotNull Runnable r) { } @Override - public CapaMeterProviderBuilder setSamplerConfig(SamplerConfig samplerConfig) { + public CapaMeterProviderBuilder setSamplerConfig(Supplier samplerConfig) { this.samplerConfig = samplerConfig; return this; } @@ -133,15 +136,14 @@ public MeterProvider buildMeterProvider() { return MeterProvider.noop(); } - List factories = bulidReaderFactories(metricsReaderConfigs); + List factories = bulidReaderFactories(metricsReaderConfigs, samplerConfig); - initSampleConfig(); SdkMeterProviderBuilder builder = SdkMeterProvider.builder() - .setExemplarFilter(CapaMetricsSampler.getInstance() - .update(samplerConfig)); + .setExemplarFilter(new CapaMetricsSampler(samplerConfig)); factories.forEach(f -> builder.registerMetricReader(f)); - return builder.build(); + SdkMeterProvider provider = builder.build(); + return new CapaMeterProvider(provider); } private void initMeterConfig() { @@ -149,10 +151,4 @@ private void initMeterConfig() { meterConfigs = SpiUtils.loadConfigNullable(FILE_PATH, MeterConfig.class); } } - - private void initSampleConfig() { - if (samplerConfig == null) { - samplerConfig = SamplerConfig.loadOrDefault(); - } - } } diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMeterProviderSettings.java b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMeterProviderSettings.java index 01967a6..e190690 100644 --- a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMeterProviderSettings.java +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMeterProviderSettings.java @@ -18,6 +18,8 @@ import group.rxcloud.capa.component.telemetry.SamplerConfig; +import java.util.function.Supplier; + /** * Settings for capa meter provider. */ @@ -48,5 +50,5 @@ public interface CapaMeterProviderSettings { * @param samplerConfig sample config. * @return current settings. */ - CapaMeterProviderSettings setSamplerConfig(SamplerConfig samplerConfig); + CapaMeterProviderSettings setSamplerConfig(Supplier samplerConfig); } diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMeterWrapper.java b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMeterWrapper.java new file mode 100644 index 0000000..07b6e75 --- /dev/null +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMeterWrapper.java @@ -0,0 +1,65 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package group.rxcloud.capa.component.telemetry.metrics; + +import group.rxcloud.capa.infrastructure.utils.SpiUtils; +import io.opentelemetry.api.metrics.Meter; +import io.opentelemetry.api.metrics.MeterBuilder; + +import javax.annotation.Nullable; + +/** + * Load capa implementation. + */ +final class CapaMeterWrapper { + + static final String FILE_SUFFIX = "telemetry"; + + static final boolean CACHE = true; + + private CapaMeterWrapper() { + } + + @Nullable + static CapaMeter wrap(String meterName, String schemaUrl, String version, Meter meter) { + if (meter instanceof CapaMeter) { + return (CapaMeter) meter; + } + CapaMeter result = SpiUtils.loadFromSpiComponentFileNullable(CapaMeter.class, + new Class[]{String.class, String.class, String.class, Meter.class}, + new Object[]{meterName, schemaUrl, version, meter}, FILE_SUFFIX, CACHE); + if (result == null) { + result = new CapaMeter(meterName, schemaUrl, version, meter); + } + return result; + } + + @Nullable + static CapaMeterBuilder wrap(String meterName, MeterBuilder meterBuilder) { + if (meterBuilder instanceof CapaMeterBuilder) { + return (CapaMeterBuilder) meterBuilder; + } + return new CapaMeterBuilder(meterName, meterBuilder); + } + + static T loadHistogramNullable(String meterName, String schemaUrl, String version, String name, String description, String unit, Class type) { + return SpiUtils.loadFromSpiComponentFileNullable( + type, new Class[]{String.class, String.class, String.class, String.class, String.class, String.class}, new Object[]{meterName, schemaUrl, version, name, description, unit}, + "telemetry", true); + } +} + diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMetricsExporter.java b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMetricsExporter.java new file mode 100644 index 0000000..d3e6025 --- /dev/null +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMetricsExporter.java @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package group.rxcloud.capa.component.telemetry.metrics; + +import group.rxcloud.capa.component.telemetry.SamplerConfig; +import io.opentelemetry.sdk.common.CompletableResultCode; +import io.opentelemetry.sdk.metrics.data.MetricData; +import io.opentelemetry.sdk.metrics.export.MetricExporter; + +import java.util.Collection; +import java.util.function.Supplier; + +/** + * + */ +public abstract class CapaMetricsExporter implements MetricExporter { + + private Supplier samplerConfig; + + public CapaMetricsExporter(Supplier samplerConfig) { + this.samplerConfig = samplerConfig; + } + + @Override + public CompletableResultCode export(Collection metrics) { + SamplerConfig config = samplerConfig.get(); + if (config != null && !config.isMetricsEnable()) { + return CompletableResultCode.ofSuccess(); + } + + return doExport(metrics); + } + + @Override + public CompletableResultCode flush() { + SamplerConfig config = samplerConfig.get(); + if (config != null && !config.isMetricsEnable()) { + return CompletableResultCode.ofSuccess(); + } + + return doFlush(); + } + + protected abstract CompletableResultCode doExport(Collection metrics); + + protected abstract CompletableResultCode doFlush(); + + @Override + public CompletableResultCode shutdown() { + return null; + } +} diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMetricsSampler.java b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMetricsSampler.java index 049a7a0..eda0109 100644 --- a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMetricsSampler.java +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMetricsSampler.java @@ -21,61 +21,40 @@ import io.opentelemetry.context.Context; import io.opentelemetry.sdk.metrics.exemplar.ExemplarFilter; +import java.util.function.Supplier; + /** * Sampler for metrics data. * Choose to sample all or none data according to the config file. */ public class CapaMetricsSampler implements ExemplarFilter { - /** - * Sampler instance, which samples all the data if no config was explicitly set. - */ - private static final CapaMetricsSampler INSTANCE = new CapaMetricsSampler(SamplerConfig.DEFAULT_CONFIG); - - /** - * Inner instance. - */ - private ExemplarFilter inner; - - /** - * Get the sampler instance. - * - * @return the sampler instance. - */ - public static CapaMetricsSampler getInstance() { - return INSTANCE; - } + private final Supplier samplerConfigSupplier; - private CapaMetricsSampler(SamplerConfig config) { - update(config); - } - - /** - * Update the sample policy. - * - * @param config new sample config. - * @return the updated sampler instance. - */ - public CapaMetricsSampler update(SamplerConfig config) { - if (config == null) { - return this; + public CapaMetricsSampler(Supplier samplerConfigSupplier) { + if (samplerConfigSupplier == null) { + samplerConfigSupplier = () -> SamplerConfig.DEFAULT_CONFIG; } - - if (config.isMetricsSample()) { - inner = ExemplarFilter.alwaysSample(); - } else { - inner = ExemplarFilter.neverSample(); - } - return this; + this.samplerConfigSupplier = samplerConfigSupplier; } @Override public boolean shouldSampleMeasurement(long value, Attributes attributes, Context context) { - return inner.shouldSampleMeasurement(value, attributes, context); + return get().shouldSampleMeasurement(value, attributes, context); } @Override public boolean shouldSampleMeasurement(double value, Attributes attributes, Context context) { - return inner.shouldSampleMeasurement(value, attributes, context); + return get().shouldSampleMeasurement(value, attributes, context); + } + + private ExemplarFilter get() { + SamplerConfig config = samplerConfigSupplier.get(); + if (config != null && !config.isMetricsEnable()) { + return ExemplarFilter.neverSample(); + } + + return ExemplarFilter.alwaysSample(); } + } diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/trace/CapaSpanBuilder.java b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/trace/CapaSpanBuilder.java index 2979570..9053817 100644 --- a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/trace/CapaSpanBuilder.java +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/trace/CapaSpanBuilder.java @@ -129,7 +129,7 @@ public SpanBuilder setStartTimestamp(long startTimestamp, TimeUnit unit) { public Span startSpan() { Span span = spanBuilder.startSpan(); if (span instanceof ReadWriteSpan) { - return CapaWrapper.wrap(tracerName, version, schemaUrl, (ReadWriteSpan) span); + return CapaTracerWrapper.wrap(tracerName, version, schemaUrl, (ReadWriteSpan) span); } return span; } diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/trace/CapaTraceSampler.java b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/trace/CapaTraceSampler.java index 4c1873b..2fc563b 100644 --- a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/trace/CapaTraceSampler.java +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/trace/CapaTraceSampler.java @@ -25,6 +25,7 @@ import io.opentelemetry.sdk.trace.samplers.SamplingResult; import java.util.List; +import java.util.function.Supplier; /** * Sampler for trace data. @@ -32,35 +33,24 @@ */ public class CapaTraceSampler implements Sampler { - private static final CapaTraceSampler INSTANCE = new CapaTraceSampler(SamplerConfig.DEFAULT_CONFIG); + private final Supplier samplerConfigSupplier; - private Sampler inner; - - public static CapaTraceSampler getInstance() { - return INSTANCE; - } - - private CapaTraceSampler(SamplerConfig config) { - update(config); - } - - public CapaTraceSampler update(SamplerConfig config) { - if (config == null) { - return this; - } - - if (config.isTraceSample()) { - inner = Sampler.alwaysOn(); - } else { - inner = Sampler.alwaysOff(); + public CapaTraceSampler(Supplier samplerConfigSupplier) { + if (samplerConfigSupplier == null) { + samplerConfigSupplier = () -> SamplerConfig.DEFAULT_CONFIG; } - return this; + this.samplerConfigSupplier = samplerConfigSupplier; } @Override public SamplingResult shouldSample(Context context, String traceId, String name, SpanKind kind, Attributes attributes, List list) { - return inner.shouldSample(context, traceId, name, kind, attributes, list); + Sampler sampler = Sampler.alwaysOn(); + SamplerConfig config = samplerConfigSupplier.get(); + if (config != null && !config.isTraceEnable()) { + sampler = Sampler.alwaysOff(); + } + return sampler.shouldSample(context, traceId, name, kind, attributes, list); } @Override diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/trace/CapaTracer.java b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/trace/CapaTracer.java index 15d0a6b..a048ee7 100644 --- a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/trace/CapaTracer.java +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/trace/CapaTracer.java @@ -39,7 +39,7 @@ public CapaTracer(String tracerName, String version, String schemaUrl, Tracer tr @Override public SpanBuilder spanBuilder(String spanName) { SpanBuilder builder = tracer.spanBuilder(spanName); - return CapaWrapper.wrap(tracerName, version, schemaUrl, spanName, builder); + return CapaTracerWrapper.wrap(tracerName, version, schemaUrl, spanName, builder); } public String getVersion() { diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/trace/CapaTracerBuilder.java b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/trace/CapaTracerBuilder.java index 06708c6..6e46ea6 100644 --- a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/trace/CapaTracerBuilder.java +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/trace/CapaTracerBuilder.java @@ -53,6 +53,6 @@ public TracerBuilder setInstrumentationVersion(String instrumentationVersion) { @Override public Tracer build() { - return CapaWrapper.wrap(tracerName, version, schemaUrl, tracerBuilder.build()); + return CapaTracerWrapper.wrap(tracerName, version, schemaUrl, tracerBuilder.build()); } } diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/trace/CapaTracerProvider.java b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/trace/CapaTracerProvider.java index 3b102ae..ad72469 100644 --- a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/trace/CapaTracerProvider.java +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/trace/CapaTracerProvider.java @@ -44,6 +44,6 @@ public Tracer get(String instrumentationName, String instrumentationVersion) { @Override public TracerBuilder tracerBuilder(String instrumentationName) { TracerBuilder tracerBuilder = provider.tracerBuilder(instrumentationName); - return CapaWrapper.wrap(instrumentationName, tracerBuilder); + return CapaTracerWrapper.wrap(instrumentationName, tracerBuilder); } } diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/trace/CapaTracerProviderBuilder.java b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/trace/CapaTracerProviderBuilder.java index 60f1bba..00a9530 100644 --- a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/trace/CapaTracerProviderBuilder.java +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/trace/CapaTracerProviderBuilder.java @@ -33,6 +33,7 @@ import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; +import java.util.function.Supplier; /** * Builder for capa tracer provider. @@ -63,7 +64,7 @@ public class CapaTracerProviderBuilder implements CapaTracerProviderSettings { /** * Sampler config. */ - private SamplerConfig samplerConfig; + private Supplier samplerConfig = SamplerConfig.DEFAULT_SUPPLIER; private static boolean addSpanLimits(SpanLimitsConfig spanLimits, SpanLimitsBuilder limits) { if (spanLimits == null) { @@ -112,7 +113,7 @@ private static void skipIdValidate(SdkTracerProvider provider) { } @Override - public CapaTracerProviderBuilder setSamplerConfig(SamplerConfig samplerConfig) { + public CapaTracerProviderBuilder setSamplerConfig(Supplier samplerConfig) { this.samplerConfig = samplerConfig; return this; } @@ -210,14 +211,7 @@ private void addSpanLimits(SdkTracerProviderBuilder builder) { } private void addSampler(SdkTracerProviderBuilder builder) { - initSampleConfig(); - builder.setSampler(Sampler.parentBased(CapaTraceSampler.getInstance().update(samplerConfig))); - } - - private void initSampleConfig() { - if (samplerConfig == null) { - samplerConfig = SamplerConfig.loadOrDefault(); - } + builder.setSampler(Sampler.parentBased(new CapaTraceSampler(samplerConfig))); } private void addProcessors(SdkTracerProviderBuilder builder) { diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/trace/CapaTracerProviderSettings.java b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/trace/CapaTracerProviderSettings.java index f1ac0da..f77b840 100644 --- a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/trace/CapaTracerProviderSettings.java +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/trace/CapaTracerProviderSettings.java @@ -20,6 +20,8 @@ import io.opentelemetry.sdk.trace.IdGenerator; import io.opentelemetry.sdk.trace.SpanProcessor; +import java.util.function.Supplier; + /** * Settings for capa trace provider. */ @@ -66,6 +68,6 @@ public interface CapaTracerProviderSettings { * @param samplerConfig sample config. * @return current settings. */ - CapaTracerProviderSettings setSamplerConfig(SamplerConfig samplerConfig); + CapaTracerProviderSettings setSamplerConfig(Supplier samplerConfig); } diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/trace/CapaWrapper.java b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/trace/CapaTracerWrapper.java similarity index 98% rename from sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/trace/CapaWrapper.java rename to sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/trace/CapaTracerWrapper.java index fec1d7c..2de9c21 100644 --- a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/trace/CapaWrapper.java +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/trace/CapaTracerWrapper.java @@ -27,13 +27,13 @@ /** * Load capa implementation. */ -final class CapaWrapper { +final class CapaTracerWrapper { static final String FILE_SUFFIX = "telemetry"; static final boolean CACHE = true; - private CapaWrapper() { + private CapaTracerWrapper() { } @Nullable diff --git a/sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/metrics/CapaMeterProviderBuilderTest.java b/sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/metrics/CapaMeterProviderBuilderTest.java index f12879e..56169e9 100644 --- a/sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/metrics/CapaMeterProviderBuilderTest.java +++ b/sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/metrics/CapaMeterProviderBuilderTest.java @@ -25,7 +25,6 @@ import java.util.Arrays; import java.util.concurrent.TimeUnit; -import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; /** @@ -48,8 +47,6 @@ public void buildWithConfigFile() { currentGroup.enumerate(lstThreads); assertTrue(Arrays.stream(lstThreads).anyMatch(t -> t.getName().contains("my-reader"))); - assertTrue(CapaMetricsSampler.getInstance().shouldSampleMeasurement(1L, null, null)); - assertTrue(CapaMetricsSampler.getInstance().shouldSampleMeasurement(1.0, null, null)); } @@ -103,9 +100,6 @@ public void addReaderConfig() { assertTrue(Arrays.stream(lstThreads).anyMatch(t -> t.getName().contains(readerConfigByInstance.getName()))); assertTrue(Arrays.stream(lstThreads).anyMatch(t -> t.getName().contains(readerConfigByPath.getName()))); - - assertTrue(CapaMetricsSampler.getInstance().shouldSampleMeasurement(1L, null, null)); - assertTrue(CapaMetricsSampler.getInstance().shouldSampleMeasurement(1.0, null, null)); } @@ -124,12 +118,12 @@ public void setMeterConfig() { readerConfigByPath.setExporterType("group.rxcloud.capa.component.telemetry.metrics.TestMetricsExporter"); SamplerConfig samplerConfig = new SamplerConfig(); - samplerConfig.setMetricsSample(false); + samplerConfig.setMetricsEnable(false); MeterProvider meterProvider = new CapaMeterProviderBuilder() .addMetricReaderConfig(readerConfigByPath) .setMeterConfig(meterConfig) - .setSamplerConfig(samplerConfig) + .setSamplerConfig(() -> samplerConfig) .buildMeterProvider(); ThreadGroup currentGroup = @@ -141,7 +135,5 @@ public void setMeterConfig() { assertTrue(Arrays.stream(lstThreads).noneMatch(t -> t.getName().contains(readerConfigByInstance.getName()))); assertTrue(Arrays.stream(lstThreads).anyMatch(t -> t.getName().contains(readerConfigByPath.getName()))); - assertFalse(CapaMetricsSampler.getInstance().shouldSampleMeasurement(1L, null, null)); - assertFalse(CapaMetricsSampler.getInstance().shouldSampleMeasurement(1.0, null, null)); } } \ No newline at end of file diff --git a/sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/metrics/CapaMetricsSamplerTest.java b/sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/metrics/CapaMetricsSamplerTest.java index d05d7e1..2673f31 100644 --- a/sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/metrics/CapaMetricsSamplerTest.java +++ b/sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/metrics/CapaMetricsSamplerTest.java @@ -17,11 +17,9 @@ package group.rxcloud.capa.component.telemetry.metrics; import group.rxcloud.capa.component.telemetry.SamplerConfig; -import group.rxcloud.capa.component.telemetry.trace.CapaTraceSampler; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; /** @@ -31,30 +29,17 @@ public class CapaMetricsSamplerTest { @Test - public void getInstance() { - assertNotNull(CapaMetricsSampler.getInstance()); - CapaMetricsSampler.getInstance().update(SamplerConfig.DEFAULT_CONFIG); - assertTrue(CapaMetricsSampler.getInstance().shouldSampleMeasurement(1L, null, null)); - assertTrue(CapaMetricsSampler.getInstance().shouldSampleMeasurement(1.0, null, null)); - - CapaMetricsSampler.getInstance().update(null); - assertTrue(CapaMetricsSampler.getInstance().shouldSampleMeasurement(1L, null, null)); - assertTrue(CapaMetricsSampler.getInstance().shouldSampleMeasurement(1.0, null, null)); - - SamplerConfig samplerConfig = new SamplerConfig(); - samplerConfig.setMetricsSample(false); - CapaMetricsSampler.getInstance().update(samplerConfig); - assertFalse(CapaMetricsSampler.getInstance().shouldSampleMeasurement(1L, null, null)); - assertFalse(CapaMetricsSampler.getInstance().shouldSampleMeasurement(1.0, null, null)); - - - samplerConfig.setMetricsSample(true); - CapaMetricsSampler.getInstance().update(samplerConfig); - assertTrue(CapaMetricsSampler.getInstance().shouldSampleMeasurement(1L, null, null)); - assertTrue(CapaMetricsSampler.getInstance().shouldSampleMeasurement(1.0, null, null)); - - CapaMetricsSampler.getInstance().update(SamplerConfig.DEFAULT_CONFIG); - - assertNotNull(CapaTraceSampler.getInstance().getDescription()); + public void testConfig() { + CapaMetricsSampler sampler = new CapaMetricsSampler(() -> SamplerConfig.DEFAULT_CONFIG); + assertTrue(sampler.shouldSampleMeasurement(1, null, null)); + sampler = new CapaMetricsSampler(() -> null); + assertTrue(sampler.shouldSampleMeasurement(1.0, null, null)); + SamplerConfig config = new SamplerConfig(); + config.setMetricsEnable(false); + sampler = new CapaMetricsSampler(() -> config); + assertFalse(sampler.shouldSampleMeasurement(1, null, null)); + config.setMetricsEnable(true); + assertTrue(sampler.shouldSampleMeasurement(1.0, null, null)); } + } \ No newline at end of file diff --git a/sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/metrics/TestMetricsExporter.java b/sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/metrics/TestMetricsExporter.java index ede215b..cc15444 100644 --- a/sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/metrics/TestMetricsExporter.java +++ b/sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/metrics/TestMetricsExporter.java @@ -16,26 +16,32 @@ */ package group.rxcloud.capa.component.telemetry.metrics; +import group.rxcloud.capa.component.telemetry.SamplerConfig; import io.opentelemetry.sdk.common.CompletableResultCode; import io.opentelemetry.sdk.metrics.data.MetricData; -import io.opentelemetry.sdk.metrics.export.MetricExporter; import java.util.Collection; +import java.util.function.Supplier; /** * @author: chenyijiang * @date: 2021/11/25 17:04 */ -public class TestMetricsExporter implements MetricExporter { +public class TestMetricsExporter extends CapaMetricsExporter { + + public TestMetricsExporter( + Supplier samplerConfig) { + super(samplerConfig); + } @Override - public CompletableResultCode export(Collection metrics) { + public CompletableResultCode doExport(Collection metrics) { metrics.forEach(System.out::println); return CompletableResultCode.ofSuccess(); } @Override - public CompletableResultCode flush() { + public CompletableResultCode doFlush() { return CompletableResultCode.ofSuccess(); } diff --git a/sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/trace/CapaTraceSamplerTest.java b/sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/trace/CapaTraceSamplerTest.java index 9bff803..cac8c0b 100644 --- a/sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/trace/CapaTraceSamplerTest.java +++ b/sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/trace/CapaTraceSamplerTest.java @@ -30,26 +30,21 @@ public class CapaTraceSamplerTest { @Test - public void getInstance() { - assertNotNull(CapaTraceSampler.getInstance()); - CapaTraceSampler.getInstance().update(SamplerConfig.DEFAULT_CONFIG); - assertEquals(SamplingResult.recordAndSample(), CapaTraceSampler.getInstance().shouldSample(null, null, null, null, null, null)); - - CapaTraceSampler.getInstance().update(null); - assertEquals(SamplingResult.recordAndSample(), CapaTraceSampler.getInstance().shouldSample(null, null, null, null, null, null)); - - SamplerConfig samplerConfig = new SamplerConfig(); - samplerConfig.setTraceSample(false); - CapaTraceSampler.getInstance().update(samplerConfig); - assertEquals(SamplingResult.drop(), CapaTraceSampler.getInstance().shouldSample(null, null, null, null, null, null)); - - - samplerConfig.setTraceSample(true); - CapaTraceSampler.getInstance().update(samplerConfig); - assertEquals(SamplingResult.recordAndSample(), CapaTraceSampler.getInstance().shouldSample(null, null, null, null, null, null)); - - CapaTraceSampler.getInstance().update(SamplerConfig.DEFAULT_CONFIG); + public void testConfig() { + CapaTraceSampler sampler = new CapaTraceSampler(() -> SamplerConfig.DEFAULT_CONFIG); + assertEquals(SamplingResult.recordAndSample(), sampler.shouldSample(null, null, null, null, null, null)); + sampler = new CapaTraceSampler(() -> null); + assertEquals(SamplingResult.recordAndSample(), sampler.shouldSample(null, null, null, null, null, null)); + SamplerConfig config = new SamplerConfig(); + config.setTraceEnable(false); + sampler = new CapaTraceSampler(() -> config); + assertEquals(SamplingResult.drop(), sampler.shouldSample(null, null, null, null, null, null)); + config.setTraceEnable(true); + assertEquals(SamplingResult.recordAndSample(), sampler.shouldSample(null, null, null, null, null, null)); + } - assertNotNull(CapaTraceSampler.getInstance().getDescription()); + @Test + public void getDescription() { + assertNotNull(new CapaTraceSampler(() -> SamplerConfig.DEFAULT_CONFIG).getDescription()); } } \ No newline at end of file diff --git a/sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/trace/CapaTracerProviderBuilderTest.java b/sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/trace/CapaTracerProviderBuilderTest.java index c3a49a6..1a19cc6 100644 --- a/sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/trace/CapaTracerProviderBuilderTest.java +++ b/sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/trace/CapaTracerProviderBuilderTest.java @@ -65,7 +65,7 @@ public void buildFromTraceConfg() { .addProcessor(processor) .setSpanLimits(outter) .setIdGenerator(IdGenerator.random()) - .setSamplerConfig(SamplerConfig.DEFAULT_CONFIG) + .setSamplerConfig(() -> SamplerConfig.DEFAULT_CONFIG) .buildTracerProvider(); Span spanAnother = provider.tracerBuilder("otherTracer").build().spanBuilder("otherSpan").startSpan(); spanAnother.end(System.currentTimeMillis(), TimeUnit.MILLISECONDS); diff --git a/sdk-component/src/test/resources/capa-sample.properties b/sdk-component/src/test/resources/capa-sample.properties deleted file mode 100644 index d22c308..0000000 --- a/sdk-component/src/test/resources/capa-sample.properties +++ /dev/null @@ -1,2 +0,0 @@ -metricsSample=true -traceSample=true \ No newline at end of file diff --git a/sdk-infrastructure/pom.xml b/sdk-infrastructure/pom.xml index 17a1891..7992479 100644 --- a/sdk-infrastructure/pom.xml +++ b/sdk-infrastructure/pom.xml @@ -23,7 +23,7 @@ capa-parent group.rxcloud - 1.0.7.RELEASE + 1.0.8-alpha-1 capa-sdk-infrastructure diff --git a/sdk-spi-demo/pom.xml b/sdk-spi-demo/pom.xml index 4256367..0f22976 100644 --- a/sdk-spi-demo/pom.xml +++ b/sdk-spi-demo/pom.xml @@ -23,7 +23,7 @@ capa-parent group.rxcloud - 1.0.7.RELEASE + 1.0.8-alpha-1 capa-sdk-spi-demo diff --git a/sdk-spi/pom.xml b/sdk-spi/pom.xml index dc3077f..f2811cc 100644 --- a/sdk-spi/pom.xml +++ b/sdk-spi/pom.xml @@ -23,7 +23,7 @@ capa-parent group.rxcloud - 1.0.7.RELEASE + 1.0.8-alpha-1 capa-sdk-spi diff --git a/sdk-spi/src/main/java/group/rxcloud/capa/spi/telemetry/CapaDoubleHistogramSpi.java b/sdk-spi/src/main/java/group/rxcloud/capa/spi/telemetry/CapaDoubleHistogramSpi.java new file mode 100644 index 0000000..f0754d3 --- /dev/null +++ b/sdk-spi/src/main/java/group/rxcloud/capa/spi/telemetry/CapaDoubleHistogramSpi.java @@ -0,0 +1,28 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package group.rxcloud.capa.spi.telemetry; + +import group.rxcloud.capa.component.telemetry.metrics.CapaDoubleHistogram; + +/** + */ +public abstract class CapaDoubleHistogramSpi extends CapaDoubleHistogram { + + public CapaDoubleHistogramSpi(String meterName, String schemaUrl, String version, String name, String description, String unit) { + super(meterName, schemaUrl, version, name, description, unit); + } +} diff --git a/sdk-spi/src/main/java/group/rxcloud/capa/spi/telemetry/CapaLongHistogramSpi.java b/sdk-spi/src/main/java/group/rxcloud/capa/spi/telemetry/CapaLongHistogramSpi.java new file mode 100644 index 0000000..a07f545 --- /dev/null +++ b/sdk-spi/src/main/java/group/rxcloud/capa/spi/telemetry/CapaLongHistogramSpi.java @@ -0,0 +1,29 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package group.rxcloud.capa.spi.telemetry; + +import group.rxcloud.capa.component.telemetry.metrics.CapaLongHistogram; + +/** + */ +public abstract class CapaLongHistogramSpi extends CapaLongHistogram { + + public CapaLongHistogramSpi(String meterName, String schemaUrl, String version, String name, String description, + String unit) { + super(meterName, schemaUrl, version, name, description, unit); + } +} diff --git a/sdk-spi/src/main/java/group/rxcloud/capa/spi/telemetry/CapaMetricsExporterSpi.java b/sdk-spi/src/main/java/group/rxcloud/capa/spi/telemetry/CapaMetricsExporterSpi.java new file mode 100644 index 0000000..5f7bdf2 --- /dev/null +++ b/sdk-spi/src/main/java/group/rxcloud/capa/spi/telemetry/CapaMetricsExporterSpi.java @@ -0,0 +1,33 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package group.rxcloud.capa.spi.telemetry; + +import group.rxcloud.capa.component.telemetry.SamplerConfig; +import group.rxcloud.capa.component.telemetry.metrics.CapaMetricsExporter; + +import java.util.function.Supplier; + +/** + * + */ +public abstract class CapaMetricsExporterSpi extends CapaMetricsExporter { + + public CapaMetricsExporterSpi( + Supplier samplerConfig) { + super(samplerConfig); + } +} diff --git a/sdk-springboot/pom.xml b/sdk-springboot/pom.xml index 8febac5..5cd1112 100644 --- a/sdk-springboot/pom.xml +++ b/sdk-springboot/pom.xml @@ -23,7 +23,7 @@ capa-parent group.rxcloud - 1.0.7.RELEASE + 1.0.8-alpha-1 sdk-springboot diff --git a/sdk/pom.xml b/sdk/pom.xml index b1e9a06..dfa787f 100644 --- a/sdk/pom.xml +++ b/sdk/pom.xml @@ -23,7 +23,7 @@ group.rxcloud capa-parent - 1.0.7.RELEASE + 1.0.8-alpha-1 capa-sdk diff --git a/sdk/src/main/java/group/rxcloud/capa/telemetry/CapaTelemetryClientBuilder.java b/sdk/src/main/java/group/rxcloud/capa/telemetry/CapaTelemetryClientBuilder.java index 9b5023a..574f827 100644 --- a/sdk/src/main/java/group/rxcloud/capa/telemetry/CapaTelemetryClientBuilder.java +++ b/sdk/src/main/java/group/rxcloud/capa/telemetry/CapaTelemetryClientBuilder.java @@ -32,6 +32,8 @@ import io.opentelemetry.sdk.trace.IdGenerator; import io.opentelemetry.sdk.trace.SpanProcessor; +import java.util.function.Supplier; + /** * A builder for the {@link CapaTelemetryClient} */ @@ -93,7 +95,7 @@ public CapaTelemetryClientBuilder addMetricReaderConfig(MetricsReaderConfig conf } @Override - public CapaTelemetryClientBuilder setSamplerConfig(SamplerConfig samplerConfig) { + public CapaTelemetryClientBuilder setSamplerConfig(Supplier samplerConfig) { meterProviderBuilder.setSamplerConfig(samplerConfig); tracerProviderBuilder.setSamplerConfig(samplerConfig); return this; diff --git a/sdk/src/test/java/group/rxcloud/capa/telemetry/CapaTelemetryClientBuilderTest.java b/sdk/src/test/java/group/rxcloud/capa/telemetry/CapaTelemetryClientBuilderTest.java index d79bf17..b1632f8 100644 --- a/sdk/src/test/java/group/rxcloud/capa/telemetry/CapaTelemetryClientBuilderTest.java +++ b/sdk/src/test/java/group/rxcloud/capa/telemetry/CapaTelemetryClientBuilderTest.java @@ -59,7 +59,7 @@ public void buildManual() throws InterruptedException { readerConfig.setExportInterval(1, TimeUnit.SECONDS); CapaTelemetryClient capaTelemetryClient = new CapaTelemetryClientBuilder() .addProcessor(new TraceProcessor()) - .setSamplerConfig(SamplerConfig.DEFAULT_CONFIG) + .setSamplerConfig(() -> SamplerConfig.DEFAULT_CONFIG) .setSpanLimits(new SpanLimitsConfig()) .setIdGenerator(IdGenerator.random()) .setTracerConfig(new TracerConfig()) diff --git a/sdk/src/test/java/group/rxcloud/capa/telemetry/MetricTestExporter.java b/sdk/src/test/java/group/rxcloud/capa/telemetry/MetricTestExporter.java index 6149cf5..3d184b6 100644 --- a/sdk/src/test/java/group/rxcloud/capa/telemetry/MetricTestExporter.java +++ b/sdk/src/test/java/group/rxcloud/capa/telemetry/MetricTestExporter.java @@ -16,22 +16,29 @@ */ package group.rxcloud.capa.telemetry; +import group.rxcloud.capa.component.telemetry.SamplerConfig; +import group.rxcloud.capa.component.telemetry.metrics.CapaMetricsExporter; import io.opentelemetry.sdk.common.CompletableResultCode; import io.opentelemetry.sdk.metrics.data.MetricData; -import io.opentelemetry.sdk.metrics.export.MetricExporter; import java.util.Collection; +import java.util.function.Supplier; -public class MetricTestExporter implements MetricExporter { +public class MetricTestExporter extends CapaMetricsExporter { + + public MetricTestExporter( + Supplier samplerConfig) { + super(samplerConfig); + } @Override - public CompletableResultCode export(Collection metrics) { + protected CompletableResultCode doExport(Collection metrics) { metrics.forEach(System.out::println); return CompletableResultCode.ofSuccess(); } @Override - public CompletableResultCode flush() { + protected CompletableResultCode doFlush() { return CompletableResultCode.ofSuccess(); } From 130b3cde3512eb71149eab2ca02a7f054fe3fcdb Mon Sep 17 00:00:00 2001 From: dgzpg Date: Wed, 1 Dec 2021 19:40:02 +0800 Subject: [PATCH 03/17] addDynamicallyAdjustLogLevel --- .../capa/examples/telemetry/DemoLog.java | 6 +- .../log/agent/CapaLog4jAppenderAgent.java | 2 +- .../log/agent/CapaLogbackAppenderAgent.java | 2 +- .../configuration/LogSwitchConfiguration.java | 128 +++++------------- .../component/log/enums/CapaLogLevel.java | 15 +- .../component/log/manager/LogManager.java | 91 +++++++++++-- 6 files changed, 134 insertions(+), 110 deletions(-) diff --git a/examples/src/main/java/group/rxcloud/capa/examples/telemetry/DemoLog.java b/examples/src/main/java/group/rxcloud/capa/examples/telemetry/DemoLog.java index e75463b..aef9e09 100644 --- a/examples/src/main/java/group/rxcloud/capa/examples/telemetry/DemoLog.java +++ b/examples/src/main/java/group/rxcloud/capa/examples/telemetry/DemoLog.java @@ -30,6 +30,10 @@ public class DemoLog { public static void main(String[] args) { - log.info("test"); + try { + log.info("test"); + }catch (Exception e){ + System.out.println(); + } } } diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/log/agent/CapaLog4jAppenderAgent.java b/sdk-component/src/main/java/group/rxcloud/capa/component/log/agent/CapaLog4jAppenderAgent.java index 6c43f85..2957661 100644 --- a/sdk-component/src/main/java/group/rxcloud/capa/component/log/agent/CapaLog4jAppenderAgent.java +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/log/agent/CapaLog4jAppenderAgent.java @@ -102,7 +102,7 @@ public static CapaLog4jAppender buildCapaLog4jAppender() { public void append(LogEvent event) { if (event != null && event.getLevel() != null) { Optional capaLogLevel = CapaLogLevel.toCapaLogLevel(event.getLevel().name()); - if (capaLogLevel.isPresent() && LogManager.isLogLevelSwitchOn(capaLogLevel.get())) { + if (capaLogLevel.isPresent() && LogManager.whetherLogsCanBeOutput(capaLogLevel.get())) { logAppender.appendLog(event); } } diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/log/agent/CapaLogbackAppenderAgent.java b/sdk-component/src/main/java/group/rxcloud/capa/component/log/agent/CapaLogbackAppenderAgent.java index 63cbb94..fe54ffd 100644 --- a/sdk-component/src/main/java/group/rxcloud/capa/component/log/agent/CapaLogbackAppenderAgent.java +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/log/agent/CapaLogbackAppenderAgent.java @@ -66,7 +66,7 @@ public static CapaLogbackAppender buildCapaLogbackAppender() { protected void append(ILoggingEvent event) { if (event != null && event.getLevel() != null) { Optional capaLogLevel = CapaLogLevel.toCapaLogLevel(event.getLevel().levelStr); - if (capaLogLevel.isPresent() && LogManager.isLogLevelSwitchOn(capaLogLevel.get())) { + if (capaLogLevel.isPresent() && LogManager.whetherLogsCanBeOutput(capaLogLevel.get())) { logbackAppender.appendLog(event); } } diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/log/configuration/LogSwitchConfiguration.java b/sdk-component/src/main/java/group/rxcloud/capa/component/log/configuration/LogSwitchConfiguration.java index 4aa5fa1..5747cfa 100644 --- a/sdk-component/src/main/java/group/rxcloud/capa/component/log/configuration/LogSwitchConfiguration.java +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/log/configuration/LogSwitchConfiguration.java @@ -17,6 +17,7 @@ package group.rxcloud.capa.component.log.configuration; import com.google.common.collect.Lists; +import com.google.common.collect.Maps; import group.rxcloud.capa.infrastructure.hook.ConfigurationHooks; import group.rxcloud.capa.infrastructure.hook.Mixer; import group.rxcloud.cloudruntimes.domain.core.configuration.SubConfigurationResp; @@ -25,14 +26,30 @@ import org.apache.commons.lang3.StringUtils; import reactor.core.publisher.Flux; +import java.util.Map; import java.util.Optional; +/** + * Log switch configuration + */ public class LogSwitchConfiguration { + /** + * Capa configuration hooks. + */ private static final Optional configurationHooks; - private static final String LOGS_SWITCH_CONFIG_FILE_NAME = "logs-switch.properties"; - private static LogsSwitchConfig logsSwitchConfig; - + /** + * Log switch config file name. + */ + private static final String LOGS_SWITCH_CONFIG_FILE_NAME = "log-level-switch.properties"; + /** + * Log switch config's value. + */ + private static Map logsSwitchConfig = Maps.newHashMap(); + + /** + * Init configuration hooks and subscribe configuration. + */ static { configurationHooks = Mixer.configurationHooksNullable(); configurationHooks.ifPresent(configurationHooks -> { @@ -40,18 +57,22 @@ public class LogSwitchConfiguration { }); } + /** + * Subscribe configuration. + * + * @param configurationHooks + */ private static void subscribeConfiguration(ConfigurationHooks configurationHooks) { - // todo get storeName and appId from hooks of configuration - String storeName = ""; - String appId = ""; - Flux> configFlux = configurationHooks.subscribeConfiguration( + String storeName = configurationHooks.registryStoreNames().get(0); + String appId = configurationHooks.defaultConfigurationAppId(); + Flux> configFlux = configurationHooks.subscribeConfiguration( storeName, appId, Lists.newArrayList(LOGS_SWITCH_CONFIG_FILE_NAME), null, StringUtils.EMPTY, StringUtils.EMPTY, - TypeRef.get(LogsSwitchConfig.class)); + TypeRef.get(Map.class)); configFlux.subscribe(resp -> { if (CollectionUtils.isNotEmpty(resp.getItems())) { logsSwitchConfig = resp.getItems().get(0).getContent(); @@ -59,91 +80,12 @@ private static void subscribeConfiguration(ConfigurationHooks configurationHooks }); } - public static LogsSwitchConfig getLogsSwitchConfig() { + /** + * Get the log switch config's values. + * + * @return + */ + public static Map getLogsSwitchConfig() { return logsSwitchConfig; } - - public static class LogsSwitchConfig { - private Boolean logsSwitch; - private Boolean allLevelSwitch; - private Boolean traceLevelSwitch; - private Boolean debugLevelSwitch; - private Boolean infoLevelSwitch; - private Boolean warnLevelSwitch; - private Boolean errorLevelSwitch; - private Boolean fatalLevelSwitch; - private Boolean offLevelSwitch; - - public Boolean getLogsSwitch() { - return logsSwitch; - } - - public void setLogsSwitch(Boolean logsSwitch) { - this.logsSwitch = logsSwitch; - } - - public Boolean getAllLevelSwitch() { - return allLevelSwitch; - } - - public void setAllLevelSwitch(Boolean allLevelSwitch) { - this.allLevelSwitch = allLevelSwitch; - } - - public Boolean getTraceLevelSwitch() { - return traceLevelSwitch; - } - - public void setTraceLevelSwitch(Boolean traceLevelSwitch) { - this.traceLevelSwitch = traceLevelSwitch; - } - - public Boolean getDebugLevelSwitch() { - return debugLevelSwitch; - } - - public void setDebugLevelSwitch(Boolean debugLevelSwitch) { - this.debugLevelSwitch = debugLevelSwitch; - } - - public Boolean getInfoLevelSwitch() { - return infoLevelSwitch; - } - - public void setInfoLevelSwitch(Boolean infoLevelSwitch) { - this.infoLevelSwitch = infoLevelSwitch; - } - - public Boolean getWarnLevelSwitch() { - return warnLevelSwitch; - } - - public void setWarnLevelSwitch(Boolean warnLevelSwitch) { - this.warnLevelSwitch = warnLevelSwitch; - } - - public Boolean getErrorLevelSwitch() { - return errorLevelSwitch; - } - - public void setErrorLevelSwitch(Boolean errorLevelSwitch) { - this.errorLevelSwitch = errorLevelSwitch; - } - - public Boolean getFatalLevelSwitch() { - return fatalLevelSwitch; - } - - public void setFatalLevelSwitch(Boolean fatalLevelSwitch) { - this.fatalLevelSwitch = fatalLevelSwitch; - } - - public Boolean getOffLevelSwitch() { - return offLevelSwitch; - } - - public void setOffLevelSwitch(Boolean offLevelSwitch) { - this.offLevelSwitch = offLevelSwitch; - } - } } diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/log/enums/CapaLogLevel.java b/sdk-component/src/main/java/group/rxcloud/capa/component/log/enums/CapaLogLevel.java index 6be30a1..da26619 100644 --- a/sdk-component/src/main/java/group/rxcloud/capa/component/log/enums/CapaLogLevel.java +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/log/enums/CapaLogLevel.java @@ -19,6 +19,9 @@ import java.util.Arrays; import java.util.Optional; +/** + * Capa log level. + */ public enum CapaLogLevel { /** * Standard order of log priorities: @@ -41,13 +44,23 @@ public enum CapaLogLevel { this.levelName = levelName; } + /** + * Convert logLevelArg to {@link CapaLogLevel} + * + * @param logLevelArg + * @return + */ public static Optional toCapaLogLevel(String logLevelArg) { return Arrays.stream(CapaLogLevel.values()) .filter(logLevel -> logLevel.levelName.equalsIgnoreCase(logLevelArg)) .findAny(); - } + /** + * Get level. + * + * @return + */ public int getLevel() { return level; } diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/log/manager/LogManager.java b/sdk-component/src/main/java/group/rxcloud/capa/component/log/manager/LogManager.java index 59d1f1b..04031a6 100644 --- a/sdk-component/src/main/java/group/rxcloud/capa/component/log/manager/LogManager.java +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/log/manager/LogManager.java @@ -19,32 +19,97 @@ import group.rxcloud.capa.component.log.configuration.LogSwitchConfiguration; import group.rxcloud.capa.component.log.enums.CapaLogLevel; +import java.util.Map; + +/** + * LogManager, to manage log output levels. + */ public class LogManager { - public static Boolean isLogLevelSwitchOn(CapaLogLevel capaLogLevel) { - LogSwitchConfiguration.LogsSwitchConfig logsSwitchConfig = LogSwitchConfiguration.getLogsSwitchConfig(); - //no configuration instance or logs switch configuration, then follow the default logic - if (logsSwitchConfig == null || Boolean.FALSE.equals(logsSwitchConfig.getLogsSwitch())) { + /** + * Dynamically adjust the log level switch name. + */ + private static final String LOG_LEVEL_SWITCH_NAME = "logLevelSwitch"; + /** + * All level switch name. + */ + private static final String ALL_LEVEL_SWITCH_NAME = "allLevelSwitch"; + /** + * Trace level switch name. + */ + private static final String TRACE_LEVEL_SWITCH_NAME = "traceLevelSwitch"; + /** + * Debug level switch name. + */ + private static final String DEBUG_LEVEL_SWITCH_NAME = "debugLevelSwitch"; + /** + * Info level switch name. + */ + private static final String INFO_LEVEL_SWITCH_NAME = "infoLevelSwitch"; + /** + * Warn level switch. + */ + private static final String WARN_LEVEL_SWITCH_NAME = "warnLevelSwitch"; + /** + * Error level switch. + */ + private static final String ERROR_LEVEL_SWITCH_NAME = "errorLevelSwitch"; + /** + * Fatal level switch + */ + private static final String FATAL_LEVEL_SWITCH_NAME = "fatalLevelSwitch"; + /** + * Off level switch. + */ + private static final String OFF_LEVEL_SWITCH_NAME = "offLevelSwitch"; + + /** + * Whether logs can be output. + * @param capaLogLevel + * @return + */ + public static Boolean whetherLogsCanBeOutput(CapaLogLevel capaLogLevel) { + Map logsSwitchConfigs = LogSwitchConfiguration.getLogsSwitchConfig(); + //no configuration instance or logs switch configuration, then follow the default logic. + if (logsSwitchConfigs == null + || logsSwitchConfigs.isEmpty() + || Boolean.FALSE.equals(logsSwitchConfigs.get(LOG_LEVEL_SWITCH_NAME))) { return isLogsLevelClosedWithDefault(capaLogLevel); } switch (capaLogLevel) { case ALL: - return logsSwitchConfig.getAllLevelSwitch() == null ? Boolean.FALSE : logsSwitchConfig.getAllLevelSwitch(); + return logsSwitchConfigs.get(ALL_LEVEL_SWITCH_NAME) == null + ? Boolean.FALSE + : logsSwitchConfigs.get(ALL_LEVEL_SWITCH_NAME); case TRACE: - return logsSwitchConfig.getTraceLevelSwitch() == null ? Boolean.FALSE : logsSwitchConfig.getTraceLevelSwitch(); + return logsSwitchConfigs.get(TRACE_LEVEL_SWITCH_NAME) == null + ? Boolean.FALSE + : logsSwitchConfigs.get(TRACE_LEVEL_SWITCH_NAME); case DEBUG: - return logsSwitchConfig.getDebugLevelSwitch() == null ? Boolean.FALSE : logsSwitchConfig.getDebugLevelSwitch(); + return logsSwitchConfigs.get(DEBUG_LEVEL_SWITCH_NAME) == null + ? Boolean.FALSE + : logsSwitchConfigs.get(DEBUG_LEVEL_SWITCH_NAME); case INFO: - return logsSwitchConfig.getInfoLevelSwitch() == null ? Boolean.TRUE : logsSwitchConfig.getInfoLevelSwitch(); + return logsSwitchConfigs.get(INFO_LEVEL_SWITCH_NAME) == null + ? Boolean.TRUE + : logsSwitchConfigs.get(INFO_LEVEL_SWITCH_NAME); case WARN: - return logsSwitchConfig.getWarnLevelSwitch() == null ? Boolean.TRUE : logsSwitchConfig.getWarnLevelSwitch(); + return logsSwitchConfigs.get(WARN_LEVEL_SWITCH_NAME) == null + ? Boolean.TRUE + : logsSwitchConfigs.get(WARN_LEVEL_SWITCH_NAME); case ERROR: - return logsSwitchConfig.getErrorLevelSwitch() == null ? Boolean.TRUE : logsSwitchConfig.getErrorLevelSwitch(); + return logsSwitchConfigs.get(ERROR_LEVEL_SWITCH_NAME) == null + ? Boolean.TRUE + : logsSwitchConfigs.get(ERROR_LEVEL_SWITCH_NAME); case FATAL: - return logsSwitchConfig.getFatalLevelSwitch() == null ? Boolean.TRUE : logsSwitchConfig.getFatalLevelSwitch(); + return logsSwitchConfigs.get(FATAL_LEVEL_SWITCH_NAME) == null + ? Boolean.TRUE + : logsSwitchConfigs.get(FATAL_LEVEL_SWITCH_NAME); case OFF: - return logsSwitchConfig.getOffLevelSwitch() == null ? Boolean.TRUE : logsSwitchConfig.getOffLevelSwitch(); + return logsSwitchConfigs.get(OFF_LEVEL_SWITCH_NAME) == null + ? Boolean.TRUE + : logsSwitchConfigs.get(OFF_LEVEL_SWITCH_NAME) == null; default: return Boolean.FALSE; } @@ -54,7 +119,7 @@ public static Boolean isLogLevelSwitchOn(CapaLogLevel capaLogLevel) { * Logs of the {@link CapaLogLevel#INFO} level or higher are normally output, but logs of the {@link CapaLogLevel#INFO} level lower are not output. * * @param capaLogLevel - * @return whether capaLogLevel's priority is equal or more then {@link CapaLogLevel#INFO}. + * @return whether the capaLogLevel's priority is equal or more then {@link CapaLogLevel#INFO}. */ private static Boolean isLogsLevelClosedWithDefault(CapaLogLevel capaLogLevel) { return capaLogLevel.getLevel() >= CapaLogLevel.INFO.getLevel(); From 6a11865aeb6a6f99656d8ed0fc0d7449885ebb7f Mon Sep 17 00:00:00 2001 From: dgzpg Date: Wed, 1 Dec 2021 19:54:17 +0800 Subject: [PATCH 04/17] merge metric --- .../log/configuration/LogSwitchConfiguration.java | 2 +- .../rxcloud/capa/component/log/manager/LogManager.java | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/log/configuration/LogSwitchConfiguration.java b/sdk-component/src/main/java/group/rxcloud/capa/component/log/configuration/LogSwitchConfiguration.java index 5747cfa..9d8d3f2 100644 --- a/sdk-component/src/main/java/group/rxcloud/capa/component/log/configuration/LogSwitchConfiguration.java +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/log/configuration/LogSwitchConfiguration.java @@ -83,7 +83,7 @@ private static void subscribeConfiguration(ConfigurationHooks configurationHooks /** * Get the log switch config's values. * - * @return + * @return the log switch config's values. */ public static Map getLogsSwitchConfig() { return logsSwitchConfig; diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/log/manager/LogManager.java b/sdk-component/src/main/java/group/rxcloud/capa/component/log/manager/LogManager.java index 04031a6..fba8605 100644 --- a/sdk-component/src/main/java/group/rxcloud/capa/component/log/manager/LogManager.java +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/log/manager/LogManager.java @@ -65,8 +65,15 @@ public class LogManager { /** * Whether logs can be output. + * If there is no corresponding configuration information or the switch of dynamic configuration log level is set to false, + * then only logs of info level and above will be output. + *

+ * If the value of the dynamic configuration log level switch is true, only the log with the log level configured as true will be output. + *

+ * If the log level is not in the {@link CapaLogLevel#values()}, the log is not output. + * * @param capaLogLevel - * @return + * @return If the log can be output, it is true, otherwise it is false. */ public static Boolean whetherLogsCanBeOutput(CapaLogLevel capaLogLevel) { Map logsSwitchConfigs = LogSwitchConfiguration.getLogsSwitchConfig(); From 8d3688fcff9f0a3d4eed5a28c980da52769780f3 Mon Sep 17 00:00:00 2001 From: dgzpg Date: Wed, 1 Dec 2021 20:23:08 +0800 Subject: [PATCH 05/17] update demo --- .../java/group/rxcloud/capa/spi/demo/log/DemoLog4jAppender.java | 2 +- .../group/rxcloud/capa/spi/demo/log/DemoLogbackAppender.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sdk-spi-demo/src/main/java/group/rxcloud/capa/spi/demo/log/DemoLog4jAppender.java b/sdk-spi-demo/src/main/java/group/rxcloud/capa/spi/demo/log/DemoLog4jAppender.java index ccf07cc..f94df0f 100644 --- a/sdk-spi-demo/src/main/java/group/rxcloud/capa/spi/demo/log/DemoLog4jAppender.java +++ b/sdk-spi-demo/src/main/java/group/rxcloud/capa/spi/demo/log/DemoLog4jAppender.java @@ -23,6 +23,6 @@ public class DemoLog4jAppender implements CapaLog4jAppenderAgent.CapaLog4jAppend @Override public void appendLog(LogEvent event) { - System.out.println("test log log4j and content is " + event.getMessage().getFormattedMessage()); + System.out.println("Test log log4j and content is " + event.getMessage().getFormattedMessage()); } } diff --git a/sdk-spi-demo/src/main/java/group/rxcloud/capa/spi/demo/log/DemoLogbackAppender.java b/sdk-spi-demo/src/main/java/group/rxcloud/capa/spi/demo/log/DemoLogbackAppender.java index 65ee325..8e5ee40 100644 --- a/sdk-spi-demo/src/main/java/group/rxcloud/capa/spi/demo/log/DemoLogbackAppender.java +++ b/sdk-spi-demo/src/main/java/group/rxcloud/capa/spi/demo/log/DemoLogbackAppender.java @@ -26,6 +26,6 @@ public DemoLogbackAppender() { @Override public void appendLog(ILoggingEvent event) { - System.out.println("test logback log and content is " + event.getFormattedMessage()); + System.out.println("Test logback log and content is " + event.getFormattedMessage()); } } From 9a80190289a613f8a58432345ad3de9b09f4e6df Mon Sep 17 00:00:00 2001 From: = Date: Wed, 1 Dec 2021 20:57:54 +0800 Subject: [PATCH 06/17] update file format --- .../capa/infrastructure/CapaClassLoader.java | 188 +++++++++--------- 1 file changed, 94 insertions(+), 94 deletions(-) diff --git a/sdk-infrastructure/src/main/java/group/rxcloud/capa/infrastructure/CapaClassLoader.java b/sdk-infrastructure/src/main/java/group/rxcloud/capa/infrastructure/CapaClassLoader.java index 203ee93..6429f40 100644 --- a/sdk-infrastructure/src/main/java/group/rxcloud/capa/infrastructure/CapaClassLoader.java +++ b/sdk-infrastructure/src/main/java/group/rxcloud/capa/infrastructure/CapaClassLoader.java @@ -1,94 +1,94 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package group.rxcloud.capa.infrastructure; - -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; -import java.util.Properties; - -/** - * The Capa SPI Class loader. - */ -public final class CapaClassLoader { - - /** - * Load component class obj. - * - * @param the target class type - * @param componentDomain the component domain - * @param superClazz the interface class type - * @return the target spi class obj - */ - public static T loadComponentClassObj(String componentDomain, Class superClazz) { - return loadComponentClassObj(componentDomain, superClazz, null, null); - } - - /** - * Load component class obj. - * - * @param the target class type - * @param componentDomain the component domain - * @param superClazz the interface class type - * @param parameterTypes the constructor parameters - * @param initargs the constructor initargs - * @return the target spi class obj - */ - public static T loadComponentClassObj(String componentDomain, Class superClazz, Class[] parameterTypes, Object[] initargs) { - Properties properties = CapaProperties.COMPONENT_PROPERTIES_SUPPLIER.apply(componentDomain); - String implClassPath = properties.getProperty(superClazz.getName()); - return loadClassObj(implClassPath, parameterTypes, initargs); - } - - /** - * Load infrastructure class obj. - * - * @param the target class type - * @param infrastructureDomain the infrastructure domain - * @param superClazz the interface class type - * @return the target spi class obj - */ - public static T loadInfrastructureClassObj(String infrastructureDomain, Class superClazz) { - return loadInfrastructureClassObj(infrastructureDomain, superClazz, null, null); - } - - /** - * Load infrastructure class obj. - * - * @param the target class type - * @param infrastructureDomain the infrastructure domain - * @param superClazz the interface class type - * @param parameterTypes the constructor parameters - * @param initargs the constructor initargs - * @return the target spi class obj - */ - public static T loadInfrastructureClassObj(String infrastructureDomain, Class superClazz, Class[] parameterTypes, Object[] initargs) { - Properties properties = CapaProperties.INFRASTRUCTURE_PROPERTIES_SUPPLIER.apply(infrastructureDomain); - String implClassPath = properties.getProperty(superClazz.getName()); - return loadClassObj(implClassPath, parameterTypes, initargs); - } - - private static T loadClassObj(String classPath, Class[] parameterTypes, Object[] initargs) { - try { - Class aClass = (Class) Class.forName(classPath); - Constructor constructor = aClass.getConstructor(parameterTypes); - Object newInstance = constructor.newInstance(initargs); - return (T) newInstance; - } catch (ClassNotFoundException | NoSuchMethodException | InvocationTargetException | InstantiationException | IllegalAccessException e) { - throw new IllegalArgumentException("Capa load class error: [" + classPath + "] not found."); - } - } -} +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package group.rxcloud.capa.infrastructure; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.util.Properties; + +/** + * The Capa SPI Class loader. + */ +public final class CapaClassLoader { + + /** + * Load component class obj. + * + * @param the target class type + * @param componentDomain the component domain + * @param superClazz the interface class type + * @return the target spi class obj + */ + public static T loadComponentClassObj(String componentDomain, Class superClazz) { + return loadComponentClassObj(componentDomain, superClazz, null, null); + } + + /** + * Load component class obj. + * + * @param the target class type + * @param componentDomain the component domain + * @param superClazz the interface class type + * @param parameterTypes the constructor parameters + * @param initargs the constructor initargs + * @return the target spi class obj + */ + public static T loadComponentClassObj(String componentDomain, Class superClazz, Class[] parameterTypes, Object[] initargs) { + Properties properties = CapaProperties.COMPONENT_PROPERTIES_SUPPLIER.apply(componentDomain); + String implClassPath = properties.getProperty(superClazz.getName()); + return loadClassObj(implClassPath, parameterTypes, initargs); + } + + /** + * Load infrastructure class obj. + * + * @param the target class type + * @param infrastructureDomain the infrastructure domain + * @param superClazz the interface class type + * @return the target spi class obj + */ + public static T loadInfrastructureClassObj(String infrastructureDomain, Class superClazz) { + return loadInfrastructureClassObj(infrastructureDomain, superClazz, null, null); + } + + /** + * Load infrastructure class obj. + * + * @param the target class type + * @param infrastructureDomain the infrastructure domain + * @param superClazz the interface class type + * @param parameterTypes the constructor parameters + * @param initargs the constructor initargs + * @return the target spi class obj + */ + public static T loadInfrastructureClassObj(String infrastructureDomain, Class superClazz, Class[] parameterTypes, Object[] initargs) { + Properties properties = CapaProperties.INFRASTRUCTURE_PROPERTIES_SUPPLIER.apply(infrastructureDomain); + String implClassPath = properties.getProperty(superClazz.getName()); + return loadClassObj(implClassPath, parameterTypes, initargs); + } + + private static T loadClassObj(String classPath, Class[] parameterTypes, Object[] initargs) { + try { + Class aClass = (Class) Class.forName(classPath); + Constructor constructor = aClass.getConstructor(parameterTypes); + Object newInstance = constructor.newInstance(initargs); + return (T) newInstance; + } catch (ClassNotFoundException | NoSuchMethodException | InvocationTargetException | InstantiationException | IllegalAccessException e) { + throw new IllegalArgumentException("Capa load class error: [" + classPath + "] not found."); + } + } +} From baaab892931fc34a3359ce905f30a4747e3e4709 Mon Sep 17 00:00:00 2001 From: = Date: Wed, 1 Dec 2021 21:00:29 +0800 Subject: [PATCH 07/17] update format --- .../capa/component/log/agent/CapaLogbackAppenderAgent.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/log/agent/CapaLogbackAppenderAgent.java b/sdk-component/src/main/java/group/rxcloud/capa/component/log/agent/CapaLogbackAppenderAgent.java index fe54ffd..fb9d86a 100644 --- a/sdk-component/src/main/java/group/rxcloud/capa/component/log/agent/CapaLogbackAppenderAgent.java +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/log/agent/CapaLogbackAppenderAgent.java @@ -84,4 +84,4 @@ public interface CapaLogbackAppender { */ void appendLog(ILoggingEvent event); } -} \ No newline at end of file +} From c1bb16f9a1686e9bb6a883e4a19667a0da2767d6 Mon Sep 17 00:00:00 2001 From: = Date: Wed, 1 Dec 2021 21:07:05 +0800 Subject: [PATCH 08/17] update format --- .../group/rxcloud/capa/component/log/enums/CapaLogLevel.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/log/enums/CapaLogLevel.java b/sdk-component/src/main/java/group/rxcloud/capa/component/log/enums/CapaLogLevel.java index da26619..099fac3 100644 --- a/sdk-component/src/main/java/group/rxcloud/capa/component/log/enums/CapaLogLevel.java +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/log/enums/CapaLogLevel.java @@ -24,8 +24,7 @@ */ public enum CapaLogLevel { /** - * Standard order of log priorities: - * ALL < TRACE < DEBUG < INFO < WARN < ERROR < FATAL < OFF + * Standard order of log priorities:ALL,TRACE,DEBUG,INFO,WARN,ERROR,FATAL,OFF */ ALL(1, "ALL"), TRACE(2, "TRACE"), From cda1bc3b3348ffb5201aee01e3e144f26d10409e Mon Sep 17 00:00:00 2001 From: = Date: Wed, 1 Dec 2021 21:14:16 +0800 Subject: [PATCH 09/17] update example --- .../capa/examples/telemetry/DemoLog.java | 79 ++++++++++--------- 1 file changed, 40 insertions(+), 39 deletions(-) diff --git a/examples/src/main/java/group/rxcloud/capa/examples/telemetry/DemoLog.java b/examples/src/main/java/group/rxcloud/capa/examples/telemetry/DemoLog.java index aef9e09..93ef506 100644 --- a/examples/src/main/java/group/rxcloud/capa/examples/telemetry/DemoLog.java +++ b/examples/src/main/java/group/rxcloud/capa/examples/telemetry/DemoLog.java @@ -1,39 +1,40 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package group.rxcloud.capa.examples.telemetry; - -import lombok.extern.slf4j.Slf4j; - -/** - * An application cannot use log4j and logback configuration to print logs at the same time. - * So if you want to test the log4j2 configuration to print logs, then you need to copy the resources/xml/log4j2.xml file to the resources directory, and then add log4j-slf4j-impl dependency to the pom file. - * Else if you want to use logback configuration to print logs, then the resources/xml/logback.xml file needs to be copied to the resources path, and the logback-classic dependency needs to be added to the pom file. - * Notice: - * 1. Resources cannot contain log4j2.xml and logback.xml files at the same time, - * 2. log4j-slf4j-impl and logback-classic cannot exist at the same time. - */ -@Slf4j -public class DemoLog { - - public static void main(String[] args) { - try { - log.info("test"); - }catch (Exception e){ - System.out.println(); - } - } -} +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package group.rxcloud.capa.examples.telemetry; + +import lombok.extern.slf4j.Slf4j; + +/** + * An application cannot use log4j and logback configuration to print logs at the same time. + * So if you want to test the log4j2 configuration to print logs, then you need to copy the resources/xml/log4j2.xml file to the resources directory, and then add log4j-slf4j-impl dependency to the pom file. + * Else if you want to use logback configuration to print logs, then the resources/xml/logback.xml file needs to be copied to the resources path, and the logback-classic dependency needs to be added to the pom file. + * Notice: + * 1. Resources cannot contain log4j2.xml and logback.xml files at the same time, + * 2. log4j-slf4j-impl and logback-classic cannot exist at the same time. + */ +@Slf4j +public class DemoLog { + + public static void main(String[] args) { + try { + log.info("test"); + }catch (Exception e){ + System.out.println(); + } + + } +} From c9736fa4271d0e6243026ffb1500867f4fce1e0f Mon Sep 17 00:00:00 2001 From: chenyijiang Date: Wed, 1 Dec 2021 21:23:20 +0800 Subject: [PATCH 10/17] Add comments and ut. Add CapaMeterSpi. --- .../telemetry/metrics/CapaMeter.java | 8 +- .../telemetry/metrics/CapaMeterBuilder.java | 6 +- .../metrics/CapaMetricsExporter.java | 4 - .../CapaDoubleHistogramBuilderTest.java | 47 +++++++++++ .../metrics/CapaMeterProviderTest.java | 51 +++++++++++ .../telemetry/metrics/CapaMeterTest.java | 65 ++++++++++++++ .../metrics/CapaMetricsExporterTest.java | 84 +++++++++++++++++++ .../metrics/TestCapaMetricsExporter.java | 51 +++++++++++ .../telemetry/metrics/TestLongHistogram.java | 53 ++++++++++++ .../capa-component-telemetry.properties | 3 +- .../capa/spi/telemetry/CapaMeterSpi.java | 30 +++++++ 11 files changed, 391 insertions(+), 11 deletions(-) create mode 100644 sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/metrics/CapaDoubleHistogramBuilderTest.java create mode 100644 sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/metrics/CapaMeterProviderTest.java create mode 100644 sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/metrics/CapaMeterTest.java create mode 100644 sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/metrics/CapaMetricsExporterTest.java create mode 100644 sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/metrics/TestCapaMetricsExporter.java create mode 100644 sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/metrics/TestLongHistogram.java create mode 100644 sdk-spi/src/main/java/group/rxcloud/capa/spi/telemetry/CapaMeterSpi.java diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMeter.java b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMeter.java index 050ba10..b2201aa 100644 --- a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMeter.java +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMeter.java @@ -29,13 +29,13 @@ @NotThreadSafe public class CapaMeter implements Meter { - private final String meterName; + protected final String meterName; - private final String schemaUrl; + protected final String schemaUrl; - private final String version; + protected final String version; - private final Meter meter; + protected final Meter meter; public CapaMeter(String meterName, String schemaUrl, String version, Meter meter) { this.meterName = meterName; diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMeterBuilder.java b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMeterBuilder.java index 330a827..e116d5a 100644 --- a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMeterBuilder.java +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMeterBuilder.java @@ -40,13 +40,15 @@ public CapaMeterBuilder(String name, MeterBuilder builder) { @Override public MeterBuilder setSchemaUrl(String schemaUrl) { this.schemaUrl = schemaUrl; - return meterBuilder.setSchemaUrl(schemaUrl); + meterBuilder.setSchemaUrl(schemaUrl); + return this; } @Override public MeterBuilder setInstrumentationVersion(String instrumentationVersion) { version = instrumentationVersion; - return meterBuilder.setInstrumentationVersion(instrumentationVersion); + meterBuilder.setInstrumentationVersion(instrumentationVersion); + return this; } @Override diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMetricsExporter.java b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMetricsExporter.java index d3e6025..3dd9786 100644 --- a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMetricsExporter.java +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMetricsExporter.java @@ -59,8 +59,4 @@ public CompletableResultCode flush() { protected abstract CompletableResultCode doFlush(); - @Override - public CompletableResultCode shutdown() { - return null; - } } diff --git a/sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/metrics/CapaDoubleHistogramBuilderTest.java b/sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/metrics/CapaDoubleHistogramBuilderTest.java new file mode 100644 index 0000000..d0796b5 --- /dev/null +++ b/sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/metrics/CapaDoubleHistogramBuilderTest.java @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package group.rxcloud.capa.component.telemetry.metrics; + +import io.opentelemetry.api.metrics.DoubleHistogramBuilder; +import io.opentelemetry.api.metrics.LongHistogramBuilder; +import io.opentelemetry.api.metrics.internal.NoopMeter; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * @author: chenyijiang + * @date: 2021/12/1 20:05 + */ +public class CapaDoubleHistogramBuilderTest { + + @Test + public void build() { + CapaDoubleHistogramBuilder builder = new CapaDoubleHistogramBuilder("", "", "", ""); + assertEquals(builder, builder.setDescription("aaa")); + assertEquals(builder, builder.setUnit("bbb")); + assertTrue(builder.build() instanceof NoopMeter.NoopDoubleHistogram); + + LongHistogramBuilder longHistogramBuilder = builder.ofLongs(); + assertEquals(longHistogramBuilder, longHistogramBuilder.setDescription("ccc")); + assertEquals(longHistogramBuilder, longHistogramBuilder.setUnit("ddd")); + assertTrue(longHistogramBuilder.build() instanceof TestLongHistogram); + + DoubleHistogramBuilder doubleHistogramBuilder = longHistogramBuilder.ofDoubles(); + assertTrue(doubleHistogramBuilder instanceof CapaDoubleHistogramBuilder); + } +} \ No newline at end of file diff --git a/sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/metrics/CapaMeterProviderTest.java b/sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/metrics/CapaMeterProviderTest.java new file mode 100644 index 0000000..66432ca --- /dev/null +++ b/sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/metrics/CapaMeterProviderTest.java @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package group.rxcloud.capa.component.telemetry.metrics; + +import io.opentelemetry.api.metrics.Meter; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * @author: chenyijiang + * @date: 2021/11/25 22:35 + */ +public class CapaMeterProviderTest { + + @Test + public void meterBuilder() { + Meter meter = new CapaMeterProviderBuilder().buildMeterProvider().meterBuilder("aaa").setSchemaUrl("url") + .setInstrumentationVersion("1.1").build(); + assertTrue(meter instanceof CapaMeter); + assertEquals("aaa", ((CapaMeter) meter).meterName); + assertEquals("1.1", ((CapaMeter) meter).version); + assertEquals("url", ((CapaMeter) meter).schemaUrl); + } + + + @Test + public void get() { + Meter meter = new CapaMeterProviderBuilder().buildMeterProvider().get("aaa"); + assertTrue(meter instanceof CapaMeter); + assertEquals("aaa", ((CapaMeter) meter).meterName); + assertNull(((CapaMeter) meter).version); + assertNull(((CapaMeter) meter).schemaUrl); + } +} \ No newline at end of file diff --git a/sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/metrics/CapaMeterTest.java b/sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/metrics/CapaMeterTest.java new file mode 100644 index 0000000..e3aad1d --- /dev/null +++ b/sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/metrics/CapaMeterTest.java @@ -0,0 +1,65 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package group.rxcloud.capa.component.telemetry.metrics; + +import io.opentelemetry.api.metrics.Meter; +import io.opentelemetry.api.metrics.internal.NoopMeter; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; + +/** + * @author: chenyijiang + * @date: 2021/12/1 19:55 + */ +public class CapaMeterTest { + + @Test + public void counterBuilder() { + Meter meter = mock(Meter.class); + CapaMeter capaMeter = new CapaMeter("", "", "", meter); + capaMeter.counterBuilder("aaa"); + verify(meter).counterBuilder("aaa"); + } + + @Test + public void upDownCounterBuilder() { + Meter meter = mock(Meter.class); + CapaMeter capaMeter = new CapaMeter("", "", "", meter); + capaMeter.upDownCounterBuilder("aaa"); + verify(meter).upDownCounterBuilder("aaa"); + } + + @Test + public void histogramBuilder() { + Meter meter = mock(Meter.class); + CapaMeter capaMeter = new CapaMeter("", "", "", meter); + assertTrue(capaMeter.histogramBuilder("aaa").setUnit("a").setDescription("desc").build() instanceof NoopMeter.NoopDoubleHistogram); + verify(meter, never()).histogramBuilder("aaa"); + } + + @Test + public void gaugeBuilder() { + Meter meter = mock(Meter.class); + CapaMeter capaMeter = new CapaMeter("", "", "", meter); + capaMeter.gaugeBuilder("aaa"); + verify(meter).gaugeBuilder("aaa"); + } +} \ No newline at end of file diff --git a/sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/metrics/CapaMetricsExporterTest.java b/sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/metrics/CapaMetricsExporterTest.java new file mode 100644 index 0000000..74f1140 --- /dev/null +++ b/sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/metrics/CapaMetricsExporterTest.java @@ -0,0 +1,84 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package group.rxcloud.capa.component.telemetry.metrics; + +import group.rxcloud.capa.component.telemetry.SamplerConfig; +import io.opentelemetry.sdk.common.CompletableResultCode; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.anyList; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; + +/** + * @author: chenyijiang + * @date: 2021/12/1 19:20 + */ +public class CapaMetricsExporterTest { + + @Test + public void export() { + TestCapaMetricsExporter exporter = spy(new TestCapaMetricsExporter(() -> SamplerConfig.DEFAULT_CONFIG)); + assertEquals(CompletableResultCode.ofSuccess(), exporter.export(new ArrayList<>())); + verify(exporter).doExport(anyList()); + } + + @Test + public void exportWithNullConfig() { + TestCapaMetricsExporter exporter = spy(new TestCapaMetricsExporter(() -> null)); + assertEquals(CompletableResultCode.ofSuccess(), exporter.export(new ArrayList<>())); + verify(exporter).doExport(anyList()); + } + + @Test + public void exportWithDisableConfig() { + TestCapaMetricsExporter exporter = spy(new TestCapaMetricsExporter(() -> new SamplerConfig() {{ setMetricsEnable(false); setTraceEnable(true);}})); + assertEquals(CompletableResultCode.ofSuccess(), exporter.export(new ArrayList<>())); + verify(exporter, never()).doExport(anyList()); + } + + @Test + public void flushWithNullConfig() { + TestCapaMetricsExporter exporter = spy(new TestCapaMetricsExporter(() -> null)); + assertEquals(CompletableResultCode.ofSuccess(), exporter.flush()); + verify(exporter).doFlush(); + } + + @Test + public void flushWithDisableConfig() { + TestCapaMetricsExporter exporter = spy(new TestCapaMetricsExporter(() -> new SamplerConfig() {{ setMetricsEnable(false); setTraceEnable(true);}})); + assertEquals(CompletableResultCode.ofSuccess(), exporter.flush()); + verify(exporter, never()).doFlush(); + } + + @Test + public void flush() { + TestCapaMetricsExporter exporter = spy(new TestCapaMetricsExporter(() -> SamplerConfig.DEFAULT_CONFIG)); + assertEquals(CompletableResultCode.ofSuccess(), exporter.flush()); + verify(exporter).doFlush(); + } + + + @Test + public void shutdown() { + assertEquals(CompletableResultCode.ofSuccess(), new TestCapaMetricsExporter(() -> SamplerConfig.DEFAULT_CONFIG).shutdown()); + } +} \ No newline at end of file diff --git a/sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/metrics/TestCapaMetricsExporter.java b/sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/metrics/TestCapaMetricsExporter.java new file mode 100644 index 0000000..8ccde0d --- /dev/null +++ b/sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/metrics/TestCapaMetricsExporter.java @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package group.rxcloud.capa.component.telemetry.metrics; + +import group.rxcloud.capa.component.telemetry.SamplerConfig; +import io.opentelemetry.sdk.common.CompletableResultCode; +import io.opentelemetry.sdk.metrics.data.MetricData; + +import java.util.Collection; +import java.util.function.Supplier; + +/** + * @author: chenyijiang + * @date: 2021/12/1 19:20 + */ +public class TestCapaMetricsExporter extends CapaMetricsExporter { + + public TestCapaMetricsExporter( + Supplier samplerConfig) { + super(samplerConfig); + } + + @Override + protected CompletableResultCode doExport(Collection metrics) { + return CompletableResultCode.ofSuccess(); + } + + @Override + protected CompletableResultCode doFlush() { + return CompletableResultCode.ofSuccess(); + } + + @Override + public CompletableResultCode shutdown() { + return CompletableResultCode.ofSuccess(); + } +} \ No newline at end of file diff --git a/sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/metrics/TestLongHistogram.java b/sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/metrics/TestLongHistogram.java new file mode 100644 index 0000000..ce6b750 --- /dev/null +++ b/sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/metrics/TestLongHistogram.java @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package group.rxcloud.capa.component.telemetry.metrics; + +import io.opentelemetry.api.common.Attributes; +import io.opentelemetry.api.metrics.BoundLongHistogram; +import io.opentelemetry.context.Context; + +/** + * @author: chenyijiang + * @date: 2021/12/1 20:08 + */ +public class TestLongHistogram extends CapaLongHistogram{ + + public TestLongHistogram(String meterName, String schemaUrl, String version, String name, String description, + String unit) { + super(meterName, schemaUrl, version, name, description, unit); + } + + @Override + public void record(long value) { + + } + + @Override + public void record(long value, Attributes attributes) { + + } + + @Override + public void record(long value, Attributes attributes, Context context) { + + } + + @Override + public BoundLongHistogram bind(Attributes attributes) { + return null; + } +} diff --git a/sdk-component/src/test/resources/capa-component-telemetry.properties b/sdk-component/src/test/resources/capa-component-telemetry.properties index 33bbdf7..f2e9505 100644 --- a/sdk-component/src/test/resources/capa-component-telemetry.properties +++ b/sdk-component/src/test/resources/capa-component-telemetry.properties @@ -1 +1,2 @@ -group.rxcloud.capa.component.telemetry.context.CapaContextAsyncWrapper=group.rxcloud.capa.component.telemetry.context.TestCapaContextAsyncWrapper \ No newline at end of file +group.rxcloud.capa.component.telemetry.context.CapaContextAsyncWrapper=group.rxcloud.capa.component.telemetry.context.TestCapaContextAsyncWrapper +group.rxcloud.capa.component.telemetry.metrics.CapaLongHistogram=group.rxcloud.capa.component.telemetry.metrics.TestLongHistogram \ No newline at end of file diff --git a/sdk-spi/src/main/java/group/rxcloud/capa/spi/telemetry/CapaMeterSpi.java b/sdk-spi/src/main/java/group/rxcloud/capa/spi/telemetry/CapaMeterSpi.java new file mode 100644 index 0000000..7f31300 --- /dev/null +++ b/sdk-spi/src/main/java/group/rxcloud/capa/spi/telemetry/CapaMeterSpi.java @@ -0,0 +1,30 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package group.rxcloud.capa.spi.telemetry; + +import group.rxcloud.capa.component.telemetry.metrics.CapaMeter; +import io.opentelemetry.api.metrics.Meter; + +/** + * SPI Capa meter. + */ +public abstract class CapaMeterSpi extends CapaMeter { + + public CapaMeterSpi(String meterName, String schemaUrl, String version, Meter meter) { + super(meterName, schemaUrl, version, meter); + } +} From e40e0d0cdec05fedcd822aafffcb5f075826e1ba Mon Sep 17 00:00:00 2001 From: chenyijiang Date: Thu, 2 Dec 2021 13:11:16 +0800 Subject: [PATCH 11/17] Add comments and ut. Add CapaMeterSpi. --- examples/pom.xml | 2 +- pom.xml | 2 +- sdk-component/pom.xml | 2 +- .../component/telemetry/SamplerConfig.java | 105 ++++++++++++------ .../CapaContextPropagatorSettings.java | 3 +- .../metrics/CapaMeterProviderSettings.java | 3 +- .../trace/CapaTracerProviderSettings.java | 3 +- .../telemetry/SamplerConfigTest.java | 38 +++++++ .../telemetry/TestMixerProvider.java | 87 +++++++++++++++ .../trace/CapaTracerProviderBuilderTest.java | 1 + ...capa-component-telemetry-common.properties | 1 + ... => capa-component-telemetry-context.json} | 0 ...on => capa-component-telemetry-meter.json} | 0 ...n => capa-component-telemetry-tracer.json} | 0 .../capa-infrastructure-mixer.properties | 1 + sdk-infrastructure/pom.xml | 2 +- sdk-spi-demo/pom.xml | 2 +- sdk-spi/pom.xml | 2 +- sdk-springboot/pom.xml | 2 +- sdk/pom.xml | 2 +- .../telemetry/CapaTelemetryClientGlobal.java | 19 ---- .../CapaTelemetryClientGlobalTest.java | 3 +- 22 files changed, 208 insertions(+), 72 deletions(-) create mode 100644 sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/SamplerConfigTest.java create mode 100644 sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/TestMixerProvider.java create mode 100644 sdk-component/src/test/resources/capa-component-telemetry-common.properties rename sdk-component/src/test/resources/{capa-context.json => capa-component-telemetry-context.json} (100%) rename sdk-component/src/test/resources/{capa-meter.json => capa-component-telemetry-meter.json} (100%) rename sdk-component/src/test/resources/{capa-tracer.json => capa-component-telemetry-tracer.json} (100%) create mode 100644 sdk-component/src/test/resources/capa-infrastructure-mixer.properties diff --git a/examples/pom.xml b/examples/pom.xml index 40f3206..6eed8d9 100644 --- a/examples/pom.xml +++ b/examples/pom.xml @@ -23,7 +23,7 @@ capa-parent group.rxcloud - 1.0.8-alpha-1 + 1.0.8-alpha-2 capa-examples diff --git a/pom.xml b/pom.xml index 9b98361..9f817f4 100644 --- a/pom.xml +++ b/pom.xml @@ -23,7 +23,7 @@ group.rxcloud capa-parent pom - 1.0.8-alpha-1 + 1.0.8-alpha-2 capa-sdk-parent SDK for Capa. https://github.com/reactivegroup diff --git a/sdk-component/pom.xml b/sdk-component/pom.xml index 370c378..608b900 100644 --- a/sdk-component/pom.xml +++ b/sdk-component/pom.xml @@ -23,7 +23,7 @@ group.rxcloud capa-parent - 1.0.8-alpha-1 + 1.0.8-alpha-2 capa-sdk-component diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/SamplerConfig.java b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/SamplerConfig.java index f53b99b..94d4bfb 100644 --- a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/SamplerConfig.java +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/SamplerConfig.java @@ -16,19 +16,20 @@ */ package group.rxcloud.capa.component.telemetry; +import com.google.common.collect.Lists; import group.rxcloud.capa.component.telemetry.metrics.CapaMeterProviderBuilder; import group.rxcloud.capa.infrastructure.CapaProperties; import group.rxcloud.capa.infrastructure.hook.ConfigurationHooks; import group.rxcloud.capa.infrastructure.hook.Mixer; -import group.rxcloud.cloudruntimes.domain.core.configuration.ConfigurationItem; +import group.rxcloud.cloudruntimes.domain.core.configuration.SubConfigurationResp; import group.rxcloud.cloudruntimes.utils.TypeRef; +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import reactor.core.publisher.Flux; import java.io.Serializable; -import java.util.Collections; -import java.util.List; -import java.util.Optional; import java.util.function.Supplier; /** @@ -36,58 +37,88 @@ */ public class SamplerConfig implements Serializable { - public static final transient String FILE_PATH = "capa-sample.properties"; + private static final long serialVersionUID = -2113523925814197551L; + + public static final transient String FILE_PATH = "capa-component-telemetry-sample.properties"; + + public static final transient String COMMON_FILE_SUFFIX = "telemetry-common"; /** * Sample all data as default. */ - public static final transient SamplerConfig DEFAULT_CONFIG = new SamplerConfig(); + public static final transient SamplerConfig DEFAULT_CONFIG = new SamplerConfig() {{ + setTraceEnable(true); + setMetricsEnable(true); + }}; - private static final transient Logger log = LoggerFactory.getLogger(CapaMeterProviderBuilder.class); + public static final transient SamplerConfig CONFIG = new SamplerConfig(); public static final transient Supplier DEFAULT_SUPPLIER = () -> { - try { - String storeName = Optional.ofNullable(CapaProperties.COMPONENT_PROPERTIES_SUPPLIER.apply("configuration") - .getProperty( - "CONFIGURATION_COMPONENT_STORE_NAME")) - .orElse("UN_CONFIGURED_STORE_CONFIG_NAME"); - Optional hooksOptional = Mixer.configurationHooksNullable(); - if (hooksOptional.isPresent()) { - List> config = hooksOptional.get().getConfiguration(storeName, - null, - Collections.singletonList(FILE_PATH), - null, - "", - "", - TypeRef.get(SamplerConfig.class)).block(); - if (!config.isEmpty()) { - SamplerConfig item = config.get(0).getContent(); - return item == null ? DEFAULT_CONFIG : item; - } + return CONFIG; + }; + + private static final transient Logger log = LoggerFactory.getLogger(CapaMeterProviderBuilder.class); + + + static { + Mixer.configurationHooksNullable().ifPresent(hooks -> { + try { + subscribeConfiguration(hooks, hooks.defaultConfigurationAppId(), true); + } catch (Throwable throwable) { + log.warn("Fail to load global telemetry config. Dynamic global config is disabled for capa telemetry.", + throwable); + } + try { + subscribeConfiguration(hooks, + CapaProperties.COMPONENT_PROPERTIES_SUPPLIER.apply(COMMON_FILE_SUFFIX).getProperty("appId"), + false); + } catch (Throwable throwable) { + log.warn("Fail to load global telemetry config. Dynamic global config is disabled for capa telemetry.", + throwable); } - } catch (Throwable throwable) { - log.warn("Fail to load config item. Dynamic config is disabled for capa telemetry.", throwable); - } + }); - return DEFAULT_CONFIG; - }; + } - private static final long serialVersionUID = -2113523925814197551L; + private Boolean metricsEnable; - private boolean metricsEnable = true; + private Boolean traceEnable; - private boolean traceEnable = true; + private static void subscribeConfiguration(ConfigurationHooks configurationHooks, String appId, boolean prior) { + String storeName = configurationHooks.registryStoreNames().get(0); + Flux> configFlux = configurationHooks.subscribeConfiguration( + storeName, + appId, + Lists.newArrayList(FILE_PATH), + null, + StringUtils.EMPTY, + StringUtils.EMPTY, + TypeRef.get(SamplerConfig.class)); + configFlux.subscribe(resp -> { + if (CollectionUtils.isNotEmpty(resp.getItems())) { + SamplerConfig config = resp.getItems().get(0).getContent(); + if (config != null) { + if (config.metricsEnable != null && (prior || CONFIG.metricsEnable == null)) { + CONFIG.metricsEnable = config.metricsEnable; + } + if (config.traceEnable != null && (prior || CONFIG.traceEnable == null)) { + CONFIG.traceEnable = config.traceEnable; + } + } + } + }); + } - public boolean isMetricsEnable() { - return metricsEnable; + public Boolean isMetricsEnable() { + return metricsEnable == null ? DEFAULT_CONFIG.metricsEnable : metricsEnable; } public void setMetricsEnable(boolean metricsEnable) { this.metricsEnable = metricsEnable; } - public boolean isTraceEnable() { - return traceEnable; + public Boolean isTraceEnable() { + return traceEnable == null ? DEFAULT_CONFIG.traceEnable : traceEnable; } public void setTraceEnable(boolean traceEnable) { diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/context/CapaContextPropagatorSettings.java b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/context/CapaContextPropagatorSettings.java index bba38ab..93b2c68 100644 --- a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/context/CapaContextPropagatorSettings.java +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/context/CapaContextPropagatorSettings.java @@ -23,8 +23,7 @@ */ public interface CapaContextPropagatorSettings { - // FIXME: 2021/11/28 change to capa-component-telemetry-context.json - String FILE_PATH = "/capa-context.json"; + String FILE_PATH = "/capa-component-telemetry-context.json"; /** * Replace the whole context config. diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMeterProviderSettings.java b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMeterProviderSettings.java index e190690..3b36b63 100644 --- a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMeterProviderSettings.java +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMeterProviderSettings.java @@ -25,8 +25,7 @@ */ public interface CapaMeterProviderSettings { - // FIXME: 2021/11/28 change to capa-component-telemetry-meter.json - String FILE_PATH ="/capa-meter.json"; + String FILE_PATH ="/capa-component-telemetry-meter.json"; /** * Replace the whole config for the meter. diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/trace/CapaTracerProviderSettings.java b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/trace/CapaTracerProviderSettings.java index f77b840..a98cf06 100644 --- a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/trace/CapaTracerProviderSettings.java +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/trace/CapaTracerProviderSettings.java @@ -27,8 +27,7 @@ */ public interface CapaTracerProviderSettings { - // FIXME: 2021/11/28 change to capa-component-telemetry-tracer.json - String FILE_PATH = "/capa-tracer.json"; + String FILE_PATH = "/capa-component-telemetry-tracer.json"; /** * Replace the whole config for the meter. diff --git a/sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/SamplerConfigTest.java b/sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/SamplerConfigTest.java new file mode 100644 index 0000000..c8c4a59 --- /dev/null +++ b/sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/SamplerConfigTest.java @@ -0,0 +1,38 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package group.rxcloud.capa.component.telemetry; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; + +/** + * @author: chenyijiang + * @date: 2021/12/2 12:33 + */ +public class SamplerConfigTest { + + @Test + public void isMetricsEnable() { + assertFalse(SamplerConfig.DEFAULT_SUPPLIER.get().isMetricsEnable()); + } + + @Test + public void isTraceEnable() { + assertFalse(SamplerConfig.DEFAULT_SUPPLIER.get().isTraceEnable()); + } +} \ No newline at end of file diff --git a/sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/TestMixerProvider.java b/sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/TestMixerProvider.java new file mode 100644 index 0000000..2f1ffe5 --- /dev/null +++ b/sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/TestMixerProvider.java @@ -0,0 +1,87 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package group.rxcloud.capa.component.telemetry; + +import com.google.common.collect.Lists; +import group.rxcloud.capa.infrastructure.hook.ConfigurationHooks; +import group.rxcloud.capa.infrastructure.hook.Mixer; +import group.rxcloud.capa.infrastructure.hook.TelemetryHooks; +import group.rxcloud.cloudruntimes.domain.core.configuration.ConfigurationItem; +import group.rxcloud.cloudruntimes.domain.core.configuration.SubConfigurationResp; +import group.rxcloud.cloudruntimes.utils.TypeRef; +import reactor.core.publisher.Flux; + +import java.util.List; +import java.util.Map; + +/** + * @author: chenyijiang + * @date: 2021/12/2 12:39 + */ +public class TestMixerProvider implements Mixer.MixerProvider { + + SamplerConfig app = new SamplerConfig() {{setMetricsEnable(false);}}; + SamplerConfig global = new SamplerConfig() {{setTraceEnable(false); setMetricsEnable(true);}}; + + + private ConfigurationHooks configurationHooks = new ConfigurationHooks() { + @Override + public List registryStoreNames() { + return Lists.newArrayList("QConfig"); + } + + @Override + public String defaultConfigurationAppId() { + return "1234567"; + } + + @Override + public Flux> subscribeConfiguration(String storeName, String appId, List keys, Map metadata, String group, String label, TypeRef type) { + + if (type.getType() == SamplerConfig.class) { + if ("123".equals(appId)) { + return Flux.just(getSubscribeResponse(global)); + } + if (defaultConfigurationAppId().equals(appId)) { + return Flux.just(getSubscribeResponse(app)); + } + return Flux.just(null); + } + return ConfigurationHooks.super.subscribeConfiguration(storeName, appId, keys, metadata, type); + } + }; + + private SubConfigurationResp getSubscribeResponse(SamplerConfig samplerConfig) { + SubConfigurationResp subConfigurationResp = new SubConfigurationResp<>(); + ConfigurationItem item = new ConfigurationItem(); + item.setContent(samplerConfig); + subConfigurationResp.setItems(Lists.newArrayList(item)); + return subConfigurationResp; + } + + @Override + public ConfigurationHooks provideConfigurationHooks() { + return configurationHooks; + } + + @Override + public TelemetryHooks provideTelemetryHooks() { + return null; + } + + +} diff --git a/sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/trace/CapaTracerProviderBuilderTest.java b/sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/trace/CapaTracerProviderBuilderTest.java index 1a19cc6..f4f2232 100644 --- a/sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/trace/CapaTracerProviderBuilderTest.java +++ b/sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/trace/CapaTracerProviderBuilderTest.java @@ -116,6 +116,7 @@ public boolean matches(ReadableSpan span) { @Test public void buildFromTraceConfig() { CapaTracerProvider provider = new CapaTracerProviderBuilder() + .setSamplerConfig(() -> SamplerConfig.DEFAULT_CONFIG) .buildTracerProvider(); Span span = provider.tracerBuilder("test") diff --git a/sdk-component/src/test/resources/capa-component-telemetry-common.properties b/sdk-component/src/test/resources/capa-component-telemetry-common.properties new file mode 100644 index 0000000..a277f74 --- /dev/null +++ b/sdk-component/src/test/resources/capa-component-telemetry-common.properties @@ -0,0 +1 @@ +appId=123 \ No newline at end of file diff --git a/sdk-component/src/test/resources/capa-context.json b/sdk-component/src/test/resources/capa-component-telemetry-context.json similarity index 100% rename from sdk-component/src/test/resources/capa-context.json rename to sdk-component/src/test/resources/capa-component-telemetry-context.json diff --git a/sdk-component/src/test/resources/capa-meter.json b/sdk-component/src/test/resources/capa-component-telemetry-meter.json similarity index 100% rename from sdk-component/src/test/resources/capa-meter.json rename to sdk-component/src/test/resources/capa-component-telemetry-meter.json diff --git a/sdk-component/src/test/resources/capa-tracer.json b/sdk-component/src/test/resources/capa-component-telemetry-tracer.json similarity index 100% rename from sdk-component/src/test/resources/capa-tracer.json rename to sdk-component/src/test/resources/capa-component-telemetry-tracer.json diff --git a/sdk-component/src/test/resources/capa-infrastructure-mixer.properties b/sdk-component/src/test/resources/capa-infrastructure-mixer.properties new file mode 100644 index 0000000..216b8de --- /dev/null +++ b/sdk-component/src/test/resources/capa-infrastructure-mixer.properties @@ -0,0 +1 @@ +group.rxcloud.capa.infrastructure.hook.Mixer$MixerProvider=group.rxcloud.capa.component.telemetry.TestMixerProvider \ No newline at end of file diff --git a/sdk-infrastructure/pom.xml b/sdk-infrastructure/pom.xml index 4d892ae..30f4bb9 100644 --- a/sdk-infrastructure/pom.xml +++ b/sdk-infrastructure/pom.xml @@ -23,7 +23,7 @@ capa-parent group.rxcloud - 1.0.8-alpha-1 + 1.0.8-alpha-2 capa-sdk-infrastructure diff --git a/sdk-spi-demo/pom.xml b/sdk-spi-demo/pom.xml index 0f22976..cdfaf90 100644 --- a/sdk-spi-demo/pom.xml +++ b/sdk-spi-demo/pom.xml @@ -23,7 +23,7 @@ capa-parent group.rxcloud - 1.0.8-alpha-1 + 1.0.8-alpha-2 capa-sdk-spi-demo diff --git a/sdk-spi/pom.xml b/sdk-spi/pom.xml index f2811cc..d77b534 100644 --- a/sdk-spi/pom.xml +++ b/sdk-spi/pom.xml @@ -23,7 +23,7 @@ capa-parent group.rxcloud - 1.0.8-alpha-1 + 1.0.8-alpha-2 capa-sdk-spi diff --git a/sdk-springboot/pom.xml b/sdk-springboot/pom.xml index 5cd1112..b39ac27 100644 --- a/sdk-springboot/pom.xml +++ b/sdk-springboot/pom.xml @@ -23,7 +23,7 @@ capa-parent group.rxcloud - 1.0.8-alpha-1 + 1.0.8-alpha-2 sdk-springboot diff --git a/sdk/pom.xml b/sdk/pom.xml index dfa787f..b03d649 100644 --- a/sdk/pom.xml +++ b/sdk/pom.xml @@ -23,7 +23,7 @@ group.rxcloud capa-parent - 1.0.8-alpha-1 + 1.0.8-alpha-2 capa-sdk diff --git a/sdk/src/main/java/group/rxcloud/capa/telemetry/CapaTelemetryClientGlobal.java b/sdk/src/main/java/group/rxcloud/capa/telemetry/CapaTelemetryClientGlobal.java index 5755b13..cc71ca9 100644 --- a/sdk/src/main/java/group/rxcloud/capa/telemetry/CapaTelemetryClientGlobal.java +++ b/sdk/src/main/java/group/rxcloud/capa/telemetry/CapaTelemetryClientGlobal.java @@ -16,7 +16,6 @@ */ package group.rxcloud.capa.telemetry; -import io.opentelemetry.api.GlobalOpenTelemetry; import io.opentelemetry.api.OpenTelemetry; import io.opentelemetry.api.metrics.Meter; import io.opentelemetry.api.metrics.MeterProvider; @@ -31,30 +30,12 @@ public class CapaTelemetryClientGlobal implements CapaTelemetryClient, OpenTelemetry { // noop as default. - private static volatile CapaTelemetryClientGlobal instance; - private TracerProvider tracerProvider = TracerProvider.noop(); private MeterProvider meterProvider = MeterProvider.noop(); private ContextPropagators contextPropagators = ContextPropagators.noop(); - public static CapaTelemetryClientGlobal getOrCreate() { - if (instance == null) { - synchronized (CapaTelemetryClientGlobal.class) { - if (instance == null) { - instance = (CapaTelemetryClientGlobal) new CapaTelemetryClientBuilder().build(); - GlobalOpenTelemetry.set(instance); - } - } - } - return instance; - } - - static void set(CapaTelemetryClientGlobal capaTelemetryClient) { - instance = capaTelemetryClient; - } - protected List registryNames; @Override diff --git a/sdk/src/test/java/group/rxcloud/capa/telemetry/CapaTelemetryClientGlobalTest.java b/sdk/src/test/java/group/rxcloud/capa/telemetry/CapaTelemetryClientGlobalTest.java index 579d125..4c2d13e 100644 --- a/sdk/src/test/java/group/rxcloud/capa/telemetry/CapaTelemetryClientGlobalTest.java +++ b/sdk/src/test/java/group/rxcloud/capa/telemetry/CapaTelemetryClientGlobalTest.java @@ -31,13 +31,12 @@ public class CapaTelemetryClientGlobalTest { @Test public void getOrCreate() { - CapaTelemetryClient client = CapaTelemetryClientGlobal.getOrCreate(); + CapaTelemetryClient client = new CapaTelemetryClientBuilder().build(); assertTrue(client instanceof CapaTelemetryClientGlobal); assertNotNull(client.getContextPropagators()); assertNotNull(((CapaTelemetryClientGlobal) client).getPropagators()); assertNotNull(((CapaTelemetryClientGlobal) client).getMeterProvider()); assertNotNull(((CapaTelemetryClientGlobal) client).getTracerProvider()); assertNotNull(GlobalOpenTelemetry.get()); - assertEquals(client, CapaTelemetryClientGlobal.getOrCreate()); } } \ No newline at end of file From fde6b5a6db87f37c4e86cc5ecda04f5ea889e7e2 Mon Sep 17 00:00:00 2001 From: chenyijiang Date: Thu, 2 Dec 2021 18:49:51 +0800 Subject: [PATCH 12/17] Add merged properties config. --- .../component/telemetry/SamplerConfig.java | 82 +++++++--------- .../telemetry/TestMixerProvider.java | 9 +- .../hook/MergedPropertiesConfig.java | 93 +++++++++++++++++++ 3 files changed, 130 insertions(+), 54 deletions(-) create mode 100644 sdk-infrastructure/src/main/java/group/rxcloud/capa/infrastructure/hook/MergedPropertiesConfig.java diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/SamplerConfig.java b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/SamplerConfig.java index 94d4bfb..f321db8 100644 --- a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/SamplerConfig.java +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/SamplerConfig.java @@ -16,18 +16,12 @@ */ package group.rxcloud.capa.component.telemetry; -import com.google.common.collect.Lists; import group.rxcloud.capa.component.telemetry.metrics.CapaMeterProviderBuilder; import group.rxcloud.capa.infrastructure.CapaProperties; -import group.rxcloud.capa.infrastructure.hook.ConfigurationHooks; +import group.rxcloud.capa.infrastructure.hook.MergedPropertiesConfig; import group.rxcloud.capa.infrastructure.hook.Mixer; -import group.rxcloud.cloudruntimes.domain.core.configuration.SubConfigurationResp; -import group.rxcloud.cloudruntimes.utils.TypeRef; -import org.apache.commons.collections.CollectionUtils; -import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import reactor.core.publisher.Flux; import java.io.Serializable; import java.util.function.Supplier; @@ -37,12 +31,6 @@ */ public class SamplerConfig implements Serializable { - private static final long serialVersionUID = -2113523925814197551L; - - public static final transient String FILE_PATH = "capa-component-telemetry-sample.properties"; - - public static final transient String COMMON_FILE_SUFFIX = "telemetry-common"; - /** * Sample all data as default. */ @@ -53,25 +41,44 @@ public class SamplerConfig implements Serializable { public static final transient SamplerConfig CONFIG = new SamplerConfig(); - public static final transient Supplier DEFAULT_SUPPLIER = () -> { - return CONFIG; - }; + private static final long serialVersionUID = -2113523925814197551L; private static final transient Logger log = LoggerFactory.getLogger(CapaMeterProviderBuilder.class); + public static transient Supplier DEFAULT_SUPPLIER = () -> { + return CONFIG; + }; static { Mixer.configurationHooksNullable().ifPresent(hooks -> { + + String fileName = "capa-component-telemetry-sample.properties"; + + String suffix = "telemetry-common"; + try { - subscribeConfiguration(hooks, hooks.defaultConfigurationAppId(), true); - } catch (Throwable throwable) { - log.warn("Fail to load global telemetry config. Dynamic global config is disabled for capa telemetry.", - throwable); - } - try { - subscribeConfiguration(hooks, - CapaProperties.COMPONENT_PROPERTIES_SUPPLIER.apply(COMMON_FILE_SUFFIX).getProperty("appId"), - false); + MergedPropertiesConfig config = new MergedPropertiesConfig(fileName, hooks + .defaultConfigurationAppId(), + CapaProperties.COMPONENT_PROPERTIES_SUPPLIER.apply(suffix).getProperty("appId")); + String metricKey = "metricsEnable"; + String traceKey = "traceEnable"; + SamplerConfig dynamicConfig = new SamplerConfig() { + @Override + public Boolean isMetricsEnable() { + return !config.containsKey(metricKey) || Boolean.TRUE.toString() + .equalsIgnoreCase(config.get(metricKey)); + } + + @Override + public Boolean isTraceEnable() { + return !config.containsKey(traceKey) || Boolean.TRUE.toString() + .equalsIgnoreCase(config.get(traceKey)); + } + }; + + DEFAULT_SUPPLIER = () -> { + return dynamicConfig; + }; } catch (Throwable throwable) { log.warn("Fail to load global telemetry config. Dynamic global config is disabled for capa telemetry.", throwable); @@ -84,31 +91,6 @@ public class SamplerConfig implements Serializable { private Boolean traceEnable; - private static void subscribeConfiguration(ConfigurationHooks configurationHooks, String appId, boolean prior) { - String storeName = configurationHooks.registryStoreNames().get(0); - Flux> configFlux = configurationHooks.subscribeConfiguration( - storeName, - appId, - Lists.newArrayList(FILE_PATH), - null, - StringUtils.EMPTY, - StringUtils.EMPTY, - TypeRef.get(SamplerConfig.class)); - configFlux.subscribe(resp -> { - if (CollectionUtils.isNotEmpty(resp.getItems())) { - SamplerConfig config = resp.getItems().get(0).getContent(); - if (config != null) { - if (config.metricsEnable != null && (prior || CONFIG.metricsEnable == null)) { - CONFIG.metricsEnable = config.metricsEnable; - } - if (config.traceEnable != null && (prior || CONFIG.traceEnable == null)) { - CONFIG.traceEnable = config.traceEnable; - } - } - } - }); - } - public Boolean isMetricsEnable() { return metricsEnable == null ? DEFAULT_CONFIG.metricsEnable : metricsEnable; } diff --git a/sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/TestMixerProvider.java b/sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/TestMixerProvider.java index 2f1ffe5..3518ada 100644 --- a/sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/TestMixerProvider.java +++ b/sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/TestMixerProvider.java @@ -27,6 +27,7 @@ import java.util.List; import java.util.Map; +import java.util.Properties; /** * @author: chenyijiang @@ -34,8 +35,8 @@ */ public class TestMixerProvider implements Mixer.MixerProvider { - SamplerConfig app = new SamplerConfig() {{setMetricsEnable(false);}}; - SamplerConfig global = new SamplerConfig() {{setTraceEnable(false); setMetricsEnable(true);}}; + Properties app = new Properties() {{put("metricsEnable", false);}}; + Properties global = new Properties() {{put("traceEnable", false); put("metricsEnable", true);}}; private ConfigurationHooks configurationHooks = new ConfigurationHooks() { @@ -52,7 +53,7 @@ public String defaultConfigurationAppId() { @Override public Flux> subscribeConfiguration(String storeName, String appId, List keys, Map metadata, String group, String label, TypeRef type) { - if (type.getType() == SamplerConfig.class) { + if (type.getType() == Properties.class) { if ("123".equals(appId)) { return Flux.just(getSubscribeResponse(global)); } @@ -65,7 +66,7 @@ public Flux> subscribeConfiguration(String storeName } }; - private SubConfigurationResp getSubscribeResponse(SamplerConfig samplerConfig) { + private SubConfigurationResp getSubscribeResponse(Properties samplerConfig) { SubConfigurationResp subConfigurationResp = new SubConfigurationResp<>(); ConfigurationItem item = new ConfigurationItem(); item.setContent(samplerConfig); diff --git a/sdk-infrastructure/src/main/java/group/rxcloud/capa/infrastructure/hook/MergedPropertiesConfig.java b/sdk-infrastructure/src/main/java/group/rxcloud/capa/infrastructure/hook/MergedPropertiesConfig.java new file mode 100644 index 0000000..91fd138 --- /dev/null +++ b/sdk-infrastructure/src/main/java/group/rxcloud/capa/infrastructure/hook/MergedPropertiesConfig.java @@ -0,0 +1,93 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package group.rxcloud.capa.infrastructure.hook; + +import group.rxcloud.cloudruntimes.domain.core.configuration.SubConfigurationResp; +import group.rxcloud.cloudruntimes.utils.TypeRef; +import reactor.core.publisher.Flux; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; +import java.util.concurrent.atomic.AtomicReferenceArray; + +/** + * Config provider to merge multiple properties file which takes the input order as their priority. + */ +public class MergedPropertiesConfig { + + private final String fileName; + + private final AtomicReferenceArray properties; + + private final Object lock = new Object(); + + private volatile Map merged; + + public MergedPropertiesConfig(String fileName, String... appIds) { + this.fileName = fileName; + properties = new AtomicReferenceArray<>(appIds.length); + merged = new HashMap<>(); + Mixer.configurationHooksNullable().ifPresent(hooks -> { + for (int i = 0; i < appIds.length; i++) { + subscribeConfigurationByAppId(hooks, appIds[i], i); + } + }); + } + + public boolean containsKey(String key) { + return merged.containsKey(key); + } + + public String get(String key) { + return merged.get(key); + } + + private void subscribeConfigurationByAppId(ConfigurationHooks configurationHooks, String appId, int index) { + String storeName = configurationHooks.registryStoreNames().get(0); + + Flux> configFlux = configurationHooks.subscribeConfiguration( + storeName, + appId, + Collections.singletonList(fileName), + null, + "", + "", + TypeRef.get(Properties.class)); + + + configFlux.subscribe(resp -> { + if (!resp.getItems().isEmpty()) { + properties.set(index, resp.getItems().get(0).getContent()); + } else { + properties.set(index, null); + } + + synchronized (lock) { + Map merged = new HashMap<>(); + for (int i = 0; i < properties.length(); i++) { + Properties item = properties.get(i); + if (item != null) { + item.forEach((k, v) -> merged.putIfAbsent(String.valueOf(k), String.valueOf(v))); + } + } + this.merged = merged; + } + }); + } +} From 40c8b6e6cbeed642a98f67958dee6bf242490b13 Mon Sep 17 00:00:00 2001 From: = Date: Thu, 2 Dec 2021 22:54:39 +0800 Subject: [PATCH 13/17] update log format --- examples/pom.xml | 10 ++++---- .../examples/{telemetry => log}/DemoLog.java | 2 +- .../resources/{log4j2.xml => logback.xml} | 23 +++++++---------- examples/src/main/resources/xml/logback.xml | 2 +- sdk-component/pom.xml | 2 +- .../{agent => }/CapaLog4jAppenderAgent.java | 12 ++++----- .../{agent => }/CapaLogbackAppenderAgent.java | 16 ++++++------ .../CapaLog4jAppenderAgentTest.java | 2 +- .../CapaLogbackAppenderAgentTest.java | 4 +-- .../{agent => }/TestCapaLog4jAppender.java | 4 +-- .../{agent => }/TestCapaLogbackAppender.java | 4 +-- .../resources/capa-component-log.properties | 4 +-- sdk-spi-demo/pom.xml | 2 +- .../capa/spi/demo/log/DemoLog4jAppender.java | 4 +-- .../spi/demo/log/DemoLogbackAppender.java | 4 +-- .../resources/capa-component-log.properties | 4 +-- sdk-spi/pom.xml | 17 +++++++------ .../capa/spi/log/CapaLog4jAppenderSpi.java | 23 +++++++++++++++++ .../capa/spi/log/CapaLogbackAppenderSpi.java | 25 +++++++++++++++++++ .../configuration/LogSwitchConfiguration.java | 2 +- .../capa/spi}/log/enums/CapaLogLevel.java | 2 +- .../capa/spi}/log/manager/LogManager.java | 6 ++--- 22 files changed, 108 insertions(+), 66 deletions(-) rename examples/src/main/java/group/rxcloud/capa/examples/{telemetry => log}/DemoLog.java (97%) rename examples/src/main/resources/{log4j2.xml => logback.xml} (65%) rename sdk-component/src/main/java/group/rxcloud/capa/component/log/{agent => }/CapaLog4jAppenderAgent.java (93%) rename sdk-component/src/main/java/group/rxcloud/capa/component/log/{agent => }/CapaLogbackAppenderAgent.java (87%) rename sdk-component/src/test/java/group/rxcloud/capa/component/log/{agent => }/CapaLog4jAppenderAgentTest.java (97%) rename sdk-component/src/test/java/group/rxcloud/capa/component/log/{agent => }/CapaLogbackAppenderAgentTest.java (94%) rename sdk-component/src/test/java/group/rxcloud/capa/component/log/{agent => }/TestCapaLog4jAppender.java (92%) rename sdk-component/src/test/java/group/rxcloud/capa/component/log/{agent => }/TestCapaLogbackAppender.java (92%) create mode 100644 sdk-spi/src/main/java/group/rxcloud/capa/spi/log/CapaLog4jAppenderSpi.java create mode 100644 sdk-spi/src/main/java/group/rxcloud/capa/spi/log/CapaLogbackAppenderSpi.java rename {sdk-component/src/main/java/group/rxcloud/capa/component => sdk-spi/src/main/java/group/rxcloud/capa/spi}/log/configuration/LogSwitchConfiguration.java (98%) rename {sdk-component/src/main/java/group/rxcloud/capa/component => sdk-spi/src/main/java/group/rxcloud/capa/spi}/log/enums/CapaLogLevel.java (97%) rename {sdk-component/src/main/java/group/rxcloud/capa/component => sdk-spi/src/main/java/group/rxcloud/capa/spi}/log/manager/LogManager.java (96%) diff --git a/examples/pom.xml b/examples/pom.xml index 6eed8d9..61176e1 100644 --- a/examples/pom.xml +++ b/examples/pom.xml @@ -31,7 +31,7 @@ capa-sdk-examples - 2.8.2 + 2.14.1 1.1.7 1.7.32 @@ -61,17 +61,17 @@ - + - + org.projectlombok diff --git a/examples/src/main/java/group/rxcloud/capa/examples/telemetry/DemoLog.java b/examples/src/main/java/group/rxcloud/capa/examples/log/DemoLog.java similarity index 97% rename from examples/src/main/java/group/rxcloud/capa/examples/telemetry/DemoLog.java rename to examples/src/main/java/group/rxcloud/capa/examples/log/DemoLog.java index 93ef506..b64ac9d 100644 --- a/examples/src/main/java/group/rxcloud/capa/examples/telemetry/DemoLog.java +++ b/examples/src/main/java/group/rxcloud/capa/examples/log/DemoLog.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package group.rxcloud.capa.examples.telemetry; +package group.rxcloud.capa.examples.log; import lombok.extern.slf4j.Slf4j; diff --git a/examples/src/main/resources/log4j2.xml b/examples/src/main/resources/logback.xml similarity index 65% rename from examples/src/main/resources/log4j2.xml rename to examples/src/main/resources/logback.xml index 3587353..37a87f2 100644 --- a/examples/src/main/resources/log4j2.xml +++ b/examples/src/main/resources/logback.xml @@ -15,17 +15,12 @@ See the License for the specific language governing permissions and limitations under the License. --> - - - - - - - - - - - - - - \ No newline at end of file + + + + + + + + + \ No newline at end of file diff --git a/examples/src/main/resources/xml/logback.xml b/examples/src/main/resources/xml/logback.xml index 268d16c..c2ddd8a 100644 --- a/examples/src/main/resources/xml/logback.xml +++ b/examples/src/main/resources/xml/logback.xml @@ -17,7 +17,7 @@ --> - + diff --git a/sdk-component/pom.xml b/sdk-component/pom.xml index 608b900..0808c0c 100644 --- a/sdk-component/pom.xml +++ b/sdk-component/pom.xml @@ -33,7 +33,7 @@ 4.9.1 1.4.10 - 2.8.2 + 2.14.1 1.1.7 19.0 3.11 diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/log/agent/CapaLog4jAppenderAgent.java b/sdk-component/src/main/java/group/rxcloud/capa/component/log/CapaLog4jAppenderAgent.java similarity index 93% rename from sdk-component/src/main/java/group/rxcloud/capa/component/log/agent/CapaLog4jAppenderAgent.java rename to sdk-component/src/main/java/group/rxcloud/capa/component/log/CapaLog4jAppenderAgent.java index 2957661..7e31d3d 100644 --- a/sdk-component/src/main/java/group/rxcloud/capa/component/log/agent/CapaLog4jAppenderAgent.java +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/log/CapaLog4jAppenderAgent.java @@ -14,10 +14,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package group.rxcloud.capa.component.log.agent; +package group.rxcloud.capa.component.log; -import group.rxcloud.capa.component.log.enums.CapaLogLevel; -import group.rxcloud.capa.component.log.manager.LogManager; import group.rxcloud.capa.infrastructure.CapaClassLoader; import org.apache.logging.log4j.core.Appender; import org.apache.logging.log4j.core.Filter; @@ -30,7 +28,6 @@ import org.apache.logging.log4j.core.config.plugins.PluginFactory; import java.io.Serializable; -import java.util.Optional; /** * The abstract log4j appender. Extend this and provide your specific impl. @@ -100,12 +97,13 @@ public static CapaLog4jAppender buildCapaLog4jAppender() { @Override public void append(LogEvent event) { - if (event != null && event.getLevel() != null) { + logAppender.append(event); + /* if (event != null && event.getLevel() != null) { Optional capaLogLevel = CapaLogLevel.toCapaLogLevel(event.getLevel().name()); if (capaLogLevel.isPresent() && LogManager.whetherLogsCanBeOutput(capaLogLevel.get())) { logAppender.appendLog(event); } - } + }*/ } /** @@ -118,6 +116,6 @@ public interface CapaLog4jAppender { * * @param event The log event. */ - void appendLog(LogEvent event); + void append(LogEvent event); } } diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/log/agent/CapaLogbackAppenderAgent.java b/sdk-component/src/main/java/group/rxcloud/capa/component/log/CapaLogbackAppenderAgent.java similarity index 87% rename from sdk-component/src/main/java/group/rxcloud/capa/component/log/agent/CapaLogbackAppenderAgent.java rename to sdk-component/src/main/java/group/rxcloud/capa/component/log/CapaLogbackAppenderAgent.java index fb9d86a..b56048f 100644 --- a/sdk-component/src/main/java/group/rxcloud/capa/component/log/agent/CapaLogbackAppenderAgent.java +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/log/CapaLogbackAppenderAgent.java @@ -14,16 +14,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package group.rxcloud.capa.component.log.agent; +package group.rxcloud.capa.component.log; import ch.qos.logback.classic.spi.ILoggingEvent; import ch.qos.logback.core.UnsynchronizedAppenderBase; -import group.rxcloud.capa.component.log.enums.CapaLogLevel; -import group.rxcloud.capa.component.log.manager.LogManager; import group.rxcloud.capa.infrastructure.CapaClassLoader; -import java.util.Optional; - /** * The agent of the logback impl. */ @@ -64,12 +60,13 @@ public static CapaLogbackAppender buildCapaLogbackAppender() { */ @Override protected void append(ILoggingEvent event) { - if (event != null && event.getLevel() != null) { + logbackAppender.append(event); + /* if (event != null && event.getLevel() != null) { Optional capaLogLevel = CapaLogLevel.toCapaLogLevel(event.getLevel().levelStr); if (capaLogLevel.isPresent() && LogManager.whetherLogsCanBeOutput(capaLogLevel.get())) { - logbackAppender.appendLog(event); + logbackAppender.append(event); } - } + }*/ } /** @@ -82,6 +79,7 @@ public interface CapaLogbackAppender { * * @param event The log event. */ - void appendLog(ILoggingEvent event); + void append(ILoggingEvent event); } } + diff --git a/sdk-component/src/test/java/group/rxcloud/capa/component/log/agent/CapaLog4jAppenderAgentTest.java b/sdk-component/src/test/java/group/rxcloud/capa/component/log/CapaLog4jAppenderAgentTest.java similarity index 97% rename from sdk-component/src/test/java/group/rxcloud/capa/component/log/agent/CapaLog4jAppenderAgentTest.java rename to sdk-component/src/test/java/group/rxcloud/capa/component/log/CapaLog4jAppenderAgentTest.java index 2b1ab7d..2cacb81 100644 --- a/sdk-component/src/test/java/group/rxcloud/capa/component/log/agent/CapaLog4jAppenderAgentTest.java +++ b/sdk-component/src/test/java/group/rxcloud/capa/component/log/CapaLog4jAppenderAgentTest.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package group.rxcloud.capa.component.log.agent; +package group.rxcloud.capa.component.log; import org.apache.logging.log4j.core.Filter; import org.apache.logging.log4j.core.Layout; diff --git a/sdk-component/src/test/java/group/rxcloud/capa/component/log/agent/CapaLogbackAppenderAgentTest.java b/sdk-component/src/test/java/group/rxcloud/capa/component/log/CapaLogbackAppenderAgentTest.java similarity index 94% rename from sdk-component/src/test/java/group/rxcloud/capa/component/log/agent/CapaLogbackAppenderAgentTest.java rename to sdk-component/src/test/java/group/rxcloud/capa/component/log/CapaLogbackAppenderAgentTest.java index e8f3759..d88b99f 100644 --- a/sdk-component/src/test/java/group/rxcloud/capa/component/log/agent/CapaLogbackAppenderAgentTest.java +++ b/sdk-component/src/test/java/group/rxcloud/capa/component/log/CapaLogbackAppenderAgentTest.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package group.rxcloud.capa.component.log.agent; +package group.rxcloud.capa.component.log; import ch.qos.logback.classic.spi.ILoggingEvent; import org.junit.jupiter.api.Assertions; @@ -34,6 +34,6 @@ void testAppend() { CapaLogbackAppenderAgent.CapaLogbackAppender capaLogbackAppender = CapaLogbackAppenderAgent.buildCapaLogbackAppender(); ILoggingEvent logEvent = Mockito.mock(ILoggingEvent.class); Mockito.when(logEvent.getFormattedMessage()).thenReturn("TEST"); - capaLogbackAppender.appendLog(logEvent); + capaLogbackAppender.append(logEvent); } } \ No newline at end of file diff --git a/sdk-component/src/test/java/group/rxcloud/capa/component/log/agent/TestCapaLog4jAppender.java b/sdk-component/src/test/java/group/rxcloud/capa/component/log/TestCapaLog4jAppender.java similarity index 92% rename from sdk-component/src/test/java/group/rxcloud/capa/component/log/agent/TestCapaLog4jAppender.java rename to sdk-component/src/test/java/group/rxcloud/capa/component/log/TestCapaLog4jAppender.java index 4dbdca6..8ecff55 100644 --- a/sdk-component/src/test/java/group/rxcloud/capa/component/log/agent/TestCapaLog4jAppender.java +++ b/sdk-component/src/test/java/group/rxcloud/capa/component/log/TestCapaLog4jAppender.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package group.rxcloud.capa.component.log.agent; +package group.rxcloud.capa.component.log; import org.apache.logging.log4j.core.LogEvent; @@ -24,7 +24,7 @@ public class TestCapaLog4jAppender implements CapaLog4jAppenderAgent.CapaLog4jAppender { @Override - public void appendLog(LogEvent event) { + public void append(LogEvent event) { System.out.println("test log log4j and content is " + event.getMessage().getFormattedMessage()); } } \ No newline at end of file diff --git a/sdk-component/src/test/java/group/rxcloud/capa/component/log/agent/TestCapaLogbackAppender.java b/sdk-component/src/test/java/group/rxcloud/capa/component/log/TestCapaLogbackAppender.java similarity index 92% rename from sdk-component/src/test/java/group/rxcloud/capa/component/log/agent/TestCapaLogbackAppender.java rename to sdk-component/src/test/java/group/rxcloud/capa/component/log/TestCapaLogbackAppender.java index 67f911f..322c3ca 100644 --- a/sdk-component/src/test/java/group/rxcloud/capa/component/log/agent/TestCapaLogbackAppender.java +++ b/sdk-component/src/test/java/group/rxcloud/capa/component/log/TestCapaLogbackAppender.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package group.rxcloud.capa.component.log.agent; +package group.rxcloud.capa.component.log; import ch.qos.logback.classic.spi.ILoggingEvent; @@ -27,7 +27,7 @@ public TestCapaLogbackAppender() { } @Override - public void appendLog(ILoggingEvent event) { + public void append(ILoggingEvent event) { System.out.println("test logback log and content is " + event.getFormattedMessage()); } } diff --git a/sdk-component/src/test/resources/capa-component-log.properties b/sdk-component/src/test/resources/capa-component-log.properties index 17fc340..984eb51 100644 --- a/sdk-component/src/test/resources/capa-component-log.properties +++ b/sdk-component/src/test/resources/capa-component-log.properties @@ -1,2 +1,2 @@ -group.rxcloud.capa.component.log.agent.CapaLog4jAppenderAgent$CapaLog4jAppender=group.rxcloud.capa.component.log.agent.TestCapaLog4jAppender -group.rxcloud.capa.component.log.agent.CapaLogbackAppenderAgent$CapaLogbackAppender=group.rxcloud.capa.component.log.agent.TestCapaLogbackAppender \ No newline at end of file +group.rxcloud.capa.component.log.CapaLog4jAppenderAgent$CapaLog4jAppender=group.rxcloud.capa.component.log.TestCapaLog4jAppender +group.rxcloud.capa.component.log.CapaLogbackAppenderAgent$CapaLogbackAppender=group.rxcloud.capa.component.log.TestCapaLogbackAppender \ No newline at end of file diff --git a/sdk-spi-demo/pom.xml b/sdk-spi-demo/pom.xml index cdfaf90..8c3e393 100644 --- a/sdk-spi-demo/pom.xml +++ b/sdk-spi-demo/pom.xml @@ -31,7 +31,7 @@ capa-sdk-spi-demo - 2.8.2 + 2.14.1 1.1.7 diff --git a/sdk-spi-demo/src/main/java/group/rxcloud/capa/spi/demo/log/DemoLog4jAppender.java b/sdk-spi-demo/src/main/java/group/rxcloud/capa/spi/demo/log/DemoLog4jAppender.java index f94df0f..36de41d 100644 --- a/sdk-spi-demo/src/main/java/group/rxcloud/capa/spi/demo/log/DemoLog4jAppender.java +++ b/sdk-spi-demo/src/main/java/group/rxcloud/capa/spi/demo/log/DemoLog4jAppender.java @@ -16,13 +16,13 @@ */ package group.rxcloud.capa.spi.demo.log; -import group.rxcloud.capa.component.log.agent.CapaLog4jAppenderAgent; +import group.rxcloud.capa.component.log.CapaLog4jAppenderAgent; import org.apache.logging.log4j.core.LogEvent; public class DemoLog4jAppender implements CapaLog4jAppenderAgent.CapaLog4jAppender { @Override - public void appendLog(LogEvent event) { + public void append(LogEvent event) { System.out.println("Test log log4j and content is " + event.getMessage().getFormattedMessage()); } } diff --git a/sdk-spi-demo/src/main/java/group/rxcloud/capa/spi/demo/log/DemoLogbackAppender.java b/sdk-spi-demo/src/main/java/group/rxcloud/capa/spi/demo/log/DemoLogbackAppender.java index 8e5ee40..32b2d1b 100644 --- a/sdk-spi-demo/src/main/java/group/rxcloud/capa/spi/demo/log/DemoLogbackAppender.java +++ b/sdk-spi-demo/src/main/java/group/rxcloud/capa/spi/demo/log/DemoLogbackAppender.java @@ -17,7 +17,7 @@ package group.rxcloud.capa.spi.demo.log; import ch.qos.logback.classic.spi.ILoggingEvent; -import group.rxcloud.capa.component.log.agent.CapaLogbackAppenderAgent; +import group.rxcloud.capa.component.log.CapaLogbackAppenderAgent; public class DemoLogbackAppender implements CapaLogbackAppenderAgent.CapaLogbackAppender { @@ -25,7 +25,7 @@ public DemoLogbackAppender() { } @Override - public void appendLog(ILoggingEvent event) { + public void append(ILoggingEvent event) { System.out.println("Test logback log and content is " + event.getFormattedMessage()); } } diff --git a/sdk-spi-demo/src/main/resources/capa-component-log.properties b/sdk-spi-demo/src/main/resources/capa-component-log.properties index 795f59b..cf90031 100644 --- a/sdk-spi-demo/src/main/resources/capa-component-log.properties +++ b/sdk-spi-demo/src/main/resources/capa-component-log.properties @@ -1,3 +1,3 @@ # log component -group.rxcloud.capa.component.log.agent.CapaLog4jAppenderAgent$CapaLog4jAppender=group.rxcloud.capa.spi.demo.log.DemoLog4jAppender -group.rxcloud.capa.component.log.agent.CapaLogbackAppenderAgent$CapaLogbackAppender=group.rxcloud.capa.spi.demo.log.DemoLogbackAppender \ No newline at end of file +group.rxcloud.capa.component.log.CapaLog4jAppenderAgent$CapaLog4jAppender=group.rxcloud.capa.spi.demo.log.DemoLog4jAppender +group.rxcloud.capa.component.log.CapaLogbackAppenderAgent$CapaLogbackAppender=group.rxcloud.capa.spi.demo.log.DemoLogbackAppender \ No newline at end of file diff --git a/sdk-spi/pom.xml b/sdk-spi/pom.xml index d77b534..5f34201 100644 --- a/sdk-spi/pom.xml +++ b/sdk-spi/pom.xml @@ -32,6 +32,7 @@ 2.14.1 + 1.1.7 @@ -54,23 +55,25 @@ - + org.apache.logging.log4j log4j-core ${log4j.version} - test + true + - org.apache.logging.log4j - log4j-slf4j-impl - ${log4j.version} - test + ch.qos.logback + logback-classic + + true + ${logback.version} diff --git a/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/CapaLog4jAppenderSpi.java b/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/CapaLog4jAppenderSpi.java new file mode 100644 index 0000000..a51ddec --- /dev/null +++ b/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/CapaLog4jAppenderSpi.java @@ -0,0 +1,23 @@ +package group.rxcloud.capa.spi.log; + +import group.rxcloud.capa.component.log.CapaLog4jAppenderAgent; +import group.rxcloud.capa.spi.log.enums.CapaLogLevel; +import group.rxcloud.capa.spi.log.manager.LogManager; +import org.apache.logging.log4j.core.LogEvent; + +import java.util.Optional; + +public abstract class CapaLog4jAppenderSpi implements CapaLog4jAppenderAgent.CapaLog4jAppender { + + @Override + public void append(LogEvent event) { + if (event != null && event.getLevel()!= null) { + Optional capaLogLevel = CapaLogLevel.toCapaLogLevel(event.getLevel().name()); + if (capaLogLevel.isPresent() && LogManager.whetherLogsCanBeOutput(capaLogLevel.get())) { + this.appendLog(event); + } + } + } + + protected abstract void appendLog(LogEvent event); +} diff --git a/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/CapaLogbackAppenderSpi.java b/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/CapaLogbackAppenderSpi.java new file mode 100644 index 0000000..58af9df --- /dev/null +++ b/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/CapaLogbackAppenderSpi.java @@ -0,0 +1,25 @@ +package group.rxcloud.capa.spi.log; + +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.UnsynchronizedAppenderBase; +import group.rxcloud.capa.component.log.CapaLogbackAppenderAgent; +import group.rxcloud.capa.spi.log.enums.CapaLogLevel; +import group.rxcloud.capa.spi.log.manager.LogManager; + +import java.util.Optional; + +public abstract class CapaLogbackAppenderSpi extends UnsynchronizedAppenderBase + implements CapaLogbackAppenderAgent.CapaLogbackAppender { + + @Override + public void append(ILoggingEvent event) { + if (event != null && event.getLevel() != null) { + Optional capaLogLevel = CapaLogLevel.toCapaLogLevel(event.getLevel().levelStr); + if (capaLogLevel.isPresent() && LogManager.whetherLogsCanBeOutput(capaLogLevel.get())) { + this.append(event); + } + } + } + + protected abstract void appendLog(ILoggingEvent event); +} diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/log/configuration/LogSwitchConfiguration.java b/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/configuration/LogSwitchConfiguration.java similarity index 98% rename from sdk-component/src/main/java/group/rxcloud/capa/component/log/configuration/LogSwitchConfiguration.java rename to sdk-spi/src/main/java/group/rxcloud/capa/spi/log/configuration/LogSwitchConfiguration.java index 9d8d3f2..849a6ea 100644 --- a/sdk-component/src/main/java/group/rxcloud/capa/component/log/configuration/LogSwitchConfiguration.java +++ b/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/configuration/LogSwitchConfiguration.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package group.rxcloud.capa.component.log.configuration; +package group.rxcloud.capa.spi.log.configuration; import com.google.common.collect.Lists; import com.google.common.collect.Maps; diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/log/enums/CapaLogLevel.java b/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/enums/CapaLogLevel.java similarity index 97% rename from sdk-component/src/main/java/group/rxcloud/capa/component/log/enums/CapaLogLevel.java rename to sdk-spi/src/main/java/group/rxcloud/capa/spi/log/enums/CapaLogLevel.java index 099fac3..4c70c3d 100644 --- a/sdk-component/src/main/java/group/rxcloud/capa/component/log/enums/CapaLogLevel.java +++ b/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/enums/CapaLogLevel.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package group.rxcloud.capa.component.log.enums; +package group.rxcloud.capa.spi.log.enums; import java.util.Arrays; import java.util.Optional; diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/log/manager/LogManager.java b/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/manager/LogManager.java similarity index 96% rename from sdk-component/src/main/java/group/rxcloud/capa/component/log/manager/LogManager.java rename to sdk-spi/src/main/java/group/rxcloud/capa/spi/log/manager/LogManager.java index fba8605..c73822c 100644 --- a/sdk-component/src/main/java/group/rxcloud/capa/component/log/manager/LogManager.java +++ b/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/manager/LogManager.java @@ -14,10 +14,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package group.rxcloud.capa.component.log.manager; +package group.rxcloud.capa.spi.log.manager; -import group.rxcloud.capa.component.log.configuration.LogSwitchConfiguration; -import group.rxcloud.capa.component.log.enums.CapaLogLevel; +import group.rxcloud.capa.spi.log.configuration.LogSwitchConfiguration; +import group.rxcloud.capa.spi.log.enums.CapaLogLevel; import java.util.Map; From 58ebe7434b9e93c5c025ef71533cd983c207dca5 Mon Sep 17 00:00:00 2001 From: = Date: Fri, 3 Dec 2021 00:15:44 +0800 Subject: [PATCH 14/17] add common configuration --- examples/pom.xml | 8 +- .../resources/{logback.xml => log4j2.xml} | 23 +++-- examples/src/main/resources/xml/logback.xml | 10 -- pom.xml | 4 + .../component/log/CapaLog4jAppenderAgent.java | 8 +- .../log/CapaLogbackAppenderAgent.java | 6 -- .../hook/MergedPropertiesConfig.java | 4 + .../capa/spi/demo/log/DemoLog4jAppender.java | 6 +- .../spi/demo/log/DemoLogbackAppender.java | 5 +- .../capa/spi/log/CapaLog4jAppenderSpi.java | 2 +- .../capa/spi/log/CapaLogbackAppenderSpi.java | 4 +- .../log/configuration/LogConfiguration.java | 79 ++++++++++++++++ .../configuration/LogSwitchConfiguration.java | 91 ------------------- .../capa/spi/log/manager/LogManager.java | 55 +++++------ 14 files changed, 139 insertions(+), 166 deletions(-) rename examples/src/main/resources/{logback.xml => log4j2.xml} (65%) create mode 100644 sdk-spi/src/main/java/group/rxcloud/capa/spi/log/configuration/LogConfiguration.java delete mode 100644 sdk-spi/src/main/java/group/rxcloud/capa/spi/log/configuration/LogSwitchConfiguration.java diff --git a/examples/pom.xml b/examples/pom.xml index 61176e1..d17eec8 100644 --- a/examples/pom.xml +++ b/examples/pom.xml @@ -61,17 +61,17 @@ - + - + org.projectlombok diff --git a/examples/src/main/resources/logback.xml b/examples/src/main/resources/log4j2.xml similarity index 65% rename from examples/src/main/resources/logback.xml rename to examples/src/main/resources/log4j2.xml index 37a87f2..3587353 100644 --- a/examples/src/main/resources/logback.xml +++ b/examples/src/main/resources/log4j2.xml @@ -15,12 +15,17 @@ See the License for the specific language governing permissions and limitations under the License. --> - - - - - - - - - \ No newline at end of file + + + + + + + + + + + + + + \ No newline at end of file diff --git a/examples/src/main/resources/xml/logback.xml b/examples/src/main/resources/xml/logback.xml index c2ddd8a..37a87f2 100644 --- a/examples/src/main/resources/xml/logback.xml +++ b/examples/src/main/resources/xml/logback.xml @@ -20,16 +20,6 @@ - - - - - - - - - - diff --git a/pom.xml b/pom.xml index 9f817f4..73611d8 100644 --- a/pom.xml +++ b/pom.xml @@ -44,6 +44,10 @@ reckless11 reckless0511@gmail.com + + dgzpg + 1711145024@qq.com + diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/log/CapaLog4jAppenderAgent.java b/sdk-component/src/main/java/group/rxcloud/capa/component/log/CapaLog4jAppenderAgent.java index 7e31d3d..e2602ef 100644 --- a/sdk-component/src/main/java/group/rxcloud/capa/component/log/CapaLog4jAppenderAgent.java +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/log/CapaLog4jAppenderAgent.java @@ -63,7 +63,7 @@ public CapaLog4jAppenderAgent(String name, Filter filter, Layout layout, boolean ignoreExceptions) { - super(name, filter, layout, ignoreExceptions); + super(name, filter, layout, ignoreExceptions, null); } /** @@ -98,12 +98,6 @@ public static CapaLog4jAppender buildCapaLog4jAppender() { @Override public void append(LogEvent event) { logAppender.append(event); - /* if (event != null && event.getLevel() != null) { - Optional capaLogLevel = CapaLogLevel.toCapaLogLevel(event.getLevel().name()); - if (capaLogLevel.isPresent() && LogManager.whetherLogsCanBeOutput(capaLogLevel.get())) { - logAppender.appendLog(event); - } - }*/ } /** diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/log/CapaLogbackAppenderAgent.java b/sdk-component/src/main/java/group/rxcloud/capa/component/log/CapaLogbackAppenderAgent.java index b56048f..d2a21f9 100644 --- a/sdk-component/src/main/java/group/rxcloud/capa/component/log/CapaLogbackAppenderAgent.java +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/log/CapaLogbackAppenderAgent.java @@ -61,12 +61,6 @@ public static CapaLogbackAppender buildCapaLogbackAppender() { @Override protected void append(ILoggingEvent event) { logbackAppender.append(event); - /* if (event != null && event.getLevel() != null) { - Optional capaLogLevel = CapaLogLevel.toCapaLogLevel(event.getLevel().levelStr); - if (capaLogLevel.isPresent() && LogManager.whetherLogsCanBeOutput(capaLogLevel.get())) { - logbackAppender.append(event); - } - }*/ } /** diff --git a/sdk-infrastructure/src/main/java/group/rxcloud/capa/infrastructure/hook/MergedPropertiesConfig.java b/sdk-infrastructure/src/main/java/group/rxcloud/capa/infrastructure/hook/MergedPropertiesConfig.java index 91fd138..4e47d5c 100644 --- a/sdk-infrastructure/src/main/java/group/rxcloud/capa/infrastructure/hook/MergedPropertiesConfig.java +++ b/sdk-infrastructure/src/main/java/group/rxcloud/capa/infrastructure/hook/MergedPropertiesConfig.java @@ -58,6 +58,10 @@ public String get(String key) { return merged.get(key); } + public Map getMerged(){ + return merged; + } + private void subscribeConfigurationByAppId(ConfigurationHooks configurationHooks, String appId, int index) { String storeName = configurationHooks.registryStoreNames().get(0); diff --git a/sdk-spi-demo/src/main/java/group/rxcloud/capa/spi/demo/log/DemoLog4jAppender.java b/sdk-spi-demo/src/main/java/group/rxcloud/capa/spi/demo/log/DemoLog4jAppender.java index 36de41d..a087c3c 100644 --- a/sdk-spi-demo/src/main/java/group/rxcloud/capa/spi/demo/log/DemoLog4jAppender.java +++ b/sdk-spi-demo/src/main/java/group/rxcloud/capa/spi/demo/log/DemoLog4jAppender.java @@ -16,13 +16,13 @@ */ package group.rxcloud.capa.spi.demo.log; -import group.rxcloud.capa.component.log.CapaLog4jAppenderAgent; +import group.rxcloud.capa.spi.log.CapaLog4jAppenderSpi; import org.apache.logging.log4j.core.LogEvent; -public class DemoLog4jAppender implements CapaLog4jAppenderAgent.CapaLog4jAppender { +public class DemoLog4jAppender extends CapaLog4jAppenderSpi { @Override - public void append(LogEvent event) { + public void appendLog(LogEvent event) { System.out.println("Test log log4j and content is " + event.getMessage().getFormattedMessage()); } } diff --git a/sdk-spi-demo/src/main/java/group/rxcloud/capa/spi/demo/log/DemoLogbackAppender.java b/sdk-spi-demo/src/main/java/group/rxcloud/capa/spi/demo/log/DemoLogbackAppender.java index 32b2d1b..3267727 100644 --- a/sdk-spi-demo/src/main/java/group/rxcloud/capa/spi/demo/log/DemoLogbackAppender.java +++ b/sdk-spi-demo/src/main/java/group/rxcloud/capa/spi/demo/log/DemoLogbackAppender.java @@ -18,14 +18,15 @@ import ch.qos.logback.classic.spi.ILoggingEvent; import group.rxcloud.capa.component.log.CapaLogbackAppenderAgent; +import group.rxcloud.capa.spi.log.CapaLogbackAppenderSpi; -public class DemoLogbackAppender implements CapaLogbackAppenderAgent.CapaLogbackAppender { +public class DemoLogbackAppender extends CapaLogbackAppenderSpi { public DemoLogbackAppender() { } @Override - public void append(ILoggingEvent event) { + public void appendLog(ILoggingEvent event) { System.out.println("Test logback log and content is " + event.getFormattedMessage()); } } diff --git a/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/CapaLog4jAppenderSpi.java b/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/CapaLog4jAppenderSpi.java index a51ddec..ee106c6 100644 --- a/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/CapaLog4jAppenderSpi.java +++ b/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/CapaLog4jAppenderSpi.java @@ -13,7 +13,7 @@ public abstract class CapaLog4jAppenderSpi implements CapaLog4jAppenderAgent.Cap public void append(LogEvent event) { if (event != null && event.getLevel()!= null) { Optional capaLogLevel = CapaLogLevel.toCapaLogLevel(event.getLevel().name()); - if (capaLogLevel.isPresent() && LogManager.whetherLogsCanBeOutput(capaLogLevel.get())) { + if (capaLogLevel.isPresent() && LogManager.whetherLogsCanOutput(capaLogLevel.get())) { this.appendLog(event); } } diff --git a/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/CapaLogbackAppenderSpi.java b/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/CapaLogbackAppenderSpi.java index 58af9df..9bf51fd 100644 --- a/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/CapaLogbackAppenderSpi.java +++ b/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/CapaLogbackAppenderSpi.java @@ -15,8 +15,8 @@ public abstract class CapaLogbackAppenderSpi extends UnsynchronizedAppenderBase< public void append(ILoggingEvent event) { if (event != null && event.getLevel() != null) { Optional capaLogLevel = CapaLogLevel.toCapaLogLevel(event.getLevel().levelStr); - if (capaLogLevel.isPresent() && LogManager.whetherLogsCanBeOutput(capaLogLevel.get())) { - this.append(event); + if (capaLogLevel.isPresent() && LogManager.whetherLogsCanOutput(capaLogLevel.get())) { + this.appendLog(event); } } } diff --git a/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/configuration/LogConfiguration.java b/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/configuration/LogConfiguration.java new file mode 100644 index 0000000..5437d15 --- /dev/null +++ b/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/configuration/LogConfiguration.java @@ -0,0 +1,79 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package group.rxcloud.capa.spi.log.configuration; + +import group.rxcloud.capa.infrastructure.CapaProperties; +import group.rxcloud.capa.infrastructure.hook.MergedPropertiesConfig; +import group.rxcloud.capa.infrastructure.hook.Mixer; +import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.api.common.Attributes; +import io.opentelemetry.api.metrics.LongCounter; +import io.opentelemetry.api.metrics.Meter; +import org.apache.commons.lang3.StringUtils; + +import java.util.Optional; + +/** + * Log switch configuration + */ +public class LogConfiguration { + /** + * Log switch config file name. + */ + private static final String LOG_CONFIGURATION_COMMON_FILE_NAME = "log-configuration.properties"; + private static MergedPropertiesConfig mergedPropertiesConfig; + + static { + Mixer.configurationHooksNullable().ifPresent(hooks -> { + String suffix = "log-common"; + try { + mergedPropertiesConfig = new MergedPropertiesConfig(LOG_CONFIGURATION_COMMON_FILE_NAME, + hooks.defaultConfigurationAppId(), + CapaProperties.COMPONENT_PROPERTIES_SUPPLIER.apply(suffix).getProperty("appId")); + } catch (Throwable throwable) { + Mixer.telemetryHooksNullable().ifPresent(telemetryHooks -> { + Meter meter = telemetryHooks.buildMeter("CloudWatchLogs").block(); + LongCounter longCounter = meter.counterBuilder("LogsError").build(); + Optional longCounterOptional = Optional.ofNullable(longCounter); + longCounterOptional.ifPresent(counter -> { + longCounter.bind(Attributes.of(AttributeKey.stringKey("LogsConfigurationError"), throwable.getMessage())) + .add(1); + }); + }); + } + }); + } + + public static boolean containsKey(String key) { + try { + return mergedPropertiesConfig != null && mergedPropertiesConfig.containsKey(key); + } finally { + return false; + } + } + + public static String get(String key) { + try { + return mergedPropertiesConfig == null + ? StringUtils.EMPTY + : mergedPropertiesConfig.get(key); + } finally { + return StringUtils.EMPTY; + } + } +} + diff --git a/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/configuration/LogSwitchConfiguration.java b/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/configuration/LogSwitchConfiguration.java deleted file mode 100644 index 849a6ea..0000000 --- a/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/configuration/LogSwitchConfiguration.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package group.rxcloud.capa.spi.log.configuration; - -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; -import group.rxcloud.capa.infrastructure.hook.ConfigurationHooks; -import group.rxcloud.capa.infrastructure.hook.Mixer; -import group.rxcloud.cloudruntimes.domain.core.configuration.SubConfigurationResp; -import group.rxcloud.cloudruntimes.utils.TypeRef; -import org.apache.commons.collections.CollectionUtils; -import org.apache.commons.lang3.StringUtils; -import reactor.core.publisher.Flux; - -import java.util.Map; -import java.util.Optional; - -/** - * Log switch configuration - */ -public class LogSwitchConfiguration { - - /** - * Capa configuration hooks. - */ - private static final Optional configurationHooks; - /** - * Log switch config file name. - */ - private static final String LOGS_SWITCH_CONFIG_FILE_NAME = "log-level-switch.properties"; - /** - * Log switch config's value. - */ - private static Map logsSwitchConfig = Maps.newHashMap(); - - /** - * Init configuration hooks and subscribe configuration. - */ - static { - configurationHooks = Mixer.configurationHooksNullable(); - configurationHooks.ifPresent(configurationHooks -> { - subscribeConfiguration(configurationHooks); - }); - } - - /** - * Subscribe configuration. - * - * @param configurationHooks - */ - private static void subscribeConfiguration(ConfigurationHooks configurationHooks) { - String storeName = configurationHooks.registryStoreNames().get(0); - String appId = configurationHooks.defaultConfigurationAppId(); - Flux> configFlux = configurationHooks.subscribeConfiguration( - storeName, - appId, - Lists.newArrayList(LOGS_SWITCH_CONFIG_FILE_NAME), - null, - StringUtils.EMPTY, - StringUtils.EMPTY, - TypeRef.get(Map.class)); - configFlux.subscribe(resp -> { - if (CollectionUtils.isNotEmpty(resp.getItems())) { - logsSwitchConfig = resp.getItems().get(0).getContent(); - } - }); - } - - /** - * Get the log switch config's values. - * - * @return the log switch config's values. - */ - public static Map getLogsSwitchConfig() { - return logsSwitchConfig; - } -} diff --git a/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/manager/LogManager.java b/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/manager/LogManager.java index c73822c..9a32b4f 100644 --- a/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/manager/LogManager.java +++ b/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/manager/LogManager.java @@ -16,11 +16,9 @@ */ package group.rxcloud.capa.spi.log.manager; -import group.rxcloud.capa.spi.log.configuration.LogSwitchConfiguration; +import group.rxcloud.capa.spi.log.configuration.LogConfiguration; import group.rxcloud.capa.spi.log.enums.CapaLogLevel; -import java.util.Map; - /** * LogManager, to manage log output levels. */ @@ -75,48 +73,43 @@ public class LogManager { * @param capaLogLevel * @return If the log can be output, it is true, otherwise it is false. */ - public static Boolean whetherLogsCanBeOutput(CapaLogLevel capaLogLevel) { - Map logsSwitchConfigs = LogSwitchConfiguration.getLogsSwitchConfig(); - //no configuration instance or logs switch configuration, then follow the default logic. - if (logsSwitchConfigs == null - || logsSwitchConfigs.isEmpty() - || Boolean.FALSE.equals(logsSwitchConfigs.get(LOG_LEVEL_SWITCH_NAME))) { + public static Boolean whetherLogsCanOutput(CapaLogLevel capaLogLevel) { + if (!LogConfiguration.containsKey(LOG_LEVEL_SWITCH_NAME) || Boolean.FALSE.equals(LogConfiguration.get(LOG_LEVEL_SWITCH_NAME))) { return isLogsLevelClosedWithDefault(capaLogLevel); } - switch (capaLogLevel) { case ALL: - return logsSwitchConfigs.get(ALL_LEVEL_SWITCH_NAME) == null + return LogConfiguration.containsKey(ALL_LEVEL_SWITCH_NAME) ? Boolean.FALSE - : logsSwitchConfigs.get(ALL_LEVEL_SWITCH_NAME); + : Boolean.parseBoolean(LogConfiguration.get(ALL_LEVEL_SWITCH_NAME)); case TRACE: - return logsSwitchConfigs.get(TRACE_LEVEL_SWITCH_NAME) == null + return LogConfiguration.containsKey(TRACE_LEVEL_SWITCH_NAME) ? Boolean.FALSE - : logsSwitchConfigs.get(TRACE_LEVEL_SWITCH_NAME); + : Boolean.parseBoolean(LogConfiguration.get(TRACE_LEVEL_SWITCH_NAME)); case DEBUG: - return logsSwitchConfigs.get(DEBUG_LEVEL_SWITCH_NAME) == null + return LogConfiguration.containsKey(DEBUG_LEVEL_SWITCH_NAME) ? Boolean.FALSE - : logsSwitchConfigs.get(DEBUG_LEVEL_SWITCH_NAME); + : Boolean.parseBoolean(LogConfiguration.get(DEBUG_LEVEL_SWITCH_NAME)); case INFO: - return logsSwitchConfigs.get(INFO_LEVEL_SWITCH_NAME) == null - ? Boolean.TRUE - : logsSwitchConfigs.get(INFO_LEVEL_SWITCH_NAME); + return LogConfiguration.containsKey(INFO_LEVEL_SWITCH_NAME) + ? Boolean.FALSE + : Boolean.parseBoolean(LogConfiguration.get(INFO_LEVEL_SWITCH_NAME)); case WARN: - return logsSwitchConfigs.get(WARN_LEVEL_SWITCH_NAME) == null - ? Boolean.TRUE - : logsSwitchConfigs.get(WARN_LEVEL_SWITCH_NAME); + return LogConfiguration.containsKey(WARN_LEVEL_SWITCH_NAME) + ? Boolean.FALSE + : Boolean.parseBoolean(LogConfiguration.get(WARN_LEVEL_SWITCH_NAME)); case ERROR: - return logsSwitchConfigs.get(ERROR_LEVEL_SWITCH_NAME) == null - ? Boolean.TRUE - : logsSwitchConfigs.get(ERROR_LEVEL_SWITCH_NAME); + return LogConfiguration.containsKey(ERROR_LEVEL_SWITCH_NAME) + ? Boolean.FALSE + : Boolean.parseBoolean(LogConfiguration.get(ERROR_LEVEL_SWITCH_NAME)); case FATAL: - return logsSwitchConfigs.get(FATAL_LEVEL_SWITCH_NAME) == null - ? Boolean.TRUE - : logsSwitchConfigs.get(FATAL_LEVEL_SWITCH_NAME); + return LogConfiguration.containsKey(FATAL_LEVEL_SWITCH_NAME) + ? Boolean.FALSE + : Boolean.parseBoolean(LogConfiguration.get(FATAL_LEVEL_SWITCH_NAME)); case OFF: - return logsSwitchConfigs.get(OFF_LEVEL_SWITCH_NAME) == null - ? Boolean.TRUE - : logsSwitchConfigs.get(OFF_LEVEL_SWITCH_NAME) == null; + return LogConfiguration.containsKey(OFF_LEVEL_SWITCH_NAME) + ? Boolean.FALSE + : Boolean.parseBoolean(LogConfiguration.get(OFF_LEVEL_SWITCH_NAME)); default: return Boolean.FALSE; } From d1b2aac19640f6ab6f0766dfa1075d5b3f6f4c59 Mon Sep 17 00:00:00 2001 From: = Date: Fri, 3 Dec 2021 00:20:54 +0800 Subject: [PATCH 15/17] add header line --- .../capa/spi/log/CapaLog4jAppenderSpi.java | 16 ++++++++++++++++ .../capa/spi/log/CapaLogbackAppenderSpi.java | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/CapaLog4jAppenderSpi.java b/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/CapaLog4jAppenderSpi.java index ee106c6..12fbf3f 100644 --- a/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/CapaLog4jAppenderSpi.java +++ b/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/CapaLog4jAppenderSpi.java @@ -1,3 +1,19 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package group.rxcloud.capa.spi.log; import group.rxcloud.capa.component.log.CapaLog4jAppenderAgent; diff --git a/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/CapaLogbackAppenderSpi.java b/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/CapaLogbackAppenderSpi.java index 9bf51fd..ebef191 100644 --- a/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/CapaLogbackAppenderSpi.java +++ b/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/CapaLogbackAppenderSpi.java @@ -1,3 +1,19 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package group.rxcloud.capa.spi.log; import ch.qos.logback.classic.spi.ILoggingEvent; From 14397a50da6adf442b7707123f738e3c29ce9b26 Mon Sep 17 00:00:00 2001 From: = Date: Fri, 3 Dec 2021 00:24:06 +0800 Subject: [PATCH 16/17] delete useless code --- .../group/rxcloud/capa/spi/demo/log/DemoLogbackAppender.java | 1 - 1 file changed, 1 deletion(-) diff --git a/sdk-spi-demo/src/main/java/group/rxcloud/capa/spi/demo/log/DemoLogbackAppender.java b/sdk-spi-demo/src/main/java/group/rxcloud/capa/spi/demo/log/DemoLogbackAppender.java index 3267727..3d86c24 100644 --- a/sdk-spi-demo/src/main/java/group/rxcloud/capa/spi/demo/log/DemoLogbackAppender.java +++ b/sdk-spi-demo/src/main/java/group/rxcloud/capa/spi/demo/log/DemoLogbackAppender.java @@ -17,7 +17,6 @@ package group.rxcloud.capa.spi.demo.log; import ch.qos.logback.classic.spi.ILoggingEvent; -import group.rxcloud.capa.component.log.CapaLogbackAppenderAgent; import group.rxcloud.capa.spi.log.CapaLogbackAppenderSpi; public class DemoLogbackAppender extends CapaLogbackAppenderSpi { From f298d9c1c587b3b2187dd8ffdad1789eda05eb53 Mon Sep 17 00:00:00 2001 From: kevinten <596823919@qq.com> Date: Fri, 3 Dec 2021 03:01:07 +0800 Subject: [PATCH 17/17] refactor: code review and format code style release: release 1.0.8.RELEASE version --- examples/pom.xml | 2 +- pom.xml | 32 +++- sdk-component/pom.xml | 34 ++-- .../capa/component/CapaLogProperties.java | 47 ++++++ .../component/CapaTelemetryProperties.java | 47 ++++++ .../component/configstore/ConfigHeaders.java | 52 +++++++ .../component/telemetry/SamplerConfig.java | 28 ++-- .../metrics/CapaMetricsExporter.java | 1 - .../telemetry/metrics/CapaMetricsSampler.java | 1 - .../telemetry/trace/CapaTracerWrapper.java | 1 - .../telemetry/trace/StatusAttributeKey.java | 1 - .../telemetry/trace/TracerConfig.java | 1 - .../capa-component-log-common.properties | 2 + ...capa-component-telemetry-common.properties | 2 + ...capa-component-telemetry-common.properties | 2 +- sdk-infrastructure/pom.xml | 6 +- .../hook/MergedPropertiesConfig.java | 21 ++- sdk-spi-demo/pom.xml | 2 +- sdk-spi/pom.xml | 28 ++-- .../log/configuration/LogConfiguration.java | 26 ++-- .../capa/spi/log/enums/CapaLogLevel.java | 6 +- .../capa/spi/log/manager/LogManager.java | 2 - .../spi/telemetry/CapaMetricsExporterSpi.java | 3 +- sdk-springboot/pom.xml | 2 +- sdk/pom.xml | 3 +- .../java/group/rxcloud/capa/package-info.java | 1 - .../telemetry/CapaTelemetryClientBuilder.java | 2 - .../telemetry/CapaTelemetryClientGlobal.java | 50 +++--- spec/proto/runtime/v1/cloud_runtimes.protobuf | 145 ++++++++++++++++-- 29 files changed, 406 insertions(+), 144 deletions(-) create mode 100644 sdk-component/src/main/java/group/rxcloud/capa/component/CapaLogProperties.java create mode 100644 sdk-component/src/main/java/group/rxcloud/capa/component/CapaTelemetryProperties.java create mode 100644 sdk-component/src/main/java/group/rxcloud/capa/component/configstore/ConfigHeaders.java create mode 100644 sdk-component/src/main/resources/sample/capa-component-log-common.properties create mode 100644 sdk-component/src/main/resources/sample/capa-component-telemetry-common.properties diff --git a/examples/pom.xml b/examples/pom.xml index d17eec8..4ab8fad 100644 --- a/examples/pom.xml +++ b/examples/pom.xml @@ -23,7 +23,7 @@ capa-parent group.rxcloud - 1.0.8-alpha-2 + 1.0.8.RELEASE capa-examples diff --git a/pom.xml b/pom.xml index 73611d8..0e0e13e 100644 --- a/pom.xml +++ b/pom.xml @@ -23,7 +23,7 @@ group.rxcloud capa-parent pom - 1.0.8-alpha-2 + 1.0.8.RELEASE capa-sdk-parent SDK for Capa. https://github.com/reactivegroup @@ -81,14 +81,17 @@ 8 UTF-8 3.8.1 - 1.0.9.RELEASE + 1.0.10.RELEASE 3.3.22.RELEASE 1.7.21 + 1.39.0 + 2.12.4 1.9.0 1.9.0-alpha - 5.3.1 3.6.0 + 2.0.2 + 3.1.2 0.13 2.7 @@ -141,7 +144,6 @@ io.projectreactor reactor-core ${reactor-core.version} - true @@ -151,6 +153,20 @@ ${slf4j.version} + + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + + + + + io.grpc + grpc-protobuf + ${grpc.version} + + io.opentelemetry @@ -176,6 +192,12 @@ ${mockito-core.version} test + + org.powermock + powermock-reflect + ${powermock.version} + test + @@ -352,7 +374,7 @@ html xml - + diff --git a/sdk-component/pom.xml b/sdk-component/pom.xml index 0808c0c..1340d7e 100644 --- a/sdk-component/pom.xml +++ b/sdk-component/pom.xml @@ -23,7 +23,7 @@ group.rxcloud capa-parent - 1.0.8-alpha-2 + 1.0.8.RELEASE capa-sdk-component @@ -35,10 +35,7 @@ 1.4.10 2.14.1 1.1.7 - 19.0 - 3.11 - 3.2.2 - + @@ -47,15 +44,16 @@ capa-sdk-infrastructure + io.opentelemetry opentelemetry-sdk - 1.9.0 + ${open.telemetry.version} io.opentelemetry opentelemetry-sdk-metrics - 1.9.0-alpha + ${open.telemetry.version.alpha} @@ -91,23 +89,24 @@ org.apache.logging.log4j log4j-core - true ${log4j.version} + true ch.qos.logback logback-core - true ${logback.version} + true ch.qos.logback logback-classic + ${logback.version} true - ${logback.version} + org.junit.jupiter junit-jupiter-engine @@ -118,21 +117,6 @@ mockito-core test - - com.google.guava - guava - ${guava-version} - - - org.apache.commons - commons-lang3 - ${commons-lang3-version} - - - commons-collections - commons-collections - ${commons-collections-version} - diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/CapaLogProperties.java b/sdk-component/src/main/java/group/rxcloud/capa/component/CapaLogProperties.java new file mode 100644 index 0000000..bdb6886 --- /dev/null +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/CapaLogProperties.java @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package group.rxcloud.capa.component; + +import group.rxcloud.capa.infrastructure.CapaProperties; + +import java.util.Properties; + +/** + * Capa log component common properties. + */ +public interface CapaLogProperties { + + abstract class Settings { + + private static String centerConfigAppId = ""; + + private static final String LOG_COMPONENT_CENTER_CONFIG_APPID = "LOG_COMPONENT_CENTER_CONFIG_APPID"; + + static { + Properties properties = CapaProperties.COMPONENT_PROPERTIES_SUPPLIER.apply("log-common"); + + centerConfigAppId = properties.getProperty(LOG_COMPONENT_CENTER_CONFIG_APPID, centerConfigAppId); + } + + public static String getCenterConfigAppId() { + return centerConfigAppId; + } + + private Settings() { + } + } +} diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/CapaTelemetryProperties.java b/sdk-component/src/main/java/group/rxcloud/capa/component/CapaTelemetryProperties.java new file mode 100644 index 0000000..9d4b695 --- /dev/null +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/CapaTelemetryProperties.java @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package group.rxcloud.capa.component; + +import group.rxcloud.capa.infrastructure.CapaProperties; + +import java.util.Properties; + +/** + * Capa telemetry component common properties. + */ +public interface CapaTelemetryProperties { + + abstract class Settings { + + private static String centerConfigAppId = ""; + + private static final String TELEMETRY_COMPONENT_CENTER_CONFIG_APPID = "TELEMETRY_COMPONENT_CENTER_CONFIG_APPID"; + + static { + Properties properties = CapaProperties.COMPONENT_PROPERTIES_SUPPLIER.apply("telemetry-common"); + + centerConfigAppId = properties.getProperty(TELEMETRY_COMPONENT_CENTER_CONFIG_APPID, centerConfigAppId); + } + + public static String getCenterConfigAppId() { + return centerConfigAppId; + } + + private Settings() { + } + } +} diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/configstore/ConfigHeaders.java b/sdk-component/src/main/java/group/rxcloud/capa/component/configstore/ConfigHeaders.java new file mode 100644 index 0000000..d15733e --- /dev/null +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/configstore/ConfigHeaders.java @@ -0,0 +1,52 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package group.rxcloud.capa.component.configstore; + +/** + * Common headers for config store. + */ +public class ConfigHeaders { + + public static final String CAPA_CONFIG_EXTENSION = "capa-config-extension"; + + /** + * Config extension mode. + */ + public enum Extension { + + /** + * Multi appId will be merge random. + */ + MERGE("0"), + /** + * The first appId is child config file, + * and will override the other appId. + */ + EXTEND("1"), + ; + + private final String mode; + + Extension(String mode) { + this.mode = mode; + } + + public String getMode() { + return mode; + } + } +} diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/SamplerConfig.java b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/SamplerConfig.java index f321db8..def1b85 100644 --- a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/SamplerConfig.java +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/SamplerConfig.java @@ -16,8 +16,8 @@ */ package group.rxcloud.capa.component.telemetry; +import group.rxcloud.capa.component.CapaTelemetryProperties; import group.rxcloud.capa.component.telemetry.metrics.CapaMeterProviderBuilder; -import group.rxcloud.capa.infrastructure.CapaProperties; import group.rxcloud.capa.infrastructure.hook.MergedPropertiesConfig; import group.rxcloud.capa.infrastructure.hook.Mixer; import org.slf4j.Logger; @@ -45,46 +45,40 @@ public class SamplerConfig implements Serializable { private static final transient Logger log = LoggerFactory.getLogger(CapaMeterProviderBuilder.class); - public static transient Supplier DEFAULT_SUPPLIER = () -> { - return CONFIG; - }; + public static transient Supplier DEFAULT_SUPPLIER = () -> CONFIG; static { Mixer.configurationHooksNullable().ifPresent(hooks -> { - String fileName = "capa-component-telemetry-sample.properties"; - - String suffix = "telemetry-common"; - try { - MergedPropertiesConfig config = new MergedPropertiesConfig(fileName, hooks - .defaultConfigurationAppId(), - CapaProperties.COMPONENT_PROPERTIES_SUPPLIER.apply(suffix).getProperty("appId")); + // TODO: 2021/12/3 Move this to SPI module. + // TODO: 2021/12/3 And use Configuration extension api to get merged file. + MergedPropertiesConfig config = new MergedPropertiesConfig( + fileName, + hooks.defaultConfigurationAppId(), + CapaTelemetryProperties.Settings.getCenterConfigAppId()); String metricKey = "metricsEnable"; String traceKey = "traceEnable"; SamplerConfig dynamicConfig = new SamplerConfig() { @Override public Boolean isMetricsEnable() { return !config.containsKey(metricKey) || Boolean.TRUE.toString() - .equalsIgnoreCase(config.get(metricKey)); + .equalsIgnoreCase(config.get(metricKey)); } @Override public Boolean isTraceEnable() { return !config.containsKey(traceKey) || Boolean.TRUE.toString() - .equalsIgnoreCase(config.get(traceKey)); + .equalsIgnoreCase(config.get(traceKey)); } }; - DEFAULT_SUPPLIER = () -> { - return dynamicConfig; - }; + DEFAULT_SUPPLIER = () -> dynamicConfig; } catch (Throwable throwable) { log.warn("Fail to load global telemetry config. Dynamic global config is disabled for capa telemetry.", throwable); } }); - } private Boolean metricsEnable; diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMetricsExporter.java b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMetricsExporter.java index 3dd9786..3ce214e 100644 --- a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMetricsExporter.java +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMetricsExporter.java @@ -58,5 +58,4 @@ public CompletableResultCode flush() { protected abstract CompletableResultCode doExport(Collection metrics); protected abstract CompletableResultCode doFlush(); - } diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMetricsSampler.java b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMetricsSampler.java index eda0109..bb93b97 100644 --- a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMetricsSampler.java +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMetricsSampler.java @@ -56,5 +56,4 @@ private ExemplarFilter get() { return ExemplarFilter.alwaysSample(); } - } diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/trace/CapaTracerWrapper.java b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/trace/CapaTracerWrapper.java index 2de9c21..04f87f6 100644 --- a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/trace/CapaTracerWrapper.java +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/trace/CapaTracerWrapper.java @@ -90,5 +90,4 @@ static CapaTracerBuilder wrap(String tracerName, TracerBuilder tracerBuilder) { } return result; } - } diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/trace/StatusAttributeKey.java b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/trace/StatusAttributeKey.java index 2730207..4795bd2 100644 --- a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/trace/StatusAttributeKey.java +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/trace/StatusAttributeKey.java @@ -36,7 +36,6 @@ private StatusAttributeKey(AttributeKey key) { this.key = key; } - @Override public String getKey() { return key.getKey(); diff --git a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/trace/TracerConfig.java b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/trace/TracerConfig.java index 5f8edb1..19cc7ab 100644 --- a/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/trace/TracerConfig.java +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/trace/TracerConfig.java @@ -77,5 +77,4 @@ public List getProcessors() { public void setProcessors(List processors) { this.processors = processors; } - } diff --git a/sdk-component/src/main/resources/sample/capa-component-log-common.properties b/sdk-component/src/main/resources/sample/capa-component-log-common.properties new file mode 100644 index 0000000..e8cf1c9 --- /dev/null +++ b/sdk-component/src/main/resources/sample/capa-component-log-common.properties @@ -0,0 +1,2 @@ +# log component center config appid +LOG_COMPONENT_CENTER_CONFIG_APPID=11111 \ No newline at end of file diff --git a/sdk-component/src/main/resources/sample/capa-component-telemetry-common.properties b/sdk-component/src/main/resources/sample/capa-component-telemetry-common.properties new file mode 100644 index 0000000..b132abc --- /dev/null +++ b/sdk-component/src/main/resources/sample/capa-component-telemetry-common.properties @@ -0,0 +1,2 @@ +# telemetry component center config appid +TELEMETRY_COMPONENT_CENTER_CONFIG_APPID=11111 \ No newline at end of file diff --git a/sdk-component/src/test/resources/capa-component-telemetry-common.properties b/sdk-component/src/test/resources/capa-component-telemetry-common.properties index a277f74..57a74aa 100644 --- a/sdk-component/src/test/resources/capa-component-telemetry-common.properties +++ b/sdk-component/src/test/resources/capa-component-telemetry-common.properties @@ -1 +1 @@ -appId=123 \ No newline at end of file +TELEMETRY_COMPONENT_CENTER_CONFIG_APPID=123 \ No newline at end of file diff --git a/sdk-infrastructure/pom.xml b/sdk-infrastructure/pom.xml index 30f4bb9..207c0c5 100644 --- a/sdk-infrastructure/pom.xml +++ b/sdk-infrastructure/pom.xml @@ -23,7 +23,7 @@ capa-parent group.rxcloud - 1.0.8-alpha-2 + 1.0.8.RELEASE capa-sdk-infrastructure @@ -33,8 +33,6 @@ 0.10.4 1.0.1-RELEASE - 1.39.0 - 2.12.4 @@ -83,14 +81,12 @@ io.grpc grpc-protobuf - ${grpc.version} com.fasterxml.jackson.core jackson-databind - ${jackson.version} diff --git a/sdk-infrastructure/src/main/java/group/rxcloud/capa/infrastructure/hook/MergedPropertiesConfig.java b/sdk-infrastructure/src/main/java/group/rxcloud/capa/infrastructure/hook/MergedPropertiesConfig.java index 4e47d5c..e51ae6c 100644 --- a/sdk-infrastructure/src/main/java/group/rxcloud/capa/infrastructure/hook/MergedPropertiesConfig.java +++ b/sdk-infrastructure/src/main/java/group/rxcloud/capa/infrastructure/hook/MergedPropertiesConfig.java @@ -28,7 +28,12 @@ /** * Config provider to merge multiple properties file which takes the input order as their priority. + *

+ * + * TODO: 2021/12/3 This should not in infrastructure layer. + * TODO: 2021/12/3 Use Configuration extension api to get merged file. */ +@Deprecated public class MergedPropertiesConfig { private final String fileName; @@ -58,7 +63,7 @@ public String get(String key) { return merged.get(key); } - public Map getMerged(){ + public Map getMerged() { return merged; } @@ -74,15 +79,15 @@ private void subscribeConfigurationByAppId(ConfigurationHooks configurationHooks "", TypeRef.get(Properties.class)); - + // FIXME: 2021/12/3 random callback? configFlux.subscribe(resp -> { - if (!resp.getItems().isEmpty()) { - properties.set(index, resp.getItems().get(0).getContent()); - } else { - properties.set(index, null); - } - synchronized (lock) { + if (!resp.getItems().isEmpty()) { + properties.set(index, resp.getItems().get(0).getContent()); + } else { + properties.set(index, null); + } + Map merged = new HashMap<>(); for (int i = 0; i < properties.length(); i++) { Properties item = properties.get(i); diff --git a/sdk-spi-demo/pom.xml b/sdk-spi-demo/pom.xml index 8c3e393..834255b 100644 --- a/sdk-spi-demo/pom.xml +++ b/sdk-spi-demo/pom.xml @@ -23,7 +23,7 @@ capa-parent group.rxcloud - 1.0.8-alpha-2 + 1.0.8.RELEASE capa-sdk-spi-demo diff --git a/sdk-spi/pom.xml b/sdk-spi/pom.xml index 5f34201..6a1178b 100644 --- a/sdk-spi/pom.xml +++ b/sdk-spi/pom.xml @@ -23,7 +23,7 @@ capa-parent group.rxcloud - 1.0.8-alpha-2 + 1.0.8.RELEASE capa-sdk-spi @@ -42,18 +42,6 @@ capa-sdk - - - org.junit.jupiter - junit-jupiter-engine - test - - - org.mockito - mockito-core - test - - true - ${logback.version} + + + + + org.junit.jupiter + junit-jupiter-engine + test + + + org.mockito + mockito-core + test diff --git a/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/configuration/LogConfiguration.java b/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/configuration/LogConfiguration.java index 5437d15..8074a9f 100644 --- a/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/configuration/LogConfiguration.java +++ b/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/configuration/LogConfiguration.java @@ -16,14 +16,13 @@ */ package group.rxcloud.capa.spi.log.configuration; -import group.rxcloud.capa.infrastructure.CapaProperties; +import group.rxcloud.capa.component.CapaLogProperties; import group.rxcloud.capa.infrastructure.hook.MergedPropertiesConfig; import group.rxcloud.capa.infrastructure.hook.Mixer; import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.Attributes; import io.opentelemetry.api.metrics.LongCounter; import io.opentelemetry.api.metrics.Meter; -import org.apache.commons.lang3.StringUtils; import java.util.Optional; @@ -31,22 +30,24 @@ * Log switch configuration */ public class LogConfiguration { + /** * Log switch config file name. */ - private static final String LOG_CONFIGURATION_COMMON_FILE_NAME = "log-configuration.properties"; + private static final String LOG_CONFIGURATION_COMMON_FILE_NAME = "capa-component-log-configuration.properties"; private static MergedPropertiesConfig mergedPropertiesConfig; static { Mixer.configurationHooksNullable().ifPresent(hooks -> { - String suffix = "log-common"; + // TODO: 2021/12/3 Use Configuration extension api to get merged file. try { - mergedPropertiesConfig = new MergedPropertiesConfig(LOG_CONFIGURATION_COMMON_FILE_NAME, + mergedPropertiesConfig = new MergedPropertiesConfig( + LOG_CONFIGURATION_COMMON_FILE_NAME, hooks.defaultConfigurationAppId(), - CapaProperties.COMPONENT_PROPERTIES_SUPPLIER.apply(suffix).getProperty("appId")); + CapaLogProperties.Settings.getCenterConfigAppId()); } catch (Throwable throwable) { Mixer.telemetryHooksNullable().ifPresent(telemetryHooks -> { - Meter meter = telemetryHooks.buildMeter("CloudWatchLogs").block(); + Meter meter = telemetryHooks.buildMeter("LogsConfiguration").block(); LongCounter longCounter = meter.counterBuilder("LogsError").build(); Optional longCounterOptional = Optional.ofNullable(longCounter); longCounterOptional.ifPresent(counter -> { @@ -60,8 +61,9 @@ public class LogConfiguration { public static boolean containsKey(String key) { try { - return mergedPropertiesConfig != null && mergedPropertiesConfig.containsKey(key); - } finally { + return mergedPropertiesConfig != null + && mergedPropertiesConfig.containsKey(key); + } catch (Exception e) { return false; } } @@ -69,10 +71,10 @@ public static boolean containsKey(String key) { public static String get(String key) { try { return mergedPropertiesConfig == null - ? StringUtils.EMPTY + ? "" : mergedPropertiesConfig.get(key); - } finally { - return StringUtils.EMPTY; + } catch (Exception e) { + return ""; } } } diff --git a/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/enums/CapaLogLevel.java b/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/enums/CapaLogLevel.java index 4c70c3d..df3a2be 100644 --- a/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/enums/CapaLogLevel.java +++ b/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/enums/CapaLogLevel.java @@ -23,6 +23,7 @@ * Capa log level. */ public enum CapaLogLevel { + /** * Standard order of log priorities:ALL,TRACE,DEBUG,INFO,WARN,ERROR,FATAL,OFF */ @@ -45,9 +46,6 @@ public enum CapaLogLevel { /** * Convert logLevelArg to {@link CapaLogLevel} - * - * @param logLevelArg - * @return */ public static Optional toCapaLogLevel(String logLevelArg) { return Arrays.stream(CapaLogLevel.values()) @@ -57,8 +55,6 @@ public static Optional toCapaLogLevel(String logLevelArg) { /** * Get level. - * - * @return */ public int getLevel() { return level; diff --git a/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/manager/LogManager.java b/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/manager/LogManager.java index 9a32b4f..5a65545 100644 --- a/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/manager/LogManager.java +++ b/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/manager/LogManager.java @@ -70,7 +70,6 @@ public class LogManager { *

* If the log level is not in the {@link CapaLogLevel#values()}, the log is not output. * - * @param capaLogLevel * @return If the log can be output, it is true, otherwise it is false. */ public static Boolean whetherLogsCanOutput(CapaLogLevel capaLogLevel) { @@ -118,7 +117,6 @@ public static Boolean whetherLogsCanOutput(CapaLogLevel capaLogLevel) { /** * Logs of the {@link CapaLogLevel#INFO} level or higher are normally output, but logs of the {@link CapaLogLevel#INFO} level lower are not output. * - * @param capaLogLevel * @return whether the capaLogLevel's priority is equal or more then {@link CapaLogLevel#INFO}. */ private static Boolean isLogsLevelClosedWithDefault(CapaLogLevel capaLogLevel) { diff --git a/sdk-spi/src/main/java/group/rxcloud/capa/spi/telemetry/CapaMetricsExporterSpi.java b/sdk-spi/src/main/java/group/rxcloud/capa/spi/telemetry/CapaMetricsExporterSpi.java index 5f7bdf2..a0fb3a7 100644 --- a/sdk-spi/src/main/java/group/rxcloud/capa/spi/telemetry/CapaMetricsExporterSpi.java +++ b/sdk-spi/src/main/java/group/rxcloud/capa/spi/telemetry/CapaMetricsExporterSpi.java @@ -26,8 +26,7 @@ */ public abstract class CapaMetricsExporterSpi extends CapaMetricsExporter { - public CapaMetricsExporterSpi( - Supplier samplerConfig) { + public CapaMetricsExporterSpi(Supplier samplerConfig) { super(samplerConfig); } } diff --git a/sdk-springboot/pom.xml b/sdk-springboot/pom.xml index b39ac27..c1a9a01 100644 --- a/sdk-springboot/pom.xml +++ b/sdk-springboot/pom.xml @@ -23,7 +23,7 @@ capa-parent group.rxcloud - 1.0.8-alpha-2 + 1.0.8.RELEASE sdk-springboot diff --git a/sdk/pom.xml b/sdk/pom.xml index b03d649..d01857c 100644 --- a/sdk/pom.xml +++ b/sdk/pom.xml @@ -23,7 +23,7 @@ group.rxcloud capa-parent - 1.0.8-alpha-2 + 1.0.8.RELEASE capa-sdk @@ -52,7 +52,6 @@ org.powermock powermock-reflect - 2.0.2 test diff --git a/sdk/src/main/java/group/rxcloud/capa/package-info.java b/sdk/src/main/java/group/rxcloud/capa/package-info.java index e643749..0888326 100644 --- a/sdk/src/main/java/group/rxcloud/capa/package-info.java +++ b/sdk/src/main/java/group/rxcloud/capa/package-info.java @@ -14,5 +14,4 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package group.rxcloud.capa; diff --git a/sdk/src/main/java/group/rxcloud/capa/telemetry/CapaTelemetryClientBuilder.java b/sdk/src/main/java/group/rxcloud/capa/telemetry/CapaTelemetryClientBuilder.java index 574f827..fb8d324 100644 --- a/sdk/src/main/java/group/rxcloud/capa/telemetry/CapaTelemetryClientBuilder.java +++ b/sdk/src/main/java/group/rxcloud/capa/telemetry/CapaTelemetryClientBuilder.java @@ -81,7 +81,6 @@ public CapaTelemetryClientBuilder addContextPropagators(TextMapPropagator proces return this; } - @Override public CapaTelemetryClientBuilder setMeterConfig(MeterConfig config) { meterProviderBuilder.setMeterConfig(config); @@ -115,5 +114,4 @@ public CapaTelemetryClient build() { return client; } - } diff --git a/sdk/src/main/java/group/rxcloud/capa/telemetry/CapaTelemetryClientGlobal.java b/sdk/src/main/java/group/rxcloud/capa/telemetry/CapaTelemetryClientGlobal.java index cc71ca9..62c3ce6 100644 --- a/sdk/src/main/java/group/rxcloud/capa/telemetry/CapaTelemetryClientGlobal.java +++ b/sdk/src/main/java/group/rxcloud/capa/telemetry/CapaTelemetryClientGlobal.java @@ -72,16 +72,14 @@ public ContextPropagators getPropagators() { @Override public Mono buildTracer(String tracerName) { - return Mono.fromSupplier(() -> { - return tracerProvider.tracerBuilder(tracerName).build(); - }); + return Mono.fromSupplier(() -> + tracerProvider.tracerBuilder(tracerName) + .build()); } @Override public Mono getContextPropagators() { - return Mono.fromSupplier(() -> { - return contextPropagators; - }); + return Mono.fromSupplier(() -> contextPropagators); } void setContextPropagators(ContextPropagators contextPropagators) { @@ -90,41 +88,43 @@ void setContextPropagators(ContextPropagators contextPropagators) { @Override public Mono buildTracer(String tracerName, String version) { - return Mono.fromSupplier(() -> { - return tracerProvider.tracerBuilder(tracerName).setInstrumentationVersion(version).build(); - }); + return Mono.fromSupplier(() -> + tracerProvider.tracerBuilder(tracerName) + .setInstrumentationVersion(version) + .build()); } @Override public Mono buildTracer(String tracerName, String version, String schemaUrl) { - return Mono.fromSupplier(() -> { - return tracerProvider.tracerBuilder(tracerName).setInstrumentationVersion(version).setSchemaUrl(schemaUrl) - .build(); - }); + return Mono.fromSupplier(() -> + tracerProvider.tracerBuilder(tracerName) + .setInstrumentationVersion(version) + .setSchemaUrl(schemaUrl) + .build()); } @Override public Mono buildMeter(String meterName) { - return Mono.fromSupplier(() -> { - return meterProvider.meterBuilder(meterName) - .build(); - }); + return Mono.fromSupplier(() -> + meterProvider.meterBuilder(meterName) + .build()); } @Override public Mono buildMeter(String meterName, String version) { - return Mono.fromSupplier(() -> { - return meterProvider.meterBuilder(meterName).setInstrumentationVersion(version) - .build(); - }); + return Mono.fromSupplier(() -> + meterProvider.meterBuilder(meterName) + .setInstrumentationVersion(version) + .build()); } @Override public Mono buildMeter(String meterName, String version, String schemaUrl) { - return Mono.fromSupplier(() -> { - return meterProvider.meterBuilder(meterName).setInstrumentationVersion(version).setSchemaUrl(schemaUrl) - .build(); - }); + return Mono.fromSupplier(() -> + meterProvider.meterBuilder(meterName) + .setInstrumentationVersion(version) + .setSchemaUrl(schemaUrl) + .build()); } @Override diff --git a/spec/proto/runtime/v1/cloud_runtimes.protobuf b/spec/proto/runtime/v1/cloud_runtimes.protobuf index afb177b..37b8f3f 100644 --- a/spec/proto/runtime/v1/cloud_runtimes.protobuf +++ b/spec/proto/runtime/v1/cloud_runtimes.protobuf @@ -10,7 +10,7 @@ option java_outer_classname = "RuntimeProto"; option java_package = "spec.proto.runtime.v1"; service Runtime { - // SayHello used for test + //SayHello used for test rpc SayHello(SayHelloRequest) returns (SayHelloResponse) {} // InvokeService do rpc calls @@ -60,9 +60,103 @@ service Runtime { // Executes transactions for a specified store rpc ExecuteStateTransaction(ExecuteStateTransactionRequest) returns (google.protobuf.Empty) {} - // Publishes events to the specific topic. + // Publishes events to the specific topic rpc PublishEvent(PublishEventRequest) returns (google.protobuf.Empty) {} + // Get file with stream + rpc GetFile(GetFileRequest) returns (stream GetFileResponse) {} + + // Put file with stream + rpc PutFile(stream PutFileRequest) returns (google.protobuf.Empty) {} + + // List all files + rpc ListFile(ListFileRequest) returns (ListFileResp){} + + // Delete specific file + rpc DelFile(DelFileRequest) returns (google.protobuf.Empty){} + + // Get file meta data, if file not exist,return code.NotFound error + rpc GetFileMeta(GetFileMetaRequest) returns (GetFileMetaResponse){} + + // Invokes binding data to specific output bindings + rpc InvokeBinding(InvokeBindingRequest) returns (InvokeBindingResponse) {} +} + +message GetFileMetaRequest{ + FileRequest request = 1; +} + +message GetFileMetaResponse{ + // The size of file + int64 size = 1; + // The modified time of file + string last_modified = 2; + FileMeta response = 3; +} + +message FileMetaValue{ + repeated string value = 1; +} + +message FileMeta{ + map metadata = 1; +} + +message GetFileRequest { + // + string store_name = 1; + // The name of the file or object want to get. + string name = 2; + // The metadata for user extension. + map metadata = 3; +} + +message GetFileResponse { + bytes data = 1; +} + +message PutFileRequest { + string store_name = 1; + // The name of the file or object want to put. + string name = 2; + // The data will be store. + bytes data = 3; + // The metadata for user extension. + map metadata = 4; +} + +message FileRequest { + string store_name = 1; + // The name of the directory + string name = 2; + // The metadata for user extension. + map metadata = 3; +} + +message ListFileRequest { + FileRequest request = 1; + int32 page_size = 2; + string marker = 3; +} + +message FileInfo { + // The name of file + string file_name = 1; + // The size of file + int64 size = 2; + // The modified time of file + string last_modified = 3; + // The metadata for user extension. + map metadata = 4; +} +message ListFileResp { + repeated FileInfo files = 1; + string marker = 2; + bool is_truncated = 3; +} + +message DelFileRequest { + FileRequest request = 1; } message GetNextIdRequest { @@ -106,7 +200,8 @@ message SequencerOptions { message GetNextIdResponse{ // The next unique id - int64 next_id = 1; + // Fixed int64 overflow problems on JavaScript https://github.com/improbable-eng/ts-protoc-gen#gotchas + int64 next_id = 1 [jstype = JS_STRING]; } message TryLockRequest { @@ -119,12 +214,9 @@ message TryLockRequest { // Required. lock_owner indicate the identifier of lock owner. // You can generate a uuid as lock_owner.For example,in golang: - // // req.LockOwner = uuid.New().String() - // // This field is per request,not per process,so it is different for each request, // which aims to prevent multi-thread in the same process trying the same lock concurrently. - // // The reason why we don't make it automatically generated is: // 1. If it is automatically generated,there must be a 'my_lock_owner_id' field in the response. // This name is so weird that we think it is inappropriate to put it into the api spec @@ -164,10 +256,13 @@ message UnlockResponse { message SayHelloRequest { string service_name = 1; string name = 2; + // Optional. This field is used to control the packet size during load tests. + google.protobuf.Any data = 3; } message SayHelloResponse { string hello = 1; + google.protobuf.Any data = 2; } message InvokeServiceRequest { @@ -484,13 +579,11 @@ message StateOptions { enum StateConsistency { CONSISTENCY_UNSPECIFIED = 0; // The API server assumes data stores are eventually consistent by default.A state store should: - // // - For read requests, the state store can return data from any of the replicas // - For write request, the state store should asynchronously replicate updates to configured quorum after acknowledging the update request. CONSISTENCY_EVENTUAL = 1; // When a strong consistency hint is attached, a state store should: - // // - For read requests, the state store should return the most up-to-date data consistently across replicas. // - For write/delete requests, the state store should synchronisely replicate updated data to configured quorum before completing the write request. CONSISTENCY_STRONG = 2; @@ -502,7 +595,10 @@ message StateOptions { // TransactionalStateOperation is the message to execute a specified operation with a key-value pair. message TransactionalStateOperation { - // Required. The type of operation to be executed + // Required. The type of operation to be executed. + // Legal values include: + // "upsert" represents an update or create operation + // "delete" represents a delete operation string operationType = 1; // Required. State values to be operated on @@ -539,4 +635,33 @@ message PublishEventRequest { // metadata property: // - key : the key of the message. map metadata = 5; -} \ No newline at end of file +} + +// InvokeBindingRequest is the message to send data to output bindings +message InvokeBindingRequest { + // The name of the output binding to invoke. + string name = 1; + + // The data which will be sent to output binding. + bytes data = 2; + + // The metadata passing to output binding components + // Common metadata property: + // - ttlInSeconds : the time to live in seconds for the message. + // If set in the binding definition will cause all messages to + // have a default time to live. The message ttl overrides any value + // in the binding definition. + map metadata = 3; + + // The name of the operation type for the binding to invoke + string operation = 4; +} + +// InvokeBindingResponse is the message returned from an output binding invocation +message InvokeBindingResponse { + // The data which will be sent to output binding. + bytes data = 1; + + // The metadata returned from an external system + map metadata = 2; +}