From 569a2d388aa2534eee457da25aca2f636ec65dd2 Mon Sep 17 00:00:00 2001 From: cleverchuk Date: Fri, 6 Dec 2024 15:04:03 -0500 Subject: [PATCH] NH-93486: honor user config --- .github/workflows/push.yml | 2 +- .../initialize/ConfigurationLoader.java | 56 ++++++++++++++----- .../initialize/ConfigurationLoaderTest.java | 7 +++ 3 files changed, 51 insertions(+), 14 deletions(-) diff --git a/.github/workflows/push.yml b/.github/workflows/push.yml index a8ee42fa..a17287b3 100644 --- a/.github/workflows/push.yml +++ b/.github/workflows/push.yml @@ -13,7 +13,6 @@ env: SW_APM_DEBUG_LEVEL: trace AGENT_DOWNLOAD_URL: https://agent-binaries.global.st-ssp.solarwinds.com/apm/java/latest/solarwinds-apm-agent.jar SW_APM_COLLECTOR: ${{ secrets.SW_APM_COLLECTOR }} - OTEL_EXPORTER_OTLP_ENDPOINT: ${{ secrets.OTEL_EXPORTER_OTLP_ENDPOINT }} SW_APM_SERVICE_KEY_AO: ${{ secrets.SW_APM_SERVICE_KEY_AO }} SW_APM_SERVICE_KEY: ${{ secrets.SW_APM_SERVICE_KEY }} GITHUB_USERNAME: ${{ github.actor }} @@ -171,6 +170,7 @@ jobs: - s3-stage-upload env: LAMBDA: "true" + OTEL_EXPORTER_OTLP_ENDPOINT: ${{ secrets.OTEL_EXPORTER_OTLP_ENDPOINT }} steps: - uses: actions/checkout@v4 diff --git a/custom/src/main/java/com/solarwinds/opentelemetry/extensions/initialize/ConfigurationLoader.java b/custom/src/main/java/com/solarwinds/opentelemetry/extensions/initialize/ConfigurationLoader.java index e95c9169..6ad47ad1 100644 --- a/custom/src/main/java/com/solarwinds/opentelemetry/extensions/initialize/ConfigurationLoader.java +++ b/custom/src/main/java/com/solarwinds/opentelemetry/extensions/initialize/ConfigurationLoader.java @@ -179,7 +179,7 @@ private static void maybeFollowOtelConfigProperties(ConfigContainer configs) { if (serviceKey != null) { String name = ServiceKeyUtils.getServiceName(serviceKey); if (name != null) { - System.setProperty("otel.service.name", name); + setSystemProperty("otel.service.name", name); } } @@ -223,12 +223,12 @@ static void configureOtelLogExport(ConfigContainer container) { } } - System.setProperty("otel.exporter.otlp.logs.protocol", "grpc"); - System.setProperty("otel.logs.exporter", "otlp"); - System.setProperty( + setSystemProperty("otel.exporter.otlp.logs.protocol", "grpc"); + setSystemProperty("otel.logs.exporter", "otlp"); + setSystemProperty( "otel.exporter.otlp.logs.headers", String.format("authorization=Bearer %s", apiKey)); - System.setProperty( + setEndpoint( "otel.exporter.otlp.logs.endpoint", String.format("https://otel.collector.%s.%s.solarwinds.com", dataCell, env)); } @@ -261,12 +261,12 @@ static void configureOtelMetricExport(ConfigContainer container) { } } - System.setProperty("otel.exporter.otlp.metrics.protocol", "grpc"); - System.setProperty("otel.metrics.exporter", "otlp"); - System.setProperty( + setSystemProperty("otel.exporter.otlp.metrics.protocol", "grpc"); + setSystemProperty("otel.metrics.exporter", "otlp"); + setSystemProperty( "otel.exporter.otlp.metrics.headers", String.format("authorization=Bearer %s", apiKey)); - System.setProperty( + setEndpoint( "otel.exporter.otlp.metrics.endpoint", String.format("https://otel.collector.%s.%s.solarwinds.com", dataCell, env)); } @@ -282,7 +282,7 @@ static void configureOtelTraceExport(ConfigContainer container) { if (collectorEndpoint != null) { if (collectorEndpoint.contains("appoptics.com")) { - System.setProperty("otel.traces.exporter", COMPONENT_NAME); + setSystemProperty("otel.traces.exporter", COMPONENT_NAME); return; } @@ -299,11 +299,11 @@ static void configureOtelTraceExport(ConfigContainer container) { } } - System.setProperty("otel.exporter.otlp.traces.protocol", "grpc"); - System.setProperty( + setSystemProperty("otel.exporter.otlp.traces.protocol", "grpc"); + setSystemProperty( "otel.exporter.otlp.traces.headers", String.format("authorization=Bearer %s", apiKey)); - System.setProperty( + setEndpoint( "otel.exporter.otlp.traces.endpoint", String.format("https://otel.collector.%s.%s.solarwinds.com", dataCell, env)); } @@ -637,4 +637,34 @@ public static boolean shouldUseOtlpForMetrics() { return (enabled == null || enabled) && (collectorEndpoint == null || !collectorEndpoint.contains("appoptics.com")); } + + private static void setSystemProperty(String key, String value) { + String propertyValue = getConfigValue(key); + if (propertyValue == null) { + propertyValue = value; + System.setProperty(key, value); + } + + logger.debug( + String.format("System configuration set with key-value -> %s = %s", key, propertyValue)); + } + + private static void setEndpoint(String key, String value) { + if (getConfigValue("otel.exporter.otlp.endpoint") == null) { + setSystemProperty(key, value); + } + } + + static String normalize(String key) { + return key.toUpperCase().replace(".", "_"); + } + + private static String getConfigValue(String key) { + String propertyValue = System.getProperty(key); + if (propertyValue == null) { + propertyValue = System.getenv(normalize(key)); + } + + return propertyValue; + } } diff --git a/custom/src/test/java/com/solarwinds/opentelemetry/extensions/initialize/ConfigurationLoaderTest.java b/custom/src/test/java/com/solarwinds/opentelemetry/extensions/initialize/ConfigurationLoaderTest.java index 68bdd3f4..27c4b95b 100644 --- a/custom/src/test/java/com/solarwinds/opentelemetry/extensions/initialize/ConfigurationLoaderTest.java +++ b/custom/src/test/java/com/solarwinds/opentelemetry/extensions/initialize/ConfigurationLoaderTest.java @@ -402,4 +402,11 @@ void returnFalseWhenDisabledForMetric() throws InvalidConfigException { assertFalse(ConfigurationLoader.shouldUseOtlpForMetrics()); ConfigManager.removeConfig(ConfigProperty.AGENT_EXPORT_METRICS_ENABLED); } + + @Test + void returnEnvironmentVariableEquivalent() { + assertEquals( + "OTEL_EXPORTER_OTLP_ENDPOINT", + ConfigurationLoader.normalize("otel.exporter.otlp.endpoint")); + } }