-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tolerate reads of 128 bit X-B3-TraceId (#239)
The first step of transitioning to 128bit `X-B3-TraceId` is tolerantly reading 32 character long ids by throwing away the high bits (any characters left of 16 characters). This allows the tracing system to more flexibly introduce 128bit trace id support in the future. Ex. when `X-B3-TraceId: 463ac35c9f6413ad48485a3953bb6124` is received, parse the lower 64 bits (right most 16 characters ex48485a3953bb6124) as the trace id. See openzipkin/b3-propagation#6
- Loading branch information
Showing
4 changed files
with
123 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 69 additions & 63 deletions
132
brave-core/src/test/java/com/github/kristofa/brave/IdConversionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,80 @@ | ||
package com.github.kristofa.brave; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
public class IdConversionTest { | ||
|
||
@Test | ||
public void testPositiveId() { | ||
final long longId = 8828218016717761634L; | ||
// This id was generated using the zipkin code. | ||
final String expectedId = "7a842183262a6c62"; | ||
assertEquals(expectedId, IdConversion.convertToString(longId)); | ||
assertEquals(longId, IdConversion.convertToLong(expectedId)); | ||
} | ||
|
||
|
||
@Test | ||
public void testNegativeId() { | ||
final long longId = -4667777584646200191L; | ||
// This id was generated using the zipkin code. | ||
final String expectedId = "bf38b90488a1e481"; | ||
assertEquals(expectedId, IdConversion.convertToString(longId)); | ||
assertEquals(longId, IdConversion.convertToLong(expectedId)); | ||
} | ||
|
||
@Test | ||
public void testZeroId() { | ||
final long longId = 0; | ||
// Zipkin prepends 0's but conversion without those zeros also works. | ||
final String expectedId = "0"; | ||
assertEquals(expectedId, IdConversion.convertToString(longId)); | ||
assertEquals(longId, IdConversion.convertToLong(expectedId)); | ||
} | ||
|
||
@Test | ||
public void testMinValueId() { | ||
final long longId = Long.MIN_VALUE; | ||
// This id was generated using the zipkin code. | ||
final String expectedId = "8000000000000000"; | ||
assertEquals(expectedId, IdConversion.convertToString(longId)); | ||
assertEquals(longId, IdConversion.convertToLong(expectedId)); | ||
} | ||
|
||
@Test | ||
public void testMaxValueId() { | ||
final long longId = Long.MAX_VALUE; | ||
// This id was generated using the zipkin code. | ||
final String expectedId = "7fffffffffffffff"; | ||
assertEquals(expectedId, IdConversion.convertToString(longId)); | ||
assertEquals(longId, IdConversion.convertToLong(expectedId)); | ||
} | ||
@Test | ||
public void testPositiveId() { | ||
final long longId = 8828218016717761634L; | ||
// This id was generated using the zipkin code. | ||
final String expectedId = "7a842183262a6c62"; | ||
assertEquals(expectedId, IdConversion.convertToString(longId)); | ||
assertEquals(longId, IdConversion.convertToLong(expectedId)); | ||
} | ||
|
||
@Test | ||
public void testNegativeId() { | ||
final long longId = -4667777584646200191L; | ||
// This id was generated using the zipkin code. | ||
final String expectedId = "bf38b90488a1e481"; | ||
assertEquals(expectedId, IdConversion.convertToString(longId)); | ||
assertEquals(longId, IdConversion.convertToLong(expectedId)); | ||
} | ||
|
||
@Test | ||
public void testZeroId() { | ||
final long longId = 0; | ||
// Zipkin prepends 0's but conversion without those zeros also works. | ||
final String expectedId = "0"; | ||
assertEquals(expectedId, IdConversion.convertToString(longId)); | ||
assertEquals(longId, IdConversion.convertToLong(expectedId)); | ||
} | ||
|
||
@Test | ||
public void testMinValueId() { | ||
final long longId = Long.MIN_VALUE; | ||
// This id was generated using the zipkin code. | ||
final String expectedId = "8000000000000000"; | ||
assertEquals(expectedId, IdConversion.convertToString(longId)); | ||
assertEquals(longId, IdConversion.convertToLong(expectedId)); | ||
} | ||
|
||
@Test | ||
public void testMaxValueId() { | ||
final long longId = Long.MAX_VALUE; | ||
// This id was generated using the zipkin code. | ||
final String expectedId = "7fffffffffffffff"; | ||
assertEquals(expectedId, IdConversion.convertToString(longId)); | ||
assertEquals(longId, IdConversion.convertToLong(expectedId)); | ||
} | ||
|
||
@Test(expected = NumberFormatException.class) | ||
public void testIdTooLong() { | ||
IdConversion.convertToLong("7ffffffffffffffff7ffffffffffffffff"); | ||
} | ||
|
||
@Test(expected = NumberFormatException.class) | ||
public void testIdTooLong() { | ||
IdConversion.convertToLong("7ffffffffffffffff"); | ||
} | ||
@Test(expected = NumberFormatException.class) | ||
public void testIdEmpty() { | ||
IdConversion.convertToLong(""); | ||
} | ||
|
||
@Test(expected = NumberFormatException.class) | ||
public void testIdEmpty() { | ||
IdConversion.convertToLong(""); | ||
} | ||
@Test(expected = NumberFormatException.class) | ||
public void testIdShouldntHavePrefix() { | ||
IdConversion.convertToLong("0x7fffffffffffffff7fffffffffffffff"); | ||
} | ||
|
||
@Test(expected = NumberFormatException.class) | ||
public void testIdShouldntHavePrefix() { | ||
IdConversion.convertToLong("0x7fffffffffffffff"); | ||
} | ||
@Test(expected = NumberFormatException.class) | ||
public void testIdShouldntBeUppercase() { | ||
IdConversion.convertToLong("7FFFFFFFFFFFFFFF"); | ||
} | ||
|
||
@Test(expected = NumberFormatException.class) | ||
public void testIdShouldntBeUppercase() { | ||
IdConversion.convertToLong("7FFFFFFFFFFFFFFF"); | ||
} | ||
@Test | ||
public void lowerHexToUnsignedLong_downgrades128bitIdsByDroppingHighBits() { | ||
assertThat(IdConversion.convertToLong("463ac35c9f6413ad48485a3953bb6124")) | ||
.isEqualTo(IdConversion.convertToLong("48485a3953bb6124")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters