Skip to content

Commit 38486fe

Browse files
committed
PR feedback
1 parent 6844a04 commit 38486fe

3 files changed

Lines changed: 43 additions & 17 deletions

File tree

api/all/src/main/java/io/opentelemetry/api/baggage/propagation/Parser.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ private enum State {
4747

4848
int parseInto(BaggageBuilder baggageBuilder) {
4949
for (int i = 0, n = baggageHeader.length(); i < n; i++) {
50+
if (entriesAdded >= maxEntries) {
51+
break;
52+
}
5053
char current = baggageHeader.charAt(i);
5154

5255
if (skipToNext) {

api/all/src/main/java/io/opentelemetry/api/baggage/propagation/W3CBaggagePropagator.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,19 +155,30 @@ private static <C> Context extractMulti(
155155
continue;
156156
}
157157

158+
int remainingBytes = MAX_BAGGAGE_BYTES - totalBytes;
158159
totalBytes += header.length();
159-
if (totalBytes > MAX_BAGGAGE_BYTES || totalEntries >= MAX_BAGGAGE_ENTRIES) {
160+
161+
if (totalEntries >= MAX_BAGGAGE_ENTRIES) {
160162
LOGGER.fine("Baggage header exceeded W3C limits, dropping remaining entries");
161163
break;
162164
}
163165

166+
String headerToParse =
167+
totalBytes > MAX_BAGGAGE_BYTES ? header.substring(0, remainingBytes) : header;
168+
164169
try {
165-
int added = extractEntries(header, baggageBuilder, MAX_BAGGAGE_ENTRIES - totalEntries);
170+
int added =
171+
extractEntries(headerToParse, baggageBuilder, MAX_BAGGAGE_ENTRIES - totalEntries);
166172
extracted = true;
167173
totalEntries += added;
168174
} catch (RuntimeException expected) {
169175
// invalid baggage header, continue
170176
}
177+
178+
if (totalBytes > MAX_BAGGAGE_BYTES) {
179+
LOGGER.fine("Baggage header exceeded W3C limits, dropping remaining entries");
180+
break;
181+
}
171182
}
172183

173184
return extracted ? context.with(baggageBuilder.build()) : context;

api/all/src/test/java/io/opentelemetry/api/baggage/propagation/W3CBaggagePropagatorTest.java

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -649,21 +649,32 @@ private static String baggageHeader(int start, int count) {
649649
@Test
650650
void extract_limit_maxBytes_exceedsLimit() {
651651
W3CBaggagePropagator propagator = W3CBaggagePropagator.getInstance();
652-
// Single header over 8192 bytes — should not be extracted
653-
char[] chars = new char[8192];
654-
Arrays.fill(chars, 'v');
655-
String header = "k=" + new String(chars);
652+
// Single header over 8192 bytes — truncated to the byte limit; the entry within budget is
653+
// extracted with a truncated value
654+
String header = "k=" + fillChars('v', 8192); // 8194 bytes; truncated to 8192 → k=<8190 v's>
656655
Context result = propagator.extract(Context.root(), ImmutableMap.of("baggage", header), getter);
657-
assertThat(Baggage.fromContext(result)).isEqualTo(Baggage.empty());
656+
assertThat(Baggage.fromContext(result).getEntryValue("k")).isEqualTo(fillChars('v', 8190));
657+
}
658+
659+
@Test
660+
void extract_limit_maxBytes_partialHeader() {
661+
W3CBaggagePropagator propagator = W3CBaggagePropagator.getInstance();
662+
// A header where the first entry is complete within the byte budget but the second entry's
663+
// key is cut off by the truncation — only the first entry is extracted.
664+
// "k1=" (3) + 8186 'v's + "," (1) + "k2=v2" (5) = 8195 bytes;
665+
// truncated to 8192 → "k1=<8186 v's>,k2" (k2's "=" is beyond the budget)
666+
String header = "k1=" + fillChars('v', 8186) + ",k2=v2";
667+
Context result = propagator.extract(Context.root(), ImmutableMap.of("baggage", header), getter);
668+
Baggage baggage = Baggage.fromContext(result);
669+
assertThat(baggage.getEntryValue("k1")).isEqualTo(fillChars('v', 8186));
670+
assertThat(baggage.getEntryValue("k2")).isNull();
658671
}
659672

660673
@Test
661674
void extract_limit_maxBytes_acrossMultipleHeaders() {
662675
W3CBaggagePropagator propagator = W3CBaggagePropagator.getInstance();
663676
// First header just under 8192 bytes is extracted; second header pushes total over the limit
664-
char[] almostMaxChars = new char[8189];
665-
Arrays.fill(almostMaxChars, 'v');
666-
String almostMax = "k=" + new String(almostMaxChars); // "k=vvv..."
677+
String almostMax = "k=" + fillChars('v', 8189); // "k=vvv..."
667678
String second = "k2=v2";
668679
Context result =
669680
propagator.extract(
@@ -690,10 +701,7 @@ void inject_limit_maxEntries() {
690701
void inject_limit_maxBytes() {
691702
W3CBaggagePropagator propagator = W3CBaggagePropagator.getInstance();
692703
// One entry whose encoded form alone exceeds the byte limit — should produce empty header
693-
char[] longValueChars = new char[8192];
694-
Arrays.fill(longValueChars, 'v');
695-
String longValue = new String(longValueChars);
696-
Baggage baggage = Baggage.builder().put("k", longValue).build();
704+
Baggage baggage = Baggage.builder().put("k", fillChars('v', 8192)).build();
697705
Map<String, String> carrier = new HashMap<>();
698706
propagator.inject(Context.root().with(baggage), carrier, Map::put);
699707
assertThat(carrier).doesNotContainKey("baggage");
@@ -703,15 +711,19 @@ void inject_limit_maxBytes() {
703711
void inject_limit_maxBytes_metadata() {
704712
// Value alone fits easily (k=v is 3 bytes), but k=v;{metadata} exceeds 8192 bytes.
705713
// Verifies that metadata length is included in the byte limit check.
706-
char[] metaChars = new char[8190];
707-
Arrays.fill(metaChars, 'x');
708714
Baggage baggage =
709-
Baggage.builder().put("k", "v", BaggageEntryMetadata.create(new String(metaChars))).build();
715+
Baggage.builder().put("k", "v", BaggageEntryMetadata.create(fillChars('x', 8190))).build();
710716
Map<String, String> carrier = new HashMap<>();
711717
W3CBaggagePropagator.getInstance().inject(Context.root().with(baggage), carrier, Map::put);
712718
assertThat(carrier).doesNotContainKey("baggage");
713719
}
714720

721+
private static String fillChars(char c, int count) {
722+
char[] chars = new char[count];
723+
Arrays.fill(chars, c);
724+
return new String(chars);
725+
}
726+
715727
@Test
716728
void toString_Valid() {
717729
assertThat(W3CBaggagePropagator.getInstance().toString()).isEqualTo("W3CBaggagePropagator");

0 commit comments

Comments
 (0)