From 68266e2107b2b996180d71481daa497bf1c79b4e Mon Sep 17 00:00:00 2001 From: Rohit Gupta Date: Thu, 14 Oct 2021 17:06:00 +0530 Subject: [PATCH 1/2] Provide an option for span-reportor to use the tracing implementation span id instead of generating its own + Avoid call to context.baggageItems() twice in SpanBuilderR.findSpanId --- .../contrib/reporter/SpanBuilderR.java | 30 ++++++++++++++----- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/span-reporter/src/main/java/io/opentracing/contrib/reporter/SpanBuilderR.java b/span-reporter/src/main/java/io/opentracing/contrib/reporter/SpanBuilderR.java index ad88cda..9bf96ed 100644 --- a/span-reporter/src/main/java/io/opentracing/contrib/reporter/SpanBuilderR.java +++ b/span-reporter/src/main/java/io/opentracing/contrib/reporter/SpanBuilderR.java @@ -39,10 +39,13 @@ public class SpanBuilderR implements Tracer.SpanBuilder { } String findSpanId(SpanContext context) { - if (context != null && context.baggageItems() != null) { - for (Map.Entry kv: context.baggageItems()) { - if (BAGGAGE_SPANID_KEY.equals(kv.getKey())) { - return kv.getValue(); + if (context != null) { + Iterable> baggageItems = context.baggageItems(); + if (baggageItems != null) { + for (Map.Entry kv: baggageItems) { + if (BAGGAGE_SPANID_KEY.equals(kv.getKey())) { + return kv.getValue(); + } } } } @@ -69,7 +72,14 @@ public Tracer.SpanBuilder asChildOf(Span parent) { @Override public Tracer.SpanBuilder addReference(String s, SpanContext spanContext) { wrapped.addReference(s, spanContext); - references.put(s, findSpanId(spanContext)); + String referenceId; + if (spanContext != null) { + referenceId = spanContext.toSpanId(); + } + else { + referenceId = findSpanId(spanContext); + } + references.put(s, referenceId); return this; } @@ -118,8 +128,14 @@ public Tracer.SpanBuilder withStartTimestamp(long l) { @Override public Span start() { Span wspan = wrapped.start(); - String spanId = UUID.randomUUID().toString(); - wspan.setBaggageItem(BAGGAGE_SPANID_KEY, spanId); + String spanId; + SpanContext spanContext = wspan.context(); + if (spanContext != null) { + spanId = spanContext.toSpanId(); + } else { + spanId = UUID.randomUUID().toString(); + wspan.setBaggageItem(BAGGAGE_SPANID_KEY, spanId); + } // shamelessly copied from io.jaegertracing.internal.JaegerTracer.SpanBuilder#start // of io.jaegertracing:jaeger-core:0.35.5 From f3478c61cf9c8a7e04330272e440e572447a2590 Mon Sep 17 00:00:00 2001 From: Rohit Gupta Date: Wed, 23 Feb 2022 13:49:21 +0530 Subject: [PATCH 2/2] Implement toString and return a value that includes the data returned by the wrapped tracer. --- .../main/java/io/opentracing/contrib/reporter/TracerR.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/span-reporter/src/main/java/io/opentracing/contrib/reporter/TracerR.java b/span-reporter/src/main/java/io/opentracing/contrib/reporter/TracerR.java index a491c45..c04ef6d 100644 --- a/span-reporter/src/main/java/io/opentracing/contrib/reporter/TracerR.java +++ b/span-reporter/src/main/java/io/opentracing/contrib/reporter/TracerR.java @@ -66,4 +66,9 @@ public void close() { this.wrapped.close(); } + @Override + public String toString() { + return "SpanReporterTracer{" + wrapped + '}'; + } + }