Skip to content

Commit 8bb40cf

Browse files
authored
Merge pull request #64 from JasmineJ1230/feature/metrics
Fix telemetry configure.
2 parents 14899b8 + e40e0d0 commit 8bb40cf

File tree

22 files changed

+208
-72
lines changed

22 files changed

+208
-72
lines changed

examples/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<parent>
2424
<artifactId>capa-parent</artifactId>
2525
<groupId>group.rxcloud</groupId>
26-
<version>1.0.8-alpha-1</version>
26+
<version>1.0.8-alpha-2</version>
2727
</parent>
2828

2929
<artifactId>capa-examples</artifactId>

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<groupId>group.rxcloud</groupId>
2424
<artifactId>capa-parent</artifactId>
2525
<packaging>pom</packaging>
26-
<version>1.0.8-alpha-1</version>
26+
<version>1.0.8-alpha-2</version>
2727
<name>capa-sdk-parent</name>
2828
<description>SDK for Capa.</description>
2929
<url>https://github.com/reactivegroup</url>

sdk-component/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<parent>
2424
<groupId>group.rxcloud</groupId>
2525
<artifactId>capa-parent</artifactId>
26-
<version>1.0.8-alpha-1</version>
26+
<version>1.0.8-alpha-2</version>
2727
</parent>
2828

2929
<artifactId>capa-sdk-component</artifactId>

sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/SamplerConfig.java

Lines changed: 68 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -16,78 +16,109 @@
1616
*/
1717
package group.rxcloud.capa.component.telemetry;
1818

19+
import com.google.common.collect.Lists;
1920
import group.rxcloud.capa.component.telemetry.metrics.CapaMeterProviderBuilder;
2021
import group.rxcloud.capa.infrastructure.CapaProperties;
2122
import group.rxcloud.capa.infrastructure.hook.ConfigurationHooks;
2223
import group.rxcloud.capa.infrastructure.hook.Mixer;
23-
import group.rxcloud.cloudruntimes.domain.core.configuration.ConfigurationItem;
24+
import group.rxcloud.cloudruntimes.domain.core.configuration.SubConfigurationResp;
2425
import group.rxcloud.cloudruntimes.utils.TypeRef;
26+
import org.apache.commons.collections.CollectionUtils;
27+
import org.apache.commons.lang3.StringUtils;
2528
import org.slf4j.Logger;
2629
import org.slf4j.LoggerFactory;
30+
import reactor.core.publisher.Flux;
2731

2832
import java.io.Serializable;
29-
import java.util.Collections;
30-
import java.util.List;
31-
import java.util.Optional;
3233
import java.util.function.Supplier;
3334

