|
1 | 1 | package io.opentelemetry.instrumentation.spring.autoconfigure;
|
2 | 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 | 3 | import io.opentelemetry.sdk.extension.incubator.fileconfig.DeclarativeConfiguration;
|
9 |
| -import io.opentelemetry.sdk.extension.incubator.fileconfig.SdkConfigProvider; |
10 | 4 | 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 | 5 | import java.io.ByteArrayInputStream;
|
15 | 6 | import java.io.IOException;
|
16 | 7 | import java.io.InputStream;
|
17 | 8 | import java.nio.charset.StandardCharsets;
|
| 9 | +import java.util.Map; |
18 | 10 | import java.util.regex.Matcher;
|
19 | 11 | import java.util.regex.Pattern;
|
| 12 | +import javax.annotation.Nullable; |
| 13 | +import org.snakeyaml.engine.v2.api.Dump; |
| 14 | +import org.snakeyaml.engine.v2.api.DumpSettings; |
| 15 | +import org.snakeyaml.engine.v2.api.Load; |
| 16 | +import org.snakeyaml.engine.v2.api.LoadSettings; |
| 17 | +import org.springframework.boot.env.OriginTrackedMapPropertySource; |
| 18 | +import org.springframework.core.env.ConfigurableEnvironment; |
| 19 | +import org.springframework.core.env.PropertySource; |
20 | 20 |
|
21 | 21 | class EmbeddedConfigFile {
|
22 | 22 |
|
23 |
| - private static final Pattern PATTERN = Pattern.compile( |
24 |
| - "^Config resource 'class path resource \\[(.+)]' via location 'optional:classpath:/'$" |
25 |
| - ); |
| 23 | + private EmbeddedConfigFile() { |
| 24 | + // Utility class |
| 25 | + } |
| 26 | + |
| 27 | + private static final Pattern PATTERN = |
| 28 | + Pattern.compile( |
| 29 | + "^Config resource 'class path resource \\[(.+)]' via location 'optional:classpath:/'$"); |
26 | 30 |
|
27 | 31 | static OpenTelemetryConfigurationModel extractModel(ConfigurableEnvironment environment)
|
28 | 32 | throws IOException {
|
29 | 33 | for (PropertySource<?> propertySource : environment.getPropertySources()) {
|
30 | 34 | 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); |
| 35 | + return getModel(environment, (OriginTrackedMapPropertySource) propertySource); |
| 36 | + } |
| 37 | + } |
| 38 | + throw new IllegalStateException("No application.yaml file found."); |
| 39 | + } |
| 40 | + |
| 41 | + private static OpenTelemetryConfigurationModel getModel( |
| 42 | + ConfigurableEnvironment environment, OriginTrackedMapPropertySource source) |
| 43 | + throws IOException { |
| 44 | + String name = source.getName(); |
| 45 | + System.out.println("Property Source: " + name); // todo remove |
| 46 | + Matcher matcher = PATTERN.matcher(name); |
| 47 | + if (matcher.matches()) { |
| 48 | + String file = matcher.group(1); |
| 49 | + System.out.println("Found application.yaml: " + file); |
38 | 50 |
|
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 |
| 51 | + try (InputStream resourceAsStream = |
| 52 | + environment.getClass().getClassLoader().getResourceAsStream(file)) { |
| 53 | + // Print the contents of the application.yaml file |
| 54 | + if (resourceAsStream != null) { |
| 55 | + // String content = new String(resourceAsStream.readAllBytes()); |
| 56 | + // System.out.println("Contents of " + file + ":"); // todo remove |
| 57 | + // System.out.println(content); // todo remove |
46 | 58 |
|
47 |
| - extractOtelConfigFile(content); |
48 |
| - } else { |
49 |
| - System.out.println("Could not find the application.yaml file in the classpath."); // todo remove |
50 |
| - } |
51 |
| - } |
| 59 | + return extractOtelConfigFile(resourceAsStream); |
| 60 | + } else { |
| 61 | + throw new IllegalStateException("Unable to load " + file + " in the classpath."); |
52 | 62 | }
|
53 | 63 | }
|
| 64 | + } else { |
| 65 | + throw new IllegalStateException( |
| 66 | + "No OpenTelemetry configuration found in the application.yaml file."); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + @Nullable |
| 71 | + @SuppressWarnings("unchecked") |
| 72 | + private static String parseOtelNode(InputStream in) { |
| 73 | + Load load = new Load(LoadSettings.builder().build()); |
| 74 | + Dump dump = new Dump(DumpSettings.builder().build()); |
| 75 | + for (Object o : load.loadAllFromInputStream(in)) { |
| 76 | + Map<String, Object> data = (Map<String, Object>) o; |
| 77 | + Map<String, Map<String, Object>> otel = (Map<String, Map<String, Object>>) data.get("otel"); |
| 78 | + if (otel != null) { |
| 79 | + return dump.dumpToString(otel); |
| 80 | + } |
54 | 81 | }
|
| 82 | + throw new IllegalStateException("No 'otel' configuration found in the YAML file."); |
55 | 83 | }
|
56 | 84 |
|
57 |
| - private static void extractOtelConfigFile(String content) throws IOException { |
| 85 | + private static OpenTelemetryConfigurationModel extractOtelConfigFile(InputStream content) { |
58 | 86 | //
|
59 | 87 | // https://github.com/open-telemetry/opentelemetry-configuration/blob/c205770a956713e512eddb056570a99737e3383a/examples/kitchen-sink.yaml#L11
|
60 | 88 |
|
61 | 89 | // 1. read to yaml tree in jackson
|
62 |
| - ObjectMapper yamlMapper = new ObjectMapper(new YAMLFactory()); |
63 |
| - JsonNode rootNode = yamlMapper.readTree(content); |
| 90 | + // ObjectMapper yamlMapper = new ObjectMapper(new YAMLFactory()); |
| 91 | + // JsonNode rootNode = yamlMapper.readTree(content); |
64 | 92 |
|
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; |
| 93 | + String node = parseOtelNode(content); |
| 94 | + if (node == null || node.isEmpty()) { |
| 95 | + throw new IllegalStateException("otel node is empty or null in the YAML file."); |
70 | 96 | }
|
71 | 97 |
|
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)); |
| 98 | + System.out.println("OpenTelemetry configuration file content:"); // todo remove |
| 99 | + System.out.println(node); // todo remove |
84 | 100 |
|
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 |
| 101 | + return DeclarativeConfiguration.parse( |
| 102 | + new ByteArrayInputStream(node.getBytes(StandardCharsets.UTF_8))); |
88 | 103 | }
|
89 | 104 | }
|
0 commit comments