Skip to content

Commit 7f7762e

Browse files
committed
NH-119398: preserve user protocol selection
1 parent f8dade9 commit 7f7762e

File tree

2 files changed

+89
-4
lines changed

2 files changed

+89
-4
lines changed

custom/src/main/java/com/solarwinds/opentelemetry/extensions/config/ConfigurationLoader.java

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,19 @@ static void configureOtelLogExport(ConfigContainer container) throws InvalidConf
219219
}
220220
}
221221

222-
setSystemProperty("otel.exporter.otlp.logs.protocol", PROTOCOL);
222+
String protocol = setProtocol("otel.exporter.otlp.logs.protocol");
223223
setSystemProperty("otel.logs.exporter", "otlp");
224224
setSystemProperty(
225225
"otel.exporter.otlp.logs.headers", String.format("authorization=Bearer %s", apiKey));
226226

227+
if (protocol.equalsIgnoreCase("grpc")) {
228+
setEndpoint(
229+
"otel.exporter.otlp.logs.endpoint",
230+
String.format("https://otel.collector.%s.%s.solarwinds.com", dataCell, env),
231+
"");
232+
return;
233+
}
234+
227235
setEndpoint(
228236
"otel.exporter.otlp.logs.endpoint",
229237
String.format("https://otel.collector.%s.%s.solarwinds.com/v1/logs", dataCell, env),
@@ -256,11 +264,19 @@ static void configureOtelMetricExport(ConfigContainer container) throws InvalidC
256264
}
257265
}
258266

259-
setSystemProperty("otel.exporter.otlp.metrics.protocol", PROTOCOL);
267+
String protocol = setProtocol("otel.exporter.otlp.metrics.protocol");
260268
setSystemProperty("otel.metrics.exporter", "otlp");
261269
setSystemProperty(
262270
"otel.exporter.otlp.metrics.headers", String.format("authorization=Bearer %s", apiKey));
263271

272+
if (protocol.equalsIgnoreCase("grpc")) {
273+
setEndpoint(
274+
"otel.exporter.otlp.metrics.endpoint",
275+
String.format("https://otel.collector.%s.%s.solarwinds.com", dataCell, env),
276+
"");
277+
return;
278+
}
279+
264280
setEndpoint(
265281
"otel.exporter.otlp.metrics.endpoint",
266282
String.format("https://otel.collector.%s.%s.solarwinds.com/v1/metrics", dataCell, env),
@@ -293,10 +309,18 @@ static void configureOtelTraceExport(ConfigContainer container) throws InvalidCo
293309
}
294310
}
295311

296-
setSystemProperty("otel.exporter.otlp.traces.protocol", PROTOCOL);
312+
String protocol = setProtocol("otel.exporter.otlp.traces.protocol");
297313
setSystemProperty(
298314
"otel.exporter.otlp.traces.headers", String.format("authorization=Bearer %s", apiKey));
299315

316+
if (protocol.equalsIgnoreCase("grpc")) {
317+
setEndpoint(
318+
"otel.exporter.otlp.traces.endpoint",
319+
String.format("https://otel.collector.%s.%s.solarwinds.com", dataCell, env),
320+
"");
321+
return;
322+
}
323+
300324
setEndpoint(
301325
"otel.exporter.otlp.traces.endpoint",
302326
String.format("https://otel.collector.%s.%s.solarwinds.com/v1/traces", dataCell, env),
@@ -630,11 +654,14 @@ public static void processConfigs(ConfigContainer configs) throws InvalidConfigE
630654
ConfigManager.initialize(configs);
631655
}
632656

