]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
common/tracer: implement encode & decode functions
authorOmri Zeneva <ozeneva@redhat.com>
Tue, 30 Nov 2021 09:05:02 +0000 (11:05 +0200)
committerOmri Zeneva <ozeneva@redhat.com>
Tue, 30 Nov 2021 09:05:02 +0000 (11:05 +0200)
Signed-off-by: Omri Zeneva <ozeneva@redhat.com>
src/common/tracer.cc
src/common/tracer.h

index ee2961ef77bbfcc47fd13ba2d89d88e67633d765..15fd7a335f58258e43e4f79ff423dde58145e1bb 100644 (file)
@@ -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<opentelemetry::trace::Tracer> 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<const char*>(span_ctx.trace_id().Id().data()), TraceId::kSize), bl);
+    encode_nohead(std::string_view(reinterpret_cast<const char*>(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<uint8_t, TraceId::kSize> trace_id;
+    std::array<uint8_t, SpanId::kSize> span_id;
+    uint8_t flags;
+    decode(trace_id, bl);
+    decode(span_id, bl);
+    decode(flags, bl);
+    span_ctx = SpanContext(
+      TraceId(nostd::span<uint8_t, TraceId::kSize>(trace_id)),
+      SpanId(nostd::span<uint8_t, SpanId::kSize>(span_id)),
+      TraceFlags(flags),
+      true);
+  }
+  DECODE_FINISH(bl);
+}
 } // namespace tracing
 
 #endif // HAVE_JAEGER
index 5e7f4e799be4c8766768fc00283d6fdadeb39183..3d0e28f9b85c7d0b97bfc1c24cdc2f8d2995e367 100644 (file)
@@ -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<opentelemetry::trace::Span>;
+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 <string_view>
 
+
+
 class Value {
  public:
   template <typename T> Value(T val) {}
 };
 
+struct jspan_context {
+  jspan_context() {}
+  jspan_context(bool sampled_flag, bool is_remote) {}
+};
+
 struct span_stub {
+  jspan_context _ctx;
   template <typename T>
   void SetAttribute(std::string_view key, const T& value) const noexcept {}
   void AddEvent(std::string_view, std::initializer_list<std::pair<std::string_view, Value>> 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