3435
/**
3536
* Sampler config.
3637
*/
3738
public class SamplerConfig implements Serializable {
3839

39-
public static final transient String FILE_PATH = "capa-sample.properties";
40+
private static final long serialVersionUID = -2113523925814197551L;
41+
42+
public static final transient String FILE_PATH = "capa-component-telemetry-sample.properties";
43+
44+
public static final transient String COMMON_FILE_SUFFIX = "telemetry-common";
4045

4146
/**
4247
* Sample all data as default.
4348
*/
44-
public static final transient SamplerConfig DEFAULT_CONFIG = new SamplerConfig();
49+
public static final transient SamplerConfig DEFAULT_CONFIG = new SamplerConfig() {{
50+
setTraceEnable(true);
51+
setMetricsEnable(true);
52+
}};
4553

46-
private static final transient Logger log = LoggerFactory.getLogger(CapaMeterProviderBuilder.class);
54+
public static final transient SamplerConfig CONFIG = new SamplerConfig();
4755

4856
public static final transient Supplier<SamplerConfig> DEFAULT_SUPPLIER = () -> {
49-
try {
50-
String storeName = Optional.ofNullable(CapaProperties.COMPONENT_PROPERTIES_SUPPLIER.apply("configuration")
51-
.getProperty(
52-
"CONFIGURATION_COMPONENT_STORE_NAME"))
53-
.orElse("UN_CONFIGURED_STORE_CONFIG_NAME");
54-
Optional<ConfigurationHooks> hooksOptional = Mixer.configurationHooksNullable();
55-
if (hooksOptional.isPresent()) {
56-
List<ConfigurationItem<SamplerConfig>> config = hooksOptional.get().getConfiguration(storeName,
57-
null,
58-
Collections.singletonList(FILE_PATH),
59-
null,
60-
"",
61-
"",
62-
TypeRef.get(SamplerConfig.class)).block();
63-
if (!config.isEmpty()) {
64-
SamplerConfig item = config.get(0).getContent();
65-
return item == null ? DEFAULT_CONFIG : item;
66-
}
57+
return CONFIG;
58+
};
59+
60+
private static final transient Logger log = LoggerFactory.getLogger(CapaMeterProviderBuilder.class);
61+
62+
63+
static {
64+
Mixer.configurationHooksNullable().ifPresent(hooks -> {
65+
try {
66+
subscribeConfiguration(hooks, hooks.defaultConfigurationAppId(), true);
67+
} catch (Throwable throwable) {
68+
log.warn("Fail to load global telemetry config. Dynamic global config is disabled for capa telemetry.",
69+
throwable);
70+
}
71+
try {
72+
subscribeConfiguration(hooks,
73+
CapaProperties.COMPONENT_PROPERTIES_SUPPLIER.apply(COMMON_FILE_SUFFIX).getProperty("appId"),
74+
false);
75+
} catch (Throwable throwable) {
76+
log.warn("Fail to load global telemetry config. Dynamic global config is disabled for capa telemetry.",
77+
throwable);
6778
}
68-
} catch (Throwable throwable) {
69-
log.warn("Fail to load config item. Dynamic config is disabled for capa telemetry.", throwable);
70-
}
79+
});
7180

72-
return DEFAULT_CONFIG;
73-
};
81+
}
7482

75-
private static final long serialVersionUID = -2113523925814197551L;
83+
private Boolean metricsEnable;
7684

77-
private boolean metricsEnable = true;
85+
private Boolean traceEnable;
7886

79-
private boolean traceEnable = true;
87+
private static void subscribeConfiguration(ConfigurationHooks configurationHooks, String appId, boolean prior) {
88+
String storeName = configurationHooks.registryStoreNames().get(0);
89+
Flux<SubConfigurationResp<SamplerConfig>> configFlux = configurationHooks.subscribeConfiguration(
90+
storeName,
91+
appId,
92+
Lists.newArrayList(FILE_PATH),
93+
null,
94+
StringUtils.EMPTY,
95+
StringUtils.EMPTY,
96+
TypeRef.get(SamplerConfig.class));
97+
configFlux.subscribe(resp -> {
98+
if (CollectionUtils.isNotEmpty(resp.getItems())) {
99+
SamplerConfig config = resp.getItems().get(0).getContent();
100+
if (config != null) {
101+
if (config.metricsEnable != null && (prior || CONFIG.metricsEnable == null)) {
102+
CONFIG.metricsEnable = config.metricsEnable;
103+
}
104+
if (config.traceEnable != null && (prior || CONFIG.traceEnable == null)) {
105+
CONFIG.traceEnable = config.traceEnable;
106+
}
107+
}
108+
}
109+
});
110+
}
80111

81-
public boolean isMetricsEnable() {
82-
return metricsEnable;
112+
public Boolean isMetricsEnable() {
113+
return metricsEnable == null ? DEFAULT_CONFIG.metricsEnable : metricsEnable;
83114
}
84115

85116
public void setMetricsEnable(boolean metricsEnable) {
86117
this.metricsEnable = metricsEnable;
87118
}
88119

89-
public boolean isTraceEnable() {
90-
return traceEnable;
120+
public Boolean isTraceEnable() {
121+
return traceEnable == null ? DEFAULT_CONFIG.traceEnable : traceEnable;
91122
}
92123

93124
public void setTraceEnable(boolean traceEnable) {

sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/context/CapaContextPropagatorSettings.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
*/
2424
public interface CapaContextPropagatorSettings {
2525

26-
// FIXME: 2021/11/28 change to capa-component-telemetry-context.json
27-
String FILE_PATH = "/capa-context.json";
26+
String FILE_PATH = "/capa-component-telemetry-context.json";
2827

2928
/**
3029
* Replace the whole context config.

sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMeterProviderSettings.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525
*/
2626
public interface CapaMeterProviderSettings {
2727

28-
// FIXME: 2021/11/28 change to capa-component-telemetry-meter.json
29-
String FILE_PATH ="/capa-meter.json";
28+
String FILE_PATH ="/capa-component-telemetry-meter.json";
3029

3130
/**
3231
* Replace the whole config for the meter.

sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/trace/CapaTracerProviderSettings.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727
*/
2828
public interface CapaTracerProviderSettings {
2929

30-
// FIXME: 2021/11/28 change to capa-component-telemetry-tracer.json
31-
String FILE_PATH = "/capa-tracer.json";
30+
String FILE_PATH = "/capa-component-telemetry-tracer.json";
3231

3332
/**
3433
* Replace the whole config for the meter.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package group.rxcloud.capa.component.telemetry;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import static org.junit.jupiter.api.Assertions.assertFalse;
22+
23+
/**
24+
* @author: chenyijiang
25+
* @date: 2021/12/2 12:33
26+
*/
27+
public class SamplerConfigTest {
28+
29+
@Test
30+
public void isMetricsEnable() {
31+
assertFalse(SamplerConfig.DEFAULT_SUPPLIER.get().isMetricsEnable());
32+
}
33+
34+
@Test
35+
public void isTraceEnable() {
36+
assertFalse(SamplerConfig.DEFAULT_SUPPLIER.get().isTraceEnable());
37+
}
38+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package group.rxcloud.capa.component.telemetry;
18+
19+
import com.google.common.collect.Lists;
20+
import group.rxcloud.capa.infrastructure.hook.ConfigurationHooks;
21+
import group.rxcloud.capa.infrastructure.hook.Mixer;
22+
import group.rxcloud.capa.infrastructure.hook.TelemetryHooks;
23+
import group.rxcloud.cloudruntimes.domain.core.configuration.ConfigurationItem;
24+
import group.rxcloud.cloudruntimes.domain.core.configuration.SubConfigurationResp;
25+
import group.rxcloud.cloudruntimes.utils.TypeRef;
26+
import reactor.core.publisher.Flux;
27+
28+
import java.util.List;
29+
import java.util.Map;
30+
31+
/**
32+
* @author: chenyijiang
33+
* @date: 2021/12/2 12:39
34+
*/
35+
public class TestMixerProvider implements Mixer.MixerProvider {
36+
37+
SamplerConfig app = new SamplerConfig() {{setMetricsEnable(false);}};
38+
SamplerConfig global = new SamplerConfig() {{setTraceEnable(false); setMetricsEnable(true);}};
39+
40+
41+
private ConfigurationHooks configurationHooks = new ConfigurationHooks() {
42+
@Override
43+
public List<String> registryStoreNames() {
44+
return Lists.newArrayList("QConfig");
45+
}
46+
47+
@Override
48+
public String defaultConfigurationAppId() {
49+
return "1234567";
50+
}
51+
52+
@Override
53+
public <T> Flux<SubConfigurationResp<T>> subscribeConfiguration(String storeName, String appId, List<String> keys, Map<String, String> metadata, String group, String label, TypeRef<T> type) {
54+
55+
if (type.getType() == SamplerConfig.class) {
56+
if ("123".equals(appId)) {
57+
return Flux.just(getSubscribeResponse(global));
58+
}
59+
if (defaultConfigurationAppId().equals(appId)) {
60+
return Flux.just(getSubscribeResponse(app));
61+
}
62+
return Flux.just(null);
63+
}
64+
return ConfigurationHooks.super.subscribeConfiguration(storeName, appId, keys, metadata, type);
65+
}
66+
};
67+
68+
private <T> SubConfigurationResp<T> getSubscribeResponse(SamplerConfig samplerConfig) {
69+
SubConfigurationResp<T> subConfigurationResp = new SubConfigurationResp<>();
70+
ConfigurationItem item = new ConfigurationItem();
71+
item.setContent(samplerConfig);
72+
subConfigurationResp.setItems(Lists.newArrayList(item));
73+
return subConfigurationResp;
74+
}
75+
76+
@Override
77+
public ConfigurationHooks provideConfigurationHooks() {
78+
return configurationHooks;
79+
}
80+
81+
@Override
82+
public TelemetryHooks provideTelemetryHooks() {
83+
return null;
84+
}
85+
86+
87+
}

sdk-component/src/test/java/group/rxcloud/capa/component/telemetry/trace/CapaTracerProviderBuilderTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ public boolean matches(ReadableSpan span) {
116116
@Test
117117
public void buildFromTraceConfig() {
118118
CapaTracerProvider provider = new CapaTracerProviderBuilder()
119+
.setSamplerConfig(() -> SamplerConfig.DEFAULT_CONFIG)
119120
.buildTracerProvider();
120121

121122
Span span = provider.tracerBuilder("test")

0 commit comments

Comments
 (0)