From d1ba858e06009211a10e4e7d93d4d9608be3875f Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Mon, 7 Oct 2024 17:46:24 +0200 Subject: [PATCH] Align SpringConfigProperties with DefaultConfigProperties (#12398) --- .../properties/SpringConfigProperties.java | 35 +++++++++++++------ .../AbstractOtelSpringStarterSmokeTest.java | 15 ++++++++ .../src/main/resources/application.yaml | 4 +++ 3 files changed, 44 insertions(+), 10 deletions(-) diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/properties/SpringConfigProperties.java b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/properties/SpringConfigProperties.java index c2a4f10aca1f..58ac3c0e54b1 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/properties/SpringConfigProperties.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/properties/SpringConfigProperties.java @@ -5,6 +5,7 @@ package io.opentelemetry.instrumentation.spring.autoconfigure.internal.properties; +import io.opentelemetry.api.internal.ConfigUtil; import io.opentelemetry.exporter.otlp.internal.OtlpConfigUtil; import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; import io.opentelemetry.sdk.autoconfigure.spi.internal.DefaultConfigProperties; @@ -65,8 +66,9 @@ public static ConfigProperties create( @Nullable @Override public String getString(String name) { - String value = environment.getProperty(name, String.class); - if (value == null && name.equals("otel.exporter.otlp.protocol")) { + String normalizedName = ConfigUtil.normalizeEnvironmentVariableKey(name); + String value = environment.getProperty(normalizedName, String.class); + if (value == null && normalizedName.equals("otel.exporter.otlp.protocol")) { // SDK autoconfigure module defaults to `grpc`, but this module aligns with recommendation // in specification to default to `http/protobuf` return OtlpConfigUtil.PROTOCOL_HTTP_PROTOBUF; @@ -77,35 +79,46 @@ public String getString(String name) { @Nullable @Override public Boolean getBoolean(String name) { - return or(environment.getProperty(name, Boolean.class), otelSdkProperties.getBoolean(name)); + return or( + environment.getProperty(ConfigUtil.normalizeEnvironmentVariableKey(name), Boolean.class), + otelSdkProperties.getBoolean(name)); } @Nullable @Override public Integer getInt(String name) { - return or(environment.getProperty(name, Integer.class), otelSdkProperties.getInt(name)); + return or( + environment.getProperty(ConfigUtil.normalizeEnvironmentVariableKey(name), Integer.class), + otelSdkProperties.getInt(name)); } @Nullable @Override public Long getLong(String name) { - return or(environment.getProperty(name, Long.class), otelSdkProperties.getLong(name)); + return or( + environment.getProperty(ConfigUtil.normalizeEnvironmentVariableKey(name), Long.class), + otelSdkProperties.getLong(name)); } @Nullable @Override public Double getDouble(String name) { - return or(environment.getProperty(name, Double.class), otelSdkProperties.getDouble(name)); + return or( + environment.getProperty(ConfigUtil.normalizeEnvironmentVariableKey(name), Double.class), + otelSdkProperties.getDouble(name)); } @SuppressWarnings("unchecked") @Override public List getList(String name) { - if (name.equals("otel.propagators")) { + + String normalizedName = ConfigUtil.normalizeEnvironmentVariableKey(name); + + if (normalizedName.equals("otel.propagators")) { return propagationProperties.getPropagators(); } - return or(environment.getProperty(name, List.class), otelSdkProperties.getList(name)); + return or(environment.getProperty(normalizedName, List.class), otelSdkProperties.getList(name)); } @Nullable @@ -123,8 +136,10 @@ public Duration getDuration(String name) { @Override public Map getMap(String name) { Map otelSdkMap = otelSdkProperties.getMap(name); + + String normalizedName = ConfigUtil.normalizeEnvironmentVariableKey(name); // maps from config properties are not supported by Environment, so we have to fake it - switch (name) { + switch (normalizedName) { case "otel.resource.attributes": return mergeWithOtel(resourceProperties.getAttributes(), otelSdkMap); case "otel.exporter.otlp.headers": @@ -139,7 +154,7 @@ public Map getMap(String name) { break; } - String value = environment.getProperty(name); + String value = environment.getProperty(normalizedName); if (value == null) { return otelSdkMap; } diff --git a/smoke-tests-otel-starter/spring-boot-common/src/main/java/io/opentelemetry/spring/smoketest/AbstractOtelSpringStarterSmokeTest.java b/smoke-tests-otel-starter/spring-boot-common/src/main/java/io/opentelemetry/spring/smoketest/AbstractOtelSpringStarterSmokeTest.java index 60a9392293d7..f5ea3eace113 100644 --- a/smoke-tests-otel-starter/spring-boot-common/src/main/java/io/opentelemetry/spring/smoketest/AbstractOtelSpringStarterSmokeTest.java +++ b/smoke-tests-otel-starter/spring-boot-common/src/main/java/io/opentelemetry/spring/smoketest/AbstractOtelSpringStarterSmokeTest.java @@ -104,6 +104,21 @@ AutoConfigurationCustomizerProvider propagatorCustomizer() { Attributes.of( AttributeKey.booleanKey("keyFromResourceCustomizer"), true)))); } + + @Bean + AutoConfigurationCustomizerProvider customizerUsingPropertyDefinedInaSpringFile() { + return customizer -> + customizer.addResourceCustomizer( + (resource, config) -> { + String valueForKeyDeclaredZsEnvVariable = config.getString("APPLICATION_PROP"); + assertThat(valueForKeyDeclaredZsEnvVariable).isNotEmpty(); + + String valueForKeyWithDash = config.getString("application.prop-with-dash"); + assertThat(valueForKeyWithDash).isNotEmpty(); + + return resource; + }); + } } @Test diff --git a/smoke-tests-otel-starter/spring-boot-common/src/main/resources/application.yaml b/smoke-tests-otel-starter/spring-boot-common/src/main/resources/application.yaml index 1995223b3841..b8aec4b09f6d 100644 --- a/smoke-tests-otel-starter/spring-boot-common/src/main/resources/application.yaml +++ b/smoke-tests-otel-starter/spring-boot-common/src/main/resources/application.yaml @@ -19,6 +19,10 @@ otel: attributes: attributeFromYaml: true # boolean will be automatically converted to string by spring +application: + prop: propValue + prop-with-dash: provWithDashValue + spring: kafka: bootstrap-servers: localhost:9094