Skip to content

Commit

Permalink
header values are be expected to be W3C baggage encoded
Browse files Browse the repository at this point in the history
  • Loading branch information
zeitlinger committed Jan 18, 2024
1 parent be4dbdd commit 3e8fb92
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
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.time.Duration;
import java.util.Arrays;
import java.util.Map;
Expand Down Expand Up @@ -161,6 +163,13 @@ default Map<String, String> headers() {
headersString = env.getOrDefault("OTEL_EXPORTER_OTLP_HEADERS", "").trim();
String metricsHeaders = env.getOrDefault("OTEL_EXPORTER_OTLP_METRICS_HEADERS", "").trim();
headersString = Objects.equals(headersString, "") ? metricsHeaders : headersString + "," + metricsHeaders;
try {
// 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);
}
}

String[] keyValues = Objects.equals(headersString, "") ? new String[] {} : headersString.split(",");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ void headersConfigTakesPrecedenceOverEnvVars() throws Exception {
@Test
void headersUseEnvVarWhenConfigNotSet() throws Exception {
OtlpConfig config = k -> null;
withEnvironmentVariable("OTEL_EXPORTER_OTLP_HEADERS", "header2=value")
.execute(() -> assertThat(config.headers()).containsEntry("header2", "value").hasSize(1));
withEnvironmentVariable("OTEL_EXPORTER_OTLP_HEADERS", "header2=va%20lue")
.execute(() -> assertThat(config.headers()).containsEntry("header2", "va lue").hasSize(1));
}

@Test
Expand Down

0 comments on commit 3e8fb92

Please sign in to comment.