11package datadog .trace .core .baggage ;
22
3+ import static java .util .Collections .emptyMap ;
4+
35import datadog .context .Context ;
46import datadog .context .propagation .CarrierSetter ;
57import datadog .context .propagation .CarrierVisitor ;
68import datadog .context .propagation .Propagator ;
79import datadog .trace .api .Config ;
8- import datadog .trace .bootstrap .instrumentation .api .BaggageContext ;
9- import datadog .trace .core .util .EscapedData ;
10+ import datadog .trace .bootstrap .instrumentation .api .Baggage ;
1011import datadog .trace .core .util .PercentEscaper ;
12+ import datadog .trace .core .util .PercentEscaper .Escaped ;
1113import java .io .UnsupportedEncodingException ;
1214import java .net .URLDecoder ;
13- import java .util .Collections ;
1415import java .util .HashMap ;
1516import java .util .Map ;
1617import java .util .function .BiConsumer ;
2021
2122@ ParametersAreNonnullByDefault
2223public class BaggagePropagator implements Propagator {
23- private static final Logger log = LoggerFactory .getLogger (BaggagePropagator .class );
24+ private static final Logger LOG = LoggerFactory .getLogger (BaggagePropagator .class );
2425 private static final PercentEscaper UTF_ESCAPER = PercentEscaper .create ();
2526 static final String BAGGAGE_KEY = "baggage" ;
2627 private final Config config ;
@@ -37,13 +38,13 @@ public BaggagePropagator(Config config) {
3738 public BaggagePropagator (boolean injectBaggage , boolean extractBaggage ) {
3839 this .injectBaggage = injectBaggage ;
3940 this .extractBaggage = extractBaggage ;
40- config = Config .get ();
41+ this . config = Config .get ();
4142 }
4243
4344 @ Override
4445 public <C > void inject (Context context , C carrier , CarrierSetter <C > setter ) {
45- int maxItems = config .getTraceBaggageMaxItems ();
46- int maxBytes = config .getTraceBaggageMaxBytes ();
46+ int maxItems = this . config .getTraceBaggageMaxItems ();
47+ int maxBytes = this . config .getTraceBaggageMaxBytes ();
4748 //noinspection ConstantValue
4849 if (!this .injectBaggage
4950 || maxItems == 0
@@ -54,52 +55,52 @@ public <C> void inject(Context context, C carrier, CarrierSetter<C> setter) {
5455 return ;
5556 }
5657
57- BaggageContext baggageContext = BaggageContext .fromContext (context );
58- if (baggageContext == null ) {
59- log .debug ("BaggageContext instance is missing from the following context {}" , context );
58+ Baggage baggage = Baggage .fromContext (context );
59+ if (baggage == null ) {
60+ LOG .debug ("Baggage instance is missing from the following context {}" , context );
6061 return ;
6162 }
6263
63- String baggageHeader = baggageContext . getW3cBaggageHeader ();
64- if (baggageHeader != null ) {
65- setter .set (carrier , BAGGAGE_KEY , baggageHeader );
64+ String headerValue = baggage . getW3cHeader ();
65+ if (headerValue != null ) {
66+ setter .set (carrier , BAGGAGE_KEY , headerValue );
6667 return ;
6768 }
6869
69- int processedBaggage = 0 ;
70+ int processedItems = 0 ;
7071 int currentBytes = 0 ;
7172 StringBuilder baggageText = new StringBuilder ();
72- for (final Map .Entry <String , String > entry : baggageContext .asMap ().entrySet ()) {
73+ for (final Map .Entry <String , String > entry : baggage .asMap ().entrySet ()) {
7374 // if there are already baggage items processed, add and allocate bytes for a comma
7475 int extraBytes = 1 ;
75- if (processedBaggage != 0 ) {
76+ if (processedItems != 0 ) {
7677 baggageText .append (',' );
7778 extraBytes ++;
7879 }
7980
80- EscapedData escapedKey = UTF_ESCAPER .escapeKey (entry .getKey ());
81- EscapedData escapedVal = UTF_ESCAPER .escapeValue (entry .getValue ());
81+ Escaped escapedKey = UTF_ESCAPER .escapeKey (entry .getKey ());
82+ Escaped escapedVal = UTF_ESCAPER .escapeValue (entry .getValue ());
8283
83- baggageText .append (escapedKey .getData () );
84+ baggageText .append (escapedKey .data );
8485 baggageText .append ('=' );
85- baggageText .append (escapedVal .getData () );
86+ baggageText .append (escapedVal .data );
8687
87- processedBaggage ++;
88+ processedItems ++;
8889 // reached the max number of baggage items allowed
89- if (processedBaggage == maxItems ) {
90+ if (processedItems == maxItems ) {
9091 break ;
9192 }
9293 // Drop newest k/v pair if adding it leads to exceeding the limit
93- if (currentBytes + escapedKey .getSize () + escapedVal .getSize () + extraBytes > maxBytes ) {
94+ if (currentBytes + escapedKey .size + escapedVal .size + extraBytes > maxBytes ) {
9495 baggageText .setLength (currentBytes );
9596 break ;
9697 }
97- currentBytes += escapedKey .getSize () + escapedVal .getSize () + extraBytes ;
98+ currentBytes += escapedKey .size + escapedVal .size + extraBytes ;
9899 }
99100
100- String baggageString = baggageText .toString ();
101- baggageContext . setW3cBaggageHeader ( baggageString );
102- setter .set (carrier , BAGGAGE_KEY , baggageString );
101+ headerValue = baggageText .toString ();
102+ baggage . setW3cHeader ( headerValue );
103+ setter .set (carrier , BAGGAGE_KEY , headerValue );
103104 }
104105
105106 @ Override
@@ -108,57 +109,52 @@ public <C> Context extract(Context context, C carrier, CarrierVisitor<C> visitor
108109 if (!this .extractBaggage || context == null || carrier == null || visitor == null ) {
109110 return context ;
110111 }
111- BaggageContextExtractor baggageContextExtractor = new BaggageContextExtractor ();
112- visitor .forEachKeyValue (carrier , baggageContextExtractor );
113- BaggageContext extractedContext = baggageContextExtractor .extractedContext ;
114- if (extractedContext == null ) {
115- return context ;
116- }
117- return extractedContext .storeInto (context );
112+ BaggageExtractor baggageExtractor = new BaggageExtractor ();
113+ visitor .forEachKeyValue (carrier , baggageExtractor );
114+ return baggageExtractor .extracted == null ? context : context .with (baggageExtractor .extracted );
118115 }
119116
120- public static class BaggageContextExtractor implements BiConsumer <String , String > {
121- private BaggageContext extractedContext ;
117+ private static class BaggageExtractor implements BiConsumer <String , String > {
118+ private static final char KEY_VALUE_SEPARATOR = '=' ;
119+ private static final char PAIR_SEPARATOR = ',' ;
120+ private Baggage extracted ;
122121
123- BaggageContextExtractor () {}
122+ private BaggageExtractor () {}
124123
125124 /** URL decode value */
126125 private String decode (final String value ) {
127126 String decoded = value ;
128127 try {
129128 decoded = URLDecoder .decode (value , "UTF-8" );
130129 } catch (final UnsupportedEncodingException | IllegalArgumentException e ) {
131- log .debug ("Failed to decode {}" , value );
130+ LOG .debug ("Failed to decode {}" , value );
132131 }
133132 return decoded ;
134133 }
135134
136135 private Map <String , String > parseBaggageHeaders (String input ) {
137136 Map <String , String > baggage = new HashMap <>();
138- char keyValueSeparator = '=' ;
139- char pairSeparator = ',' ;
140137 int start = 0 ;
141-
142- int pairSeparatorInd = input .indexOf (pairSeparator );
138+ int pairSeparatorInd = input .indexOf (PAIR_SEPARATOR );
143139 pairSeparatorInd = pairSeparatorInd == -1 ? input .length () : pairSeparatorInd ;
144- int kvSeparatorInd = input .indexOf (keyValueSeparator );
140+ int kvSeparatorInd = input .indexOf (KEY_VALUE_SEPARATOR );
145141 while (kvSeparatorInd != -1 ) {
146142 int end = pairSeparatorInd ;
147143 if (kvSeparatorInd > end ) {
148- log .debug (
144+ LOG .debug (
149145 "Dropping baggage headers due to key with no value {}" , input .substring (start , end ));
150- return Collections . emptyMap ();
146+ return emptyMap ();
151147 }
152148 String key = decode (input .substring (start , kvSeparatorInd ).trim ());
153149 String value = decode (input .substring (kvSeparatorInd + 1 , end ).trim ());
154150 if (key .isEmpty () || value .isEmpty ()) {
155- log .debug ("Dropping baggage headers due to empty k/v {}:{}" , key , value );
156- return Collections . emptyMap ();
151+ LOG .debug ("Dropping baggage headers due to empty k/v {}:{}" , key , value );
152+ return emptyMap ();
157153 }
158154 baggage .put (key , value );
159155
160- kvSeparatorInd = input .indexOf (keyValueSeparator , pairSeparatorInd + 1 );
161- pairSeparatorInd = input .indexOf (pairSeparator , pairSeparatorInd + 1 );
156+ kvSeparatorInd = input .indexOf (KEY_VALUE_SEPARATOR , pairSeparatorInd + 1 );
157+ pairSeparatorInd = input .indexOf (PAIR_SEPARATOR , pairSeparatorInd + 1 );
162158 pairSeparatorInd = pairSeparatorInd == -1 ? input .length () : pairSeparatorInd ;
163159 start = end + 1 ;
164160 }
@@ -168,10 +164,10 @@ private Map<String, String> parseBaggageHeaders(String input) {
168164 @ Override
169165 public void accept (String key , String value ) {
170166 // Only process tags that are relevant to baggage
171- if (key != null && key .equalsIgnoreCase (BAGGAGE_KEY )) {
167+ if (BAGGAGE_KEY .equalsIgnoreCase (key )) {
172168 Map <String , String > baggage = parseBaggageHeaders (value );
173169 if (!baggage .isEmpty ()) {
174- extractedContext = BaggageContext .create (baggage , value );
170+ this . extracted = Baggage .create (baggage , value );
175171 }
176172 }
177173 }
0 commit comments