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

An extension of enum opentracing::SpanReferenceType, for a new Span. #206

Merged
merged 14 commits into from
Mar 2, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ JAEGER_SAMPLER_PARAM | The sampler parameter (double)
JAEGER_SAMPLING_ENDPOINT | The url for the remote sampling conf when using sampler type remote. Default is http://127.0.0.1:5778/sampling
JAEGER_TAGS | A comma separated list of `name = value` tracer level tags, which get added to all reported spans. The value can also refer to an environment variable using the format `${envVarName:default}`, where the `:default` is optional, and identifies a value to be used if the environment variable cannot be found

### SelfRef
Jaeger Tracer supports an additional reference type call 'SelfRef'. This allows a caller to provide traceId and spanId for the root span.
Must be the lone reference.
Usage example:
jaegertracing::SpanContext spanContextWithUserIDs { {1, 2}, 3, 0, 0, jaegertracing::SpanContext::StrMap() }; // TraceId and SpanID must be != 0
Alek86 marked this conversation as resolved.
Show resolved Hide resolved
Alek86 marked this conversation as resolved.
Show resolved Hide resolved
auto span = opentracing::Tracer::Global()->StartSpan("tracedFunction1", {jaegertracing::SelfRef(&spanContextWithUserIDs)});
Alek86 marked this conversation as resolved.
Show resolved Hide resolved


## License

[Apache 2.0 License](./LICENSE).
Expand Down
35 changes: 31 additions & 4 deletions src/jaegertracing/Tracer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ using SystemClock = Tracer::SystemClock;
using SteadyClock = Tracer::SteadyClock;
using TimePoints = std::tuple<SystemClock::time_point, SteadyClock::time_point>;

// An extension of enum opentracing::SpanReferenceType, for a new Span. Only to copy traceID and (for non-root spans) spanID
// spanID for root spans still traceID.low()
Alek86 marked this conversation as resolved.
Show resolved Hide resolved
const static int SpanReferenceType_JaegerSpecific_SelfRef = 99;

TimePoints determineStartTimes(const opentracing::StartSpanOptions& options)
{
if (options.start_system_timestamp == SystemClock::time_point() &&
Expand Down Expand Up @@ -67,19 +71,32 @@ 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;
if (self && (parent || !references.empty()))
Alek86 marked this conversation as resolved.
Show resolved Hide resolved
{
throw std::exception(); // Self reference must be the only reference
}

std::vector<Tag> samplerTags;
auto newTrace = false;
SpanContext ctx;
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();
}
else {
if (_options & kGen128BitOption) {
highID = randomID();
}
lowID = randomID();
}
const TraceID traceID(highID, randomID());
const auto spanID = traceID.low();
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 Down Expand Up @@ -194,6 +211,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 Expand Up @@ -245,4 +268,8 @@ Tracer::make(const std::string& serviceName,
options));
}

opentracing::SpanReference SelfRef(const opentracing::SpanContext* span_context) noexcept {
return {static_cast<opentracing::SpanReferenceType>(SpanReferenceType_JaegerSpecific_SelfRef), span_context};
}

} // namespace jaegertracing
8 changes: 8 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,12 @@ 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
Alek86 marked this conversation as resolved.
Show resolved Hide resolved
//
// See opentracing::SpanReference
opentracing::SpanReference SelfRef(const opentracing::SpanContext* span_context) noexcept;

} // namespace jaegertracing

#endif // JAEGERTRACING_TRACER_H