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

Commit

Permalink
Merge d1681d4 into 9e1b39c
Browse files Browse the repository at this point in the history
  • Loading branch information
Alek86 authored Feb 18, 2020
2 parents 9e1b39c + d1681d4 commit a54cfd8
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 5 deletions.
6 changes: 6 additions & 0 deletions examples/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ 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::SelfRef(&spanContextWithUserIDs)});
//}

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

// An extension of enum opentracing::SpanReferenceType, for a new Span. Only to copy traceID and spanID
const static int SpanReferenceType_JaegerSpecific_SelfRef = 99;

class Reference {
public:
using Type = opentracing::SpanReferenceType;
Expand Down
25 changes: 20 additions & 5 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,11 +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());
const auto spanID = traceID.low();
else {
if (_options & kGen128BitOption) {
highID = randomID();
}
lowID = randomID();
}
const TraceID traceID(highID, lowID);
const auto spanID = self ? self->spanID() : traceID.low();
const auto parentID = 0;
auto flags = static_cast<unsigned char>(0);
if (parent && parent->isDebugIDContainerOnly()) {
Expand All @@ -101,7 +110,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 +203,12 @@ Tracer::analyzeReferences(const std::vector<OpenTracingRef>& references) const
continue;
}

if (static_cast<int>(ref.first) == SpanReferenceType_JaegerSpecific_SelfRef)
{
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;
};


// jaegertracing::SelfRef returns a StartSpanOption pointing to the Span which traceID and spanID should become the new Span's IDs
//
// See opentracing::SpanReference
inline opentracing::SpanReference SelfRef(const opentracing::SpanContext* span_context) noexcept {
return {static_cast<opentracing::SpanReferenceType>(SpanReferenceType_JaegerSpecific_SelfRef), span_context};
}

} // namespace jaegertracing

#endif // JAEGERTRACING_TRACER_H
14 changes: 14 additions & 0 deletions src/jaegertracing/TracerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -490,4 +490,18 @@ TEST(Tracer, testTracerTags)
ASSERT_EQ(std::string("test-service"), jaegerTracer->serviceName());
}

TEST(Tracer, testTracerStartSpanSelfRef)
{
const auto handle = testutils::TracerUtil::installGlobalTracer();
const auto tracer = std::static_pointer_cast<Tracer>(opentracing::Tracer::Global());
{
jaegertracing::SpanContext spanSelfContext { {1, 2}, 3, 0, 0, jaegertracing::SpanContext::StrMap() };
auto span = tracer->StartSpan("tracedFunction1", {jaegertracing::SelfRef(&spanSelfContext)});
auto jaegerSpan = dynamic_cast<jaegertracing::Span&>(*span.get());
ASSERT_EQ(jaegerSpan.context().traceID(), jaegertracing::TraceID(1, 2));
ASSERT_EQ(jaegerSpan.context().spanID(), 3);
}
tracer->Close();
}

} // namespace jaegertracing

0 comments on commit a54cfd8

Please sign in to comment.