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

Commit 741b1af

Browse files
golleryurishkuro
authored andcommitted
Add jaeger-debug-id as a tag (#190)
* Add jaeger-debug-id as a tag If the trace was started with a debugID, adding jaeger-debug-id as tag ensures that the trace will be sampled in the "debug" mode. Additionally, the root span will have this ID as a searchable tag. Resolves: #136 Signed-off-by: Chris Goller <[email protected]> * Add test for jaeger-debug-id correlation tag creation Signed-off-by: Chris Goller <[email protected]>
1 parent 2c39cc2 commit 741b1af

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

src/jaegertracing/Tracer.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17+
#include "jaegertracing/Tag.h"
1718
#include "jaegertracing/Tracer.h"
1819
#include "jaegertracing/Reference.h"
1920
#include "jaegertracing/TraceID.h"
@@ -85,6 +86,7 @@ Tracer::StartSpanWithOptions(string_view operationName,
8586
flags |=
8687
(static_cast<unsigned char>(SpanContext::Flag::kSampled) |
8788
static_cast<unsigned char>(SpanContext::Flag::kDebug));
89+
samplerTags.push_back(Tag(kJaegerDebugHeader, parent->debugID()));
8890
}
8991
else {
9092
const auto samplingStatus =

src/jaegertracing/TracerTest.cpp

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ absTimeDiff(const typename ClockType::time_point& lhs,
118118
TEST(Tracer, testTracer)
119119
{
120120
{
121-
121+
122122
const auto handle = testutils::TracerUtil::installGlobalTracer();
123123
const auto tracer =
124124
std::static_pointer_cast<Tracer>(opentracing::Tracer::Global());
@@ -269,6 +269,48 @@ TEST(Tracer, testTracer)
269269
//std::this_thread::sleep_for(std::chrono::milliseconds(10000));
270270
}
271271

272+
TEST(Tracer, testDebugSpan)
273+
{
274+
const auto handle = testutils::TracerUtil::installGlobalTracer();
275+
const auto tracer =
276+
std::static_pointer_cast<Tracer>(opentracing::Tracer::Global());
277+
278+
// we force span sampling and require debug-id as the extracted
279+
// jaeger-debug-id correlation value.
280+
std::string correlationID = "debug-id";
281+
282+
StrMap headers ={
283+
{kJaegerDebugHeader, correlationID},
284+
};
285+
286+
WriterMock<opentracing::HTTPHeadersWriter> headerWriter(headers);
287+
ReaderMock<opentracing::HTTPHeadersReader> headerReader(headers);
288+
289+
const FakeSpanContext fakeCtx;
290+
tracer->Inject(fakeCtx, headerWriter);
291+
auto ext = tracer->Extract(headerReader);
292+
293+
const SpanContext* ctx = dynamic_cast<SpanContext*>(ext->release());
294+
opentracing::StartSpanOptions opts;
295+
opts.references.emplace_back(opentracing::SpanReferenceType::ChildOfRef, ctx);
296+
297+
auto otspan = tracer->StartSpanWithOptions("name", opts);
298+
// downcast to jaegertracing span implementation so we can inspect tags
299+
const std::unique_ptr<Span> span(dynamic_cast<Span*>(otspan.release()));
300+
301+
const auto& spanTags = span->tags();
302+
auto spanItr =
303+
std::find_if(std::begin(spanTags), std::end(spanTags), [](const Tag& tag) {
304+
return tag.key() == kJaegerDebugHeader;
305+
});
306+
ASSERT_NE(std::end(spanTags), spanItr);
307+
ASSERT_TRUE(spanItr->value().is<std::string>());
308+
ASSERT_EQ(spanItr->value().get<std::string>(), correlationID);
309+
310+
tracer->Close();
311+
opentracing::Tracer::InitGlobal(opentracing::MakeNoopTracer());
312+
}
313+
272314
TEST(Tracer, testConstructorFailure)
273315
{
274316
Config config;

0 commit comments

Comments
 (0)