diff --git a/examples/pom.xml b/examples/pom.xml index 739a0b7..4ab8fad 100644 --- a/examples/pom.xml +++ b/examples/pom.xml @@ -23,7 +23,7 @@ capa-parent group.rxcloud - 1.0.7.RELEASE + 1.0.8.RELEASE capa-examples @@ -31,7 +31,7 @@ capa-sdk-examples - 2.8.2 + 2.14.1 1.1.7 1.7.32 @@ -67,7 +67,7 @@ ${log4j.version} - - + - - - - - - - - - - diff --git a/pom.xml b/pom.xml index b8f7b9d..0e0e13e 100644 --- a/pom.xml +++ b/pom.xml @@ -23,7 +23,7 @@ group.rxcloud capa-parent pom - 1.0.7.RELEASE + 1.0.8.RELEASE capa-sdk-parent SDK for Capa. https://github.com/reactivegroup @@ -44,6 +44,10 @@ reckless11 reckless0511@gmail.com + + dgzpg + 1711145024@qq.com + @@ -77,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 @@ -137,7 +144,6 @@ io.projectreactor reactor-core ${reactor-core.version} - true @@ -147,6 +153,20 @@ ${slf4j.version} + + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + + + + + io.grpc + grpc-protobuf + ${grpc.version} + + io.opentelemetry @@ -172,6 +192,12 @@ ${mockito-core.version} test + + org.powermock + powermock-reflect + ${powermock.version} + test + @@ -348,7 +374,7 @@ html xml - + @@ -414,5 +440,4 @@ - \ No newline at end of file diff --git a/sdk-component/pom.xml b/sdk-component/pom.xml index 22941a3..1340d7e 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.RELEASE capa-sdk-component @@ -33,9 +33,9 @@ 4.9.1 1.4.10 - 2.8.2 + 2.14.1 1.1.7 - + @@ -44,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} @@ -88,15 +89,22 @@ org.apache.logging.log4j log4j-core - true ${log4j.version} + true ch.qos.logback logback-core + ${logback.version} true + + + ch.qos.logback + logback-classic ${logback.version} + + true 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/log/agent/CapaLog4jAppenderAgent.java b/sdk-component/src/main/java/group/rxcloud/capa/component/log/CapaLog4jAppenderAgent.java similarity index 95% 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 e05cd68..e2602ef 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,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 group.rxcloud.capa.infrastructure.CapaClassLoader; import org.apache.logging.log4j.core.Appender; @@ -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); } /** @@ -97,7 +97,7 @@ public static CapaLog4jAppender buildCapaLog4jAppender() { @Override public void append(LogEvent event) { - logAppender.appendLog(event); + logAppender.append(event); } /** @@ -110,6 +110,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 84% 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 c4c0d54..d2a21f9 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,15 +14,16 @@ * 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.infrastructure.CapaClassLoader; /** * The agent of the logback impl. */ -public class CapaLogbackAppenderAgent extends UnsynchronizedAppenderBase { +public class CapaLogbackAppenderAgent extends UnsynchronizedAppenderBase { /** * The log component type. @@ -58,20 +59,21 @@ public static CapaLogbackAppender buildCapaLogbackAppender() { * @param event The log event. */ @Override - protected void append(EVENT event) { - logbackAppender.appendLog(event); + protected void append(ILoggingEvent event) { + logbackAppender.append(event); } /** * The abstract api of the logback appender impl.Implement this and provide your specific impl. */ - public interface CapaLogbackAppender { + public interface CapaLogbackAppender { /** * Deal with the log. * * @param event The log event. */ - void appendLog(EVENT event); + void append(ILoggingEvent event); } } + 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..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,66 +16,88 @@ */ package group.rxcloud.capa.component.telemetry; -import group.rxcloud.capa.infrastructure.utils.SpiUtils; +import group.rxcloud.capa.component.CapaTelemetryProperties; +import group.rxcloud.capa.component.telemetry.metrics.CapaMeterProviderBuilder; +import group.rxcloud.capa.infrastructure.hook.MergedPropertiesConfig; +import group.rxcloud.capa.infrastructure.hook.Mixer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.Serializable; -import java.util.Properties; +import java.util.function.Supplier; /** * Sampler config. */ public class SamplerConfig implements Serializable { - public static final 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; + public static final transient SamplerConfig DEFAULT_CONFIG = new SamplerConfig() {{ + setTraceEnable(true); + setMetricsEnable(true); + }}; - private boolean metricsSample = true; + public static final transient SamplerConfig CONFIG = new SamplerConfig(); - private boolean traceSample = true; - - private boolean logSample = true; + private static final long serialVersionUID = -2113523925814197551L; - public boolean isMetricsSample() { - return metricsSample; + private static final transient Logger log = LoggerFactory.getLogger(CapaMeterProviderBuilder.class); + + public static transient Supplier DEFAULT_SUPPLIER = () -> CONFIG; + + static { + Mixer.configurationHooksNullable().ifPresent(hooks -> { + String fileName = "capa-component-telemetry-sample.properties"; + try { + // 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)); + } + + @Override + public Boolean isTraceEnable() { + return !config.containsKey(traceKey) || Boolean.TRUE.toString() + .equalsIgnoreCase(config.get(traceKey)); + } + }; + + DEFAULT_SUPPLIER = () -> dynamicConfig; + } catch (Throwable throwable) { + log.warn("Fail to load global telemetry config. Dynamic global config is disabled for capa telemetry.", + throwable); + } + }); } - public void setMetricsSample(boolean metricsSample) { - this.metricsSample = metricsSample; - } + private Boolean metricsEnable; - public boolean isTraceSample() { - return traceSample; - } + private Boolean traceEnable; - public void setTraceSample(boolean traceSample) { - this.traceSample = traceSample; + public Boolean isMetricsEnable() { + return metricsEnable == null ? DEFAULT_CONFIG.metricsEnable : metricsEnable; } - public boolean isLogSample() { - return logSample; + public void setMetricsEnable(boolean metricsEnable) { + this.metricsEnable = metricsEnable; } - public void setLogSample(boolean logSample) { - this.logSample = logSample; + public Boolean isTraceEnable() { + return traceEnable == null ? DEFAULT_CONFIG.traceEnable : traceEnable; } - 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/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/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..b2201aa --- /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 { + + protected final String meterName; + + protected final String schemaUrl; + + protected final String version; + + protected 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..e116d5a --- /dev/null +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMeterBuilder.java @@ -0,0 +1,58 @@ +/* + * 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; + meterBuilder.setSchemaUrl(schemaUrl); + return this; + } + + @Override + public MeterBuilder setInstrumentationVersion(String instrumentationVersion) { + version = instrumentationVersion; + meterBuilder.setInstrumentationVersion(instrumentationVersion); + return this; + } + + @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..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 @@ -18,13 +18,14 @@ import group.rxcloud.capa.component.telemetry.SamplerConfig; +import java.util.function.Supplier; + /** * Settings for capa meter provider. */ 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. @@ -48,5 +49,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..3ce214e --- /dev/null +++ b/sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMetricsExporter.java @@ -0,0 +1,61 @@ +/* + * 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(); +} 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..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 @@ -21,61 +21,39 @@ 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 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; - } + private final Supplier samplerConfigSupplier; - if (config.isMetricsSample()) { - inner = ExemplarFilter.alwaysSample(); - } else { - inner = ExemplarFilter.neverSample(); + public CapaMetricsSampler(Supplier samplerConfigSupplier) { + if (samplerConfigSupplier == null) { + samplerConfigSupplier = () -> SamplerConfig.DEFAULT_CONFIG; } - 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..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 @@ -20,13 +20,14 @@ import io.opentelemetry.sdk.trace.IdGenerator; import io.opentelemetry.sdk.trace.SpanProcessor; +import java.util.function.Supplier; + /** * Settings for capa trace provider. */ 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. @@ -66,6 +67,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..04f87f6 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 @@ -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/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 74% 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 16b61fc..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,10 +14,9 @@ * 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; -import org.apache.logging.log4j.message.Message; +import ch.qos.logback.classic.spi.ILoggingEvent; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.mockito.Mockito; @@ -33,10 +32,8 @@ void testBuildCapaLogbackAppender() { @Test void testAppend() { CapaLogbackAppenderAgent.CapaLogbackAppender capaLogbackAppender = CapaLogbackAppenderAgent.buildCapaLogbackAppender(); - LogEvent logEvent = Mockito.mock(LogEvent.class); - Message message = Mockito.mock(Message.class); - Mockito.when(logEvent.getMessage()).thenReturn(Mockito.mock(Message.class)); - Mockito.when(message.getFormattedMessage()).thenReturn("TEST"); - capaLogbackAppender.appendLog(logEvent); + ILoggingEvent logEvent = Mockito.mock(ILoggingEvent.class); + Mockito.when(logEvent.getFormattedMessage()).thenReturn("TEST"); + 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 82% 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 af58bcd..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,21 +14,21 @@ * 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; +import ch.qos.logback.classic.spi.ILoggingEvent; /** * The capa logback appender used in tests only. */ -public class TestCapaLogbackAppender implements CapaLogbackAppenderAgent.CapaLogbackAppender { +public class TestCapaLogbackAppender implements CapaLogbackAppenderAgent.CapaLogbackAppender { public TestCapaLogbackAppender() { } @Override - public void appendLog(LogEvent event) { - System.out.println("test logback log and content is " + event.getMessage().getFormattedMessage()); + public void append(ILoggingEvent event) { + System.out.println("test logback log and content is " + event.getFormattedMessage()); } } 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..3518ada --- /dev/null +++ b/sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/TestMixerProvider.java @@ -0,0 +1,88 @@ +/* + * 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; +import java.util.Properties; + +/** + * @author: chenyijiang + * @date: 2021/12/2 12:39 + */ +public class TestMixerProvider implements Mixer.MixerProvider { + + Properties app = new Properties() {{put("metricsEnable", false);}}; + Properties global = new Properties() {{put("traceEnable", false); put("metricsEnable", 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() == Properties.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(Properties 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/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/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/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/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/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/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..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 @@ -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); @@ -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-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-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..57a74aa --- /dev/null +++ b/sdk-component/src/test/resources/capa-component-telemetry-common.properties @@ -0,0 +1 @@ +TELEMETRY_COMPONENT_CENTER_CONFIG_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-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-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-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 4c17bbb..207c0c5 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.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 new file mode 100644 index 0000000..e51ae6c --- /dev/null +++ b/sdk-infrastructure/src/main/java/group/rxcloud/capa/infrastructure/hook/MergedPropertiesConfig.java @@ -0,0 +1,102 @@ +/* + * 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. + *

