Skip to content

Commit a63cffa

Browse files
committed
pr review
1 parent 51757fc commit a63cffa

File tree

5 files changed

+58
-60
lines changed

5 files changed

+58
-60
lines changed

javaagent-extension-api/src/main/java/io/opentelemetry/javaagent/extension/internal/ConfigPropertiesTranslator.java

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

javaagent-extension-api/src/main/java/io/opentelemetry/javaagent/extension/internal/DeclarativeConfigPropertiesBridge.java

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,25 @@ final class DeclarativeConfigPropertiesBridge implements ConfigProperties {
5353

5454
private static final String OTEL_INSTRUMENTATION_PREFIX = "otel.instrumentation.";
5555

56-
private final ConfigPropertiesTranslator translator;
5756
@Nullable private final DeclarativeConfigProperties baseNode;
5857

58+
// lookup order matters - we choose the first match
59+
private final Map<String, String> translationMap;
60+
private final Map<String, Object> overrideValues;
61+
5962
DeclarativeConfigPropertiesBridge(
60-
@Nullable DeclarativeConfigProperties baseNode, ConfigPropertiesTranslator translator) {
63+
@Nullable DeclarativeConfigProperties baseNode,
64+
Map<String, String> translationMap,
65+
Map<String, Object> overrideValues) {
6166
this.baseNode = baseNode;
62-
this.translator = translator;
67+
this.translationMap = translationMap;
68+
this.overrideValues = overrideValues;
6369
}
6470

6571
@Nullable
6672
@Override
6773
public String getString(String propertyName) {
68-
Object value = translator.get(propertyName);
74+
Object value = getOverride(propertyName);
6975
if (value != null) {
7076
return value.toString();
7177
}
@@ -75,7 +81,7 @@ public String getString(String propertyName) {
7581
@Nullable
7682
@Override
7783
public Boolean getBoolean(String propertyName) {
78-
Object value = translator.get(propertyName);
84+
Object value = getOverride(propertyName);
7985
if (value != null) {
8086
return (Boolean) value;
8187
}
@@ -85,7 +91,7 @@ public Boolean getBoolean(String propertyName) {
8591
@Nullable
8692
@Override
8793
public Integer getInt(String propertyName) {
88-
Object value = translator.get(propertyName);
94+
Object value = getOverride(propertyName);
8995
if (value != null) {
9096
return (Integer) value;
9197
}
@@ -95,7 +101,7 @@ public Integer getInt(String propertyName) {
95101
@Nullable
96102
@Override
97103
public Long getLong(String propertyName) {
98-
Object value = translator.get(propertyName);
104+
Object value = getOverride(propertyName);
99105
if (value != null) {
100106
return (Long) value;
101107
}
@@ -105,7 +111,7 @@ public Long getLong(String propertyName) {
105111
@Nullable
106112
@Override
107113
public Double getDouble(String propertyName) {
108-
Object value = translator.get(propertyName);
114+
Object value = getOverride(propertyName);
109115
if (value != null) {
110116
return (Double) value;
111117
}
@@ -115,7 +121,7 @@ public Double getDouble(String propertyName) {
115121
@Nullable
116122
@Override
117123
public Duration getDuration(String propertyName) {
118-
Object value = translator.get(propertyName);
124+
Object value = getOverride(propertyName);
119125
if (value != null) {
120126
return (Duration) value;
121127
}
@@ -129,7 +135,7 @@ public Duration getDuration(String propertyName) {
129135
@SuppressWarnings("unchecked")
130136
@Override
131137
public List<String> getList(String propertyName) {
132-
Object value = translator.get(propertyName);
138+
Object value = getOverride(propertyName);
133139
if (value != null) {
134140
return (List<String>) value;
135141
}
@@ -143,7 +149,7 @@ public List<String> getList(String propertyName) {
143149
@SuppressWarnings("unchecked")
144150
@Override
145151
public Map<String, String> getMap(String propertyName) {
146-
Object fixed = translator.get(propertyName);
152+
Object fixed = getOverride(propertyName);
147153
if (fixed != null) {
148154
return (Map<String, String>) fixed;
149155
}
@@ -173,7 +179,7 @@ private <T> T getPropertyValue(
173179
return null;
174180
}
175181

176-
String[] segments = getSegments(translator.translateProperty(property));
182+
String[] segments = getSegments(translateProperty(property));
177183
if (segments.length == 0) {
178184
return null;
179185
}
@@ -197,4 +203,18 @@ private static String[] getSegments(String property) {
197203
// Split the remainder of the property on "."
198204
return property.replace('-', '_').split("\\.");
199205
}
206+
207+
private String translateProperty(String property) {
208+
for (Map.Entry<String, String> entry : translationMap.entrySet()) {
209+
if (property.startsWith(entry.getKey())) {
210+
return entry.getValue() + property.substring(entry.getKey().length());
211+
}
212+
}
213+
return property;
214+
}
215+
216+
@Nullable
217+
private Object getOverride(String propertyName) {
218+
return overrideValues.get(propertyName);
219+
}
200220
}

javaagent-extension-api/src/main/java/io/opentelemetry/javaagent/extension/internal/DeclarativeConfigPropertiesBridgeBuilder.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,36 @@
2626
*/
2727
public class DeclarativeConfigPropertiesBridgeBuilder {
2828
private final LinkedHashMap<String, String> translationMap = new LinkedHashMap<>();
29-
private final Map<String, Object> fixedValues = new LinkedHashMap<>();
29+
private final Map<String, Object> overrideValues = new LinkedHashMap<>();
3030

3131
public DeclarativeConfigPropertiesBridgeBuilder() {}
3232

33+
/**
34+
* Adds a translation from a property prefix to a YAML path.
35+
*
36+
* <p>For example, if the property prefix is "otel.javaagent" and the YAML path is "agent", then
37+
* any property starting with "otel.javaagent." will be resolved against the "agent" node in the
38+
* instrumentation/java section of the YAML configuration.
39+
*
40+
* @param propertyPrefix the prefix of the property to translate
41+
* @param yamlPath the YAML path to resolve the property against
42+
*/
3343
@CanIgnoreReturnValue
3444
public DeclarativeConfigPropertiesBridgeBuilder addTranslation(
35-
String propertyName, String yamlPath) {
36-
translationMap.put(propertyName, yamlPath);
45+
String propertyPrefix, String yamlPath) {
46+
translationMap.put(propertyPrefix, yamlPath);
3747
return this;
3848
}
3949

50+
/**
51+
* Adds a fixed override value for a property.
52+
*
53+
* @param propertyName the name of the property to override
54+
* @param value the value to return when the property is requested
55+
*/
4056
@CanIgnoreReturnValue
41-
public DeclarativeConfigPropertiesBridgeBuilder addFixedValue(String propertyName, Object value) {
42-
fixedValues.put(propertyName, value);
57+
public DeclarativeConfigPropertiesBridgeBuilder addOverride(String propertyName, Object value) {
58+
overrideValues.put(propertyName, value);
4359
return this;
4460
}
4561

@@ -67,7 +83,6 @@ public ConfigProperties resolveInstrumentationConfig(
6783
instrumentationConfig = DeclarativeConfigProperties.empty();
6884
}
6985
return new DeclarativeConfigPropertiesBridge(
70-
instrumentationConfig.getStructured("java", empty()),
71-
new ConfigPropertiesTranslator(translationMap, fixedValues));
86+
instrumentationConfig.getStructured("java", empty()), translationMap, overrideValues);
7287
}
7388
}

javaagent-extension-api/src/test/java/io/opentelemetry/javaagent/extension/internal/DeclarativeConfigPropertiesBridgeTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ void agentTranslation() {
148148
create(
149149
new DeclarativeConfigPropertiesBridgeBuilder()
150150
.addTranslation("otel.javaagent", "agent")
151-
.addFixedValue("otel.javaagent.debug", true)
152-
.addFixedValue("otel.javaagent.logging", "application"));
151+
.addOverride("otel.javaagent.debug", true)
152+
.addOverride("otel.javaagent.logging", "application"));
153153

154154
assertThat(bridge.getBoolean("otel.javaagent.debug")).isTrue();
155155
assertThat(bridge.getBoolean("otel.javaagent.experimental.indy")).isTrue();

javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/OpenTelemetryInstaller.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ public static AutoConfiguredOpenTelemetrySdk installOpenTelemetrySdk(
5454
// these properties are used to initialize the SDK before the configuration file
5555
// is loaded for consistency, we pass them to the bridge, so that they can be read
5656
// later with the same value from the {@link DeclarativeConfigPropertiesBridge}
57-
.addFixedValue(
57+
.addOverride(
5858
"otel.javaagent.debug", earlyConfig.getBoolean("otel.javaagent.debug", false))
59-
.addFixedValue(
59+
.addOverride(
6060
"otel.javaagent.logging", earlyConfig.getString("otel.javaagent.logging"))
6161
.resolveInstrumentationConfig(configProvider.getInstrumentationConfig()),
6262
configProvider);

0 commit comments

Comments
 (0)