|
16 | 16 | */ |
17 | 17 | package group.rxcloud.capa.component.telemetry; |
18 | 18 |
|
| 19 | +import com.google.common.collect.Lists; |
19 | 20 | import group.rxcloud.capa.component.telemetry.metrics.CapaMeterProviderBuilder; |
20 | 21 | import group.rxcloud.capa.infrastructure.CapaProperties; |
21 | 22 | import group.rxcloud.capa.infrastructure.hook.ConfigurationHooks; |
22 | 23 | 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; |
24 | 25 | import group.rxcloud.cloudruntimes.utils.TypeRef; |
| 26 | +import org.apache.commons.collections.CollectionUtils; |
| 27 | +import org.apache.commons.lang3.StringUtils; |
25 | 28 | import org.slf4j.Logger; |
26 | 29 | import org.slf4j.LoggerFactory; |
| 30 | +import reactor.core.publisher.Flux; |
27 | 31 |
|
28 | 32 | import java.io.Serializable; |
29 | | -import java.util.Collections; |
30 | | -import java.util.List; |
31 | | -import java.util.Optional; |
32 | 33 | import java.util.function.Supplier; |
33 | 34 |
|
34 | 35 | /** |
35 | 36 | * Sampler config. |
36 | 37 | */ |
37 | 38 | public class SamplerConfig implements Serializable { |
38 | 39 |
|
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"; |
40 | 45 |
|
41 | 46 | /** |
42 | 47 | * Sample all data as default. |
43 | 48 | */ |
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 | + }}; |
45 | 53 |
|
46 | | - private static final transient Logger log = LoggerFactory.getLogger(CapaMeterProviderBuilder.class); |
| 54 | + public static final transient SamplerConfig CONFIG = new SamplerConfig(); |
47 | 55 |
|
48 | 56 | 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); |
67 | 78 | } |
68 | | - } catch (Throwable throwable) { |
69 | | - log.warn("Fail to load config item. Dynamic config is disabled for capa telemetry.", throwable); |
70 | | - } |
| 79 | + }); |
71 | 80 |
|
72 | | - return DEFAULT_CONFIG; |
73 | | - }; |
| 81 | + } |
74 | 82 |
|
75 | | - private static final long serialVersionUID = -2113523925814197551L; |
| 83 | + private Boolean metricsEnable; |
76 | 84 |
|
77 | | - private boolean metricsEnable = true; |
| 85 | + private Boolean traceEnable; |
78 | 86 |
|
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 | + } |
80 | 111 |
|
81 | | - public boolean isMetricsEnable() { |
82 | | - return metricsEnable; |
| 112 | + public Boolean isMetricsEnable() { |
| 113 | + return metricsEnable == null ? DEFAULT_CONFIG.metricsEnable : metricsEnable; |
83 | 114 | } |
84 | 115 |
|
85 | 116 | public void setMetricsEnable(boolean metricsEnable) { |
86 | 117 | this.metricsEnable = metricsEnable; |
87 | 118 | } |
88 | 119 |
|
89 | | - public boolean isTraceEnable() { |
90 | | - return traceEnable; |
| 120 | + public Boolean isTraceEnable() { |
| 121 | + return traceEnable == null ? DEFAULT_CONFIG.traceEnable : traceEnable; |
91 | 122 | } |
92 | 123 |
|
93 | 124 | public void setTraceEnable(boolean traceEnable) { |
|
0 commit comments