+ * + * 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; + + 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); + } + + public Map getMerged() { + return merged; + } + + 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)); + + // FIXME: 2021/12/3 random callback? + configFlux.subscribe(resp -> { + 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); + if (item != null) { + item.forEach((k, v) -> merged.putIfAbsent(String.valueOf(k), String.valueOf(v))); + } + } + this.merged = merged; + } + }); + } +} diff --git a/sdk-spi-demo/pom.xml b/sdk-spi-demo/pom.xml index 4256367..834255b 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.RELEASE capa-sdk-spi-demo @@ -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 ccf07cc..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.agent.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 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 c6029cd..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,15 +17,15 @@ 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.spi.log.CapaLogbackAppenderSpi; -public class DemoLogbackAppender implements CapaLogbackAppenderAgent.CapaLogbackAppender { +public class DemoLogbackAppender extends CapaLogbackAppenderSpi { 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()); } } 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 dc3077f..6a1178b 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.RELEASE capa-sdk-spi @@ -32,6 +32,7 @@ 2.14.1 + 1.1.7 @@ -41,35 +42,37 @@ capa-sdk - - - org.junit.jupiter - junit-jupiter-engine - test - - - org.mockito - mockito-core - test - - - + org.apache.logging.log4j log4j-core ${log4j.version} + true + + + + ch.qos.logback + logback-classic + ${logback.version} + + true + + + + + org.junit.jupiter + junit-jupiter-engine test - org.apache.logging.log4j - log4j-slf4j-impl - ${log4j.version} + org.mockito + mockito-core test 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..12fbf3f --- /dev/null +++ b/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/CapaLog4jAppenderSpi.java @@ -0,0 +1,39 @@ +/* + * 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; +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.whetherLogsCanOutput(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..ebef191 --- /dev/null +++ b/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/CapaLogbackAppenderSpi.java @@ -0,0 +1,41 @@ +/* + * 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; +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.whetherLogsCanOutput(capaLogLevel.get())) { + this.appendLog(event); + } + } + } + + protected abstract void appendLog(ILoggingEvent 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..8074a9f --- /dev/null +++ b/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/configuration/LogConfiguration.java @@ -0,0 +1,81 @@ +/* + * 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.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 java.util.Optional; + +/** + * Log switch configuration + */ +public class LogConfiguration { + + /** + * Log switch config file name. + */ + private static final String LOG_CONFIGURATION_COMMON_FILE_NAME = "capa-component-log-configuration.properties"; + private static MergedPropertiesConfig mergedPropertiesConfig; + + static { + Mixer.configurationHooksNullable().ifPresent(hooks -> { + // TODO: 2021/12/3 Use Configuration extension api to get merged file. + try { + mergedPropertiesConfig = new MergedPropertiesConfig( + LOG_CONFIGURATION_COMMON_FILE_NAME, + hooks.defaultConfigurationAppId(), + CapaLogProperties.Settings.getCenterConfigAppId()); + } catch (Throwable throwable) { + Mixer.telemetryHooksNullable().ifPresent(telemetryHooks -> { + Meter meter = telemetryHooks.buildMeter("LogsConfiguration").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); + } catch (Exception e) { + return false; + } + } + + public static String get(String key) { + try { + return mergedPropertiesConfig == null + ? "" + : mergedPropertiesConfig.get(key); + } 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 new file mode 100644 index 0000000..df3a2be --- /dev/null +++ b/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/enums/CapaLogLevel.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.spi.log.enums; + +import java.util.Arrays; +import java.util.Optional; + +/** + * Capa log level. + */ +public enum CapaLogLevel { + + /** + * Standard order of log priorities:ALL,TRACE,DEBUG,INFO,WARN,ERROR,FATAL,OFF + */ + ALL(1, "ALL"), + TRACE(2, "TRACE"), + DEBUG(3, "DEBUG"), + INFO(4, "INFO"), + WARN(5, "WARN"), + ERROR(6, "ERROR"), + FATAL(7, "FATAL"), + OFF(8, "OFF"); + + final int level; + final String levelName; + + CapaLogLevel(int level, String levelName) { + this.level = level; + this.levelName = levelName; + } + + /** + * Convert logLevelArg to {@link CapaLogLevel} + */ + public static Optional toCapaLogLevel(String logLevelArg) { + return Arrays.stream(CapaLogLevel.values()) + .filter(logLevel -> logLevel.levelName.equalsIgnoreCase(logLevelArg)) + .findAny(); + } + + /** + * Get level. + */ + public int getLevel() { + return level; + } + + public String getLevelName() { + return levelName; + } +} 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 new file mode 100644 index 0000000..5a65545 --- /dev/null +++ b/sdk-spi/src/main/java/group/rxcloud/capa/spi/log/manager/LogManager.java @@ -0,0 +1,125 @@ +/* + * 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.manager; + +import group.rxcloud.capa.spi.log.configuration.LogConfiguration; +import group.rxcloud.capa.spi.log.enums.CapaLogLevel; + +/** + * LogManager, to manage log output levels. + */ +public class LogManager { + + /** + * 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. + * 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. + * + * @return If the log can be output, it is true, otherwise it is false. + */ + 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 LogConfiguration.containsKey(ALL_LEVEL_SWITCH_NAME) + ? Boolean.FALSE + : Boolean.parseBoolean(LogConfiguration.get(ALL_LEVEL_SWITCH_NAME)); + case TRACE: + return LogConfiguration.containsKey(TRACE_LEVEL_SWITCH_NAME) + ? Boolean.FALSE + : Boolean.parseBoolean(LogConfiguration.get(TRACE_LEVEL_SWITCH_NAME)); + case DEBUG: + return LogConfiguration.containsKey(DEBUG_LEVEL_SWITCH_NAME) + ? Boolean.FALSE + : Boolean.parseBoolean(LogConfiguration.get(DEBUG_LEVEL_SWITCH_NAME)); + case INFO: + return LogConfiguration.containsKey(INFO_LEVEL_SWITCH_NAME) + ? Boolean.FALSE + : Boolean.parseBoolean(LogConfiguration.get(INFO_LEVEL_SWITCH_NAME)); + case WARN: + return LogConfiguration.containsKey(WARN_LEVEL_SWITCH_NAME) + ? Boolean.FALSE + : Boolean.parseBoolean(LogConfiguration.get(WARN_LEVEL_SWITCH_NAME)); + case ERROR: + return LogConfiguration.containsKey(ERROR_LEVEL_SWITCH_NAME) + ? Boolean.FALSE + : Boolean.parseBoolean(LogConfiguration.get(ERROR_LEVEL_SWITCH_NAME)); + case FATAL: + return LogConfiguration.containsKey(FATAL_LEVEL_SWITCH_NAME) + ? Boolean.FALSE + : Boolean.parseBoolean(LogConfiguration.get(FATAL_LEVEL_SWITCH_NAME)); + case OFF: + return LogConfiguration.containsKey(OFF_LEVEL_SWITCH_NAME) + ? Boolean.FALSE + : Boolean.parseBoolean(LogConfiguration.get(OFF_LEVEL_SWITCH_NAME)); + default: + return Boolean.FALSE; + } + } + + /** + * Logs of the {@link CapaLogLevel#INFO} level or higher are normally output, but logs of the {@link CapaLogLevel#INFO} level lower are not output. + * + * @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(); + } +} 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/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); + } +} 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..a0fb3a7 --- /dev/null +++ b/sdk-spi/src/main/java/group/rxcloud/capa/spi/telemetry/CapaMetricsExporterSpi.java @@ -0,0 +1,32 @@ +/* + * 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..c1a9a01 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.RELEASE sdk-springboot diff --git a/sdk/pom.xml b/sdk/pom.xml index b1e9a06..d01857c 100644 --- a/sdk/pom.xml +++ b/sdk/pom.xml @@ -23,7 +23,7 @@ group.rxcloud capa-parent - 1.0.7.RELEASE + 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 9b5023a..fb8d324 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} */ @@ -79,7 +81,6 @@ public CapaTelemetryClientBuilder addContextPropagators(TextMapPropagator proces return this; } - @Override public CapaTelemetryClientBuilder setMeterConfig(MeterConfig config) { meterProviderBuilder.setMeterConfig(config); @@ -93,7 +94,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; @@ -113,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 5755b13..62c3ce6 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 @@ -91,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) { @@ -109,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/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/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 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(); } 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; +}