Skip to content

Commit a9ecc0d

Browse files
author
Kapil Rastogi
authored
Adding B3 compatible hexchar id generator (#114)
* Adding B3 compatible hexchar id generator * Handling 0 while generating random longs * Handling 0 while generating random longs * Using ThreadLocalRandom to make it thread safe * Using ThreadLocalRandom to make it thread safe * Legacy method generate to not return null
1 parent 2fe1bf0 commit a9ecc0d

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2019 Expedia, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
package com.expedia.www.haystack.client.idgenerators;
18+
19+
import java.util.concurrent.ThreadLocalRandom;
20+
21+
/**
22+
* Generates random and unique B3 compatible hexchar ids.
23+
* Note that traceId will be in 128 bit while spanId and parentSpanId will be 64 bit.
24+
*/
25+
public class HexcharIdGenerator implements IdGenerator {
26+
27+
@Override
28+
public String generateTraceId() {
29+
return String.format("%016X", nextRandomLong()).concat(String.format("%016X", nextRandomLong()));
30+
}
31+
32+
@Override
33+
public String generateSpanId() {
34+
return String.format("%016X", nextRandomLong());
35+
}
36+
37+
@Override
38+
public Object generate() {
39+
return String.format("%016X", nextRandomLong()).concat(String.format("%016X", nextRandomLong()));
40+
}
41+
42+
/**
43+
* Generates a new 64-bit id, taking care to dodge zero which can be confused with absent
44+
*/
45+
private long nextRandomLong() {
46+
long nextId = ThreadLocalRandom.current().nextLong(Long.MAX_VALUE);
47+
while (nextId == 0L) {
48+
nextId = ThreadLocalRandom.current().nextLong(Long.MAX_VALUE);
49+
}
50+
return nextId;
51+
}
52+
}

core/src/test/java/com/expedia/www/haystack/client/TracerBuildTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.expedia.www.haystack.client;
1818

1919
import com.expedia.www.haystack.client.dispatchers.NoopDispatcher;
20+
import com.expedia.www.haystack.client.idgenerators.HexcharIdGenerator;
2021
import com.expedia.www.haystack.client.idgenerators.IdGenerator;
2122
import com.expedia.www.haystack.client.idgenerators.LongIdGenerator;
2223
import com.expedia.www.haystack.client.idgenerators.RandomUUIDGenerator;
@@ -64,5 +65,13 @@ public void testTracerBuildUUIDv4IdGenerator(){
6465
Assert.assertNotNull(tracer);
6566
}
6667

68+
@Test
69+
public void testTracerBuildHexcharIdGenerator(){
70+
IdGenerator idGenerator = new HexcharIdGenerator();
71+
Tracer.Builder tracerBuild = new Tracer.Builder(new NoopMetricsRegistry(), "TestTracer", new NoopDispatcher());
72+
tracerBuild.withIdGenerator(idGenerator);
73+
Tracer tracer = tracerBuild.build();
74+
Assert.assertNotNull(tracer);
75+
}
6776

6877
}

0 commit comments

Comments
 (0)