76 lines
2.8 KiB
C++
76 lines
2.8 KiB
C++
#include "observability/tracing/tracer.h"
|
|||
|
|
|
||
|
|
#include "observability/config.h"
|
||
|
|
#include "observability/tracing/batch_processor.h"
|
||
|
|
|
||
|
|
namespace molo::trace {
|
||
|
|
|
||
|
|
Tracer::Tracer(std::string name, std::shared_ptr<Sampler> sampler, BatchProcessor* processor)
|
||
|
|
: name_(std::move(name)),
|
||
|
|
sampler_(std::move(sampler)),
|
||
|
|
processor_(processor),
|
||
|
|
started_(0),
|
||
|
|
dropped_(0) {
|
||
|
|
if (!sampler_) sampler_ = std::make_shared<AlwaysOnSampler>();
|
||
|
|
}
|
||
|
|
|
||
|
|
Tracer::Tracer(std::string name)
|
||
|
|
: Tracer(std::move(name), std::make_shared<AlwaysOnSampler>(), nullptr) {}
|
||
|
|
|
||
|
|
Span Tracer::start_span(std::string_view name, SpanKind kind, const SpanContext& parent) {
|
||
|
|
started_.fetch_add(1, std::memory_order_relaxed);
|
||
|
|
|
||
|
|
const bool has_parent = parent.is_valid();
|
||
|
|
TraceId trace_id = has_parent ? parent.trace_id : TraceId::generate();
|
||
|
|
SpanId span_id = SpanId::generate();
|
||
|
|
// Head-based and immutable: children inherit the parent decision,
|
||
|
|
// roots ask the sampler exactly once.
|
||
|
|
const bool sampled = has_parent ? parent.is_sampled() : sampler_->should_sample(trace_id);
|
||
|
|
|
||
|
|
SpanContext context;
|
||
|
|
context.trace_id = trace_id;
|
||
|
|
context.span_id = span_id;
|
||
|
|
context.flags.value = sampled ? TraceFlags::kSampled : 0;
|
||
|
|
if (has_parent) context.tracestate = parent.tracestate;
|
||
|
|
|
||
|
|
if constexpr (!molo::obs::kTracingOn) {
|
||
|
|
dropped_.fetch_add(1, std::memory_order_relaxed);
|
||
|
|
return Span::null_with_context(context);
|
||
|
|
}
|
||
|
|
if (!molo::obs::RuntimeToggles::instance().tracing.load(std::memory_order_relaxed)) {
|
||
|
|
dropped_.fetch_add(1, std::memory_order_relaxed);
|
||
|
|
return Span::null_with_context(context);
|
||
|
|
}
|
||
|
|
if (!sampled) return Span::null_with_context(context);
|
||
|
|
BatchProcessor* processor = processor_.load(std::memory_order_relaxed);
|
||
|
|
if (processor == nullptr) {
|
||
|
|
dropped_.fetch_add(1, std::memory_order_relaxed);
|
||
|
|
return Span::null_with_context(context);
|
||
|
|
}
|
||
|
|
SpanId parent_span = has_parent ? parent.span_id : SpanId{};
|
||
|
|
return Span(std::string(name), kind, context, parent_span,
|
||
|
|
[this](FinishedSpan finished) {
|
||
|
|
BatchProcessor* target = processor_.load(std::memory_order_relaxed);
|
||
|
|
if (target != nullptr) {
|
||
|
|
target->add(std::move(finished));
|
||
|
|
} else {
|
||
|
|
dropped_.fetch_add(1, std::memory_order_relaxed);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
void Tracer::set_processor(BatchProcessor* processor) {
|
||
|
|
processor_.store(processor, std::memory_order_relaxed);
|
||
|
|
}
|
||
|
|
|
||
|
|
std::uint64_t Tracer::spans_started() const { return started_.load(std::memory_order_relaxed); }
|
||
|
|
|
||
|
|
std::uint64_t Tracer::spans_dropped() const { return dropped_.load(std::memory_order_relaxed); }
|
||
|
|
|
||
|
|
Tracer& Tracer::global() {
|
||
|
|
static Tracer instance("molo-global", std::make_shared<AlwaysOnSampler>(), nullptr);
|
||
|
|
return instance;
|
||
|
|
}
|
||
|
|
|
||
|
|
} // namespace molo::trace
|