]> git.proxmox.com Git - ceph.git/blob - ceph/src/common/tracer.cc
update ceph source to reef 18.2.0
[ceph.git] / ceph / src / common / tracer.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #include "common/ceph_context.h"
5 #include "global/global_context.h"
6 #include "tracer.h"
7
8 #ifdef HAVE_JAEGER
9 #include "opentelemetry/sdk/trace/batch_span_processor.h"
10 #include "opentelemetry/sdk/trace/tracer_provider.h"
11 #include "opentelemetry/exporters/jaeger/jaeger_exporter.h"
12
13 namespace tracing {
14
15 const opentelemetry::nostd::shared_ptr<opentelemetry::trace::Tracer> Tracer::noop_tracer = opentelemetry::trace::Provider::GetTracerProvider()->GetTracer("no-op", OPENTELEMETRY_SDK_VERSION);
16 const jspan Tracer::noop_span = noop_tracer->StartSpan("noop");
17
18 using bufferlist = ceph::buffer::list;
19
20 Tracer::Tracer(opentelemetry::nostd::string_view service_name) {
21 init(service_name);
22 }
23
24 void Tracer::init(opentelemetry::nostd::string_view service_name) {
25 if (!tracer) {
26 opentelemetry::exporter::jaeger::JaegerExporterOptions exporter_options;
27 if (g_ceph_context) {
28 exporter_options.server_port = g_ceph_context->_conf.get_val<int64_t>("jaeger_agent_port");
29 }
30 const opentelemetry::sdk::trace::BatchSpanProcessorOptions processor_options;
31 const auto jaeger_resource = opentelemetry::sdk::resource::Resource::Create(std::move(opentelemetry::sdk::resource::ResourceAttributes{{"service.name", service_name}}));
32 auto jaeger_exporter = std::unique_ptr<opentelemetry::sdk::trace::SpanExporter>(new opentelemetry::exporter::jaeger::JaegerExporter(exporter_options));
33 auto processor = std::unique_ptr<opentelemetry::sdk::trace::SpanProcessor>(new opentelemetry::sdk::trace::BatchSpanProcessor(std::move(jaeger_exporter), processor_options));
34 const auto provider = opentelemetry::nostd::shared_ptr<opentelemetry::trace::TracerProvider>(new opentelemetry::sdk::trace::TracerProvider(std::move(processor), jaeger_resource));
35 opentelemetry::trace::Provider::SetTracerProvider(provider);
36 tracer = provider->GetTracer(service_name, OPENTELEMETRY_SDK_VERSION);
37 }
38 }
39
40 jspan Tracer::start_trace(opentelemetry::nostd::string_view trace_name) {
41 if (is_enabled()) {
42 return tracer->StartSpan(trace_name);
43 }
44 return noop_span;
45 }
46
47 jspan Tracer::start_trace(opentelemetry::nostd::string_view trace_name, bool trace_is_enabled) {
48 if (trace_is_enabled) {
49 return tracer->StartSpan(trace_name);
50 }
51 return noop_tracer->StartSpan(trace_name);
52 }
53
54 jspan Tracer::add_span(opentelemetry::nostd::string_view span_name, const jspan& parent_span) {
55 if (is_enabled() && parent_span->IsRecording()) {
56 opentelemetry::trace::StartSpanOptions span_opts;
57 span_opts.parent = parent_span->GetContext();
58 return tracer->StartSpan(span_name, span_opts);
59 }
60 return noop_span;
61 }
62
63 jspan Tracer::add_span(opentelemetry::nostd::string_view span_name, const jspan_context& parent_ctx) {
64 if (is_enabled() && parent_ctx.IsValid()) {
65 opentelemetry::trace::StartSpanOptions span_opts;
66 span_opts.parent = parent_ctx;
67 return tracer->StartSpan(span_name, span_opts);
68 }
69 return noop_span;
70 }
71
72 bool Tracer::is_enabled() const {
73 return g_ceph_context->_conf->jaeger_tracing_enable;
74 }
75
76 void encode(const jspan_context& span_ctx, bufferlist& bl, uint64_t f) {
77 ENCODE_START(1, 1, bl);
78 using namespace opentelemetry;
79 using namespace trace;
80 auto is_valid = span_ctx.IsValid();
81 encode(is_valid, bl);
82 if (is_valid) {
83 encode_nohead(std::string_view(reinterpret_cast<const char*>(span_ctx.trace_id().Id().data()), TraceId::kSize), bl);
84 encode_nohead(std::string_view(reinterpret_cast<const char*>(span_ctx.span_id().Id().data()), SpanId::kSize), bl);
85 encode(span_ctx.trace_flags().flags(), bl);
86 }
87 ENCODE_FINISH(bl);
88 }
89
90 void decode(jspan_context& span_ctx, bufferlist::const_iterator& bl) {
91 using namespace opentelemetry;
92 using namespace trace;
93 DECODE_START(1, bl);
94 bool is_valid;
95 decode(is_valid, bl);
96 if (is_valid) {
97 std::array<uint8_t, TraceId::kSize> trace_id;
98 std::array<uint8_t, SpanId::kSize> span_id;
99 uint8_t flags;
100 decode(trace_id, bl);
101 decode(span_id, bl);
102 decode(flags, bl);
103 span_ctx = SpanContext(
104 TraceId(nostd::span<uint8_t, TraceId::kSize>(trace_id)),
105 SpanId(nostd::span<uint8_t, SpanId::kSize>(span_id)),
106 TraceFlags(flags),
107 true);
108 }
109 DECODE_FINISH(bl);
110 }
111 } // namespace tracing
112
113 #endif // HAVE_JAEGER