Skip to content

Commit

Permalink
Implement span context shim trace_id and span_id
Browse files Browse the repository at this point in the history
  • Loading branch information
chusitoo committed Jan 5, 2023
1 parent 25aeac8 commit 8f10779
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
10 changes: 10 additions & 0 deletions opentracing-shim/include/span_context_shim.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,18 @@ class SpanContextShim final : public opentracing::SpanContext
using VisitBaggageItem = std::function<bool(const std::string& key, const std::string& value)>;
void ForeachBaggageItem(VisitBaggageItem f) const override;
std::unique_ptr<opentracing::SpanContext> Clone() const noexcept override;
std::string ToTraceID() const noexcept override;
std::string ToSpanID() const noexcept override;

private:
template <typename T>
static std::string toHexString(const T &id_item)
{
char buf[T::kSize * 2];
id_item.ToLowerBase16(buf);
return std::string(buf, sizeof(buf));
}

opentelemetry::trace::SpanContext context_;
BaggagePtr baggage_;
};
Expand Down
10 changes: 10 additions & 0 deletions opentracing-shim/src/span_context_shim.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,15 @@ std::unique_ptr<opentracing::SpanContext> SpanContextShim::Clone() const noexcep
return std::unique_ptr<SpanContextShim>(new (std::nothrow) SpanContextShim(context_, baggage_));
}

std::string SpanContextShim::ToTraceID() const noexcept
{
return toHexString(context_.trace_id());
}

std::string SpanContextShim::ToSpanID() const noexcept
{
return toHexString(context_.span_id());
}

} // namespace opentracingshim
OPENTELEMETRY_END_NAMESPACE
2 changes: 1 addition & 1 deletion opentracing-shim/test/tracer_shim_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,4 @@ TEST_F(TracerShimTest, ExtractOnlyBaggage)
std::string value;
ASSERT_TRUE(span_context_shim->BaggageItem("foo", value));
ASSERT_EQ(value, "bar");
}
}

0 comments on commit 8f10779

Please sign in to comment.