19
19
package co .elastic .otel .dynamicconfig ;
20
20
21
21
import co .elastic .opamp .client .CentralConfigurationManager ;
22
+ import co .elastic .opamp .client .CentralConfigurationManagerImpl ;
22
23
import co .elastic .opamp .client .CentralConfigurationProcessor ;
23
24
import co .elastic .otel .logging .AgentLog ;
24
25
import io .opentelemetry .sdk .autoconfigure .spi .ConfigProperties ;
25
26
import io .opentelemetry .sdk .trace .SdkTracerProviderBuilder ;
26
27
import java .text .MessageFormat ;
27
28
import java .time .Duration ;
29
+ import java .time .format .DateTimeParseException ;
28
30
import java .util .HashSet ;
29
31
import java .util .Map ;
30
32
import java .util .Set ;
@@ -69,7 +71,7 @@ public static void init(SdkTracerProviderBuilder providerBuilder, ConfigProperti
69
71
centralConfigurationManager .start (
70
72
configuration -> {
71
73
logger .fine ("Received configuration: " + configuration );
72
- Configs .applyConfigurations (configuration );
74
+ Configs .applyConfigurations (configuration , centralConfigurationManager );
73
75
return CentralConfigurationProcessor .Result .SUCCESS ;
74
76
});
75
77
@@ -121,37 +123,46 @@ public static class Configs {
121
123
new SendTraces (),
122
124
new DeactivateAllInstrumentations (),
123
125
new DeactivateInstrumentations (),
124
- new LoggingLevel ())
126
+ new LoggingLevel (),
127
+ new PollingInterval ())
125
128
.collect (Collectors .toMap (ConfigOption ::getConfigName , option -> option ));
126
129
}
127
130
128
- public static synchronized void applyConfigurations (Map <String , String > configuration ) {
131
+ public static synchronized void applyConfigurations (
132
+ Map <String , String > configuration ,
133
+ CentralConfigurationManager centralConfigurationManager ) {
129
134
Set <String > copyOfCurrentNonDefaultConfigsApplied =
130
135
new HashSet <>(currentNonDefaultConfigsApplied );
131
136
configuration .forEach (
132
137
(configurationName , configurationValue ) -> {
133
138
copyOfCurrentNonDefaultConfigsApplied .remove (configurationName );
134
- applyConfiguration (configurationName , configurationValue );
139
+ applyConfiguration (configurationName , configurationValue , centralConfigurationManager );
135
140
currentNonDefaultConfigsApplied .add (configurationName );
136
141
});
137
142
if (!copyOfCurrentNonDefaultConfigsApplied .isEmpty ()) {
138
143
// We have configs that were applied previously but have now been set back to default and
139
144
// have been removed from the configs being sent - so for all of these we need to set the
140
145
// config back to default
141
146
for (String configurationName : copyOfCurrentNonDefaultConfigsApplied ) {
142
- applyDefaultConfiguration (configurationName );
147
+ applyDefaultConfiguration (configurationName , centralConfigurationManager );
143
148
currentNonDefaultConfigsApplied .remove (configurationName );
144
149
}
145
150
}
146
151
}
147
152
148
- public static void applyDefaultConfiguration (String configurationName ) {
149
- configNameToConfig .get (configurationName ).updateToDefault ();
153
+ public static void applyDefaultConfiguration (
154
+ String configurationName , CentralConfigurationManager centralConfigurationManager ) {
155
+ configNameToConfig .get (configurationName ).updateToDefault (centralConfigurationManager );
150
156
}
151
157
152
- public static void applyConfiguration (String configurationName , String configurationValue ) {
158
+ public static void applyConfiguration (
159
+ String configurationName ,
160
+ String configurationValue ,
161
+ CentralConfigurationManager centralConfigurationManager ) {
153
162
if (configNameToConfig .containsKey (configurationName )) {
154
- configNameToConfig .get (configurationName ).updateOrLog (configurationValue );
163
+ configNameToConfig
164
+ .get (configurationName )
165
+ .updateOrLog (configurationValue , centralConfigurationManager );
155
166
} else {
156
167
logger .warning (
157
168
"Ignoring unknown confguration option: '"
@@ -193,18 +204,21 @@ protected boolean getBoolean(String configurationValue, String error) {
193
204
}
194
205
}
195
206
196
- public void updateOrLog (String configurationValue ) {
207
+ public void updateOrLog (
208
+ String configurationValue , CentralConfigurationManager centralConfigurationManager ) {
197
209
try {
198
- update (configurationValue );
210
+ update (configurationValue , centralConfigurationManager );
199
211
} catch (IllegalArgumentException e ) {
200
212
logger .warning (e .getMessage ());
201
213
}
202
214
}
203
215
204
- abstract void update (String configurationValue ) throws IllegalArgumentException ;
216
+ abstract void update (
217
+ String configurationValue , CentralConfigurationManager centralConfigurationManager )
218
+ throws IllegalArgumentException ;
205
219
206
- public void updateToDefault () {
207
- update (defaultConfigStringValue );
220
+ public void updateToDefault (CentralConfigurationManager centralConfigurationManager ) {
221
+ update (defaultConfigStringValue , centralConfigurationManager );
208
222
}
209
223
210
224
protected DynamicConfiguration config () {
@@ -218,7 +232,8 @@ public static final class SendLogs extends ConfigOption {
218
232
}
219
233
220
234
@ Override
221
- void update (String configurationValue ) throws IllegalArgumentException {
235
+ void update (String configurationValue , CentralConfigurationManager centralConfigurationManager )
236
+ throws IllegalArgumentException {
222
237
config ().setSendingLogs (getBoolean (configurationValue ));
223
238
}
224
239
}
@@ -229,7 +244,8 @@ public static final class SendMetrics extends ConfigOption {
229
244
}
230
245
231
246
@ Override
232
- void update (String configurationValue ) throws IllegalArgumentException {
247
+ void update (String configurationValue , CentralConfigurationManager centralConfigurationManager )
248
+ throws IllegalArgumentException {
233
249
config ().setSendingMetrics (getBoolean (configurationValue ));
234
250
}
235
251
}
@@ -240,7 +256,8 @@ public static final class SendTraces extends ConfigOption {
240
256
}
241
257
242
258
@ Override
243
- void update (String configurationValue ) throws IllegalArgumentException {
259
+ void update (String configurationValue , CentralConfigurationManager centralConfigurationManager )
260
+ throws IllegalArgumentException {
244
261
config ().setSendingSpans (getBoolean (configurationValue ));
245
262
}
246
263
}
@@ -251,7 +268,8 @@ public static final class DeactivateAllInstrumentations extends ConfigOption {
251
268
}
252
269
253
270
@ Override
254
- void update (String configurationValue ) throws IllegalArgumentException {
271
+ void update (String configurationValue , CentralConfigurationManager centralConfigurationManager )
272
+ throws IllegalArgumentException {
255
273
if (getBoolean (configurationValue )) {
256
274
config ().deactivateAllInstrumentations ();
257
275
} else {
@@ -266,7 +284,8 @@ public static final class DeactivateInstrumentations extends ConfigOption {
266
284
}
267
285
268
286
@ Override
269
- void update (String configurationValue ) throws IllegalArgumentException {
287
+ void update (String configurationValue , CentralConfigurationManager centralConfigurationManager )
288
+ throws IllegalArgumentException {
270
289
config ().deactivateInstrumentations (configurationValue );
271
290
}
272
291
}
@@ -277,8 +296,30 @@ public static final class LoggingLevel extends ConfigOption {
277
296
}
278
297
279
298
@ Override
280
- void update (String configurationValue ) throws IllegalArgumentException {
299
+ void update (String configurationValue , CentralConfigurationManager centralConfigurationManager )
300
+ throws IllegalArgumentException {
281
301
AgentLog .setLevel (configurationValue );
282
302
}
283
303
}
304
+
305
+ public static final class PollingInterval extends ConfigOption {
306
+ PollingInterval () {
307
+ super ("polling_interval" , "30s" );
308
+ }
309
+
310
+ @ Override
311
+ void update (String configurationValue , CentralConfigurationManager centralConfigurationManager )
312
+ throws IllegalArgumentException {
313
+ if (centralConfigurationManager instanceof CentralConfigurationManagerImpl ) {
314
+ try {
315
+ Duration duration = Duration .parse (configurationValue );
316
+ ((CentralConfigurationManagerImpl ) centralConfigurationManager )
317
+ .resetPeriodicDelay (duration );
318
+ } catch (DateTimeParseException e ) {
319
+ logger .warning (
320
+ "Failed to update the polling interval, value passed was invalid: " + e .getMessage ());
321
+ }
322
+ }
323
+ }
324
+ }
284
325
}
0 commit comments