Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,20 @@ class EmbeddedConfigFile {
private EmbeddedConfigFile() {}

static OpenTelemetryConfigurationModel extractModel(ConfigurableEnvironment environment) {
Map<String, Object> props = extractSpringProperties(environment);
Map<String, String> props = extractSpringProperties(environment);
return convertToOpenTelemetryConfigurationModel(props);
}

private static Map<String, Object> extractSpringProperties(ConfigurableEnvironment environment) {
private static Map<String, String> extractSpringProperties(ConfigurableEnvironment environment) {
MutablePropertySources propertySources = environment.getPropertySources();

Map<String, Object> props = new HashMap<>();
Map<String, String> props = new HashMap<>();
for (PropertySource<?> propertySource : propertySources) {
if (propertySource instanceof EnumerablePropertySource<?>) {
for (String propertyName :
((EnumerablePropertySource<?>) propertySource).getPropertyNames()) {
if (propertyName.startsWith("otel.")) {
Object property = propertySource.getProperty(propertyName);
// Resolve ${} placeholders in String values while preserving types for others
if (property instanceof String) {
property = environment.resolvePlaceholders((String) property);
}
String property = environment.getProperty(propertyName);
if (Objects.equals(property, "")) {
property = null; // spring returns empty string for yaml null
}
Expand Down Expand Up @@ -100,7 +96,7 @@ private static Map<String, Object> extractSpringProperties(ConfigurableEnvironme
}

static OpenTelemetryConfigurationModel convertToOpenTelemetryConfigurationModel(
Map<String, Object> flatProps) {
Map<String, String> flatProps) {
Map<String, Object> nested = convertFlatPropsToNested(flatProps);

return getObjectMapper().convertValue(nested, OpenTelemetryConfigurationModel.class);
Expand All @@ -116,12 +112,12 @@ static ObjectMapper getObjectMapper() {
* ["one", "two"]}}}}
*/
@SuppressWarnings("unchecked")
static Map<String, Object> convertFlatPropsToNested(Map<String, Object> flatProps) {
static Map<String, Object> convertFlatPropsToNested(Map<String, String> flatProps) {
Map<String, Object> result = new HashMap<>();

for (Map.Entry<String, Object> entry : flatProps.entrySet()) {
for (Map.Entry<String, String> entry : flatProps.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
String value = entry.getValue();

// Split the key by dots
String[] parts = key.split("\\.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static java.util.Objects.requireNonNull;

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.incubator.ExtendedOpenTelemetry;
import io.opentelemetry.api.incubator.config.ConfigProvider;
import io.opentelemetry.api.trace.TracerProvider;
import io.opentelemetry.common.ComponentLoader;
Expand Down Expand Up @@ -61,7 +62,7 @@
import org.springframework.core.env.Environment;

/**
* Create {@link io.opentelemetry.api.OpenTelemetry} bean if bean is missing.
* Create {@link OpenTelemetry} bean if bean is missing.
*
* <p>Adds span exporter beans to the active tracer provider.
*
Expand Down Expand Up @@ -172,12 +173,12 @@ public OpenTelemetry openTelemetry(
model, new OpenTelemetrySdkComponentLoader(applicationContext));
Runtime.getRuntime().addShutdownHook(new Thread(sdk::close));
logStart();
return sdk;
return new SpringOpenTelemetrySdk(sdk, SpringConfigProvider.create(model));
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since we're already converting the yaml into declarative config model above (in order to support DeclarativeConfigurationCustomizers), why do it again in SpringConfigProvider?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the type coersion was needed.
I may have been wrong - but then we should remove spring config provider altogether.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, I've removed the spring config provider

}

@Bean
public ConfigProvider configProvider(OpenTelemetryConfigurationModel model) {
return SpringConfigProvider.create(model);
public ConfigProvider configProvider(OpenTelemetry openTelemetry) {
return ((ExtendedOpenTelemetry) openTelemetry).getConfigProvider();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class EmbeddedConfigFileTest {

@Test
void convertFlatPropsToNested_simpleProperties() {
Map<String, Object> flatProps = new HashMap<>();
Map<String, String> flatProps = new HashMap<>();
flatProps.put("resource.service.name", "my-service");
flatProps.put("traces.exporter", "otlp");

Expand Down Expand Up @@ -55,7 +55,7 @@ void convertFlatPropsToNested_simpleProperties() {

@Test
void convertFlatPropsToNested_arrayProperties() {
Map<String, Object> flatProps = new HashMap<>();
Map<String, String> flatProps = new HashMap<>();
flatProps.put("instrumentation.java.list[0]", "one");
flatProps.put("instrumentation.java.list[1]", "two");
flatProps.put("instrumentation.java.list[2]", "three");
Expand Down Expand Up @@ -89,7 +89,7 @@ void convertFlatPropsToNested_arrayProperties() {

@Test
void convertFlatPropsToNested_mixedPropertiesAndArrays() {
Map<String, Object> flatProps = new HashMap<>();
Map<String, String> flatProps = new HashMap<>();
flatProps.put("resource.service.name", "test-service");
flatProps.put("resource.attributes[0]", "key1=value1");
flatProps.put("resource.attributes[1]", "key2=value2");
Expand Down Expand Up @@ -124,7 +124,7 @@ void convertFlatPropsToNested_mixedPropertiesAndArrays() {

@Test
void convertFlatPropsToNested_emptyMap() {
Map<String, Object> flatProps = new HashMap<>();
Map<String, String> flatProps = new HashMap<>();

Map<String, Object> result = EmbeddedConfigFile.convertFlatPropsToNested(flatProps);

Expand All @@ -133,7 +133,7 @@ void convertFlatPropsToNested_emptyMap() {

@Test
void convertFlatPropsToNested_singleLevelProperty() {
Map<String, Object> flatProps = new HashMap<>();
Map<String, String> flatProps = new HashMap<>();
flatProps.put("enabled", "true");

Map<String, Object> result = EmbeddedConfigFile.convertFlatPropsToNested(flatProps);
Expand All @@ -143,7 +143,7 @@ void convertFlatPropsToNested_singleLevelProperty() {

@Test
void convertFlatPropsToNested_arrayWithGaps() {
Map<String, Object> flatProps = new HashMap<>();
Map<String, String> flatProps = new HashMap<>();
flatProps.put("list[0]", "first");
flatProps.put("list[2]", "third");

Expand All @@ -161,7 +161,7 @@ void convertFlatPropsToNested_arrayWithGaps() {

@Test
void convertFlatPropsToNested_deeplyNestedProperties() {
Map<String, Object> flatProps = new HashMap<>();
Map<String, String> flatProps = new HashMap<>();
flatProps.put("a.b.c.d.e", "deep-value");

Map<String, Object> result = EmbeddedConfigFile.convertFlatPropsToNested(flatProps);
Expand All @@ -183,7 +183,7 @@ void convertFlatPropsToNested_deeplyNestedProperties() {

@Test
void convertFlatPropsToNested_nestedArrays() {
Map<String, Object> flatProps = new HashMap<>();
Map<String, String> flatProps = new HashMap<>();
flatProps.put("outer[0].inner[0]", "value1");
flatProps.put("outer[0].inner[1]", "value2");
flatProps.put("outer[1].inner[0]", "value3");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,13 @@ void initializeProvidersAndOpenTelemetry() {
assertThat(c.getDeclarativeConfig("foo"))
.isNotNull()
.satisfies(
instrumentationConfig ->
assertThat(instrumentationConfig.getString("bar"))
.isEqualTo("baz"))));
instrumentationConfig -> {
assertThat(instrumentationConfig.getString("string_key"))
.isEqualTo("string_value");
assertThat(instrumentationConfig.getBoolean("bool_key")).isTrue();
assertThat(instrumentationConfig.getLong("int_key"))
.isEqualTo(42);
})));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ otel:
instrumentation/development:
java:
foo:
bar: baz
string_key: string_value
int_key: 42
bool_key: true