55
66package io .opentelemetry .javaagent .tooling .instrumentation .http ;
77
8+ import static io .opentelemetry .api .incubator .config .DeclarativeConfigProperties .empty ;
9+ import static java .util .Collections .emptyList ;
810import static java .util .logging .Level .WARNING ;
911
1012import com .google .auto .service .AutoService ;
13+ import io .opentelemetry .api .incubator .config .DeclarativeConfigProperties ;
14+ import io .opentelemetry .instrumentation .api .incubator .config .internal .InstrumentationConfig ;
15+ import io .opentelemetry .javaagent .bootstrap .internal .AgentInstrumentationConfig ;
1116import io .opentelemetry .javaagent .tooling .BeforeAgentListener ;
1217import io .opentelemetry .sdk .autoconfigure .AutoConfiguredOpenTelemetrySdk ;
13- import io .opentelemetry .sdk .autoconfigure .internal .AutoConfigureUtil ;
14- import io .opentelemetry .sdk .autoconfigure .spi .ConfigProperties ;
1518import java .util .logging .Logger ;
1619import java .util .regex .Pattern ;
1720import java .util .regex .PatternSyntaxException ;
@@ -23,19 +26,36 @@ public final class RegexUrlTemplateCustomizerInitializer implements BeforeAgentL
2326
2427 @ Override
2528 public void beforeAgent (AutoConfiguredOpenTelemetrySdk autoConfiguredOpenTelemetrySdk ) {
26- ConfigProperties config = AutoConfigureUtil .getConfig (autoConfiguredOpenTelemetrySdk );
27- if (config == null ) {
28- return ;
29- }
29+ InstrumentationConfig config = AgentInstrumentationConfig .get ();
3030 // url template is emitted only when http client experimental telemetry is enabled
3131 boolean urlTemplateEnabled =
3232 config .getBoolean ("otel.instrumentation.http.client.emit-experimental-telemetry" , false );
3333 if (!urlTemplateEnabled ) {
3434 return ;
3535 }
36- String rules = config .getString ("otel.instrumentation.http.client.url-template-rules" );
37- if (rules != null && !rules .isEmpty ()) {
38- parse (rules );
36+ if (config .isDeclarative ()) {
37+ DeclarativeConfigProperties configuration =
38+ config .getDeclarativeConfig ("http" ).getStructured ("client" , empty ());
39+ configuration
40+ .getStructuredList ("url_template_rules" , emptyList ())
41+ .forEach (
42+ rule -> {
43+ String patternString = rule .getString ("pattern" , "" );
44+ String template = rule .getString ("template" , "" );
45+ if (patternString .isEmpty () || template .isEmpty ()) {
46+ return ;
47+ }
48+ boolean override = rule .getBoolean ("override" , false );
49+ Pattern pattern = toPattern (patternString );
50+ if (pattern != null ) {
51+ UrlTemplateRules .addRule (pattern , template , override );
52+ }
53+ });
54+ } else {
55+ String rules = config .getString ("otel.instrumentation.http.client.url-template-rules" );
56+ if (rules != null && !rules .isEmpty ()) {
57+ parse (rules );
58+ }
3959 }
4060 }
4161
@@ -54,28 +74,33 @@ static void parse(String rules) {
5474 continue ;
5575 }
5676
57- Pattern pattern ;
58- try {
59- String patternString = parts [0 ].trim ();
60- // ensure that pattern matches the whole url
61- if (!patternString .startsWith ("^" )) {
62- patternString = "^" + patternString ;
63- }
64- if (!patternString .endsWith ("$" )) {
65- patternString = patternString + "$" ;
66- }
67- pattern = Pattern .compile (patternString );
68- } catch (PatternSyntaxException exception ) {
69- logger .log (
70- WARNING ,
71- "Invalid pattern in http client url template customization rule \" "
72- + parts [0 ].trim ()
73- + "\" ." ,
74- exception );
77+ Pattern pattern = toPattern (parts [0 ].trim ());
78+ if (pattern == null ) {
7579 continue ;
7680 }
7781 UrlTemplateRules .addRule (
7882 pattern , parts [1 ].trim (), parts .length == 3 && Boolean .parseBoolean (parts [2 ].trim ()));
7983 }
8084 }
85+
86+ private static Pattern toPattern (String patternString ) {
87+ try {
88+ // ensure that pattern matches the whole url
89+ if (!patternString .startsWith ("^" )) {
90+ patternString = "^" + patternString ;
91+ }
92+ if (!patternString .endsWith ("$" )) {
93+ patternString = patternString + "$" ;
94+ }
95+ return Pattern .compile (patternString );
96+ } catch (PatternSyntaxException exception ) {
97+ logger .log (
98+ WARNING ,
99+ "Invalid pattern in http client url template customization rule \" "
100+ + patternString
101+ + "\" ." ,
102+ exception );
103+ return null ;
104+ }
105+ }
81106}
0 commit comments