Skip to content

Commit 682fc65

Browse files
committed
update config bridge from agent
1 parent c5c8e86 commit 682fc65

File tree

7 files changed

+104
-145
lines changed

7 files changed

+104
-145
lines changed

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

Lines changed: 0 additions & 37 deletions
This file was deleted.

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,19 @@
55

66
package io.opentelemetry.contrib.sdk.autoconfigure;
77

8-
/**
9-
* This class is internal and is hence not for public use. Its APIs are unstable and can change at
10-
* any time.
11-
*/
128
public final class ConfigPropertiesUtil {
139
private ConfigPropertiesUtil() {}
1410

1511
public static String propertyYamlPath(String propertyName) {
16-
return DeclarativeConfigPropertiesBridge.yamlPath(propertyName);
12+
return yamlPath(propertyName);
1713
}
14+
15+
static String yamlPath(String property) {
16+
String[] segments = DeclarativeConfigPropertiesBridge.getSegments(property);
17+
if (segments.length == 0) {
18+
throw new IllegalArgumentException("Invalid property: " + property);
19+
}
20+
21+
return "'instrumentation/development' / 'java' / '" + String.join("' / '", segments) + "'";
22+
}
1823
}

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

Lines changed: 37 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.util.HashMap;
1515
import java.util.List;
1616
import java.util.Map;
17+
import java.util.Objects;
1718
import java.util.function.BiFunction;
1819
import javax.annotation.Nullable;
1920

