1616 */
1717package group .rxcloud .capa .component .telemetry ;
1818
19- import com .google .common .collect .Lists ;
2019import group .rxcloud .capa .component .telemetry .metrics .CapaMeterProviderBuilder ;
2120import group .rxcloud .capa .infrastructure .CapaProperties ;
22- import group .rxcloud .capa .infrastructure .hook .ConfigurationHooks ;
21+ import group .rxcloud .capa .infrastructure .hook .MergedPropertiesConfig ;
2322import group .rxcloud .capa .infrastructure .hook .Mixer ;
24- import group .rxcloud .cloudruntimes .domain .core .configuration .SubConfigurationResp ;
25- import group .rxcloud .cloudruntimes .utils .TypeRef ;
26- import org .apache .commons .collections .CollectionUtils ;
27- import org .apache .commons .lang3 .StringUtils ;
2823import org .slf4j .Logger ;
2924import org .slf4j .LoggerFactory ;
30- import reactor .core .publisher .Flux ;
3125
3226import java .io .Serializable ;
3327import java .util .function .Supplier ;
3731 */
3832public class SamplerConfig implements Serializable {
3933
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" ;
45-
4634 /**
4735 * Sample all data as default.
4836 */
@@ -53,25 +41,44 @@ public class SamplerConfig implements Serializable {
5341
5442 public static final transient SamplerConfig CONFIG = new SamplerConfig ();
5543
56- public static final transient Supplier <SamplerConfig > DEFAULT_SUPPLIER = () -> {
57- return CONFIG ;
58- };
44+ private static final long serialVersionUID = -2113523925814197551L ;
5945
6046 private static final transient Logger log = LoggerFactory .getLogger (CapaMeterProviderBuilder .class );
6147
48+ public static transient Supplier <SamplerConfig > DEFAULT_SUPPLIER = () -> {
49+ return CONFIG ;
50+ };
6251
6352 static {
6453 Mixer .configurationHooksNullable ().ifPresent (hooks -> {
54+
55+ String fileName = "capa-component-telemetry-sample.properties" ;
56+
57+ String suffix = "telemetry-common" ;
58+
6559 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 );
60+ MergedPropertiesConfig config = new MergedPropertiesConfig (fileName , hooks
61+ .defaultConfigurationAppId (),
62+ CapaProperties .COMPONENT_PROPERTIES_SUPPLIER .apply (suffix ).getProperty ("appId" ));
63+ String metricKey = "metricsEnable" ;
64+ String traceKey = "traceEnable" ;
65+ SamplerConfig dynamicConfig = new SamplerConfig () {
66+ @ Override
67+ public Boolean isMetricsEnable () {
68+ return !config .containsKey (metricKey ) || Boolean .TRUE .toString ()
69+ .equalsIgnoreCase (config .get (metricKey ));
70+ }
71+
72+ @ Override
73+ public Boolean isTraceEnable () {
74+ return !config .containsKey (traceKey ) || Boolean .TRUE .toString ()
75+ .equalsIgnoreCase (config .get (traceKey ));
76+ }
77+ };
78+
79+ DEFAULT_SUPPLIER = () -> {
80+ return dynamicConfig ;
81+ };
7582 } catch (Throwable throwable ) {
7683 log .warn ("Fail to load global telemetry config. Dynamic global config is disabled for capa telemetry." ,
7784 throwable );
@@ -84,31 +91,6 @@ public class SamplerConfig implements Serializable {
8491
8592 private Boolean traceEnable ;
8693
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- }
111-
11294 public Boolean isMetricsEnable () {
11395 return metricsEnable == null ? DEFAULT_CONFIG .metricsEnable : metricsEnable ;
11496 }
0 commit comments