Skip to content

Commit

Permalink
URL decode headers from environment variables in OtlpConfig (#4595)
Browse files Browse the repository at this point in the history
The headers from environment variables are expected to be URL encoded according to the spec. This change tries to URL decode them.

See open-telemetry/opentelemetry-specification#3832
See https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/exporter.md#specifying-headers-via-environment-variables
  • Loading branch information
zeitlinger authored Jan 31, 2024
1 parent 58aa718 commit 33bb267
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.micrometer.core.instrument.config.validate.Validated;
import io.micrometer.core.instrument.push.PushRegistryConfig;

import java.net.URLDecoder;
import java.util.Arrays;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -131,6 +132,14 @@ 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 (Exception 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 @@ -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;

Expand Down Expand Up @@ -62,8 +63,19 @@ 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,header3=f oo")
.execute(() -> assertThat(config.headers()).containsEntry("header2", "va lue")
.containsEntry("header3", "f oo")
.hasSize(2));
}

@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
Expand Down

0 comments on commit 33bb267

Please sign in to comment.