Skip to content

Commit 59874e7

Browse files
committed
Restore "generate" method and add defaults for generateSpanId and generateTraceId
1 parent 46f30b8 commit 59874e7

4 files changed

Lines changed: 47 additions & 18 deletions

File tree

core/src/main/java/com/expedia/www/haystack/client/idgenerators/IdGenerator.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,25 @@
1717
package com.expedia.www.haystack.client.idgenerators;
1818

1919
public interface IdGenerator {
20-
Object generateTraceId();
2120

22-
Object generateSpanId();
21+
/**
22+
* Generates a unique id
23+
* @deprecated This method was deprecated in 0.3.1. Use {@link #generateTraceId()} and {@link #generateSpanId()} instead
24+
*/
25+
@Deprecated
26+
Object generate();
27+
28+
/**
29+
* Generates a random unique identifier for a trace.
30+
*/
31+
default Object generateTraceId() {
32+
return generate();
33+
}
34+
35+
/**
36+
* Generates a random identifier for a span. It should be unique within a trace.
37+
*/
38+
default Object generateSpanId() {
39+
return generate();
40+
}
2341
}

core/src/main/java/com/expedia/www/haystack/client/idgenerators/LongIdGenerator.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,15 @@
1616
*/
1717
package com.expedia.www.haystack.client.idgenerators;
1818

19-
20-
import com.expedia.www.haystack.client.idgenerators.IdGenerator;
21-
2219
import java.util.concurrent.ThreadLocalRandom;
2320

21+
/**
22+
* Generates random and unique Longs as ids for Traces and Spans.
23+
*/
2424
public class LongIdGenerator implements IdGenerator {
2525

2626
@Override
27-
public Long generateTraceId() {
28-
return ThreadLocalRandom.current().nextLong(Long.MAX_VALUE);
29-
}
30-
31-
@Override
32-
public Long generateSpanId() {
27+
public Long generate() {
3328
return ThreadLocalRandom.current().nextLong(Long.MAX_VALUE);
3429
}
3530
}

core/src/main/java/com/expedia/www/haystack/client/idgenerators/RandomUUIDGenerator.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,20 @@
1919

2020
import java.util.UUID;
2121

22+
/**
23+
* Generates UUIDs as ids for Traces and Spans.
24+
* <p/>
25+
* Given that the span only needs to be unique within a trace, the UUID for spans will only contain the least
26+
* significant bits and will be left padded with zeroes.
27+
* <p/>
28+
* Consider using the TimeBasedUUIDGenerator which is more performant.
29+
* <p/>
30+
* @see com.expedia.www.haystack.client.idgenerators.TimeBasedUUIDGenerator
31+
*/
2232
public class RandomUUIDGenerator implements IdGenerator {
2333

2434
@Override
25-
public UUID generateTraceId() {
35+
public UUID generate() {
2636
return UUID.randomUUID();
2737
}
2838

core/src/main/java/com/expedia/www/haystack/client/idgenerators/TimeBasedUUIDGenerator.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,25 @@
1717
package com.expedia.www.haystack.client.idgenerators;
1818

1919

20-
import com.fasterxml.uuid.Generators;
21-
2220
import java.util.UUID;
2321

22+
import static com.fasterxml.uuid.Generators.timeBasedGenerator;
23+
24+
/**
25+
* Generates UUIDs as ids for Traces and Spans.
26+
* <p/>
27+
* Given that the span only needs to be unique within a trace, the UUID for spans will only contain the least
28+
* significant bits and will be left padded with zeroes.
29+
*/
2430
public class TimeBasedUUIDGenerator implements IdGenerator {
2531

2632
@Override
27-
public UUID generateTraceId() {
28-
return Generators.timeBasedGenerator().generate();
33+
public UUID generate() {
34+
return timeBasedGenerator().generate();
2935
}
3036

3137
@Override
32-
public Object generateSpanId() {
33-
return Generators.timeBasedGenerator().generate();
38+
public UUID generateSpanId() {
39+
return new UUID(0, timeBasedGenerator().generate().getLeastSignificantBits());
3440
}
3541
}

0 commit comments

Comments
 (0)