Skip to content

Commit

Permalink
Align SpringConfigProperties with DefaultConfigProperties
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanbisutti committed Oct 4, 2024
1 parent f235209 commit 5485755
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.normalizePropertyKey(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;
Expand All @@ -77,35 +79,45 @@ 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.normalizePropertyKey(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.normalizePropertyKey(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.normalizePropertyKey(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.normalizePropertyKey(name), Double.class),
otelSdkProperties.getDouble(name));
}

@SuppressWarnings("unchecked")
@Override
public List<String> getList(String name) {
if (name.equals("otel.propagators")) {
String normalizedName = ConfigUtil.normalizePropertyKey(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
Expand All @@ -123,8 +135,10 @@ public Duration getDuration(String name) {
@Override
public Map<String, String> getMap(String name) {
Map<String, String> otelSdkMap = otelSdkProperties.getMap(name);

String normalizedName = ConfigUtil.normalizePropertyKey(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":
Expand All @@ -139,7 +153,7 @@ public Map<String, String> getMap(String name) {
break;
}

String value = environment.getProperty(name);
String value = environment.getProperty(normalizedName);
if (value == null) {
return otelSdkMap;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,19 @@ AutoConfigurationCustomizerProvider propagatorCustomizer() {
Attributes.of(
AttributeKey.booleanKey("keyFromResourceCustomizer"), true))));
}

@Bean
AutoConfigurationCustomizerProvider customizerUsingPropertyDefinedInASpringFile() {
return customizer ->
customizer.addResourceCustomizer(
(resource, config) ->
{
String propValue = config.getString("APPLICATION-PROP");
assertThat(propValue).isNotEmpty();
return resource;
}
);
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ otel:
attributes:
attributeFromYaml: true # boolean will be automatically converted to string by spring

application:
prop: propValue

spring:
kafka:
bootstrap-servers: localhost:9094
Expand Down

0 comments on commit 5485755

Please sign in to comment.