44#pragma once
55
66#include " acconfig.h"
7- #include " include/buffer .h"
7+ #include " include/encoding .h"
88
99#ifdef HAVE_JAEGER
1010#include " opentelemetry/trace/provider.h"
@@ -16,6 +16,11 @@ using jspan_attribute = opentelemetry::common::AttributeValue;
1616
1717namespace tracing {
1818
19+ static constexpr int TraceIdkSize = 16 ;
20+ static constexpr int SpanIdkSize = 8 ;
21+ static_assert (TraceIdkSize == opentelemetry::trace::TraceId::kSize );
22+ static_assert (SpanIdkSize == opentelemetry::trace::SpanId::kSize );
23+
1924class Tracer {
2025 private:
2126 const static opentelemetry::nostd::shared_ptr<opentelemetry::trace::Tracer> noop_tracer;
@@ -24,6 +29,7 @@ class Tracer {
2429 opentelemetry::nostd::shared_ptr<opentelemetry::trace::Tracer> tracer;
2530
2631 public:
32+
2733 Tracer () = default ;
2834
2935 void init (CephContext* _cct, opentelemetry::nostd::string_view service_name);
@@ -46,8 +52,41 @@ class Tracer {
4652
4753};
4854
49- void encode (const jspan_context& span, ceph::buffer::list& bl, uint64_t f = 0 );
50- void decode (jspan_context& span_ctx, ceph::buffer::list::const_iterator& bl);
55+ inline void encode (const jspan_context& span_ctx, bufferlist& bl, uint64_t f = 0 ) {
56+ ENCODE_START (1 , 1 , bl);
57+ using namespace opentelemetry ;
58+ using namespace trace ;
59+ auto is_valid = span_ctx.IsValid ();
60+ encode (is_valid, bl);
61+ if (is_valid) {
62+ encode_nohead (std::string_view (reinterpret_cast <const char *>(span_ctx.trace_id ().Id ().data ()), TraceIdkSize), bl);
63+ encode_nohead (std::string_view (reinterpret_cast <const char *>(span_ctx.span_id ().Id ().data ()), SpanIdkSize), bl);
64+ encode (span_ctx.trace_flags ().flags (), bl);
65+ }
66+ ENCODE_FINISH (bl);
67+ }
68+
69+ inline void decode (jspan_context& span_ctx, bufferlist::const_iterator& bl) {
70+ using namespace opentelemetry ;
71+ using namespace trace ;
72+ DECODE_START (1 , bl);
73+ bool is_valid;
74+ decode (is_valid, bl);
75+ if (is_valid) {
76+ std::array<uint8_t , TraceIdkSize> trace_id;
77+ std::array<uint8_t , SpanIdkSize> span_id;
78+ uint8_t flags;
79+ decode (trace_id, bl);
80+ decode (span_id, bl);
81+ decode (flags, bl);
82+ span_ctx = SpanContext (
83+ TraceId (nostd::span<uint8_t , TraceIdkSize>(trace_id)),
84+ SpanId (nostd::span<uint8_t , SpanIdkSize>(span_id)),
85+ TraceFlags (flags),
86+ true );
87+ }
88+ DECODE_FINISH (bl);
89+ }
5190
5291} // namespace tracing
5392
@@ -63,10 +102,20 @@ class Value {
63102
64103using jspan_attribute = Value;
65104
66- struct jspan_context {
67- jspan_context () {}
68- jspan_context (bool sampled_flag, bool is_remote) {}
105+ namespace opentelemetry {
106+ inline namespace v1 {
107+ namespace trace {
108+ class SpanContext {
109+ public:
110+ SpanContext () = default ;
111+ SpanContext (bool sampled_flag, bool is_remote) {}
112+ bool IsValid () const { return false ;}
69113};
114+ } // namespace trace
115+ } // namespace v1
116+ } // namespace opentelemetry
117+
118+ using jspan_context = opentelemetry::v1::trace::SpanContext;
70119
71120class jspan {
72121 jspan_context _ctx;
@@ -76,7 +125,7 @@ class jspan {
76125 void AddEvent (std::string_view) {}
77126 void AddEvent (std::string_view, std::initializer_list<std::pair<std::string_view, jspan_attribute>> fields) {}
78127 template <typename T> void AddEvent (std::string_view name, const T& fields = {}) {}
79- const jspan_context& GetContext () { return _ctx; }
128+ jspan_context GetContext () const { return _ctx; }
80129 void UpdateName (std::string_view) {}
81130 bool IsRecording () { return false ; }
82131};
@@ -103,9 +152,20 @@ struct Tracer {
103152 jspan_ptr add_span (std::string_view span_name, const jspan_context& parent_ctx) { return {}; }
104153};
105154
106- inline void encode (const jspan_context& span, bufferlist& bl, uint64_t f=0 ) {}
107- inline void decode (jspan_context& span_ctx, ceph::buffer::list::const_iterator& bl) {}
155+ inline void encode (const jspan_context& span_ctx, bufferlist& bl, uint64_t f = 0 ) {
156+ ENCODE_START (1 , 1 , bl);
157+ // jaeger is missing, set "is_valid" to false.
158+ bool is_valid = false ;
159+ encode (is_valid, bl);
160+ ENCODE_FINISH (bl);
161+ }
108162
163+ inline void decode (jspan_context& span_ctx, bufferlist::const_iterator& bl) {
164+ DECODE_START (254 , bl);
165+ // jaeger is missing, consume the buffer but do not decode it.
166+ DECODE_FINISH (bl);
109167}
110168
169+ } // namespace tracing
170+
111171#endif // !HAVE_JAEGER
0 commit comments