1717package com .expedia .www .haystack .client ;
1818
1919import 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 ;
2022import com .expedia .www .haystack .client .metrics .*;
2123import com .expedia .www .haystack .client .metrics .Timer ;
2224import com .expedia .www .haystack .client .metrics .Timer .Sample ;
3941import java .util .*;
4042
4143public 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}
0 commit comments