From 3328b40c0ffe33b2c23e15398e7564b48dfb401c Mon Sep 17 00:00:00 2001 From: Gregor Zeitlinger Date: Fri, 19 Jan 2024 12:09:17 +0100 Subject: [PATCH] header values are be expected to be W3C baggage encoded --- .../java/io/micrometer/registry/otlp/OtlpConfig.java | 6 +++--- .../io/micrometer/registry/otlp/OtlpConfigTest.java | 10 ++++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/implementations/micrometer-registry-otlp/src/main/java/io/micrometer/registry/otlp/OtlpConfig.java b/implementations/micrometer-registry-otlp/src/main/java/io/micrometer/registry/otlp/OtlpConfig.java index ec20dbadbd..d6910e5a53 100644 --- a/implementations/micrometer-registry-otlp/src/main/java/io/micrometer/registry/otlp/OtlpConfig.java +++ b/implementations/micrometer-registry-otlp/src/main/java/io/micrometer/registry/otlp/OtlpConfig.java @@ -18,7 +18,6 @@ import io.micrometer.core.instrument.config.validate.Validated; import io.micrometer.core.instrument.push.PushRegistryConfig; -import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.util.Arrays; import java.util.Map; @@ -137,8 +136,9 @@ default Map headers() { // headers are encoded as URL - see // https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/exporter.md#specifying-headers-via-environment-variables headersString = URLDecoder.decode(headersString, "UTF-8"); - } catch (UnsupportedEncodingException e) { - throw new IllegalArgumentException("cannot decode header value: " + headersString, e); + } + catch (Exception e) { + throw new IllegalArgumentException("Cannot decode header value: " + headersString, e); } } diff --git a/implementations/micrometer-registry-otlp/src/test/java/io/micrometer/registry/otlp/OtlpConfigTest.java b/implementations/micrometer-registry-otlp/src/test/java/io/micrometer/registry/otlp/OtlpConfigTest.java index 77c4928740..a3c52f2fe0 100644 --- a/implementations/micrometer-registry-otlp/src/test/java/io/micrometer/registry/otlp/OtlpConfigTest.java +++ b/implementations/micrometer-registry-otlp/src/test/java/io/micrometer/registry/otlp/OtlpConfigTest.java @@ -23,6 +23,7 @@ import java.util.stream.Stream; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static uk.org.webcompere.systemstubs.SystemStubs.withEnvironmentVariable; import static uk.org.webcompere.systemstubs.SystemStubs.withEnvironmentVariables; @@ -66,6 +67,15 @@ void headersUseEnvVarWhenConfigNotSet() throws Exception { .execute(() -> assertThat(config.headers()).containsEntry("header2", "va lue").hasSize(1)); } + @Test + void headersDecodingError() throws Exception { + OtlpConfig config = k -> null; + withEnvironmentVariable("OTEL_EXPORTER_OTLP_HEADERS", "header2=%-1").execute(() -> { + assertThatThrownBy(config::headers).isInstanceOf(IllegalArgumentException.class) + .hasMessage("Cannot decode header value: header2=%-1,"); + }); + } + @Test void combineHeadersFromEnvVars() throws Exception { OtlpConfig config = k -> null;