Skip to content
This repository has been archived by the owner on Aug 30, 2022. It is now read-only.

Commit

Permalink
Merge f1a32f7 into 9e1b39c
Browse files Browse the repository at this point in the history
  • Loading branch information
Alek86 authored Feb 17, 2020
2 parents 9e1b39c + f1a32f7 commit 017fb23
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
5 changes: 5 additions & 0 deletions examples/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ void tracedSubroutine(const std::unique_ptr<opentracing::Span>& parentSpan)

void tracedFunction()
{
// jaegertracing::SpanContext spanContextWithUserIDs { {111, 222}, 333, 0, 0, {} }; // TraceId and SpanID must be != 0
// auto span = opentracing::Tracer::Global()->StartSpan(
// "tracedFunction1", {jaegertracing::JaegerSpecific_UseTheseIDs(&spanContextWithUserIDs)});
// // spanID of span is 222, not 333, because it's a root span

auto span = opentracing::Tracer::Global()->StartSpan("tracedFunction");
tracedSubroutine(span);
}
Expand Down
4 changes: 4 additions & 0 deletions src/jaegertracing/Reference.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ namespace thrift {
class SpanRef;
}

// An extension of enum opentracing::SpanReferenceType, for a new Span. Only to copy traceID and (for non-root spans) spanID
// spanID for root spans == traceID.low()
const static int SpanReferenceType_JaegerSpecific_UseTheseIDs = 99;

class Reference {
public:
using Type = opentracing::SpanReferenceType;
Expand Down
24 changes: 20 additions & 4 deletions src/jaegertracing/Tracer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ Tracer::StartSpanWithOptions(string_view operationName,
try {
const auto result = analyzeReferences(options.references);
const auto* parent = result._parent;
const auto* self = result._self;
const auto& references = result._references;

std::vector<Tag> samplerTags;
Expand All @@ -75,10 +76,19 @@ Tracer::StartSpanWithOptions(string_view operationName,
if (!parent || !parent->isValid()) {
newTrace = true;
auto highID = static_cast<uint64_t>(0);
if (_options & kGen128BitOption) {
highID = randomID();
auto lowID = static_cast<uint64_t>(0);
if (self) {
highID = self->traceID().high();
lowID = self->traceID().low();
}
const TraceID traceID(highID, randomID());
else {
if (_options & kGen128BitOption) {
highID = randomID();
}
lowID = randomID();
}
const TraceID traceID(highID, lowID);
// self.spanID is ignored for the root span
const auto spanID = traceID.low();
const auto parentID = 0;
auto flags = static_cast<unsigned char>(0);
Expand All @@ -101,7 +111,7 @@ Tracer::StartSpanWithOptions(string_view operationName,
}
else {
const auto traceID = parent->traceID();
const auto spanID = randomID();
const auto spanID = self ? self->spanID() : randomID();
const auto parentID = parent->spanID();
const auto flags = parent->flags();
ctx = SpanContext(traceID, spanID, parentID, flags, StrMap());
Expand Down Expand Up @@ -194,6 +204,12 @@ Tracer::analyzeReferences(const std::vector<OpenTracingRef>& references) const
continue;
}

if (static_cast<int>(ref.first) == SpanReferenceType_JaegerSpecific_UseTheseIDs)
{
result._self = ctx;
continue; // not a reference
}

result._references.emplace_back(Reference(*ctx, ref.first));

if (!hasParent) {
Expand Down
10 changes: 10 additions & 0 deletions src/jaegertracing/Tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -273,11 +273,13 @@ class Tracer : public opentracing::Tracer,
struct AnalyzedReferences {
AnalyzedReferences()
: _parent(nullptr)
, _self(nullptr)
, _references()
{
}

const SpanContext* _parent;
const SpanContext* _self;
std::vector<Reference> _references;
};

Expand All @@ -301,6 +303,14 @@ class Tracer : public opentracing::Tracer,
int _options;
};


// JaegerSpecific_Self returns a StartSpanOption pointing to the Span that should be used as the started span context
//
// See opentracing::SpanReference
inline opentracing::SpanReference JaegerSpecific_UseTheseIDs(const opentracing::SpanContext* span_context) noexcept {
return {static_cast<opentracing::SpanReferenceType>(SpanReferenceType_JaegerSpecific_UseTheseIDs), span_context};
}

} // namespace jaegertracing

#endif // JAEGERTRACING_TRACER_H

0 comments on commit 017fb23

Please sign in to comment.