55import static datadog .trace .api .config .GeneralConfig .TRACE_DEBUG ;
66import static datadog .trace .api .config .TraceInstrumentationConfig .LOGS_INJECTION_ENABLED ;
77import static datadog .trace .api .config .TracerConfig .BAGGAGE_MAPPING ;
8- import static datadog .trace .api .config .TracerConfig .HEADER_TAGS ;
8+ import static datadog .trace .api .config .TracerConfig .REQUEST_HEADER_TAGS ;
9+ import static datadog .trace .api .config .TracerConfig .RESPONSE_HEADER_TAGS ;
910import static datadog .trace .api .config .TracerConfig .SERVICE_MAPPING ;
1011import static datadog .trace .api .config .TracerConfig .TRACE_SAMPLE_RATE ;
1112import static datadog .trace .util .CollectionUtils .tryMakeImmutableMap ;
1213
14+ import datadog .trace .util .Strings ;
1315import java .util .Collection ;
1416import java .util .Collections ;
1517import java .util .HashMap ;
1618import java .util .Locale ;
1719import java .util .Map ;
1820import java .util .function .BiFunction ;
21+ import java .util .function .Function ;
1922
2023/** Manages dynamic configuration for a particular {@link Tracer} instance. */
2124public final class DynamicConfig <S extends DynamicConfig .Snapshot > {
25+
26+ static final Function <Map .Entry <String , String >, String > KEY = DynamicConfig ::key ;
27+ static final Function <Map .Entry <String , String >, String > VALUE = DynamicConfig ::value ;
28+ static final Function <Map .Entry <String , String >, String > LOWER_KEY = DynamicConfig ::lowerKey ;
29+ static final Function <Map .Entry <String , String >, String > REQUEST_TAG = DynamicConfig ::requestTag ;
30+ static final Function <Map .Entry <String , String >, String > RESPONSE_TAG =
31+ DynamicConfig ::responseTag ;
32+
2233 BiFunction <Builder , S , S > snapshotFactory ;
2334
2435 S initialSnapshot ;
@@ -68,17 +79,17 @@ public final class Builder {
6879 boolean dataStreamsEnabled ;
6980
7081 Map <String , String > serviceMapping ;
71- Map <String , String > headerTags ;
82+ Map <String , String > requestHeaderTags ;
83+ Map <String , String > responseHeaderTags ;
7284 Map <String , String > baggageMapping ;
7385
74- boolean overrideResponseTags ;
75-
7686 Double traceSampleRate ;
7787
7888 Builder (Snapshot snapshot ) {
7989 if (null == snapshot ) {
8090 this .serviceMapping = Collections .emptyMap ();
81- this .headerTags = Collections .emptyMap ();
91+ this .requestHeaderTags = Collections .emptyMap ();
92+ this .responseHeaderTags = Collections .emptyMap ();
8293 this .baggageMapping = Collections .emptyMap ();
8394 this .logsInjectionEnabled = ConfigDefaults .DEFAULT_LOGS_INJECTION_ENABLED ;
8495 } else {
@@ -89,11 +100,10 @@ public final class Builder {
89100 this .dataStreamsEnabled = snapshot .dataStreamsEnabled ;
90101
91102 this .serviceMapping = snapshot .serviceMapping ;
92- this .headerTags = snapshot .headerTags ;
103+ this .requestHeaderTags = snapshot .requestHeaderTags ;
104+ this .responseHeaderTags = snapshot .responseHeaderTags ;
93105 this .baggageMapping = snapshot .baggageMapping ;
94106
95- this .overrideResponseTags = snapshot .overrideResponseTags ;
96-
97107 this .traceSampleRate = snapshot .traceSampleRate ;
98108 }
99109 }
@@ -126,8 +136,8 @@ public Builder setHeaderTags(Map<String, String> headerTags) {
126136 if (Config .get ().getRequestHeaderTags ().equals (headerTags )
127137 && !Config .get ().getResponseHeaderTags ().equals (headerTags )) {
128138 // using static config; don't override separate static config for response header tags
129- this .headerTags = cleanMapping ( headerTags . entrySet (), true , true );
130- this .overrideResponseTags = false ;
139+ this .requestHeaderTags = Config . get (). getRequestHeaderTags ( );
140+ this .responseHeaderTags = Config . get (). getResponseHeaderTags () ;
131141 return this ;
132142 } else {
133143 return setHeaderTags (headerTags .entrySet ());
@@ -140,19 +150,19 @@ public Builder setBaggageMapping(Map<String, String> baggageMapping) {
140150
141151 public Builder setServiceMapping (
142152 Collection <? extends Map .Entry <String , String >> serviceMapping ) {
143- this .serviceMapping = cleanMapping (serviceMapping , false , false );
153+ this .serviceMapping = cleanMapping (serviceMapping , KEY , VALUE );
144154 return this ;
145155 }
146156
147157 public Builder setHeaderTags (Collection <? extends Map .Entry <String , String >> headerTags ) {
148- this .headerTags = cleanMapping (headerTags , true , true );
149- this .overrideResponseTags = true ;
158+ this .requestHeaderTags = cleanMapping (headerTags , LOWER_KEY , REQUEST_TAG );
159+ this .responseHeaderTags = cleanMapping ( headerTags , LOWER_KEY , RESPONSE_TAG ) ;
150160 return this ;
151161 }
152162
153163 public Builder setBaggageMapping (
154164 Collection <? extends Map .Entry <String , String >> baggageMapping ) {
155- this .baggageMapping = cleanMapping (baggageMapping , true , false );
165+ this .baggageMapping = cleanMapping (baggageMapping , LOWER_KEY , VALUE );
156166 return this ;
157167 }
158168
@@ -163,19 +173,11 @@ public Builder setTraceSampleRate(Double traceSampleRate) {
163173
164174 private Map <String , String > cleanMapping (
165175 Collection <? extends Map .Entry <String , String >> mapping ,
166- boolean lowerCaseKeys ,
167- boolean lowerCaseValues ) {
176+ Function < Map . Entry < String , String >, String > keyMapper ,
177+ Function < Map . Entry < String , String >, String > valueMapper ) {
168178 final Map <String , String > cleanedMapping = new HashMap <>(mapping .size () * 4 / 3 );
169179 for (Map .Entry <String , String > association : mapping ) {
170- String key = association .getKey ().trim ();
171- if (lowerCaseKeys ) {
172- key = key .toLowerCase (Locale .ROOT );
173- }
174- String value = association .getValue ().trim ();
175- if (lowerCaseValues ) {
176- value = value .toLowerCase (Locale .ROOT );
177- }
178- cleanedMapping .put (key , value );
180+ cleanedMapping .put (keyMapper .apply (association ), valueMapper .apply (association ));
179181 }
180182 return tryMakeImmutableMap (cleanedMapping );
181183 }
@@ -195,6 +197,36 @@ public DynamicConfig<S> apply() {
195197 }
196198 }
197199
200+ static String key (Map .Entry <String , String > association ) {
201+ return Strings .trim (association .getKey ());
202+ }
203+
204+ static String value (Map .Entry <String , String > association ) {
205+ return Strings .trim (association .getValue ());
206+ }
207+
208+ static String lowerKey (Map .Entry <String , String > association ) {
209+ return key (association ).toLowerCase (Locale .ROOT );
210+ }
211+
212+ static String requestTag (Map .Entry <String , String > association ) {
213+ String requestTag = value (association );
214+ if (requestTag .isEmpty ()) {
215+ // normalization is only applied when generating default tag names; see ConfigConverter
216+ requestTag = "http.request.headers." + Strings .normalizedHeaderTag (association .getKey ());
217+ }
218+ return requestTag ;
219+ }
220+
221+ static String responseTag (Map .Entry <String , String > association ) {
222+ String responseTag = value (association );
223+ if (responseTag .isEmpty ()) {
224+ // normalization is only applied when generating default tag names; see ConfigConverter
225+ responseTag = "http.response.headers." + Strings .normalizedHeaderTag (association .getKey ());
226+ }
227+ return responseTag ;
228+ }
229+
198230 static void reportConfigChange (Snapshot newSnapshot ) {
199231 Map <String , Object > update = new HashMap <>();
200232
@@ -204,7 +236,8 @@ static void reportConfigChange(Snapshot newSnapshot) {
204236 update .put (DATA_STREAMS_ENABLED , newSnapshot .dataStreamsEnabled );
205237
206238 update .put (SERVICE_MAPPING , newSnapshot .serviceMapping );
207- update .put (HEADER_TAGS , newSnapshot .headerTags );
239+ update .put (REQUEST_HEADER_TAGS , newSnapshot .requestHeaderTags );
240+ update .put (RESPONSE_HEADER_TAGS , newSnapshot .responseHeaderTags );
208241 update .put (BAGGAGE_MAPPING , newSnapshot .baggageMapping );
209242
210243 maybePut (update , TRACE_SAMPLE_RATE , newSnapshot .traceSampleRate );
@@ -227,11 +260,10 @@ public static class Snapshot implements TraceConfig {
227260 final boolean dataStreamsEnabled ;
228261
229262 final Map <String , String > serviceMapping ;
230- final Map <String , String > headerTags ;
263+ final Map <String , String > requestHeaderTags ;
264+ final Map <String , String > responseHeaderTags ;
231265 final Map <String , String > baggageMapping ;
232266
233- final boolean overrideResponseTags ;
234-
235267 final Double traceSampleRate ;
236268
237269 protected Snapshot (DynamicConfig <?>.Builder builder , Snapshot oldSnapshot ) {
@@ -242,11 +274,10 @@ protected Snapshot(DynamicConfig<?>.Builder builder, Snapshot oldSnapshot) {
242274 this .dataStreamsEnabled = builder .dataStreamsEnabled ;
243275
244276 this .serviceMapping = builder .serviceMapping ;
245- this .headerTags = builder .headerTags ;
277+ this .requestHeaderTags = builder .requestHeaderTags ;
278+ this .responseHeaderTags = builder .responseHeaderTags ;
246279 this .baggageMapping = builder .baggageMapping ;
247280
248- this .overrideResponseTags = builder .overrideResponseTags ;
249-
250281 this .traceSampleRate = builder .traceSampleRate ;
251282 }
252283
@@ -276,13 +307,13 @@ public Map<String, String> getServiceMapping() {
276307 }
277308
278309 @ Override
279- public Map <String , String > getHeaderTags () {
280- return headerTags ;
310+ public Map <String , String > getRequestHeaderTags () {
311+ return requestHeaderTags ;
281312 }
282313
283314 @ Override
284315 public Map <String , String > getResponseHeaderTags () {
285- return overrideResponseTags ? headerTags : Config . get (). getResponseHeaderTags () ;
316+ return responseHeaderTags ;
286317 }
287318
288319 @ Override
0 commit comments