Skip to content

Commit 7e4587c

Browse files
authored
Fix declarative config bridge mapping and add tests (#15697)
1 parent b8eae5b commit 7e4587c

File tree

2 files changed

+282
-56
lines changed

2 files changed

+282
-56
lines changed

declarative-config-bridge/src/main/java/io/opentelemetry/instrumentation/config/bridge/ConfigPropertiesBackedDeclarativeConfigProperties.java

Lines changed: 48 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,20 @@ public final class ConfigPropertiesBackedDeclarativeConfigProperties
2929

3030
private static final String GENERAL_PEER_SERVICE_MAPPING = "general.peer.service_mapping";
3131

32-
private static final Map<String, String> LIST_MAPPINGS;
32+
private static final Map<String, String> SPECIAL_MAPPINGS;
3333

3434
static {
35-
LIST_MAPPINGS = new HashMap<>();
36-
LIST_MAPPINGS.put(
35+
SPECIAL_MAPPINGS = new HashMap<>();
36+
SPECIAL_MAPPINGS.put(
3737
"general.http.client.request_captured_headers",
3838
"otel.instrumentation.http.client.capture-request-headers");
39-
LIST_MAPPINGS.put(
39+
SPECIAL_MAPPINGS.put(
4040
"general.http.client.response_captured_headers",
4141
"otel.instrumentation.http.client.capture-response-headers");
42-
LIST_MAPPINGS.put(
42+
SPECIAL_MAPPINGS.put(
4343
"general.http.server.request_captured_headers",
4444
"otel.instrumentation.http.server.capture-request-headers");
45-
LIST_MAPPINGS.put(
45+
SPECIAL_MAPPINGS.put(
4646
"general.http.server.response_captured_headers",
4747
"otel.instrumentation.http.server.capture-response-headers");
4848
}
@@ -65,36 +65,31 @@ private ConfigPropertiesBackedDeclarativeConfigProperties(
6565
@Nullable
6666
@Override
6767
public String getString(String name) {
68-
String fullPath = pathWithName(name);
69-
return configProperties.getString(toPropertyKey(fullPath));
68+
return configProperties.getString(resolvePropertyKey(name));
7069
}
7170

7271
@Nullable
7372
@Override
7473
public Boolean getBoolean(String name) {
75-
String fullPath = pathWithName(name);
76-
return configProperties.getBoolean(toPropertyKey(fullPath));
74+
return configProperties.getBoolean(resolvePropertyKey(name));
7775
}
7876

7977
@Nullable
8078
@Override
8179
public Integer getInt(String name) {
82-
String fullPath = pathWithName(name);
83-
return configProperties.getInt(toPropertyKey(fullPath));
80+
return configProperties.getInt(resolvePropertyKey(name));
8481
}
8582

8683
@Nullable
8784
@Override
8885
public Long getLong(String name) {
89-
String fullPath = pathWithName(name);
90-
return configProperties.getLong(toPropertyKey(fullPath));
86+
return configProperties.getLong(resolvePropertyKey(name));
9187
}
9288

9389
@Nullable
9490
@Override
9591
public Double getDouble(String name) {
96-
String fullPath = pathWithName(name);
97-
return configProperties.getDouble(toPropertyKey(fullPath));
92+
return configProperties.getDouble(resolvePropertyKey(name));
9893
}
9994

10095
/**
@@ -117,20 +112,7 @@ public <T> List<T> getScalarList(String name, Class<T> scalarType) {
117112
if (scalarType != String.class) {
118113
return null;
119114
}
120-
String fullPath = pathWithName(name);
121-
122-
// Check explicit list mappings first
123-
String mappedKey = LIST_MAPPINGS.get(fullPath);
124-
if (mappedKey != null) {
125-
List<String> list = configProperties.getList(mappedKey);
126-
if (!list.isEmpty()) {
127-
return (List<T>) list;
128-
}
129-
return null;
130-
}
131-
132-
// Standard mapping
133-
List<String> list = configProperties.getList(toPropertyKey(fullPath));
115+
List<String> list = configProperties.getList(resolvePropertyKey(name));
134116
if (list.isEmpty()) {
135117
return null;
136118
}
@@ -158,49 +140,59 @@ public ComponentLoader getComponentLoader() {
158140
return configProperties.getComponentLoader();
159141
}
160142

161-
private String pathWithName(String name) {
162-
if (path.isEmpty()) {
163-
return name;
143+
private String resolvePropertyKey(String name) {
144+
String fullPath = pathWithName(name);
145+
146+
// Check explicit property mappings first
147+
String mappedKey = SPECIAL_MAPPINGS.get(fullPath);
148+
if (mappedKey != null) {
149+
return mappedKey;
164150
}
165-
return String.join(".", path) + "." + name;
166-
}
167151

168-
private static String toPropertyKey(String fullPath) {
169-
String translatedPath = translatePath(fullPath);
152+
if (!fullPath.startsWith("java.")) {
153+
return "";
154+
}
155+
156+
// Remove "java." prefix and translate the remaining path
157+
String[] segments = fullPath.substring(5).split("\\.");
158+
StringBuilder translatedPath = new StringBuilder();
159+
160+
for (int i = 0; i < segments.length; i++) {
161+
if (i > 0) {
162+
translatedPath.append(".");
163+
}
164+
translatedPath.append(translateName(segments[i]));
165+
}
166+
167+
String translated = translatedPath.toString();
170168

171169
// Handle agent prefix: java.agent.* → otel.javaagent.*
172-
if (translatedPath.startsWith("agent.")) {
173-
return "otel.java" + translatedPath;
170+
if (translated.startsWith("agent.")) {
171+
return "otel.java" + translated;
174172
}
175173

176174
// Handle jmx prefix: java.jmx.* → otel.jmx.*
177-
if (translatedPath.startsWith("jmx.")) {
178-
return "otel." + translatedPath;
175+
if (translated.startsWith("jmx.")) {
176+
return "otel." + translated;
179177
}
180178

181179
// Standard mapping
182-
return "otel.instrumentation." + translatedPath;
180+
return "otel.instrumentation." + translated;
183181
}
184182

185-
private static String translatePath(String path) {
186-
StringBuilder result = new StringBuilder();
187-
for (String segment : path.split("\\.")) {
188-
// Skip "java" segment - it doesn't exist in system properties
189-
if ("java".equals(segment)) {
190-
continue;
191-
}
192-
if (result.length() > 0) {
193-
result.append(".");
194-
}
195-
result.append(translateName(segment));
183+
private String pathWithName(String name) {
184+
if (path.isEmpty()) {
185+
return name;
196186
}
197-
return result.toString();
187+
return String.join(".", path) + "." + name;
198188
}
199189

200190
private static String translateName(String name) {
201191
if (name.endsWith("/development")) {
202-
return "experimental."
203-
+ name.substring(0, name.length() - "/development".length()).replace('_', '-');
192+
name = name.substring(0, name.length() - "/development".length());
193+
if (!name.contains("experimental")) {
194+
name = "experimental." + name;
195+
}
204196
}
205197
return name.replace('_', '-');
206198
}

0 commit comments

Comments
 (0)