From 053351c234560055a5fcbdb93a75e7370323a016 Mon Sep 17 00:00:00 2001 From: Omri Zeneva Date: Tue, 30 Nov 2021 11:05:02 +0200 Subject: [PATCH] common/tracer: implement encode & decode functions Signed-off-by: Omri Zeneva --- src/common/tracer.cc | 57 +++++++++++++++++++++++++++++++++++++++----- src/common/tracer.h | 23 +++++++++++++++++- 2 files changed, 73 insertions(+), 7 deletions(-) diff --git a/src/common/tracer.cc b/src/common/tracer.cc index ee2961ef77bbf..15fd7a335f582 100644 --- a/src/common/tracer.cc +++ b/src/common/tracer.cc @@ -7,10 +7,15 @@ #ifdef HAVE_JAEGER +#include "opentelemetry/trace/propagation/jaeger.h" +#include "opentelemetry/trace/propagation/detail/hex.h" + namespace tracing { const opentelemetry::nostd::shared_ptr Tracer::noop_tracer = opentelemetry::trace::Provider::GetTracerProvider()->GetTracer("no-op", OPENTELEMETRY_SDK_VERSION); +using bufferlist = ceph::buffer::list; + Tracer::Tracer(opentelemetry::nostd::string_view service_name) { init(service_name); } @@ -39,14 +44,19 @@ jspan Tracer::start_trace(opentelemetry::nostd::string_view trace_name) { return noop_tracer->StartSpan(trace_name); } -jspan Tracer::add_span(opentelemetry::nostd::string_view span_name, jspan& parent_span) { +jspan Tracer::add_span(opentelemetry::nostd::string_view span_name, const jspan& parent_span) { if (is_enabled() && parent_span) { const auto parent_ctx = parent_span->GetContext(); - if (parent_ctx.IsValid()) { - opentelemetry::trace::StartSpanOptions span_opts; - span_opts.parent = parent_ctx; - return tracer->StartSpan(span_name, span_opts); - } + return add_span(span_name, parent_ctx); + } + return noop_tracer->StartSpan(span_name); +} + +jspan Tracer::add_span(opentelemetry::nostd::string_view span_name, const jspan_context& parent_ctx) { + if (is_enabled() && parent_ctx.IsValid()) { + opentelemetry::trace::StartSpanOptions span_opts; + span_opts.parent = parent_ctx; + return tracer->StartSpan(span_name, span_opts); } return noop_tracer->StartSpan(span_name); } @@ -55,6 +65,41 @@ bool Tracer::is_enabled() const { return g_ceph_context->_conf->jaeger_tracing_enable; } +void encode(const jspan_context& span_ctx, bufferlist& bl, uint64_t f) { + ENCODE_START(1, 1, bl); + using namespace opentelemetry; + using namespace trace; + auto is_valid = span_ctx.IsValid(); + encode(is_valid, bl); + if (is_valid) { + encode_nohead(std::string_view(reinterpret_cast(span_ctx.trace_id().Id().data()), TraceId::kSize), bl); + encode_nohead(std::string_view(reinterpret_cast(span_ctx.span_id().Id().data()), SpanId::kSize), bl); + encode(span_ctx.trace_flags().flags(), bl); + } + ENCODE_FINISH(bl); +} + +void decode(jspan_context& span_ctx, bufferlist::const_iterator& bl) { + using namespace opentelemetry; + using namespace trace; + DECODE_START(1, bl); + bool is_valid; + decode(is_valid, bl); + if (is_valid) { + std::array trace_id; + std::array span_id; + uint8_t flags; + decode(trace_id, bl); + decode(span_id, bl); + decode(flags, bl); + span_ctx = SpanContext( + TraceId(nostd::span(trace_id)), + SpanId(nostd::span(span_id)), + TraceFlags(flags), + true); + } + DECODE_FINISH(bl); +} } // namespace tracing #endif // HAVE_JAEGER diff --git a/src/common/tracer.h b/src/common/tracer.h index 5e7f4e799be4c..3d0e28f9b85c7 100644 --- a/src/common/tracer.h +++ b/src/common/tracer.h @@ -4,6 +4,7 @@ #pragma once #include "acconfig.h" +#include "include/buffer.h" #ifdef HAVE_JAEGER @@ -13,6 +14,7 @@ #include "opentelemetry/sdk/trace/tracer_provider.h" using jspan = opentelemetry::nostd::shared_ptr; +using jspan_context = opentelemetry::trace::SpanContext; namespace tracing { @@ -33,10 +35,16 @@ class Tracer { // this span represents a trace, since it has no parent. jspan start_trace(opentelemetry::nostd::string_view trace_name); // creates and returns a new span with `span_name` which parent span is `parent_span' - jspan add_span(opentelemetry::nostd::string_view span_name, jspan& parent_span); + jspan add_span(opentelemetry::nostd::string_view span_name, const jspan& parent_span); + // creates and return a new span with `span_name` + // the span is added to the trace which it's context is `parent_ctx`. + // parent_ctx contains the required information of the trace. + jspan add_span(opentelemetry::nostd::string_view span_name, const jspan_context& parent_ctx); }; +void encode(const jspan_context& span, ceph::buffer::list& bl, uint64_t f = 0); +void decode(jspan_context& span_ctx, ceph::buffer::list::const_iterator& bl); } // namespace tracing @@ -45,15 +53,25 @@ class Tracer { #include + + class Value { public: template Value(T val) {} }; +struct jspan_context { + jspan_context() {} + jspan_context(bool sampled_flag, bool is_remote) {} +}; + struct span_stub { + jspan_context _ctx; template void SetAttribute(std::string_view key, const T& value) const noexcept {} void AddEvent(std::string_view, std::initializer_list> fields) {} + const jspan_context& GetContext() { return _ctx; } + void UpdateName(std::string_view) {} }; class jspan { @@ -74,9 +92,12 @@ struct Tracer { bool is_enabled() const { return false; } jspan start_trace(std::string_view) { return {}; } jspan add_span(std::string_view, const jspan&) { return {}; } + jspan add_span(std::string_view span_name, const jspan_context& parent_ctx) { return {}; } void init(std::string_view service_name) {} void shutdown() {} }; + inline void encode(const jspan_context& span, bufferlist& bl, uint64_t f=0) {} + inline void decode(jspan_context& span_ctx, ceph::buffer::list::const_iterator& bl) {} } #endif // !HAVE_JAEGER -- 2.39.5