|
1 | | -/* |
2 | | - * Copyright The OpenTelemetry Authors |
3 | | - * SPDX-License-Identifier: Apache-2.0 |
4 | | - */ |
5 | | - |
6 | 1 | package io.opentelemetry.javaagent.tooling.config; |
7 | 2 |
|
8 | | -import io.opentelemetry.api.incubator.config.ConfigProvider; |
9 | | -import io.opentelemetry.instrumentation.api.internal.ConfigPropertiesUtil; |
10 | | -import io.opentelemetry.javaagent.extension.DeclarativeConfigPropertiesBridge; |
11 | | -import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; |
12 | 3 | import io.opentelemetry.sdk.autoconfigure.spi.internal.DefaultConfigProperties; |
13 | | -import io.opentelemetry.sdk.extension.incubator.fileconfig.DeclarativeConfiguration; |
14 | | -import io.opentelemetry.sdk.extension.incubator.fileconfig.SdkConfigProvider; |
15 | | -import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.OpenTelemetryConfigurationModel; |
16 | | -import java.io.FileInputStream; |
17 | | -import java.io.IOException; |
18 | | -import java.util.Collections; |
19 | | -import java.util.Map; |
20 | | -import javax.annotation.Nonnull; |
21 | 4 | import javax.annotation.Nullable; |
| 5 | +import java.util.Collections; |
22 | 6 |
|
23 | | -/** |
24 | | - * Agent config class that is only supposed to be used before the SDK (and {@link |
25 | | - * io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties}) is initialized. |
26 | | - */ |
27 | | -public final class EarlyInitAgentConfig { |
28 | | - |
29 | | - public static EarlyInitAgentConfig create() { |
30 | | - String configurationFile = |
31 | | - DefaultConfigProperties.create(Collections.emptyMap()) |
32 | | - .getString("otel.experimental.config.file"); |
33 | | - |
34 | | - return configurationFile != null |
35 | | - ? new EarlyInitAgentConfig(loadConfigurationModel(configurationFile)) |
36 | | - : new EarlyInitAgentConfig(ConfigurationFile.getProperties()); |
37 | | - } |
38 | | - |
39 | | - private final Map<String, String> configFileContents; |
40 | | - private final @Nullable OpenTelemetryConfigurationModel configurationModel; |
41 | | - private final @Nullable ConfigProvider configProvider; |
42 | | - private final @Nullable ConfigProperties declarativeConfigProperties; |
43 | | - |
44 | | - private EarlyInitAgentConfig(Map<String, String> configFileContents) { |
45 | | - this.configFileContents = configFileContents; |
46 | | - this.configurationModel = null; |
47 | | - this.configProvider = null; |
48 | | - this.declarativeConfigProperties = null; |
49 | | - } |
50 | | - |
51 | | - private EarlyInitAgentConfig(@Nonnull OpenTelemetryConfigurationModel configurationModel) { |
52 | | - this.configFileContents = null; |
53 | | - this.configurationModel = configurationModel; |
54 | | - this.configProvider = SdkConfigProvider.create(configurationModel); |
55 | | - this.declarativeConfigProperties = new DeclarativeConfigPropertiesBridge(this.configProvider); |
56 | | - } |
57 | | - |
58 | | - @Nullable |
59 | | - public OpenTelemetryConfigurationModel getConfigurationModel() { |
60 | | - return configurationModel; |
61 | | - } |
62 | | - |
63 | | - @Nullable |
64 | | - public ConfigProvider getConfigProvider() { |
65 | | - return configProvider; |
66 | | - } |
67 | | - |
68 | | - @Nullable |
69 | | - public ConfigProperties getDeclarativeConfigProperties() { |
70 | | - return declarativeConfigProperties; |
71 | | - } |
72 | | - |
| 7 | +public interface EarlyInitAgentConfig { |
73 | 8 | @Nullable |
74 | | - public String getString(String propertyName) { |
75 | | - if (declarativeConfigProperties != null) { |
76 | | - return declarativeConfigProperties.getString(propertyName); |
77 | | - } |
78 | | - String value = ConfigPropertiesUtil.getString(propertyName); |
79 | | - if (value != null) { |
80 | | - return value; |
81 | | - } |
82 | | - return configFileContents.get(propertyName); |
83 | | - } |
| 9 | + String getString(String propertyName); |
84 | 10 |
|
85 | | - public boolean getBoolean(String propertyName, boolean defaultValue) { |
86 | | - if (declarativeConfigProperties != null) { |
87 | | - return declarativeConfigProperties.getBoolean(propertyName, defaultValue); |
88 | | - } |
| 11 | + boolean getBoolean(String propertyName, boolean defaultValue); |
89 | 12 |
|
90 | | - String configFileValueStr = configFileContents.get(propertyName); |
91 | | - boolean configFileValue = |
92 | | - configFileValueStr == null ? defaultValue : Boolean.parseBoolean(configFileValueStr); |
93 | | - return ConfigPropertiesUtil.getBoolean(propertyName, configFileValue); |
94 | | - } |
| 13 | + int getInt(String propertyName, int defaultValue); |
95 | 14 |
|
96 | | - public int getInt(String propertyName, int defaultValue) { |
97 | | - if (declarativeConfigProperties != null) { |
98 | | - return declarativeConfigProperties.getInt(propertyName, defaultValue); |
99 | | - } |
100 | | - try { |
101 | | - String configFileValueStr = configFileContents.get(propertyName); |
102 | | - int configFileValue = |
103 | | - configFileValueStr == null ? defaultValue : Integer.parseInt(configFileValueStr); |
104 | | - return ConfigPropertiesUtil.getInt(propertyName, configFileValue); |
105 | | - } catch (NumberFormatException ignored) { |
106 | | - return defaultValue; |
107 | | - } |
108 | | - } |
| 15 | + void logEarlyConfigErrorsIfAny(); |
109 | 16 |
|
110 | | - public void logEarlyConfigErrorsIfAny() { |
111 | | - ConfigurationFile.logErrorIfAny(); |
112 | | - } |
| 17 | + static EarlyInitAgentConfig create() { |
| 18 | + String configurationFile = |
| 19 | + DefaultConfigProperties.create(Collections.emptyMap()) |
| 20 | + .getString("otel.experimental.config.file"); |
113 | 21 |
|
114 | | - private static OpenTelemetryConfigurationModel loadConfigurationModel(String configurationFile) { |
115 | | - try (FileInputStream fis = new FileInputStream(configurationFile)) { |
116 | | - return DeclarativeConfiguration.parse(fis); |
117 | | - } catch (IOException e) { |
118 | | - throw new RuntimeException(e); |
119 | | - } |
| 22 | + return configurationFile != null |
| 23 | + ? new DeclarativeConfigEarlyInitAgentConfig(configurationFile) |
| 24 | + : new LegacyConfigFileEarlyInitAgentConfig(); |
120 | 25 | } |
121 | 26 | } |
0 commit comments