Skip to content

Commit 1aa789c

Browse files
committed
use unified bridge
1 parent 25c42bf commit 1aa789c

File tree

9 files changed

+204
-91
lines changed

9 files changed

+204
-91
lines changed

declarative-config-bridge/src/main/java/io/opentelemetry/contrib/sdk/autoconfigure/ConfigPropertiesUtil.java

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,9 @@
1010
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
1111
import io.opentelemetry.sdk.autoconfigure.internal.AutoConfigureUtil;
1212
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
13-
import io.opentelemetry.sdk.extension.incubator.fileconfig.SdkConfigProvider;
14-
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.OpenTelemetryConfigurationModel;
15-
import java.util.Collections;
16-
import java.util.Map;
1713
import javax.annotation.Nullable;
1814

19-
public class ConfigPropertiesUtil {
15+
public final class ConfigPropertiesUtil {
2016
private ConfigPropertiesUtil() {}
2117

2218
/** Resolve {@link ConfigProperties} from the {@code autoConfiguredOpenTelemetrySdk}. */
@@ -31,41 +27,35 @@ public static ConfigProperties resolveConfigProperties(
3127
AutoConfigureUtil.getConfigProvider(autoConfiguredOpenTelemetrySdk);
3228
if (configProvider != null) {
3329
return resolveInstrumentationConfig(
34-
configProvider.getInstrumentationConfig(), Collections.emptyMap());
30+
configProvider.getInstrumentationConfig(), propertyTranslatorBuilder());
3531
}
3632
// Should never happen
3733
throw new IllegalStateException(
3834
"AutoConfiguredOpenTelemetrySdk does not have ConfigProperties or DeclarativeConfigProperties. This is likely a programming error in opentelemetry-java");
3935
}
4036

41-
public static ConfigProperties resolveModel(OpenTelemetryConfigurationModel model) {
42-
return resolveModel(model, Collections.emptyMap());
43-
}
44-
45-
public static ConfigProperties resolveModel(
46-
OpenTelemetryConfigurationModel model, Map<String, String> translationMap) {
47-
return resolveInstrumentationConfig(
48-
SdkConfigProvider.create(model).getInstrumentationConfig(), translationMap);
49-
}
50-
5137
public static ConfigProperties resolveInstrumentationConfig(
5238
@Nullable DeclarativeConfigProperties instrumentationConfig) {
53-
return resolveInstrumentationConfig(instrumentationConfig, Collections.emptyMap());
39+
return resolveInstrumentationConfig(instrumentationConfig, propertyTranslatorBuilder());
5440
}
5541

5642
public static ConfigProperties resolveInstrumentationConfig(
5743
@Nullable DeclarativeConfigProperties instrumentationConfig,
58-
Map<String, String> translationMap) {
44+
PropertyTranslatorBuilder builder) {
5945
return DeclarativeConfigPropertiesBridge.fromInstrumentationConfig(
60-
instrumentationConfig, translationMap);
46+
instrumentationConfig, builder.build());
6147
}
6248

6349
public static ConfigProperties resolveConfig(
64-
@Nullable DeclarativeConfigProperties config, Map<String, String> translationMap) {
65-
return DeclarativeConfigPropertiesBridge.create(config, translationMap);
50+
@Nullable DeclarativeConfigProperties config, PropertyTranslatorBuilder builder) {
51+
return DeclarativeConfigPropertiesBridge.create(config, builder.build());
6652
}
6753

6854
public static String propertyYamlPath(String propertyName) {
6955
return DeclarativeConfigPropertiesBridge.yamlPath(propertyName);
7056
}
57+
58+
public static PropertyTranslatorBuilder propertyTranslatorBuilder() {
59+
return new PropertyTranslatorBuilder();
60+
}
7161
}

