From 052fdc1fc9063b2084d29c87d4675139c7bf5322 Mon Sep 17 00:00:00 2001 From: Lauri Tulmin Date: Wed, 16 Oct 2024 13:12:57 +0300 Subject: [PATCH 1/2] Allow setting otlp protocol for profiler with otel.exporter.otlp.protocol --- .../opentelemetry/profiler/Configuration.java | 8 +++-- .../profiler/ConfigurationTest.java | 29 +++++++++++++++++++ .../profiler/LogExporterBuilderTest.java | 6 ++-- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/profiler/src/main/java/com/splunk/opentelemetry/profiler/Configuration.java b/profiler/src/main/java/com/splunk/opentelemetry/profiler/Configuration.java index 029e9485f..fa4cbeca1 100644 --- a/profiler/src/main/java/com/splunk/opentelemetry/profiler/Configuration.java +++ b/profiler/src/main/java/com/splunk/opentelemetry/profiler/Configuration.java @@ -47,7 +47,8 @@ public class Configuration implements AutoConfigurationCustomizerProvider { public static final String CONFIG_KEY_RECORDING_DURATION = "splunk.profiler.recording.duration"; public static final String CONFIG_KEY_KEEP_FILES = "splunk.profiler.keep-files"; public static final String CONFIG_KEY_INGEST_URL = "splunk.profiler.logs-endpoint"; - public static final String CONFIG_KEY_OTLP_PROTOCOL = "splunk.profiler.otlp.protocol"; + public static final String CONFIG_KEY_PROFILER_OTLP_PROTOCOL = "splunk.profiler.otlp.protocol"; + public static final String CONFIG_KEY_OTLP_PROTOCOL = "otel.exporter.otlp.protocol"; public static final String CONFIG_KEY_OTEL_OTLP_URL = "otel.exporter.otlp.endpoint"; public static final String CONFIG_KEY_MEMORY_ENABLED = PROFILER_MEMORY_ENABLED_PROPERTY; public static final String CONFIG_KEY_MEMORY_EVENT_RATE_LIMIT_ENABLED = @@ -82,7 +83,6 @@ Map defaultProperties() { config.put(CONFIG_KEY_MEMORY_ENABLED, String.valueOf(DEFAULT_MEMORY_ENABLED)); config.put(CONFIG_KEY_MEMORY_EVENT_RATE, DEFAULT_MEMORY_EVENT_RATE); config.put(CONFIG_KEY_CALL_STACK_INTERVAL, DEFAULT_CALL_STACK_INTERVAL.toMillis() + "ms"); - config.put(CONFIG_KEY_OTLP_PROTOCOL, "http/protobuf"); return config; } @@ -116,7 +116,9 @@ private static String getDefaultLogsEndpoint(ConfigProperties config) { } public static String getOtlpProtocol(ConfigProperties config) { - return config.getString(CONFIG_KEY_OTLP_PROTOCOL); + return config.getString( + CONFIG_KEY_PROFILER_OTLP_PROTOCOL, + config.getString(CONFIG_KEY_OTLP_PROTOCOL, "http/protobuf")); } public static boolean getMemoryEnabled(ConfigProperties config) { diff --git a/profiler/src/test/java/com/splunk/opentelemetry/profiler/ConfigurationTest.java b/profiler/src/test/java/com/splunk/opentelemetry/profiler/ConfigurationTest.java index b23e129da..8b89e0827 100644 --- a/profiler/src/test/java/com/splunk/opentelemetry/profiler/ConfigurationTest.java +++ b/profiler/src/test/java/com/splunk/opentelemetry/profiler/ConfigurationTest.java @@ -22,6 +22,10 @@ import static org.mockito.Mockito.when; import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; +import io.opentelemetry.sdk.autoconfigure.spi.internal.DefaultConfigProperties; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; import org.junit.jupiter.api.Test; class ConfigurationTest { @@ -67,4 +71,29 @@ void getConfigUrlSplunkRealm() { String result = Configuration.getConfigUrl(config); assertNull(result); } + + @Test + void getOtlpProtocolDefault() { + String result = + Configuration.getOtlpProtocol(DefaultConfigProperties.create(Collections.emptyMap())); + assertEquals(result, "http/protobuf"); + } + + @Test + void getOtlpProtocolOtelPropertySet() { + String result = + Configuration.getOtlpProtocol( + DefaultConfigProperties.create( + Collections.singletonMap("otel.exporter.otlp.protocol", "test"))); + assertEquals(result, "test"); + } + + @Test + void getOtlpProtocol() { + Map map = new HashMap<>(); + map.put("otel.exporter.otlp.protocol", "test1"); + map.put("splunk.profiler.otlp.protocol", "test2"); + String result = Configuration.getOtlpProtocol(DefaultConfigProperties.create(map)); + assertEquals(result, "test2"); + } } diff --git a/profiler/src/test/java/com/splunk/opentelemetry/profiler/LogExporterBuilderTest.java b/profiler/src/test/java/com/splunk/opentelemetry/profiler/LogExporterBuilderTest.java index 5d434f981..02602d64b 100644 --- a/profiler/src/test/java/com/splunk/opentelemetry/profiler/LogExporterBuilderTest.java +++ b/profiler/src/test/java/com/splunk/opentelemetry/profiler/LogExporterBuilderTest.java @@ -75,7 +75,8 @@ void testCustomEndpointGrpc() { when(builder.addHeader(EXTRA_CONTENT_TYPE, STACKTRACES_HEADER_VALUE)).thenReturn(builder); when(builder.build()).thenReturn(expected); - when(config.getString(Configuration.CONFIG_KEY_OTLP_PROTOCOL)).thenReturn("grpc"); + when(config.getString(Configuration.CONFIG_KEY_PROFILER_OTLP_PROTOCOL, null)) + .thenReturn("grpc"); when(config.getString(Configuration.CONFIG_KEY_OTEL_OTLP_URL, null)) .thenReturn("http://shadowed.example.com:9122/"); when(config.getString(Configuration.CONFIG_KEY_INGEST_URL, "http://shadowed.example.com:9122/")) @@ -97,7 +98,8 @@ void testCustomEndpointHttp() { when(builder.addHeader(EXTRA_CONTENT_TYPE, STACKTRACES_HEADER_VALUE)).thenReturn(builder); when(builder.build()).thenReturn(expected); - when(config.getString(Configuration.CONFIG_KEY_OTLP_PROTOCOL)).thenReturn("http/protobuf"); + when(config.getString(Configuration.CONFIG_KEY_PROFILER_OTLP_PROTOCOL, null)) + .thenReturn("http/protobuf"); when(config.getString(Configuration.CONFIG_KEY_OTEL_OTLP_URL, null)) .thenReturn("http://shadowed.example.com:9122/"); when(config.getString( From 8eb03622b4cc390c15fb5e1bb6dee5d2eb5ce358 Mon Sep 17 00:00:00 2001 From: Lauri Tulmin Date: Thu, 17 Oct 2024 16:29:09 +0300 Subject: [PATCH 2/2] currect argument order for assertEquals --- .../opentelemetry/profiler/ConfigurationTest.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/profiler/src/test/java/com/splunk/opentelemetry/profiler/ConfigurationTest.java b/profiler/src/test/java/com/splunk/opentelemetry/profiler/ConfigurationTest.java index 8b89e0827..a9381dea9 100644 --- a/profiler/src/test/java/com/splunk/opentelemetry/profiler/ConfigurationTest.java +++ b/profiler/src/test/java/com/splunk/opentelemetry/profiler/ConfigurationTest.java @@ -40,7 +40,7 @@ void getConfigUrl_endpointDefined() { when(config.getString(Configuration.CONFIG_KEY_INGEST_URL, otelEndpoint)) .thenReturn(logsEndpoint); String result = Configuration.getConfigUrl(config); - assertEquals(result, logsEndpoint); + assertEquals(logsEndpoint, result); } @Test @@ -50,7 +50,7 @@ void getConfigUrl_endpointNotDefined() { when(config.getString(Configuration.CONFIG_KEY_INGEST_URL, otelEndpoint)) .thenReturn(otelEndpoint); String result = Configuration.getConfigUrl(config); - assertEquals(result, otelEndpoint); + assertEquals(otelEndpoint, result); } @Test @@ -76,7 +76,7 @@ void getConfigUrlSplunkRealm() { void getOtlpProtocolDefault() { String result = Configuration.getOtlpProtocol(DefaultConfigProperties.create(Collections.emptyMap())); - assertEquals(result, "http/protobuf"); + assertEquals("http/protobuf", result); } @Test @@ -85,7 +85,7 @@ void getOtlpProtocolOtelPropertySet() { Configuration.getOtlpProtocol( DefaultConfigProperties.create( Collections.singletonMap("otel.exporter.otlp.protocol", "test"))); - assertEquals(result, "test"); + assertEquals("test", result); } @Test @@ -94,6 +94,6 @@ void getOtlpProtocol() { map.put("otel.exporter.otlp.protocol", "test1"); map.put("splunk.profiler.otlp.protocol", "test2"); String result = Configuration.getOtlpProtocol(DefaultConfigProperties.create(map)); - assertEquals(result, "test2"); + assertEquals("test2", result); } }