Skip to content

Commit bb2d0ac

Browse files
Opamp migration (#789)
* Adding opamp lib to the catalog * Adapting CentralConfig to upstream opamp * Validating OpampManager * Clean up local opamp implementation * Updating opampmanager constructor visibility * Removing mockwebserver * Using wiremock for opamp test * Spotless * Excluding otel transitive dependency * Update custom/src/main/java/co/elastic/otel/dynamicconfig/internal/OpampManager.java Co-authored-by: jackshirazi <[email protected]> * Adding test for opamp exponential backoff * Improving test for opamp exponential backoff * Updating comment * Updating comment --------- Co-authored-by: jackshirazi <[email protected]>
1 parent d657d8a commit bb2d0ac

File tree

64 files changed

+553
-54758
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+553
-54758
lines changed

custom/build.gradle.kts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ val instrumentations = listOf<String>(
88

99
dependencies {
1010
implementation(project(":common"))
11-
implementation(project(":opamp"))
11+
implementation(libs.opentelemetry.opamp) {
12+
// exclude transitive dependency as it's provided through agent packaging
13+
exclude(group = "io.opentelemetry", module = "opentelemetry-api")
14+
}
15+
implementation(libs.dslJson)
1216
implementation(project(":inferred-spans"))
1317
implementation(project(":universal-profiling-integration"))
1418
implementation(project(":resources"))
@@ -49,6 +53,7 @@ dependencies {
4953
testImplementation("io.opentelemetry.javaagent:opentelemetry-testing-common")
5054
testImplementation("io.opentelemetry:opentelemetry-sdk-testing")
5155
testImplementation(libs.freemarker)
56+
testImplementation(libs.wiremockjre8)
5257
}
5358

5459
tasks {

custom/src/main/java/co/elastic/otel/dynamicconfig/CentralConfig.java

Lines changed: 39 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@
1818
*/
1919
package co.elastic.otel.dynamicconfig;
2020

21-
import co.elastic.opamp.client.CentralConfigurationManager;
22-
import co.elastic.opamp.client.CentralConfigurationManagerImpl;
23-
import co.elastic.opamp.client.CentralConfigurationProcessor;
21+
import co.elastic.otel.dynamicconfig.internal.OpampManager;
2422
import co.elastic.otel.logging.AgentLog;
2523
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
2624
import io.opentelemetry.sdk.trace.SdkTracerProviderBuilder;
25+
import java.io.IOException;
2726
import java.text.MessageFormat;
2827
import java.time.Duration;
2928
import java.time.format.DateTimeParseException;
3029
import java.util.HashSet;
3130
import java.util.Map;
3231
import java.util.Set;
32+
import java.util.logging.Level;
3333
import java.util.logging.Logger;
3434
import java.util.stream.Collectors;
3535
import java.util.stream.Stream;
@@ -60,27 +60,31 @@ public static void init(SdkTracerProviderBuilder providerBuilder, ConfigProperti
6060
logger.info("Starting OpAmp client for: " + serviceName + " on endpoint " + endpoint);
6161
DynamicInstrumentation.setTracerConfigurator(
6262
providerBuilder, DynamicConfiguration.UpdatableConfigurator.INSTANCE);
63-
CentralConfigurationManager centralConfigurationManager =
64-
CentralConfigurationManager.builder()
63+
OpampManager opampManager =
64+
OpampManager.builder()
6565
.setServiceName(serviceName)
6666
.setPollingInterval(Duration.ofSeconds(30))
6767
.setConfigurationEndpoint(endpoint)
6868
.setServiceEnvironment(environment)
6969
.build();
7070

71-
centralConfigurationManager.start(
71+
opampManager.start(
7272
configuration -> {
7373
logger.fine("Received configuration: " + configuration);
74-
Configs.applyConfigurations(configuration, centralConfigurationManager);
75-
return CentralConfigurationProcessor.Result.SUCCESS;
74+
Configs.applyConfigurations(configuration, opampManager);
75+
return OpampManager.CentralConfigurationProcessor.Result.SUCCESS;
7676
});
7777

7878
Runtime.getRuntime()
7979
.addShutdownHook(
8080
new Thread(
8181
() -> {
82-
logger.info("=========== Shutting down OpAmp client for: " + serviceName);
83-
centralConfigurationManager.stop();
82+
logger.info("=========== Shutting down OpAMP client for: " + serviceName);
83+
try {
84+
opampManager.close();
85+
} catch (IOException e) {
86+
logger.log(Level.SEVERE, "Error during OpAMP shutdown", e);
87+
}
8488
}));
8589
}
8690

@@ -129,40 +133,35 @@ public static class Configs {
129133
}
130134

131135
public static synchronized void applyConfigurations(
132-
Map<String, String> configuration,
133-
CentralConfigurationManager centralConfigurationManager) {
136+
Map<String, String> configuration, OpampManager opampManager) {
134137
Set<String> copyOfCurrentNonDefaultConfigsApplied =
135138
new HashSet<>(currentNonDefaultConfigsApplied);
136139
configuration.forEach(
137140
(configurationName, configurationValue) -> {
138141
copyOfCurrentNonDefaultConfigsApplied.remove(configurationName);
139-
applyConfiguration(configurationName, configurationValue, centralConfigurationManager);
142+
applyConfiguration(configurationName, configurationValue, opampManager);
140143
currentNonDefaultConfigsApplied.add(configurationName);
141144
});
142145
if (!copyOfCurrentNonDefaultConfigsApplied.isEmpty()) {
143146
// We have configs that were applied previously but have now been set back to default and
144147
// have been removed from the configs being sent - so for all of these we need to set the
145148
// config back to default
146149
for (String configurationName : copyOfCurrentNonDefaultConfigsApplied) {
147-
applyDefaultConfiguration(configurationName, centralConfigurationManager);
150+
applyDefaultConfiguration(configurationName, opampManager);
148151
currentNonDefaultConfigsApplied.remove(configurationName);
149152
}
150153
}
151154
}
152155

153156
public static void applyDefaultConfiguration(
154-
String configurationName, CentralConfigurationManager centralConfigurationManager) {
155-
configNameToConfig.get(configurationName).updateToDefault(centralConfigurationManager);
157+
String configurationName, OpampManager opampManager) {
158+
configNameToConfig.get(configurationName).updateToDefault(opampManager);
156159
}
157160

158161
public static void applyConfiguration(
159-
String configurationName,
160-
String configurationValue,
161-
CentralConfigurationManager centralConfigurationManager) {
162+
String configurationName, String configurationValue, OpampManager opampManager) {
162163
if (configNameToConfig.containsKey(configurationName)) {
163-
configNameToConfig
164-
.get(configurationName)
165-
.updateOrLog(configurationValue, centralConfigurationManager);
164+
configNameToConfig.get(configurationName).updateOrLog(configurationValue, opampManager);
166165
} else {
167166
logger.warning(
168167
"Ignoring unknown confguration option: '"
@@ -204,21 +203,19 @@ protected boolean getBoolean(String configurationValue, String error) {
204203
}
205204
}
206205

207-
public void updateOrLog(
208-
String configurationValue, CentralConfigurationManager centralConfigurationManager) {
206+
public void updateOrLog(String configurationValue, OpampManager opampManager) {
209207
try {
210-
update(configurationValue, centralConfigurationManager);
208+
update(configurationValue, opampManager);
211209
} catch (IllegalArgumentException e) {
212210
logger.warning(e.getMessage());
213211
}
214212
}
215213

216-
abstract void update(
217-
String configurationValue, CentralConfigurationManager centralConfigurationManager)
214+
abstract void update(String configurationValue, OpampManager opampManager)
218215
throws IllegalArgumentException;
219216

220-
public void updateToDefault(CentralConfigurationManager centralConfigurationManager) {
221-
update(defaultConfigStringValue, centralConfigurationManager);
217+
public void updateToDefault(OpampManager opampManager) {
218+
update(defaultConfigStringValue, opampManager);
222219
}
223220

224221
protected DynamicConfiguration config() {
@@ -232,7 +229,7 @@ public static final class SendLogs extends ConfigOption {
232229
}
233230

234231
@Override
235-
void update(String configurationValue, CentralConfigurationManager centralConfigurationManager)
232+
void update(String configurationValue, OpampManager opampManager)
236233
throws IllegalArgumentException {
237234
config().setSendingLogs(getBoolean(configurationValue));
238235
}
@@ -244,7 +241,7 @@ public static final class SendMetrics extends ConfigOption {
244241
}
245242

246243
@Override
247-
void update(String configurationValue, CentralConfigurationManager centralConfigurationManager)
244+
void update(String configurationValue, OpampManager opampManager)
248245
throws IllegalArgumentException {
249246
config().setSendingMetrics(getBoolean(configurationValue));
250247
}
@@ -256,7 +253,7 @@ public static final class SendTraces extends ConfigOption {
256253
}
257254

258255
@Override
259-
void update(String configurationValue, CentralConfigurationManager centralConfigurationManager)
256+
void update(String configurationValue, OpampManager opampManager)
260257
throws IllegalArgumentException {
261258
config().setSendingSpans(getBoolean(configurationValue));
262259
}
@@ -268,7 +265,7 @@ public static final class DeactivateAllInstrumentations extends ConfigOption {
268265
}
269266

270267
@Override
271-
void update(String configurationValue, CentralConfigurationManager centralConfigurationManager)
268+
void update(String configurationValue, OpampManager opampManager)
272269
throws IllegalArgumentException {
273270
if (getBoolean(configurationValue)) {
274271
config().deactivateAllInstrumentations();
@@ -284,7 +281,7 @@ public static final class DeactivateInstrumentations extends ConfigOption {
284281
}
285282

286283
@Override
287-
void update(String configurationValue, CentralConfigurationManager centralConfigurationManager)
284+
void update(String configurationValue, OpampManager opampManager)
288285
throws IllegalArgumentException {
289286
config().deactivateInstrumentations(configurationValue);
290287
}
@@ -296,7 +293,7 @@ public static final class LoggingLevel extends ConfigOption {
296293
}
297294

298295
@Override
299-
void update(String configurationValue, CentralConfigurationManager centralConfigurationManager)
296+
void update(String configurationValue, OpampManager opampManager)
300297
throws IllegalArgumentException {
301298
AgentLog.setLevel(configurationValue);
302299
}
@@ -308,17 +305,14 @@ public static final class PollingInterval extends ConfigOption {
308305
}
309306

310307
@Override
311-
void update(String configurationValue, CentralConfigurationManager centralConfigurationManager)
308+
void update(String configurationValue, OpampManager opampManager)
312309
throws IllegalArgumentException {
313-
if (centralConfigurationManager instanceof CentralConfigurationManagerImpl) {
314-
try {
315-
Duration duration = Duration.parse("PT" + 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-
}
310+
try {
311+
Duration duration = Duration.parse("PT" + configurationValue);
312+
opampManager.setPollingDelay(duration);
313+
} catch (DateTimeParseException e) {
314+
logger.warning(
315+
"Failed to update the polling interval, value passed was invalid: " + e.getMessage());
322316
}
323317
}
324318
}

0 commit comments

Comments
 (0)