declarative-config-bridge/src/main/java/io/opentelemetry/contrib/sdk/autoconfigure/DeclarativeConfigPropertiesBridge.java

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,81 +50,114 @@ final class DeclarativeConfigPropertiesBridge implements ConfigProperties {
5050

5151
private static final String OTEL_INSTRUMENTATION_PREFIX = "otel.instrumentation.";
5252

53+
private final PropertyTranslator translator;
5354
@Nullable private final DeclarativeConfigProperties baseNode;
54-
private final Map<String, String> translationMap;
5555

5656
static DeclarativeConfigPropertiesBridge fromInstrumentationConfig(
57-
@Nullable DeclarativeConfigProperties instrumentationConfig,
58-
Map<String, String> translationMap) {
57+
@Nullable DeclarativeConfigProperties instrumentationConfig, PropertyTranslator translator) {
5958
if (instrumentationConfig == null) {
6059
instrumentationConfig = DeclarativeConfigProperties.empty();
6160
}
6261
return new DeclarativeConfigPropertiesBridge(
63-
instrumentationConfig.getStructured("java", empty()), translationMap);
62+
instrumentationConfig.getStructured("java", empty()), translator);
6463
}
6564

6665
static DeclarativeConfigPropertiesBridge create(
67-
@Nullable DeclarativeConfigProperties node, Map<String, String> translationMap) {
68-
return new DeclarativeConfigPropertiesBridge(node, translationMap);
66+
@Nullable DeclarativeConfigProperties node, PropertyTranslator translator) {
67+
return new DeclarativeConfigPropertiesBridge(node, translator);
6968
}
7069

7170
private DeclarativeConfigPropertiesBridge(
72-
@Nullable DeclarativeConfigProperties baseNode, Map<String, String> translationMap) {
71+
@Nullable DeclarativeConfigProperties baseNode, PropertyTranslator translator) {
7372
this.baseNode = baseNode;
74-
this.translationMap = translationMap;
73+
this.translator = translator;
7574
}
7675

7776
@Nullable
7877
@Override
7978
public String getString(String propertyName) {
79+
Object value = translator.get(propertyName);
80+
if (value != null) {
81+
return value.toString();
82+
}
8083
return getPropertyValue(propertyName, DeclarativeConfigProperties::getString);
8184
}
8285

8386
@Nullable
8487
@Override
8588
public Boolean getBoolean(String propertyName) {
89+
Object value = translator.get(propertyName);
90+
if (value != null) {
91+
return (Boolean) value;
92+
}
8693
return getPropertyValue(propertyName, DeclarativeConfigProperties::getBoolean);
8794
}
8895

8996
@Nullable
9097
@Override
9198
public Integer getInt(String propertyName) {
99+
Object value = translator.get(propertyName);
100+
if (value != null) {
101+
return (Integer) value;
102+
}
92103
return getPropertyValue(propertyName, DeclarativeConfigProperties::getInt);
93104
}
94105

95106
@Nullable
96107
@Override
97108
public Long getLong(String propertyName) {
109+
Object value = translator.get(propertyName);
110+
if (value != null) {
111+
return (Long) value;
112+
}
98113
return getPropertyValue(propertyName, DeclarativeConfigProperties::getLong);
99114
}
100115

101116
@Nullable
102117
@Override
103118
public Double getDouble(String propertyName) {
119+
Object value = translator.get(propertyName);
120+
if (value != null) {
121+
return (Double) value;
122+
}
104123
return getPropertyValue(propertyName, DeclarativeConfigProperties::getDouble);
105124
}
106125

107126
@Nullable
108127
@Override
109128
public Duration getDuration(String propertyName) {
129+
Object value = translator.get(propertyName);
130+
if (value != null) {
131+
return (Duration) value;
132+
}
110133
Long millis = getPropertyValue(propertyName, DeclarativeConfigProperties::getLong);
111134
if (millis == null) {
112135
return null;
113136
}
114137
return Duration.ofMillis(millis);
115138
}
116139

140+
@SuppressWarnings("unchecked")
117141
@Override
118142
public List<String> getList(String propertyName) {
143+
Object value = translator.get(propertyName);
144+
if (value != null) {
145+
return (List<String>) value;
146+
}
119147
List<String> propertyValue =
120148
getPropertyValue(
121149
propertyName,
122150
(properties, lastPart) -> properties.getScalarList(lastPart, String.class));
123151
return propertyValue == null ? Collections.emptyList() : propertyValue;
124152
}
125153

