|
16 | 16 | */ |
17 | 17 | package group.rxcloud.capa.component.telemetry; |
18 | 18 |
|
19 | | -import group.rxcloud.capa.infrastructure.utils.SpiUtils; |
| 19 | +import group.rxcloud.capa.component.telemetry.metrics.CapaMeterProviderBuilder; |
| 20 | +import group.rxcloud.capa.infrastructure.CapaProperties; |
| 21 | +import group.rxcloud.capa.infrastructure.hook.ConfigurationHooks; |
| 22 | +import group.rxcloud.capa.infrastructure.hook.Mixer; |
| 23 | +import group.rxcloud.cloudruntimes.domain.core.configuration.ConfigurationItem; |
| 24 | +import group.rxcloud.cloudruntimes.utils.TypeRef; |
| 25 | +import org.slf4j.Logger; |
| 26 | +import org.slf4j.LoggerFactory; |
20 | 27 |
|
21 | 28 | import java.io.Serializable; |
22 | | -import java.util.Properties; |
| 29 | +import java.util.Collections; |
| 30 | +import java.util.List; |
| 31 | +import java.util.Optional; |
| 32 | +import java.util.function.Supplier; |
23 | 33 |
|
24 | 34 | /** |
25 | 35 | * Sampler config. |
26 | 36 | */ |
27 | 37 | public class SamplerConfig implements Serializable { |
28 | 38 |
|
29 | | - public static final String FILE_PATH = "/capa-sample.properties"; |
| 39 | + public static final transient String FILE_PATH = "capa-sample.properties"; |
30 | 40 |
|
31 | 41 | /** |
32 | 42 | * Sample all data as default. |
33 | 43 | */ |
34 | 44 | public static final transient SamplerConfig DEFAULT_CONFIG = new SamplerConfig(); |
35 | 45 |
|
36 | | - private static final long serialVersionUID = -2113523925814197551L; |
37 | | - |
38 | | - private boolean metricsSample = true; |
| 46 | + private static final transient Logger log = LoggerFactory.getLogger(CapaMeterProviderBuilder.class); |
| 47 | + |
| 48 | + 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 | + } |
| 67 | + } |
| 68 | + } catch (Throwable throwable) { |
| 69 | + log.warn("Fail to load config item. Dynamic config is disabled for capa telemetry.", throwable); |
| 70 | + } |
39 | 71 |
|
40 | | - private boolean traceSample = true; |
| 72 | + return DEFAULT_CONFIG; |
| 73 | + }; |
41 | 74 |
|
42 | | - private boolean logSample = true; |
| 75 | + private static final long serialVersionUID = -2113523925814197551L; |
43 | 76 |
|
44 | | - public boolean isMetricsSample() { |
45 | | - return metricsSample; |
46 | | - } |
| 77 | + private boolean metricsEnable = true; |
47 | 78 |
|
48 | | - public void setMetricsSample(boolean metricsSample) { |
49 | | - this.metricsSample = metricsSample; |
50 | | - } |
| 79 | + private boolean traceEnable = true; |
51 | 80 |
|
52 | | - public boolean isTraceSample() { |
53 | | - return traceSample; |
| 81 | + public boolean isMetricsEnable() { |
| 82 | + return metricsEnable; |
54 | 83 | } |
55 | 84 |
|
56 | | - public void setTraceSample(boolean traceSample) { |
57 | | - this.traceSample = traceSample; |
| 85 | + public void setMetricsEnable(boolean metricsEnable) { |
| 86 | + this.metricsEnable = metricsEnable; |
58 | 87 | } |
59 | 88 |
|
60 | | - public boolean isLogSample() { |
61 | | - return logSample; |
| 89 | + public boolean isTraceEnable() { |
| 90 | + return traceEnable; |
62 | 91 | } |
63 | 92 |
|
64 | | - public void setLogSample(boolean logSample) { |
65 | | - this.logSample = logSample; |
66 | | - } |
67 | | - |
68 | | - public static SamplerConfig loadOrDefault() { |
69 | | - Properties properties = SpiUtils.loadPropertiesNullable(FILE_PATH); |
70 | | - if (properties == null) { |
71 | | - return DEFAULT_CONFIG; |
72 | | - } |
73 | | - |
74 | | - SamplerConfig result = new SamplerConfig(); |
75 | | - result.setMetricsSample(Boolean.valueOf(properties.getProperty("metricsSample", Boolean.TRUE.toString()))); |
76 | | - result.setTraceSample(Boolean.valueOf(properties.getProperty("traceSample", Boolean.TRUE.toString()))); |
77 | | - result.setLogSample(Boolean.valueOf(properties.getProperty("logSample", Boolean.TRUE.toString()))); |
78 | | - |
79 | | - return result; |
| 93 | + public void setTraceEnable(boolean traceEnable) { |
| 94 | + this.traceEnable = traceEnable; |
80 | 95 | } |
81 | 96 | } |
0 commit comments