Skip to content

Commit 780435d

Browse files
committed
embedded config file
1 parent 8e9076b commit 780435d

File tree

3 files changed

+94
-3
lines changed

3 files changed

+94
-3
lines changed

instrumentation/spring/spring-boot-autoconfigure/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ dependencies {
3636
annotationProcessor("org.springframework.boot:spring-boot-autoconfigure-processor:$springBootVersion")
3737
annotationProcessor("org.springframework.boot:spring-boot-configuration-processor:$springBootVersion")
3838
implementation("javax.validation:validation-api")
39+
implementation("com.fasterxml.jackson.core:jackson-core")
3940

4041
implementation(project(":instrumentation-annotations-support"))
4142
implementation(project(":instrumentation:kafka:kafka-clients:kafka-clients-2.6:library"))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package io.opentelemetry.instrumentation.spring.autoconfigure;
2+
3+
import com.fasterxml.jackson.databind.JsonNode;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import io.opentelemetry.api.GlobalOpenTelemetry;
6+
import io.opentelemetry.api.incubator.config.GlobalConfigProvider;
7+
import io.opentelemetry.sdk.OpenTelemetrySdk;
8+
import io.opentelemetry.sdk.extension.incubator.fileconfig.DeclarativeConfiguration;
9+
import io.opentelemetry.sdk.extension.incubator.fileconfig.SdkConfigProvider;
10+
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.OpenTelemetryConfigurationModel;
11+
import org.springframework.boot.env.OriginTrackedMapPropertySource;
12+
import org.springframework.core.env.ConfigurableEnvironment;
13+
import org.springframework.core.env.PropertySource;
14+
import java.io.ByteArrayInputStream;
15+
import java.io.IOException;
16+
import java.io.InputStream;
17+
import java.nio.charset.StandardCharsets;
18+
import java.util.regex.Matcher;
19+
import java.util.regex.Pattern;
20+
21+
class EmbeddedConfigFile {
22+
23+
private static final Pattern PATTERN = Pattern.compile(
24+
"^Config resource 'class path resource \\[(.+)]' via location 'optional:classpath:/'$"
25+
);
26+
27+
static OpenTelemetryConfigurationModel extractModel(ConfigurableEnvironment environment)
28+
throws IOException {
29+
for (PropertySource<?> propertySource : environment.getPropertySources()) {
30+
if (propertySource instanceof OriginTrackedMapPropertySource) {
31+
OriginTrackedMapPropertySource source = (OriginTrackedMapPropertySource) propertySource;
32+
String name = source.getName();
33+
System.out.println("Property Source: " + name); // todo remove
34+
Matcher matcher = PATTERN.matcher(name);
35+
if (matcher.matches()) {
36+
String file = matcher.group(1);
37+
System.out.println("Found application.yaml: " + file);
38+
39+
try (InputStream resourceAsStream =
40+
environment.getClass().getClassLoader().getResourceAsStream(file)) {
41+
// Print the contents of the application.yaml file
42+
if (resourceAsStream != null) {
43+
String content = new String(resourceAsStream.readAllBytes());
44+
System.out.println("Contents of " + file + ":"); // todo remove
45+
System.out.println(content); // todo remove
46+
47+
extractOtelConfigFile(content);
48+
} else {
49+
System.out.println("Could not find the application.yaml file in the classpath."); // todo remove
50+
}
51+
}
52+
}
53+
}
54+
}
55+
}
56+
57+
private static void extractOtelConfigFile(String content) throws IOException {
58+
//
59+
// https://github.com/open-telemetry/opentelemetry-configuration/blob/c205770a956713e512eddb056570a99737e3383a/examples/kitchen-sink.yaml#L11
60+
61+
// 1. read to yaml tree in jackson
62+
ObjectMapper yamlMapper = new ObjectMapper(new YAMLFactory());
63+
JsonNode rootNode = yamlMapper.readTree(content);
64+
65+
// 2. find the "otel" node
66+
JsonNode otelNode = rootNode.get("otel");
67+
if (otelNode == null) {
68+
System.out.println("No 'otel' configuration found in the YAML file."); // todo remove
69+
return;
70+
}
71+
72+
String str = yamlMapper.writeValueAsString(otelNode);
73+
74+
OpenTelemetryConfigurationModel model =
75+
DeclarativeConfiguration.parse(
76+
new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8)));
77+
OpenTelemetrySdk sdk =
78+
DeclarativeConfiguration.create(model); // can pass ComponentLoader as second arg
79+
80+
Runtime.getRuntime().addShutdownHook(new Thread(sdk::close));
81+
82+
GlobalOpenTelemetry.set(sdk);
83+
GlobalConfigProvider.set(SdkConfigProvider.create(model));
84+
85+
System.out.println("OpenTelemetry SDK initialized with configuration from: " + sdk); // todo remove
86+
System.out.println("OpenTelemetry configuration file content:"); // todo remove
87+
System.out.println(str); // todo remove
88+
}
89+
}

instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/OpenTelemetryAutoConfiguration.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import io.opentelemetry.sdk.autoconfigure.spi.internal.DefaultConfigProperties;
2727
import io.opentelemetry.sdk.extension.incubator.fileconfig.DeclarativeConfiguration;
2828
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.OpenTelemetryConfigurationModel;
29+
import java.io.IOException;
2930
import java.util.Collections;
3031
import java.util.List;
3132
import java.util.Optional;
@@ -44,6 +45,7 @@
4445
import org.springframework.context.annotation.Bean;
4546
import org.springframework.context.annotation.Conditional;
4647
import org.springframework.context.annotation.Configuration;
48+
import org.springframework.core.env.ConfigurableEnvironment;
4749
import org.springframework.core.env.Environment;
4850

4951
/**
@@ -147,9 +149,8 @@ static class EmbeddedConfigFileConfig {
147149

148150
@Bean
149151
public OpenTelemetryConfigurationModel openTelemetryConfigurationModel(
150-
Environment environment) {
151-
// todo declarative configuration
152-
return null;
152+
ConfigurableEnvironment environment) throws IOException {
153+
return EmbeddedConfigFile.extractModel(environment);
153154
}
154155

155156
@Bean

0 commit comments

Comments
 (0)