88import java .util .LinkedHashMap ;
99import java .util .List ;
1010import java .util .Map ;
11- import java .util .Optional ;
11+ import java .util .function . Function ;
1212import javax .annotation .Nullable ;
1313
1414/**
1717 */
1818public class InstrumentationConfigUtil {
1919
20- // TODO (jack-berg): add helper function to access nested structures with dot notation
21-
2220 /**
2321 * Return a map representation of the peer service map entries in {@code
2422 * .instrumentation.general.peer.service_mapping}, or null if none is configured.
@@ -27,25 +25,24 @@ public class InstrumentationConfigUtil {
2725 */
2826 @ Nullable
2927 public static Map <String , String > peerServiceMapping (ConfigProvider configProvider ) {
30- Optional <List <DeclarativeConfigProperties >> optServiceMappingList =
31- Optional .ofNullable (configProvider .getInstrumentationConfig ())
32- .map (instrumentationConfig -> instrumentationConfig .getStructured ("general" ))
33- .map (generalConfig -> generalConfig .getStructured ("peer" ))
34- .map (httpConfig -> httpConfig .getStructuredList ("service_mapping" ));
35- if (!optServiceMappingList .isPresent ()) {
28+ List <DeclarativeConfigProperties > serviceMappingList =
29+ getOrNull (
30+ configProvider ,
31+ config -> config .getStructuredList ("service_mapping" ),
32+ "general" ,
33+ "peer" );
34+ if (serviceMappingList == null ) {
3635 return null ;
3736 }
3837 Map <String , String > serviceMapping = new LinkedHashMap <>();
39- optServiceMappingList
40- .get ()
41- .forEach (
42- entry -> {
43- String peer = entry .getString ("peer" );
44- String service = entry .getString ("service" );
45- if (peer != null && service != null ) {
46- serviceMapping .put (peer , service );
47- }
48- });
38+ serviceMappingList .forEach (
39+ entry -> {
40+ String peer = entry .getString ("peer" );
41+ String service = entry .getString ("service" );
42+ if (peer != null && service != null ) {
43+ serviceMapping .put (peer , service );
44+ }
45+ });
4946 return serviceMapping .isEmpty () ? null : serviceMapping ;
5047 }
5148
@@ -57,13 +54,12 @@ public static Map<String, String> peerServiceMapping(ConfigProvider configProvid
5754 */
5855 @ Nullable
5956 public static List <String > httpClientRequestCapturedHeaders (ConfigProvider configProvider ) {
60- return Optional .ofNullable (configProvider .getInstrumentationConfig ())
61- .map (instrumentationConfig -> instrumentationConfig .getStructured ("general" ))
62- .map (generalConfig -> generalConfig .getStructured ("http" ))
63- .map (httpConfig -> httpConfig .getStructured ("client" ))
64- .map (clientConfig -> clientConfig .getScalarList ("request_captured_headers" , String .class ))
65- .filter (list -> !list .isEmpty ())
66- .orElse (null );
57+ return getOrNull (
58+ configProvider ,
59+ config -> config .getScalarList ("request_captured_headers" , String .class ),
60+ "general" ,
61+ "http" ,
62+ "client" );
6763 }
6864
6965 /**
@@ -74,13 +70,12 @@ public static List<String> httpClientRequestCapturedHeaders(ConfigProvider confi
7470 */
7571 @ Nullable
7672 public static List <String > httpClientResponseCapturedHeaders (ConfigProvider configProvider ) {
77- return Optional .ofNullable (configProvider .getInstrumentationConfig ())
78- .map (instrumentationConfig -> instrumentationConfig .getStructured ("general" ))
79- .map (generalConfig -> generalConfig .getStructured ("http" ))
80- .map (httpConfig -> httpConfig .getStructured ("client" ))
81- .map (clientConfig -> clientConfig .getScalarList ("response_captured_headers" , String .class ))
82- .filter (list -> !list .isEmpty ())
83- .orElse (null );
73+ return getOrNull (
74+ configProvider ,
75+ config -> config .getScalarList ("response_captured_headers" , String .class ),
76+ "general" ,
77+ "http" ,
78+ "client" );
8479 }
8580
8681 /**
@@ -91,13 +86,12 @@ public static List<String> httpClientResponseCapturedHeaders(ConfigProvider conf
9186 */
9287 @ Nullable
9388 public static List <String > httpServerRequestCapturedHeaders (ConfigProvider configProvider ) {
94- return Optional .ofNullable (configProvider .getInstrumentationConfig ())
95- .map (instrumentationConfig -> instrumentationConfig .getStructured ("general" ))
96- .map (generalConfig -> generalConfig .getStructured ("http" ))
97- .map (httpConfig -> httpConfig .getStructured ("server" ))
98- .map (clientConfig -> clientConfig .getScalarList ("request_captured_headers" , String .class ))
99- .filter (list -> !list .isEmpty ())
100- .orElse (null );
89+ return getOrNull (
90+ configProvider ,
91+ config -> config .getScalarList ("request_captured_headers" , String .class ),
92+ "general" ,
93+ "http" ,
94+ "server" );
10195 }
10296
10397 /**
@@ -108,13 +102,12 @@ public static List<String> httpServerRequestCapturedHeaders(ConfigProvider confi
108102 */
109103 @ Nullable
110104 public static List <String > httpSeverResponseCapturedHeaders (ConfigProvider configProvider ) {
111- return Optional .ofNullable (configProvider .getInstrumentationConfig ())
112- .map (instrumentationConfig -> instrumentationConfig .getStructured ("general" ))
113- .map (generalConfig -> generalConfig .getStructured ("http" ))
114- .map (httpConfig -> httpConfig .getStructured ("server" ))
115- .map (clientConfig -> clientConfig .getScalarList ("response_captured_headers" , String .class ))
116- .filter (list -> !list .isEmpty ())
117- .orElse (null );
105+ return getOrNull (
106+ configProvider ,
107+ config -> config .getScalarList ("response_captured_headers" , String .class ),
108+ "general" ,
109+ "http" ,
110+ "server" );
118111 }
119112
120113 /**
@@ -125,10 +118,25 @@ public static List<String> httpSeverResponseCapturedHeaders(ConfigProvider confi
125118 @ Nullable
126119 public static DeclarativeConfigProperties javaInstrumentationConfig (
127120 ConfigProvider configProvider , String instrumentationName ) {
128- return Optional .ofNullable (configProvider .getInstrumentationConfig ())
129- .map (instrumentationConfig -> instrumentationConfig .getStructured ("java" ))
130- .map (generalConfig -> generalConfig .getStructured (instrumentationName ))
131- .orElse (null );
121+ return getOrNull (configProvider , config -> config .getStructured (instrumentationName ), "java" );
122+ }
123+
124+ @ Nullable
125+ private static <T > T getOrNull (
126+ ConfigProvider configProvider ,
127+ Function <DeclarativeConfigProperties , T > accessor ,
128+ String ... segments ) {
129+ DeclarativeConfigProperties config = configProvider .getInstrumentationConfig ();
130+ if (config == null ) {
131+ return null ;
132+ }
133+ for (String segment : segments ) {
134+ config = config .getStructured (segment );
135+ if (config == null ) {
136+ return null ;
137+ }
138+ }
139+ return accessor .apply (config );
132140 }
133141
134142 private InstrumentationConfigUtil () {}
0 commit comments