Skip to content

Commit bd9b4e8

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

File tree

2 files changed

+100
-8
lines changed

2 files changed

+100
-8
lines changed

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

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
import com.solarwinds.opentelemetry.extensions.config.parser.json.TransactionNamingSchemesParser;
5050
import com.solarwinds.opentelemetry.extensions.config.parser.json.TransactionSettingsConfigParser;
5151
import com.solarwinds.opentelemetry.extensions.config.parser.json.UrlSampleRateConfigParser;
52+
import lombok.Getter;
53+
5254
import java.io.File;
5355
import java.io.FileInputStream;
5456
import java.io.FileNotFoundException;
@@ -70,7 +72,6 @@
7072
import java.util.concurrent.Executors;
7173
import java.util.concurrent.ScheduledExecutorService;
7274
import java.util.stream.Collectors;
73-
import lombok.Getter;
7475

7576
public class ConfigurationLoader {
7677
private static final Logger logger = LoggerFactory.getLogger();
@@ -219,15 +220,23 @@ static void configureOtelLogExport(ConfigContainer container) throws InvalidConf
219220
}
220221
}
221222

222-
setSystemProperty("otel.exporter.otlp.logs.protocol", PROTOCOL);
223+
String protocol = setProtocol("otel.exporter.otlp.logs.protocol");
223224
setSystemProperty("otel.logs.exporter", "otlp");
224225
setSystemProperty(
225226
"otel.exporter.otlp.logs.headers", String.format("authorization=Bearer %s", apiKey));
226227

228+
if (protocol.equalsIgnoreCase("grpc")) {
229+
setEndpoint(
230+
"otel.exporter.otlp.logs.endpoint",
231+
String.format("https://otel.collector.%s.%s.solarwinds.com", dataCell, env),
232+
"");
233+
return;
234+
}
235+
227236
setEndpoint(
228-
"otel.exporter.otlp.logs.endpoint",
229-
String.format("https://otel.collector.%s.%s.solarwinds.com/v1/logs", dataCell, env),
230-
"/v1/logs");
237+
"otel.exporter.otlp.logs.endpoint",
238+
String.format("https://otel.collector.%s.%s.solarwinds.com/v1/logs", dataCell, env),
239+
"/v1/logs");
231240
}
232241
}
233242

@@ -256,11 +265,19 @@ static void configureOtelMetricExport(ConfigContainer container) throws InvalidC
256265
}
257266
}
258267

259-
setSystemProperty("otel.exporter.otlp.metrics.protocol", PROTOCOL);
268+
String protocol = setProtocol("otel.exporter.otlp.metrics.protocol");
260269
setSystemProperty("otel.metrics.exporter", "otlp");
261270
setSystemProperty(
262271
"otel.exporter.otlp.metrics.headers", String.format("authorization=Bearer %s", apiKey));
263272

273+
if (protocol.equalsIgnoreCase("grpc")) {
274+
setEndpoint(
275+
"otel.exporter.otlp.metrics.endpoint",
276+
String.format("https://otel.collector.%s.%s.solarwinds.com", dataCell, env),
277+
"");
278+
return;
279+
}
280+
264281
setEndpoint(
265282
"otel.exporter.otlp.metrics.endpoint",
266283
String.format("https://otel.collector.%s.%s.solarwinds.com/v1/metrics", dataCell, env),
@@ -293,10 +310,18 @@ static void configureOtelTraceExport(ConfigContainer container) throws InvalidCo
293310
}
294311
}
295312

296-
setSystemProperty("otel.exporter.otlp.traces.protocol", PROTOCOL);
313+
String protocol = setProtocol("otel.exporter.otlp.traces.protocol");
297314
setSystemProperty(
298315
"otel.exporter.otlp.traces.headers", String.format("authorization=Bearer %s", apiKey));
299316