154+
@SuppressWarnings("unchecked")
126155
@Override
127156
public Map<String, String> getMap(String propertyName) {
157+
Object fixed = translator.get(propertyName);
158+
if (fixed != null) {
159+
return (Map<String, String>) fixed;
160+
}
128161
DeclarativeConfigProperties propertyValue =
129162
getPropertyValue(propertyName, DeclarativeConfigProperties::getStructured);
130163
if (propertyValue == null) {
@@ -151,7 +184,7 @@ private <T> T getPropertyValue(
151184
return null;
152185
}
153186

154-
String[] segments = getSegments(translate(property));
187+
String[] segments = getSegments(translator.translateProperty(property));
155188
if (segments.length == 0) {
156189
return null;
157190
}
@@ -168,21 +201,12 @@ private <T> T getPropertyValue(
168201
return extractor.apply(target, lastPart);
169202
}
170203

171-
private String translate(String property) {
172-
for (Map.Entry<String, String> entry : translationMap.entrySet()) {
173-
if (property.startsWith(entry.getKey())) {
174-
return entry.getValue() + property.substring(entry.getKey().length());
175-
}
176-
}
177-
return property;
178-
}
179-
180204
private static String[] getSegments(String property) {
181205
if (property.startsWith(OTEL_INSTRUMENTATION_PREFIX)) {
182206
property = property.substring(OTEL_INSTRUMENTATION_PREFIX.length());
183207
}
184208
// Split the remainder of the property on "."
185-
return property.split("\\.");
209+
return property.replace('-', '_').split("\\.");
186210
}
187211

188212
static String yamlPath(String property) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.contrib.sdk.autoconfigure;
7+
8+
import java.util.LinkedHashMap;
9+
import java.util.Map;
10+
import javax.annotation.Nullable;
11+
12+
@SuppressWarnings("NonApiType")
13+
class PropertyTranslator {
14+
// lookup order matters - we choose the first match
15+
private final LinkedHashMap<String, String> translationMap;
16+
private final Map<String, Object> fixedValues;
17+
18+
PropertyTranslator(
19+
LinkedHashMap<String, String> translationMap, Map<String, Object> fixedValues) {
20+
this.translationMap = translationMap;
21+
this.fixedValues = fixedValues;
22+
}
23+
24+
String translateProperty(String property) {
25+
for (Map.Entry<String, String> entry : translationMap.entrySet()) {
26+
if (property.startsWith(entry.getKey())) {
27+
return entry.getValue() + property.substring(entry.getKey().length());
28+
}
29+
}
30+
return property;
31+
}
32+
33+
@Nullable
34+
public Object get(String propertyName) {
35+
return fixedValues.get(propertyName);
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.contrib.sdk.autoconfigure;
7+
8+
import com.google.errorprone.annotations.CanIgnoreReturnValue;
9+
import java.util.LinkedHashMap;
10+
import java.util.Map;
11+
12+
public class PropertyTranslatorBuilder {
13+
private final LinkedHashMap<String, String> translationMap = new LinkedHashMap<>();
14+
private final Map<String, Object> fixedValues = new LinkedHashMap<>();
15+
16+
PropertyTranslatorBuilder() {}
17+
18+
@CanIgnoreReturnValue
19+
public PropertyTranslatorBuilder addTranslation(String propertyName, String yamlPath) {
20+
translationMap.put(propertyName, yamlPath);
21+
return this;
22+
}
23+
24+
@CanIgnoreReturnValue
25+
public PropertyTranslatorBuilder addFixedValue(String propertyName, Object value) {
26+
fixedValues.put(propertyName, value);
27+
return this;
28+
}
29+
30+
PropertyTranslator build() {
31+
return new PropertyTranslator(translationMap, fixedValues);
32+
}
33+
}

0 commit comments

Comments
 (0)