From 8f10779195c51c3d20cac2b06238e554a8363862 Mon Sep 17 00:00:00 2001 From: Alex Emirov Date: Thu, 5 Jan 2023 00:38:08 -0500 Subject: [PATCH] Implement span context shim trace_id and span_id --- opentracing-shim/include/span_context_shim.h | 10 ++++++++++ opentracing-shim/src/span_context_shim.cc | 10 ++++++++++ opentracing-shim/test/tracer_shim_test.cc | 2 +- 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/opentracing-shim/include/span_context_shim.h b/opentracing-shim/include/span_context_shim.h index f0493a983f..a008f1e531 100644 --- a/opentracing-shim/include/span_context_shim.h +++ b/opentracing-shim/include/span_context_shim.h @@ -28,8 +28,18 @@ class SpanContextShim final : public opentracing::SpanContext using VisitBaggageItem = std::function; void ForeachBaggageItem(VisitBaggageItem f) const override; std::unique_ptr Clone() const noexcept override; + std::string ToTraceID() const noexcept override; + std::string ToSpanID() const noexcept override; private: + template + 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_; }; diff --git a/opentracing-shim/src/span_context_shim.cc b/opentracing-shim/src/span_context_shim.cc index 9d1c60618c..207f7b1be6 100644 --- a/opentracing-shim/src/span_context_shim.cc +++ b/opentracing-shim/src/span_context_shim.cc @@ -33,5 +33,15 @@ std::unique_ptr SpanContextShim::Clone() const noexcep return std::unique_ptr(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 \ No newline at end of file diff --git a/opentracing-shim/test/tracer_shim_test.cc b/opentracing-shim/test/tracer_shim_test.cc index 6e96bba571..a69bd0a7a3 100644 --- a/opentracing-shim/test/tracer_shim_test.cc +++ b/opentracing-shim/test/tracer_shim_test.cc @@ -181,4 +181,4 @@ TEST_F(TracerShimTest, ExtractOnlyBaggage) std::string value; ASSERT_TRUE(span_context_shim->BaggageItem("foo", value)); ASSERT_EQ(value, "bar"); -} +} \ No newline at end of file