15
15
import java .util .HashMap ;
16
16
import java .util .List ;
17
17
import java .util .Map ;
18
+ import java .util .Objects ;
18
19
import java .util .function .BiFunction ;
20
+ import java .util .stream .Collectors ;
19
21
import javax .annotation .Nullable ;
20
22
21
23
/**
@@ -52,16 +54,37 @@ public final class DeclarativeConfigPropertiesBridge implements ConfigProperties
52
54
private static final String OTEL_INSTRUMENTATION_PREFIX = "otel.instrumentation." ;
53
55
private static final String OTEL_JAVA_AGENT_PREFIX = "otel.javaagent." ;
54
56
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 <>();
56
59
57
60
// The node at .instrumentation.java
58
61
private final DeclarativeConfigProperties instrumentationJavaNode ;
59
62
60
- // todo
61
- // private final DeclarativeConfigProperties instrumentationGeneralNode;
63
+ private final DeclarativeConfigProperties instrumentationGeneralNode ;
62
64
63
65
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" )));
65
88
}
66
89
67
90
public DeclarativeConfigPropertiesBridge (ConfigProvider configProvider ) {
@@ -70,7 +93,7 @@ public DeclarativeConfigPropertiesBridge(ConfigProvider configProvider) {
70
93
inst = DeclarativeConfigProperties .empty ();
71
94
}
72
95
instrumentationJavaNode = inst .getStructured ("java" , empty ());
73
- // instrumentationGeneralNode = inst.getStructured("general", empty());
96
+ instrumentationGeneralNode = inst .getStructured ("general" , empty ());
74
97
}
75
98
76
99
@ Nullable
@@ -124,6 +147,10 @@ public List<String> getList(String propertyName) {
124
147
125
148
@ Override
126
149
public Map <String , String > getMap (String propertyName ) {
150
+ if ("otel.instrumentation.common.peer-service-mapping" .equals (propertyName )) {
151
+ return getPeerServiceMapping (this );
152
+ }
153
+
127
154
DeclarativeConfigProperties propertyValue =
128
155
getPropertyValue (propertyName , DeclarativeConfigProperties ::getStructured );
129
156
if (propertyValue == null ) {
@@ -146,17 +173,26 @@ public Map<String, String> getMap(String propertyName) {
146
173
@ Nullable
147
174
private <T > T getPropertyValue (
148
175
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 );
152
183
}
184
+ return null ;
185
+ }
153
186
187
+ private static <T > T splitOnDot (
188
+ String path ,
189
+ DeclarativeConfigProperties target ,
190
+ BiFunction <DeclarativeConfigProperties , String , T > extractor ) {
154
191
// Split the remainder of the property on ".", and walk to the N-1 entry
155
- String [] segments = suffix .split ("\\ ." );
192
+ String [] segments = path .split ("\\ ." );
156
193
if (segments .length == 0 ) {
157
194
return null ;
158
195
}
159
- DeclarativeConfigProperties target = instrumentationJavaNode ;
160
196
if (segments .length > 1 ) {
161
197
for (int i = 0 ; i < segments .length - 1 ; i ++) {
162
198
target = target .getStructured (segments [i ], empty ());
@@ -166,8 +202,8 @@ private <T> T getPropertyValue(
166
202
return extractor .apply (target , lastPart );
167
203
}
168
204
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 );
171
207
if (special != null ) {
172
208
return special ;
173
209
}
0 commit comments