Skip to content

Commit 3159432

Browse files
author
nsahai8
authored
Merge pull request #103 from ExpediaDotCom/id_generator_for_tracer
Id generator for tracer
2 parents 00cf71a + a87ed0b commit 3159432

File tree

9 files changed

+247
-21
lines changed

9 files changed

+247
-21
lines changed

core/pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,14 @@
106106
<artifactId>awaitility</artifactId>
107107
<scope>test</scope>
108108
</dependency>
109+
110+
111+
<dependency>
112+
<groupId>com.fasterxml.uuid</groupId>
113+
<artifactId>java-uuid-generator</artifactId>
114+
<version>3.1.4</version>
115+
</dependency>
116+
109117
</dependencies>
110118

111119
</project>

core/src/main/java/com/expedia/www/haystack/client/SpanContext.java

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

19+
import org.apache.commons.lang3.Validate;
1920
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
2021
import org.apache.commons.lang3.builder.ToStringStyle;
2122

@@ -25,9 +26,9 @@
2526
public class SpanContext implements io.opentracing.SpanContext {
2627

2728
private final Map<String, String> baggage;
28-
private final UUID traceId;
29-
private final UUID spanId;
30-
private final UUID parentId;
29+
private final Object traceId;
30+
private final Object spanId;
31+
private final Object parentId;
3132
private boolean extractedContext;
3233

3334
public SpanContext(UUID traceId, UUID spanId, UUID parentId) {
@@ -44,9 +45,7 @@ public SpanContext(UUID traceId, UUID spanId, UUID parentId, boolean extractedCo
4445
}
4546

4647
SpanContext(UUID traceId, UUID spanId, UUID parentId, Map<String, String> baggage, boolean extractedContext) {
47-
if (baggage == null) {
48-
throw new NullPointerException();
49-
}
48+
Validate.notNull(baggage);
5049

5150
this.traceId = traceId;
5251
this.spanId = spanId;
@@ -55,6 +54,24 @@ public SpanContext(UUID traceId, UUID spanId, UUID parentId, boolean extractedCo
5554
this.extractedContext = extractedContext;
5655
}
5756

57+
58+
SpanContext(Object traceId, Object spanId, Object parentId, Map<String, String> baggage, boolean extractedContext) {
59+
Validate.notNull(baggage);
60+
61+
this.traceId = traceId;
62+
this.spanId = spanId;
63+
this.parentId = parentId;
64+
this.baggage = Collections.unmodifiableMap(baggage);
65+
this.extractedContext = extractedContext;
66+
}
67+
68+
public SpanContext(Object traceId, Object spanId, Object parentId) {
69+
this(traceId, spanId, parentId, false);
70+
}
71+
72+
public SpanContext(Object traceId, Object spanId, Object parentId, boolean extractedContext) {
73+
this(traceId, spanId, parentId, Collections.emptyMap(), extractedContext);
74+
}
5875
@Override
5976
public int hashCode() {
6077
return Objects.hash(traceId, spanId, parentId, baggage);
@@ -104,21 +121,21 @@ public String getBaggageItem(String key) {
104121
/**
105122
* @return the traceId
106123
*/
107-
public UUID getTraceId() {
124+
public Object getTraceId() {
108125
return traceId;
109126
}
110127

111128
/**
112129
* @return the spanId
113130
*/
114-
public UUID getSpanId() {
131+
public Object getSpanId() {
115132
return spanId;
116133
}
117134

118135
/**
119136
* @return the parentId
120137
*/
121-
public UUID getParentId() {
138+
public Object getParentId() {
122139
return parentId;
123140
}
124141

core/src/main/java/com/expedia/www/haystack/client/Tracer.java

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

1919
import com.expedia.www.haystack.client.dispatchers.Dispatcher;
20+
import com.expedia.www.haystack.client.idgenerators.IdGenerator;
21+
import com.expedia.www.haystack.client.idgenerators.LongIdGenerator;
2022
import com.expedia.www.haystack.client.metrics.*;
2123
import com.expedia.www.haystack.client.metrics.Timer;
2224
import com.expedia.www.haystack.client.metrics.Timer.Sample;
@@ -39,7 +41,12 @@
3941
import java.util.*;
4042

4143
public class Tracer implements io.opentracing.Tracer {
44+
45+
private final static IdGenerator DEFAULT_ID_GENERATOR = new LongIdGenerator();
46+
private final static Boolean DEFAULT_DUAL_SPAN_MODE = false;
47+
4248
private final Dispatcher dispatcher;
49+
private final IdGenerator idGenerator;
4350
protected final Clock clock;
4451
protected final PropagationRegistry registry;
4552
private final String serviceName;
@@ -64,14 +71,15 @@ public class Tracer implements io.opentracing.Tracer {
6471

6572
public Tracer(String serviceName, ScopeManager scopeManager, Clock clock,
6673
Dispatcher dispatcher, PropagationRegistry registry, Metrics metrics) {
67-
this(serviceName, scopeManager, clock, dispatcher, registry, metrics, false);
68-
74+
this(serviceName, scopeManager, clock, dispatcher, registry, metrics, DEFAULT_DUAL_SPAN_MODE, DEFAULT_ID_GENERATOR);
6975
}
7076
public Tracer(String serviceName, ScopeManager scopeManager, Clock clock,
71-
Dispatcher dispatcher, PropagationRegistry registry, Metrics metrics, boolean dualSpanMode) {
77+
Dispatcher dispatcher, PropagationRegistry registry,
78+
Metrics metrics, boolean dualSpanMode, IdGenerator idGenerator) {
7279
this.serviceName = serviceName;
7380
this.scopeManager = scopeManager;
7481
this.clock = clock;
82+
this.idGenerator = idGenerator;
7583
this.dispatcher = dispatcher;
7684
this.registry = registry;
7785
this.dualSpanMode = dualSpanMode;
@@ -272,13 +280,19 @@ boolean isServerSpan() {
272280
}
273281

274282
protected SpanContext createNewContext() {
275-
return createContext(UUID.randomUUID(), UUID.randomUUID(), null, Collections.emptyMap());
283+
284+
return createContext(tracer.idGenerator.generate(), tracer.idGenerator.generate(), null, Collections.emptyMap());
276285
}
277286

278287
protected SpanContext createContext(UUID traceId, UUID spanId, UUID parentId, Map<String, String> baggage) {
279288
return new SpanContext(traceId, spanId, parentId, baggage, false);
280289
}
281290

291+
292+
protected SpanContext createContext(Object traceId, Object spanId, Object parentId, Map<String, String> baggage) {//doubt parentId
293+
return new SpanContext(traceId, spanId, parentId, baggage, false);
294+
}
295+
282296
protected SpanContext createDependentContext() {
283297
Reference parent = references.get(0);
284298
for (Reference reference : references) {
@@ -360,6 +374,7 @@ public static class Builder {
360374
protected Dispatcher dispatcher;
361375
protected PropagationRegistry registry = new PropagationRegistry();
362376
protected Metrics metrics;
377+
protected IdGenerator idGenerator;
363378
private boolean dualSpanMode;
364379

365380
public Builder(MetricsRegistry registry, String serviceName, Dispatcher dispatcher) {
@@ -390,6 +405,11 @@ public Builder withClock(Clock clock) {
390405
return this;
391406
}
392407

408+
public Builder withIdGenerator(IdGenerator idGenerator) {
409+
this.idGenerator = idGenerator;
410+
return this;
411+
}
412+
393413
public <T> Builder withFormat(Format<T> format, Injector<T> injector) {
394414
registry.register(format, injector);
395415
return this;
@@ -422,7 +442,8 @@ public Builder withDualSpanMode() {
422442
}
423443

424444
public Tracer build() {
425-
return new Tracer(serviceName, scopeManager, clock, dispatcher, registry, metrics, dualSpanMode);
445+
idGenerator = idGenerator == null ? DEFAULT_ID_GENERATOR : idGenerator;
446+
return new Tracer(serviceName, scopeManager, clock, dispatcher, registry, metrics, dualSpanMode, idGenerator);
426447
}
427448
}
428449
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
public interface IdGenerator {
20+
Object generate();
21+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
20+
import com.expedia.www.haystack.client.idgenerators.IdGenerator;
21+
22+
import java.util.concurrent.ThreadLocalRandom;
23+
24+
public class LongIdGenerator implements IdGenerator {
25+
26+
@Override
27+
public Long generate() {
28+
return ThreadLocalRandom.current().nextLong(Long.MAX_VALUE);
29+
}
30+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
20+
import java.util.UUID;
21+
22+
public class RandomUUIDGenerator implements IdGenerator {
23+
24+
@Override
25+
public UUID generate() {
26+
return UUID.randomUUID();
27+
}
28+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
20+
import com.fasterxml.uuid.Generators;
21+
22+
import java.util.UUID;
23+
24+
25+
public class TimeBasedUUIDGenerator implements IdGenerator {
26+
27+
@Override
28+
public UUID generate() {
29+
return Generators.timeBasedGenerator().generate();
30+
}
31+
}

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.expedia.www.haystack.client.dispatchers.Dispatcher;
2020
import com.expedia.www.haystack.client.dispatchers.NoopDispatcher;
21+
import com.expedia.www.haystack.client.idgenerators.RandomUUIDGenerator;
2122
import com.expedia.www.haystack.client.metrics.NoopMetricsRegistry;
2223
import com.expedia.www.haystack.client.propagation.MapBackedTextMap;
2324
import io.opentracing.References;
@@ -67,7 +68,7 @@ public void testChildOfWithDualSpanType() {
6768
//create a client span
6869
final Tracer clientTracer = new Tracer.Builder(new NoopMetricsRegistry(),
6970
"ClientService",
70-
dispatcher).withDualSpanMode().build();
71+
dispatcher).withDualSpanMode().withIdGenerator(new RandomUUIDGenerator()).build();
7172
final Span clientSpan = clientTracer.buildSpan("Api_call")
7273
.withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT)
7374
.start();
@@ -77,7 +78,7 @@ public void testChildOfWithDualSpanType() {
7778
//create a server
7879
final Tracer serverTracer = new Tracer.Builder(new NoopMetricsRegistry(),
7980
"ServerService",
80-
dispatcher).withDualSpanMode().build();
81+
dispatcher).withDualSpanMode().withIdGenerator(new RandomUUIDGenerator()).build();
8182
final SpanContext wireContext = serverTracer.extract(Format.Builtin.TEXT_MAP, wireData);
8283
final Span serverSpan = serverTracer.buildSpan("Api")
8384
.asChildOf(wireContext)
@@ -97,7 +98,7 @@ public void testChildOfWithSingleSpanType() {
9798
//create a client span
9899
final Tracer clientTracer = new Tracer.Builder(new NoopMetricsRegistry(),
99100
"ClientService",
100-
dispatcher).build();
101+
dispatcher).withIdGenerator(new RandomUUIDGenerator()).build();
101102
final Span clientSpan = clientTracer.buildSpan("Api_call")
102103
.withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT)
103104
.start();
@@ -107,7 +108,7 @@ public void testChildOfWithSingleSpanType() {
107108
//create a server
108109
final Tracer serverTracer = new Tracer.Builder(new NoopMetricsRegistry(),
109110
"ServerService",
110-
dispatcher).build();
111+
dispatcher).withIdGenerator(new RandomUUIDGenerator()).build();
111112
final SpanContext wireContext = serverTracer.extract(Format.Builtin.TEXT_MAP, wireData);
112113
final Span serverSpan = serverTracer.buildSpan("Api")
113114
.asChildOf(wireContext)
@@ -128,7 +129,8 @@ public void testChildOfWithSingleSpanTypeAndExtractedContext() {
128129
//create a client span
129130
final Tracer clientTracer = new Tracer.Builder(new NoopMetricsRegistry(),
130131
"ClientService",
131-
dispatcher).build();
132+
dispatcher).withIdGenerator(new RandomUUIDGenerator()).build();
133+
132134
final Span clientSpan = clientTracer.buildSpan("Api_call")
133135
.withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT)
134136
.start();
@@ -157,7 +159,7 @@ public void testChildOfSingleSpanTypeWithExtractedContextDoesNotPropagateExtract
157159
//create a client span
158160
final Tracer clientTracer = new Tracer.Builder(new NoopMetricsRegistry(),
159161
"ClientService",
160-
dispatcher).build();
162+
dispatcher).withIdGenerator(new RandomUUIDGenerator()).build();
161163
final Span clientSpan = clientTracer.buildSpan("Api_call")
162164
.withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT)
163165
.start();
@@ -167,7 +169,7 @@ public void testChildOfSingleSpanTypeWithExtractedContextDoesNotPropagateExtract
167169
//create a server
168170
final Tracer serverTracer = new Tracer.Builder(new NoopMetricsRegistry(),
169171
"ServerService",
170-
dispatcher).build();
172+
dispatcher).withIdGenerator(new RandomUUIDGenerator()).build();
171173
final SpanContext wireContext = serverTracer.extract(Format.Builtin.TEXT_MAP, wireData);
172174
final Span serverSpan = serverTracer.buildSpan("Api")
173175
.asChildOf(wireContext)

0 commit comments

Comments
 (0)