@@ -45,81 +46,60 @@
4546
* common:
4647
* string_key: value
4748
* </pre>
48-
*
49-
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change
50-
* at any time.
5149
*/
5250
final class DeclarativeConfigPropertiesBridge implements ConfigProperties {
5351

5452
private static final String OTEL_INSTRUMENTATION_PREFIX = "otel.instrumentation.";
5553

56-
private final ConfigPropertiesTranslator translator;
57-
@Nullable private final DeclarativeConfigProperties baseNode;
54+
private final DeclarativeConfigProperties baseNode;
55+
56+
// lookup order matters - we choose the first match
57+
private final Map<String, String> mappings;
58+
private final Map<String, Object> overrideValues;
5859

5960
DeclarativeConfigPropertiesBridge(
60-
@Nullable DeclarativeConfigProperties baseNode, ConfigPropertiesTranslator translator) {
61-
this.baseNode = baseNode;
62-
this.translator = translator;
61+
DeclarativeConfigProperties baseNode,
62+
Map<String, String> mappings,
63+
Map<String, Object> overrideValues) {
64+
this.baseNode = Objects.requireNonNull(baseNode);
65+
this.mappings = mappings;
66+
this.overrideValues = overrideValues;
6367
}
6468

6569
@Nullable
6670
@Override
6771
public String getString(String propertyName) {
68-
Object value = translator.get(propertyName);
69-
if (value != null) {
70-
return value.toString();
71-
}
72-
return getPropertyValue(propertyName, DeclarativeConfigProperties::getString);
72+
return getPropertyValue(propertyName, String.class, DeclarativeConfigProperties::getString);
7373
}
7474

7575
@Nullable
7676
@Override
7777
public Boolean getBoolean(String propertyName) {
78-
Object value = translator.get(propertyName);
79-
if (value != null) {
80-
return (Boolean) value;
81-
}
82-
return getPropertyValue(propertyName, DeclarativeConfigProperties::getBoolean);
78+
return getPropertyValue(propertyName, Boolean.class, DeclarativeConfigProperties::getBoolean);
8379
}
8480

8581
@Nullable
8682
@Override
8783
public Integer getInt(String propertyName) {
88-
Object value = translator.get(propertyName);
89-
if (value != null) {
90-
return (Integer) value;
91-
}
92-
return getPropertyValue(propertyName, DeclarativeConfigProperties::getInt);
84+
return getPropertyValue(propertyName, Integer.class, DeclarativeConfigProperties::getInt);
9385
}
9486

9587
@Nullable
9688
@Override
9789
public Long getLong(String propertyName) {
98-
Object value = translator.get(propertyName);
99-
if (value != null) {
100-
return (Long) value;
101-
}
102-
return getPropertyValue(propertyName, DeclarativeConfigProperties::getLong);
90+
return getPropertyValue(propertyName, Long.class, DeclarativeConfigProperties::getLong);
10391
}
10492

10593
@Nullable
10694
@Override
10795
public Double getDouble(String propertyName) {
108-
Object value = translator.get(propertyName);
109-
if (value != null) {
110-
return (Double) value;
111-
}
112-
return getPropertyValue(propertyName, DeclarativeConfigProperties::getDouble);
96+
return getPropertyValue(propertyName, Double.class, DeclarativeConfigProperties::getDouble);
11397
}
11498

11599
@Nullable
116100
@Override
117101
public Duration getDuration(String propertyName) {
118-
Object value = translator.get(propertyName);
119-
if (value != null) {
120-
return (Duration) value;
121-
}
122-
Long millis = getPropertyValue(propertyName, DeclarativeConfigProperties::getLong);
102+
Long millis = getPropertyValue(propertyName, Long.class, DeclarativeConfigProperties::getLong);
123103
if (millis == null) {
124104
return null;
125105
}
@@ -129,26 +109,22 @@ public Duration getDuration(String propertyName) {
129109
@SuppressWarnings("unchecked")
130110
@Override
131111
public List<String> getList(String propertyName) {
132-
Object value = translator.get(propertyName);
133-
if (value != null) {
134-
return (List<String>) value;
135-
}
136112
List<String> propertyValue =
137113
getPropertyValue(
138114
propertyName,
115+
List.class,
139116
(properties, lastPart) -> properties.getScalarList(lastPart, String.class));
140117
return propertyValue == null ? Collections.emptyList() : propertyValue;
141118
}
142119

143120
@SuppressWarnings("unchecked")
144121
@Override
145122
public Map<String, String> getMap(String propertyName) {
146-
Object fixed = translator.get(propertyName);
147-
if (fixed != null) {
148-
return (Map<String, String>) fixed;
149-
}
150123
DeclarativeConfigProperties propertyValue =
151-
getPropertyValue(propertyName, DeclarativeConfigProperties::getStructured);
124+
getPropertyValue(
125+
propertyName,
126+
DeclarativeConfigProperties.class,
127+
DeclarativeConfigProperties::getStructured);
152128
if (propertyValue == null) {
153129
return Collections.emptyMap();
154130
}
@@ -168,12 +144,15 @@ public Map<String, String> getMap(String propertyName) {
168144

169145
@Nullable
170146
private <T> T getPropertyValue(
171-
String property, BiFunction<DeclarativeConfigProperties, String, T> extractor) {
172-
if (baseNode == null) {
173-
return null;
147+
String property,
148+
Class<T> clazz,
149+
BiFunction<DeclarativeConfigProperties, String, T> extractor) {
150+
T override = clazz.cast(overrideValues.get(property));
151+
if (override != null) {
152+
return override;
174153
}
175154

176-
String[] segments = getSegments(translator.translateProperty(property));
155+
String[] segments = getSegments(translateProperty(property));
177156
if (segments.length == 0) {
178157
return null;
179158
}
@@ -190,20 +169,20 @@ private <T> T getPropertyValue(
190169
return extractor.apply(target, lastPart);
191170
}
192171

193-
private static String[] getSegments(String property) {
172+
static String[] getSegments(String property) {
194173
if (property.startsWith(OTEL_INSTRUMENTATION_PREFIX)) {
195174
property = property.substring(OTEL_INSTRUMENTATION_PREFIX.length());
196175
}
197176
// Split the remainder of the property on "."
198177
return property.replace('-', '_').split("\\.");
199178
}
200179

201-
static String yamlPath(String property) {
202-
String[] segments = getSegments(property);
203-
if (segments.length == 0) {
204-
throw new IllegalArgumentException("Invalid property: " + property);
180+
private String translateProperty(String property) {
181+
for (Map.Entry<String, String> entry : mappings.entrySet()) {
182+
if (property.startsWith(entry.getKey())) {
183+
return entry.getValue() + property.substring(entry.getKey().length());
184+
}
205185
}
206-
207-
return "'instrumentation/development' / 'java' / '" + String.join("' / '", segments) + "'";
186+
return property;
208187
}
209188
}

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

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,39 +13,57 @@
1313
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
1414
import io.opentelemetry.sdk.autoconfigure.internal.AutoConfigureUtil;
1515
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
16+
import java.util.HashMap;
1617
import java.util.LinkedHashMap;
1718
import java.util.Map;
1819
import javax.annotation.Nullable;
1920

2021
/**
2122
* A builder for {@link DeclarativeConfigPropertiesBridge} that allows adding translations and fixed
2223
* values for properties.
23-
*
24-
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change
25-
* at any time.
2624
*/
2725
public class DeclarativeConfigPropertiesBridgeBuilder {
28-
private final LinkedHashMap<String, String> translationMap = new LinkedHashMap<>();
29-
private final Map<String, Object> fixedValues = new LinkedHashMap<>();
26+
/**
27+
* order is important here, so we use LinkedHashMap - see {@link #addMapping(String, String)} for
28+
* more details
29+
*/
30+
private final Map<String, String> mappings = new LinkedHashMap<>();
31+
32+
private final Map<String, Object> overrideValues = new HashMap<>();
3033

3134
public DeclarativeConfigPropertiesBridgeBuilder() {}
3235

36+
/**
37+
* Adds a mapping from a property prefix to a YAML path.
38+
*
39+
* <p>For example, if the property prefix is "otel.javaagent" and the YAML path is "agent", then
40+
* any property starting with "otel.javaagent." will be resolved against the "agent" node in the
41+
* instrumentation/java section of the YAML configuration.
42+
*
43+
* @param propertyPrefix the prefix of the property to translate
44+
* @param yamlPath the YAML path to resolve the property against
45+
*/
3346
@CanIgnoreReturnValue
34-
public DeclarativeConfigPropertiesBridgeBuilder addTranslation(
35-
String propertyName, String yamlPath) {
36-
translationMap.put(propertyName, yamlPath);
47+
public DeclarativeConfigPropertiesBridgeBuilder addMapping(
48+
String propertyPrefix, String yamlPath) {
49+
mappings.put(propertyPrefix, yamlPath);
3750
return this;
3851
}
3952

53+
/**
54+
* Adds a fixed override value for a property.
55+
*
56+
* @param propertyName the name of the property to override
57+
* @param value the value to return when the property is requested
58+
*/
4059
@CanIgnoreReturnValue
41-
public DeclarativeConfigPropertiesBridgeBuilder addFixedValue(String propertyName, Object value) {
42-
fixedValues.put(propertyName, value);
60+
public DeclarativeConfigPropertiesBridgeBuilder addOverride(String propertyName, Object value) {
61+
overrideValues.put(propertyName, value);
4362
return this;
4463
}
4564

46-
/** Resolve {@link ConfigProperties} from the {@code autoConfiguredOpenTelemetrySdk}. */
47-
public ConfigProperties resolveConfigProperties(
48-
AutoConfiguredOpenTelemetrySdk autoConfiguredOpenTelemetrySdk) {
65+
/** Build {@link ConfigProperties} from the {@code autoConfiguredOpenTelemetrySdk}. */
66+
public ConfigProperties build(AutoConfiguredOpenTelemetrySdk autoConfiguredOpenTelemetrySdk) {
4967
ConfigProperties sdkConfigProperties =
5068
AutoConfigureUtil.getConfig(autoConfiguredOpenTelemetrySdk);
5169
if (sdkConfigProperties != null) {
@@ -54,23 +72,31 @@ public ConfigProperties resolveConfigProperties(
5472
ConfigProvider configProvider =
5573
AutoConfigureUtil.getConfigProvider(autoConfiguredOpenTelemetrySdk);
5674
if (configProvider != null) {
57-
return resolveInstrumentationConfig(configProvider.getInstrumentationConfig());
75+
return buildFromInstrumentationConfig(configProvider.getInstrumentationConfig());
5876
}
5977
// Should never happen
6078
throw new IllegalStateException(
6179
"AutoConfiguredOpenTelemetrySdk does not have ConfigProperties or DeclarativeConfigProperties. This is likely a programming error in opentelemetry-java");
6280
}
6381

64-
public ConfigProperties resolveInstrumentationConfig(
82+
/**
83+
* Build {@link ConfigProperties} from the {@link DeclarativeConfigProperties} provided by the
84+
* instrumentation configuration.
85+
*
86+
* <p>If the provided {@code instrumentationConfig} is null, an empty {@link
87+
* DeclarativeConfigProperties} will be used.
88+
*
89+
* @param instrumentationConfig the instrumentation configuration to build from
90+
* @return a new instance of {@link ConfigProperties}
91+
*/
92+
public ConfigProperties buildFromInstrumentationConfig(
6593
@Nullable DeclarativeConfigProperties instrumentationConfig) {
94+
// leave the name "build" for a future method that builds from a DeclarativeConfigProperties
95+
// instance that doesn't come from the top-level instrumentation config
6696
if (instrumentationConfig == null) {
6797
instrumentationConfig = DeclarativeConfigProperties.empty();
6898
}
69-
return resolveConfig(instrumentationConfig.getStructured("java", empty()));
70-
}
71-
72-
public ConfigProperties resolveConfig(@Nullable DeclarativeConfigProperties config) {
7399
return new DeclarativeConfigPropertiesBridge(
74-
config, new ConfigPropertiesTranslator(translationMap, fixedValues));
100+
instrumentationConfig.getStructured("java", empty()), mappings, overrideValues);
75101
}
76102
}

declarative-config-bridge/src/test/java/io/opentelemetry/contrib/sdk/autoconfigure/DeclarativeConfigPropertiesBridgeBuilderTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ void shouldUseConfigPropertiesForAutoConfiguration() {
3333
.thenReturn(configPropertiesMock);
3434

3535
ConfigProperties configProperties =
36-
new DeclarativeConfigPropertiesBridgeBuilder().resolveConfigProperties(sdkMock);
36+
new DeclarativeConfigPropertiesBridgeBuilder().build(sdkMock);
3737

3838
assertThat(configProperties).isSameAs(configPropertiesMock);
3939
}
@@ -62,7 +62,7 @@ void shouldUseConfigProviderForDeclarativeConfiguration() {
6262
.thenReturn(configProviderMock);
6363

6464
ConfigProperties configProperties =
65-
new DeclarativeConfigPropertiesBridgeBuilder().resolveConfigProperties(sdkMock);
65+
new DeclarativeConfigPropertiesBridgeBuilder().build(sdkMock);
6666

6767
assertThat(configProperties.getString(propertyName)).isEqualTo(expectedValue);
6868
}
@@ -82,9 +82,9 @@ void shouldUseConfigProviderForDeclarativeConfiguration_noInstrumentationConfig(
8282
.thenReturn(configProviderMock);
8383

8484
ConfigProperties configProperties =
85-
new DeclarativeConfigPropertiesBridgeBuilder().resolveConfigProperties(sdkMock);
85+
new DeclarativeConfigPropertiesBridgeBuilder().build(sdkMock);
8686

87-
assertThat(configProperties.getString("testProperty")).isEqualTo(null);
87+
assertThat(configProperties.getString("testProperty")).isNull();
8888
}
8989
}
9090
}

0 commit comments

Comments
 (0)