Skip to content

Commit bb5f7dc

Browse files
committed
map general props
1 parent 46406b4 commit bb5f7dc

File tree

3 files changed

+91
-19
lines changed

3 files changed

+91
-19
lines changed

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

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
import java.util.HashMap;
1616
import java.util.List;
1717
import java.util.Map;
18+
import java.util.Objects;
1819
import java.util.function.BiFunction;
20+
import java.util.stream.Collectors;
1921
import javax.annotation.Nullable;
2022

2123
/**
@@ -52,16 +54,37 @@ public final class DeclarativeConfigPropertiesBridge implements ConfigProperties
5254
private static final String OTEL_INSTRUMENTATION_PREFIX = "otel.instrumentation.";
5355
private static final String OTEL_JAVA_AGENT_PREFIX = "otel.javaagent.";
5456

55-
private static final Map<String, String> MAPPING_RULES = new HashMap<>();
57+
private static final Map<String, String> JAVA_MAPPING_RULES = new HashMap<>();
58+
private static final Map<String, String> GENERAL_MAPPING_RULES = new HashMap<>();
5659

5760
// The node at .instrumentation.java
5861
private final DeclarativeConfigProperties instrumentationJavaNode;
5962

60-
// todo
61-
// private final DeclarativeConfigProperties instrumentationGeneralNode;
63+
private final DeclarativeConfigProperties instrumentationGeneralNode;
6264

6365
static {
64-
MAPPING_RULES.put("otel.instrumentation.common.default-enabled", "common.default.enabled");
66+
JAVA_MAPPING_RULES.put("otel.instrumentation.common.default-enabled", "common.default.enabled");
67+
68+
// not supported in SDK yet (this is strictly typed)
69+
// GENERAL_MAPPING_RULES.put("otel.instrumentation.http.known-methods", "http.known_methods");
70+
GENERAL_MAPPING_RULES.put("otel.instrumentation.http.client.capture-request-headers", "http.client.request_captured_headers");
71+
GENERAL_MAPPING_RULES.put("otel.instrumentation.http.client.capture-response-headers", "http.client.response_captured_headers");
72+
GENERAL_MAPPING_RULES.put("otel.instrumentation.http.server.capture-request-headers", "http.server.request_captured_headers");
73+
GENERAL_MAPPING_RULES.put("otel.instrumentation.http.server.capture-response-headers", "http.server.response_captured_headers");
74+
}
75+
76+
private static Map<String, String> getPeerServiceMapping(
77+
DeclarativeConfigPropertiesBridge bridge) {
78+
List<DeclarativeConfigProperties> configProperties =
79+
bridge
80+
.instrumentationGeneralNode
81+
.getStructured("peer", empty())
82+
.getStructuredList("service_mapping", Collections.emptyList());
83+
return configProperties.stream()
84+
.collect(
85+
Collectors.toMap(
86+
e -> Objects.requireNonNull(e.getString("peer"), "peer must not be null"),
87+
e -> Objects.requireNonNull(e.getString("service"), "service must not be null")));
6588
}
6689

6790
public DeclarativeConfigPropertiesBridge(ConfigProvider configProvider) {
@@ -70,7 +93,7 @@ public DeclarativeConfigPropertiesBridge(ConfigProvider configProvider) {
7093
inst = DeclarativeConfigProperties.empty();
7194
}
7295
instrumentationJavaNode = inst.getStructured("java", empty());
73-
// instrumentationGeneralNode = inst.getStructured("general", empty());
96+
instrumentationGeneralNode = inst.getStructured("general", empty());
7497
}
7598

7699
@Nullable
@@ -124,6 +147,10 @@ public List<String> getList(String propertyName) {
124147

125148
@Override
126149
public Map<String, String> getMap(String propertyName) {
150+
if ("otel.instrumentation.common.peer-service-mapping".equals(propertyName)) {
151+
return getPeerServiceMapping(this);
152+
}
153+
127154
DeclarativeConfigProperties propertyValue =
128155
getPropertyValue(propertyName, DeclarativeConfigProperties::getStructured);
129156
if (propertyValue == null) {
@@ -146,17 +173,26 @@ public Map<String, String> getMap(String propertyName) {
146173
@Nullable
147174
private <T> T getPropertyValue(
148175
String property, BiFunction<DeclarativeConfigProperties, String, T> extractor) {
149-
String suffix = getSuffix(property);
150-
if (suffix == null) {
151-
return null;
176+
String generalPath = GENERAL_MAPPING_RULES.get(property);
177+
if (generalPath != null) {
178+
return splitOnDot(generalPath, instrumentationGeneralNode, extractor);
179+
}
180+
String javaPath = getJavaPath(property);
181+
if (javaPath != null) {
182+
return splitOnDot(javaPath, instrumentationJavaNode, extractor);
152183
}
184+
return null;
185+
}
153186

187+
private static <T> T splitOnDot(
188+
String path,
189+
DeclarativeConfigProperties target,
190+
BiFunction<DeclarativeConfigProperties, String, T> extractor) {
154191
// Split the remainder of the property on ".", and walk to the N-1 entry
155-
String[] segments = suffix.split("\\.");
192+
String[] segments = path.split("\\.");
156193
if (segments.length == 0) {
157194
return null;
158195
}
159-
DeclarativeConfigProperties target = instrumentationJavaNode;
160196
if (segments.length > 1) {
161197
for (int i = 0; i < segments.length - 1; i++) {
162198
target = target.getStructured(segments[i], empty());
@@ -166,8 +202,8 @@ private <T> T getPropertyValue(
166202
return extractor.apply(target, lastPart);
167203
}
168204

169-
private static String getSuffix(String property) {
170-
String special = MAPPING_RULES.get(property);
205+
private static String getJavaPath(String property) {
206+
String special = JAVA_MAPPING_RULES.get(property);
171207
if (special != null) {
172208
return special;
173209
}

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package io.opentelemetry.javaagent.extension;
77

88
import static org.assertj.core.api.Assertions.assertThat;
9+
import static org.assertj.core.data.MapEntry.entry;
910

1011
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
1112
import io.opentelemetry.sdk.extension.incubator.fileconfig.DeclarativeConfiguration;
@@ -54,7 +55,6 @@ void getProperties() {
5455
.isTrue();
5556

5657
// common cases
57-
assertThat(bridge.getBoolean("otel.instrumentation.common.default-enabled")).isTrue();
5858
assertThat(bridge.getBoolean("otel.instrumentation.runtime-telemetry.enabled")).isFalse();
5959

6060
// check all the types
@@ -108,11 +108,21 @@ void getProperties() {
108108
assertThat(bridge.getMap("otel.instrumentation.other-instrumentation.map_key", expectedMap))
109109
.isEqualTo(expectedMap);
110110

111-
// verify vendor specific property names are preserved in unchanged form (prefix is not stripped
112-
// as for otel.instrumentation.*)
113-
assertThat(bridge.getBoolean("acme.full_name.preserved")).isTrue();
114-
115-
// todo agent properties
111+
// specific property mappings
116112
assertThat(bridge.getBoolean("otel.javaagent.experimental.indy")).isTrue();
113+
assertThat(bridge.getBoolean("otel.instrumentation.common.default-enabled")).isFalse();
114+
assertThat(bridge.getMap("otel.instrumentation.common.peer-service-mapping"))
115+
.containsOnly(entry("1.2.3.4", "FooService"), entry("2.3.4.5", "BarService"));
116+
// not supported in SDK yet (this is strictly typed)
117+
// assertThat(bridge.getList("otel.instrumentation.http.known-methods"))
118+
// .containsExactly("GET", "POST", "PUT");
119+
assertThat(bridge.getList("otel.instrumentation.http.client.capture-request-headers"))
120+
.containsExactly("Content-Type", "Accept");
121+
assertThat(bridge.getList("otel.instrumentation.http.client.capture-response-headers"))
122+
.containsExactly("Content-Type", "Content-Encoding");
123+
assertThat(bridge.getList("otel.instrumentation.http.server.capture-request-headers"))
124+
.containsExactly("Content-Type", "Accept");
125+
assertThat(bridge.getList("otel.instrumentation.http.server.capture-response-headers"))
126+
.containsExactly("Content-Type", "Content-Encoding");
117127
}
118128
}

javaagent-extension-api/src/test/resources/config.yaml

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,39 @@
11
disabled: true
22
file_format: 0.4
33
instrumentation/development:
4+
general:
5+
peer:
6+
service_mapping:
7+
- peer: 1.2.3.4
8+
service: FooService
9+
- peer: 2.3.4.5
10+
service: BarService
11+
http:
12+
# known_methods:
13+
# - GET
14+
# - POST
15+
# - PUT
16+
client:
17+
request_captured_headers:
18+
- Content-Type
19+
- Accept
20+
response_captured_headers:
21+
- Content-Type
22+
- Content-Encoding
23+
server:
24+
request_captured_headers:
25+
- Content-Type
26+
- Accept
27+
response_captured_headers:
28+
- Content-Type
29+
- Content-Encoding
430
java:
531
agent:
632
experimental:
733
indy: true
834
common:
935
default:
10-
enabled: true
36+
enabled: false
1137
runtime_telemetry:
1238
enabled: false
1339
example_instrumentation:

0 commit comments

Comments
 (0)