317+
if (protocol.equalsIgnoreCase("grpc")){
318+
setEndpoint(
319+
"otel.exporter.otlp.traces.endpoint",
320+
String.format("https://otel.collector.%s.%s.solarwinds.com", dataCell, env),
321+
"");
322+
return;
323+
}
324+
300325
setEndpoint(
301326
"otel.exporter.otlp.traces.endpoint",
302327
String.format("https://otel.collector.%s.%s.solarwinds.com/v1/traces", dataCell, env),
@@ -630,11 +655,14 @@ public static void processConfigs(ConfigContainer configs) throws InvalidConfigE
630655
ConfigManager.initialize(configs);
631656
}
632657

633-
private static void setSystemProperty(String key, String value) {
658+
private static String setSystemProperty(String key, String value) {
634659
String propertyValue = getConfigValue(key);
635660
if (propertyValue == null) {
636661
System.setProperty(key, value);
662+
return value;
637663
}
664+
665+
return propertyValue;
638666
}
639667

640668
private static void setEndpoint(String key, String value, String path) {
@@ -646,6 +674,14 @@ private static void setEndpoint(String key, String value, String path) {
646674
}
647675
}
648676

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

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,62 @@ 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(
476+
key = "otel.exporter.otlp.metrics.protocol",
477+
value = "grpc")
478+
@ClearSystemProperty(key = "otel.exporter.otlp.metrics.endpoint")
479+
void verifyMetricsEndpointIsProperlyConfiguredForGRPC() throws InvalidConfigException {
480+
ConfigContainer configContainer = new ConfigContainer();
481+
configContainer.putByStringValue(ConfigProperty.AGENT_SERVICE_KEY, "token:service");
482+
483+
configContainer.putByStringValue(ConfigProperty.AGENT_EXPORT_METRICS_ENABLED, "true");
484+
ConfigurationLoader.configureOtelMetricExport(configContainer);
485+
assertEquals(
486+
"https://otel.collector.na-01.cloud.solarwinds.com",
487+
System.getProperty("otel.exporter.otlp.metrics.endpoint"));
488+
}
489+
490+
@Test
491+
@SetSystemProperty(
492+
key = "otel.exporter.otlp.endpoint",
493+
value = "https://otel.collector.na-01.cloud.solarwinds.com")
494+
@SetSystemProperty(
495+
key = "otel.exporter.otlp.traces.protocol",
496+
value = "grpc")
497+
@ClearSystemProperty(key = "otel.exporter.otlp.traces.endpoint")
498+
void verifyTracesEndpointIsProperlyConfiguredForGRPC() throws InvalidConfigException {
499+
ConfigContainer configContainer = new ConfigContainer();
500+
configContainer.putByStringValue(ConfigProperty.AGENT_SERVICE_KEY, "token:service");
501+
502+
ConfigurationLoader.configureOtelTraceExport(configContainer);
503+
assertEquals(
504+
"https://otel.collector.na-01.cloud.solarwinds.com",
505+
System.getProperty("otel.exporter.otlp.traces.endpoint"));
506+
}
507+
508+
@Test
509+
@SetSystemProperty(
510+
key = "otel.exporter.otlp.endpoint",
511+
value = "https://otel.collector.na-01.cloud.solarwinds.com")
512+
@SetSystemProperty(
513+
key = "otel.exporter.otlp.logs.protocol",
514+
value = "grpc")
515+
@ClearSystemProperty(key = "otel.exporter.otlp.logs.endpoint")
516+
void verifyLogsEndpointIsProperlyConfiguredForGRPC() throws InvalidConfigException {
517+
ConfigContainer configContainer = new ConfigContainer();
518+
configContainer.putByStringValue(ConfigProperty.AGENT_SERVICE_KEY, "token:service");
519+
520+
configContainer.putByStringValue(ConfigProperty.AGENT_EXPORT_LOGS_ENABLED, "true");
521+
ConfigurationLoader.configureOtelLogExport(configContainer);
522+
assertEquals(
523+
"https://otel.collector.na-01.cloud.solarwinds.com",
524+
System.getProperty("otel.exporter.otlp.logs.endpoint"));
525+
}
526+
471527
@Test
472528
void returnEnvironmentVariableEquivalent() {
473529
assertEquals(

0 commit comments

Comments
 (0)