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