Skip to content

Commit d3d2b16

Browse files
committed
Fix TraceState key validation limits to match W3C spec
1 parent 64acfd0 commit d3d2b16

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

api/all/src/main/java/io/opentelemetry/api/trace/ArrayBasedTraceStateBuilder.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
final class ArrayBasedTraceStateBuilder implements TraceStateBuilder {
1616

17-
private static final int MAX_VENDOR_ID_SIZE = 13;
17+
private static final int MAX_VENDOR_ID_SIZE = 14;
1818

1919
// Needs to be in this class to avoid initialization deadlock because super class depends on
2020
// subclass (the auto-value generate class).
@@ -24,7 +24,7 @@ final class ArrayBasedTraceStateBuilder implements TraceStateBuilder {
2424
private static final int MAX_ENTRIES = 32;
2525
private static final int KEY_MAX_SIZE = 256;
2626
private static final int VALUE_MAX_SIZE = 256;
27-
private static final int MAX_TENANT_ID_SIZE = 240;
27+
private static final int MAX_TENANT_ID_SIZE = 241;
2828

2929
// Later calls to put must be at the front of trace state. We append to the list and then reverse
3030
// when finished.
@@ -157,11 +157,11 @@ private static boolean isKeyValid(@Nullable String key) {
157157
return false;
158158
}
159159
isMultiTenantVendorKey = true;
160-
// tenant id (the part to the left of the '@' sign) must be 240 characters or less
160+
// tenant id (the part to the left of the '@' sign) must be 241 characters or less
161161
if (i > MAX_TENANT_ID_SIZE) {
162162
return false;
163163
}
164-
// vendor id (the part to the right of the '@' sign) must be 1-13 characters long
164+
// vendor id (the part to the right of the '@' sign) must be 1-14 characters long
165165
int remainingKeyChars = key.length() - i - 1;
166166
if (remainingKeyChars > MAX_VENDOR_ID_SIZE || remainingKeyChars == 0) {
167167
return false;

api/all/src/test/java/io/opentelemetry/api/trace/TraceStateTest.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,22 +123,39 @@ void testValidAtSignVendorNamePrefix() {
123123
}
124124

125125
@Test
126-
void testVendorIdLongerThan13Characters() {
127-
assertThat(TraceState.builder().put("1@nrabcdefghijkl", FIRST_VALUE).build())
126+
void testVendorIdWith14Characters() {
127+
String key = "1@nrabcdefghijkl";
128+
assertThat(TraceState.builder().put(key, FIRST_VALUE).build().get(key)).isEqualTo(FIRST_VALUE);
129+
}
130+
131+
@Test
132+
void testVendorIdLongerThan14Characters() {
133+
assertThat(TraceState.builder().put("1@nrabcdefghijklm", FIRST_VALUE).build())
128134
.isEqualTo(TraceState.getDefault());
129135
}
130136

131137
@Test
132-
void testVendorIdLongerThan13Characters_longTenantId() {
133-
assertThat(TraceState.builder().put("12345678901234567890@nrabcdefghijkl", FIRST_VALUE).build())
138+
void testVendorIdLongerThan14Characters_longTenantId() {
139+
assertThat(
140+
TraceState.builder().put("12345678901234567890@nrabcdefghijklm", FIRST_VALUE).build())
134141
.isEqualTo(TraceState.getDefault());
135142
}
136143

137144
@Test
138-
void tenantIdLongerThan240Characters() {
145+
void tenantIdWith241Characters() {
139146
char[] chars = new char[241];
140147
Arrays.fill(chars, 'a');
141148
String tenantId = new String(chars);
149+
assertThat(
150+
TraceState.builder().put(tenantId + "@nr", FIRST_VALUE).build().get(tenantId + "@nr"))
151+
.isEqualTo(FIRST_VALUE);
152+
}
153+
154+
@Test
155+
void tenantIdLongerThan241Characters() {
156+
char[] chars = new char[242];
157+
Arrays.fill(chars, 'a');
158+
String tenantId = new String(chars);
142159
assertThat(TraceState.builder().put(tenantId + "@nr", FIRST_VALUE).build())
143160
.isEqualTo(TraceState.getDefault());
144161
}

0 commit comments

Comments
 (0)