633-
private static void setSystemProperty(String key, String value) {
657+
private static String setSystemProperty(String key, String value) {
634658
String propertyValue = getConfigValue(key);
635659
if (propertyValue == null) {
636660
System.setProperty(key, value);
661+
return value;
637662
}
663+
664+
return propertyValue;
638665
}
639666

640667
private static void setEndpoint(String key, String value, String path) {
@@ -646,6 +673,14 @@ private static void setEndpoint(String key, String value, String path) {
646673
}
647674
}
648675

676+
private static String setProtocol(String key) {
677+
String protocol = getConfigValue("otel.exporter.otlp.protocol");
678+
if (protocol == null) {
679+
return setSystemProperty(key, ConfigurationLoader.PROTOCOL);
680+
}
681+
return protocol;
682+
}
683+
649684
static String normalize(String key) {
650685
return key.toUpperCase().replace(".", "_");
651686
}

custom/src/test/java/com/solarwinds/opentelemetry/extensions/config/ConfigurationLoaderTest.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,56 @@ void verifyTraceEndpointIsProperlyConfigured() throws InvalidConfigException {
468468
System.getProperty("otel.exporter.otlp.traces.endpoint"));
469469
}
470470

471+
@Test
472+
@SetSystemProperty(
473+
key = "otel.exporter.otlp.endpoint",
474+
value = "https://otel.collector.na-01.cloud.solarwinds.com")
475+
@SetSystemProperty(key = "otel.exporter.otlp.metrics.protocol", value = "grpc")
476+
@ClearSystemProperty(key = "otel.exporter.otlp.metrics.endpoint")
477+
void verifyMetricsEndpointIsProperlyConfiguredForGRPC() throws InvalidConfigException {
478+
ConfigContainer configContainer = new ConfigContainer();
479+
configContainer.putByStringValue(ConfigProperty.AGENT_SERVICE_KEY, "token:service");
480+
481+
configContainer.putByStringValue(ConfigProperty.AGENT_EXPORT_METRICS_ENABLED, "true");
482+
ConfigurationLoader.configureOtelMetricExport(configContainer);
483+
assertEquals(
484+
"https://otel.collector.na-01.cloud.solarwinds.com",
485+
System.getProperty("otel.exporter.otlp.metrics.endpoint"));
486+
}
487+
488+
@Test
489+
@SetSystemProperty(
490+
key = "otel.exporter.otlp.endpoint",
491+
value = "https://otel.collector.na-01.cloud.solarwinds.com")
492+
@SetSystemProperty(key = "otel.exporter.otlp.traces.protocol", value = "grpc")
493+
@ClearSystemProperty(key = "otel.exporter.otlp.traces.endpoint")
494+
void verifyTracesEndpointIsProperlyConfiguredForGRPC() throws InvalidConfigException {
495+
ConfigContainer configContainer = new ConfigContainer();
496+
configContainer.putByStringValue(ConfigProperty.AGENT_SERVICE_KEY, "token:service");
497+
498+
ConfigurationLoader.configureOtelTraceExport(configContainer);
499+
assertEquals(
500+
"https://otel.collector.na-01.cloud.solarwinds.com",
501+
System.getProperty("otel.exporter.otlp.traces.endpoint"));
502+
}
503+
504+
@Test
505+
@SetSystemProperty(
506+
key = "otel.exporter.otlp.endpoint",
507+
value = "https://otel.collector.na-01.cloud.solarwinds.com")
508+
@SetSystemProperty(key = "otel.exporter.otlp.logs.protocol", value = "grpc")
509+
@ClearSystemProperty(key = "otel.exporter.otlp.logs.endpoint")
510+
void verifyLogsEndpointIsProperlyConfiguredForGRPC() throws InvalidConfigException {
511+
ConfigContainer configContainer = new ConfigContainer();
512+
configContainer.putByStringValue(ConfigProperty.AGENT_SERVICE_KEY, "token:service");
513+
514+
configContainer.putByStringValue(ConfigProperty.AGENT_EXPORT_LOGS_ENABLED, "true");
515+
ConfigurationLoader.configureOtelLogExport(configContainer);
516+
assertEquals(
517+
"https://otel.collector.na-01.cloud.solarwinds.com",
518+
System.getProperty("otel.exporter.otlp.logs.endpoint"));
519+
}
520+
471521
@Test
472522
void returnEnvironmentVariableEquivalent() {
473523
assertEquals(

0 commit comments

Comments
 (0)