|
| 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 | +} |
0 commit comments