feat: implement MoloApp plans 01-15 with integration

Full C++23 framework tree: runtime, observability, security,
data, app, realtime, supervisor, clients, CLI, example plus
enterprise tenancy, audit, operability, governance,
supplychain. Postgres via docker compose for tests.
This commit is contained in:
2026-10-23 18:20:04 +02:00
parent 54068f1953
commit 3c4334cec9
1068 changed files with 100899 additions and 2 deletions
@@ -0,0 +1,96 @@
#include "observability/tracing/batch_processor.h"
namespace molo::trace {
BatchProcessor::BatchProcessor(std::shared_ptr<Exporter> exporter, std::size_t max_buffer,
std::size_t batch_size, std::chrono::seconds interval)
: exporter_(std::move(exporter)),
max_buffer_(max_buffer),
batch_size_(batch_size),
interval_(interval) {
if (max_buffer_ < 1) max_buffer_ = 1;
if (batch_size_ < 1) batch_size_ = 1;
worker_ = std::thread([this] { run(); });
}
BatchProcessor::~BatchProcessor() {
{
std::lock_guard<std::mutex> lock(mutex_);
running_ = false;
}
cv_.notify_all();
if (worker_.joinable()) worker_.join();
flush();
}
void BatchProcessor::add(FinishedSpan span) {
bool ready = false;
{
std::lock_guard<std::mutex> lock(mutex_);
if (queue_.size() >= max_buffer_) {
dropped_.fetch_add(1, std::memory_order_relaxed);
return;
}
queue_.push_back(std::move(span));
ready = queue_.size() >= batch_size_;
}
if (ready) cv_.notify_one();
}
void BatchProcessor::flush() {
for (;;) {
std::vector<FinishedSpan> batch;
{
std::unique_lock<std::mutex> lock(mutex_);
if (queue_.empty()) {
flushed_cv_.wait(lock, [this] { return in_flight_ == 0; });
return;
}
batch.reserve(batch_size_);
for (auto it = queue_.begin(); it != queue_.end() && batch.size() < batch_size_;) {
batch.push_back(std::move(*it));
it = queue_.erase(it);
}
++in_flight_;
}
bool ok = false;
try {
ok = exporter_->export_spans(std::move(batch));
} catch (...) {
ok = false;
}
{
std::lock_guard<std::mutex> lock(mutex_);
--in_flight_;
if (ok) {
exported_.fetch_add(1, std::memory_order_relaxed);
} else {
failed_.fetch_add(1, std::memory_order_relaxed);
}
}
flushed_cv_.notify_all();
}
}
std::uint64_t BatchProcessor::dropped() const { return dropped_.load(std::memory_order_relaxed); }
std::uint64_t BatchProcessor::exported_total() const {
return exported_.load(std::memory_order_relaxed);
}
std::uint64_t BatchProcessor::failed_batches() const {
return failed_.load(std::memory_order_relaxed);
}
void BatchProcessor::run() {
for (;;) {
{
std::unique_lock<std::mutex> lock(mutex_);
cv_.wait_for(lock, interval_, [this] { return !running_ || queue_.size() >= batch_size_; });
if (!running_) return;
}
flush();
}
}
} // namespace molo::trace
@@ -0,0 +1,62 @@
#pragma once
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <cstddef>
#include <cstdint>
#include <deque>
#include <memory>
#include <mutex>
#include <thread>
#include <vector>
#include "observability/config.h"
#include "observability/tracing/exporter.h"
namespace molo::trace {
// Bounded span buffer plus background batch export. Exports when the batch
// size is reached or the interval elapses, whichever comes first. A full
// buffer drops spans and counts the drop, never grows memory.
class BatchProcessor {
public:
explicit BatchProcessor(std::shared_ptr<Exporter> exporter,
std::size_t max_buffer = molo::obs::Caps::kSpanBuffer,
std::size_t batch_size = molo::obs::Caps::kSpanBatch,
std::chrono::seconds interval = std::chrono::seconds(
molo::obs::Caps::kSpanBatchIntervalSec));
~BatchProcessor();
BatchProcessor(const BatchProcessor&) = delete;
BatchProcessor& operator=(const BatchProcessor&) = delete;
void add(FinishedSpan span);
// Exports everything queued and waits for in-flight exports, so every
// span added before the call is exported when it returns.
void flush();
std::uint64_t dropped() const;
std::uint64_t exported_total() const;
std::uint64_t failed_batches() const;
private:
void run();
std::shared_ptr<Exporter> exporter_;
std::size_t max_buffer_;
std::size_t batch_size_;
std::chrono::seconds interval_;
mutable std::mutex mutex_;
std::condition_variable cv_;
std::condition_variable flushed_cv_;
std::deque<FinishedSpan> queue_;
std::size_t in_flight_ = 0;
std::thread worker_;
bool running_ = true;
std::atomic<std::uint64_t> dropped_{0};
std::atomic<std::uint64_t> exported_{0};
std::atomic<std::uint64_t> failed_{0};
};
} // namespace molo::trace
+19
View File
@@ -0,0 +1,19 @@
#pragma once
#include <string>
#include <vector>
#include "observability/tracing/span.h"
namespace molo::trace {
// Sink interface for finished spans. Exporters are the only tracing
// components allowed to touch I/O.
class Exporter {
public:
virtual ~Exporter() = default;
virtual bool export_spans(std::vector<FinishedSpan> batch) = 0;
virtual std::string name() const = 0;
};
} // namespace molo::trace
+56
View File
@@ -0,0 +1,56 @@
#pragma once
#include <atomic>
#include <chrono>
#include <cstdint>
#include <functional>
#include <string>
#include <utility>
#include <vector>
namespace molo::trace {
// Minimal HTTP POST interface used by the OTLP exporter. The production
// sender is the framework HTTP client (wired by 08-clients-openapi.md);
// until then NullHttpSender is the marked stub.
class HttpSender {
public:
using HeaderList = std::vector<std::pair<std::string, std::string>>;
virtual ~HttpSender() = default;
virtual bool post(const std::string& url, const HeaderList& headers, const std::string& body,
std::chrono::seconds timeout) = 0;
};
// STUB: drops every request and reports failure. Replace with the
// framework HTTP client sender once 08-clients-openapi.md lands.
class NullHttpSender : public HttpSender {
public:
bool post(const std::string& /*url*/, const HeaderList& /*headers*/,
const std::string& /*body*/, std::chrono::seconds /*timeout*/) override {
attempts_.fetch_add(1, std::memory_order_relaxed);
return false;
}
std::uint64_t attempts() const { return attempts_.load(std::memory_order_relaxed); }
private:
std::atomic<std::uint64_t> attempts_{0};
};
// Test and wiring double: delegates to a callback.
class FunctionHttpSender : public HttpSender {
public:
using PostFn = std::function<bool(const std::string&, const HeaderList&, const std::string&,
std::chrono::seconds)>;
explicit FunctionHttpSender(PostFn fn) : fn_(std::move(fn)) {}
bool post(const std::string& url, const HeaderList& headers, const std::string& body,
std::chrono::seconds timeout) override {
return fn_(url, headers, body, timeout);
}
private:
PostFn fn_;
};
} // namespace molo::trace
+170
View File
@@ -0,0 +1,170 @@
#pragma once
#include <array>
#include <atomic>
#include <cstdint>
#include <optional>
#include <random>
#include <string>
#include <string_view>
#include <thread>
namespace molo::trace {
namespace detail {
inline int hex_value(char ch) {
if (ch >= '0' && ch <= '9') return ch - '0';
if (ch >= 'a' && ch <= 'f') return ch - 'a' + 10;
if (ch >= 'A' && ch <= 'F') return ch - 'A' + 10;
return -1;
}
inline void append_hex(std::string& out, const std::uint8_t* bytes, std::size_t count) {
static constexpr char kDigits[] = "0123456789abcdef";
for (std::size_t i = 0; i < count; ++i) {
out += kDigits[bytes[i] >> 4];
out += kDigits[bytes[i] & 0x0f];
}
}
inline std::mt19937_64& id_rng() {
static std::atomic<std::uint64_t> salt{0};
std::size_t thread_salt = std::hash<std::thread::id>{}(std::this_thread::get_id());
thread_local std::mt19937_64 rng{std::random_device{}() ^ thread_salt ^
salt.fetch_add(0x9e3779b97f4a7c15ULL, std::memory_order_relaxed)};
return rng;
}
} // namespace detail
// 128-bit trace ID, W3C hex encoded (32 chars).
class TraceId {
public:
TraceId() = default;
static TraceId generate() {
TraceId id;
do {
std::uint64_t hi = detail::id_rng()();
std::uint64_t lo = detail::id_rng()();
for (int i = 0; i < 8; ++i) {
id.bytes_[i] = static_cast<std::uint8_t>(hi >> (56 - 8 * i));
id.bytes_[8 + i] = static_cast<std::uint8_t>(lo >> (56 - 8 * i));
}
} while (!id.valid());
return id;
}
static std::optional<TraceId> from_hex(std::string_view text) {
if (text.size() != 32) return std::nullopt;
TraceId id;
for (std::size_t i = 0; i < 16; ++i) {
int hi = detail::hex_value(text[2 * i]);
int lo = detail::hex_value(text[2 * i + 1]);
if (hi < 0 || lo < 0) return std::nullopt;
id.bytes_[i] = static_cast<std::uint8_t>((hi << 4) | lo);
}
return id;
}
std::string to_hex() const {
std::string out;
out.reserve(32);
detail::append_hex(out, bytes_.data(), bytes_.size());
return out;
}
bool valid() const {
for (std::uint8_t byte : bytes_) {
if (byte != 0) return true;
}
return false;
}
std::uint64_t prefix_u64() const {
std::uint64_t value = 0;
for (int i = 0; i < 8; ++i) value = (value << 8) | bytes_[i];
return value;
}
bool operator==(const TraceId& other) const { return bytes_ == other.bytes_; }
bool operator!=(const TraceId& other) const { return !(*this == other); }
private:
std::array<std::uint8_t, 16> bytes_{};
};
// 64-bit span ID, W3C hex encoded (16 chars).
class SpanId {
public:
SpanId() = default;
static SpanId generate() {
SpanId id;
do {
std::uint64_t raw = detail::id_rng()();
for (int i = 0; i < 8; ++i) id.bytes_[i] = static_cast<std::uint8_t>(raw >> (56 - 8 * i));
} while (!id.valid());
return id;
}
static std::optional<SpanId> from_hex(std::string_view text) {
if (text.size() != 16) return std::nullopt;
SpanId id;
for (std::size_t i = 0; i < 8; ++i) {
int hi = detail::hex_value(text[2 * i]);
int lo = detail::hex_value(text[2 * i + 1]);
if (hi < 0 || lo < 0) return std::nullopt;
id.bytes_[i] = static_cast<std::uint8_t>((hi << 4) | lo);
}
return id;
}
std::string to_hex() const {
std::string out;
out.reserve(16);
detail::append_hex(out, bytes_.data(), bytes_.size());
return out;
}
bool valid() const {
for (std::uint8_t byte : bytes_) {
if (byte != 0) return true;
}
return false;
}
bool operator==(const SpanId& other) const { return bytes_ == other.bytes_; }
bool operator!=(const SpanId& other) const { return !(*this == other); }
private:
std::array<std::uint8_t, 8> bytes_{};
};
// One-byte W3C trace flags. Bit 0 is the sampled flag.
struct TraceFlags {
std::uint8_t value = 0;
static constexpr std::uint8_t kSampled = 0x01;
bool is_sampled() const { return (value & kSampled) != 0; }
std::string to_hex() const {
std::string out;
out.reserve(2);
detail::append_hex(out, &value, 1);
return out;
}
static std::optional<TraceFlags> from_hex(std::string_view text) {
if (text.size() != 2) return std::nullopt;
int hi = detail::hex_value(text[0]);
int lo = detail::hex_value(text[1]);
if (hi < 0 || lo < 0) return std::nullopt;
TraceFlags flags;
flags.value = static_cast<std::uint8_t>((hi << 4) | lo);
return flags;
}
};
} // namespace molo::trace
@@ -0,0 +1,38 @@
#pragma once
#include <mutex>
#include <string>
#include <vector>
#include "observability/tracing/exporter.h"
namespace molo::trace {
// In-memory exporter for tests and inspection. Unbounded by design for
// test use only; production paths use the bounded batch processor.
class MemoryExporter : public Exporter {
public:
bool export_spans(std::vector<FinishedSpan> batch) override {
std::lock_guard<std::mutex> lock(mutex_);
for (auto& span : batch) spans_.push_back(std::move(span));
return true;
}
std::string name() const override { return "memory"; }
std::vector<FinishedSpan> spans() const {
std::lock_guard<std::mutex> lock(mutex_);
return spans_;
}
void clear() {
std::lock_guard<std::mutex> lock(mutex_);
spans_.clear();
}
private:
mutable std::mutex mutex_;
std::vector<FinishedSpan> spans_;
};
} // namespace molo::trace
+192
View File
@@ -0,0 +1,192 @@
#include "observability/tracing/otlp_http.h"
#include <cmath>
#include <cstdio>
#include <thread>
#include "observability/logging/json_format.h"
namespace molo::trace {
namespace {
int kind_number(SpanKind kind) {
switch (kind) {
case SpanKind::Internal:
return 1;
case SpanKind::Server:
return 2;
case SpanKind::Client:
return 3;
case SpanKind::Producer:
return 4;
case SpanKind::Consumer:
return 5;
}
return 1;
}
int status_number(StatusCode code) {
switch (code) {
case StatusCode::Unset:
return 0;
case StatusCode::Ok:
return 1;
case StatusCode::Error:
return 2;
}
return 0;
}
std::string nanos_json(std::chrono::system_clock::time_point time) {
auto nanos = std::chrono::duration_cast<std::chrono::nanoseconds>(time.time_since_epoch()).count();
if (nanos < 0) nanos = 0;
return "\"" + std::to_string(static_cast<unsigned long long>(nanos)) + "\"";
}
std::string attribute_value_json(const AttributeValue& value) {
return std::visit(
[](auto&& raw) -> std::string {
using T = std::decay_t<decltype(raw)>;
if constexpr (std::is_same_v<T, std::string>) {
return "{\"stringValue\":\"" + molo::log::escape_json(raw) + "\"}";
} else if constexpr (std::is_same_v<T, bool>) {
return std::string("{\"boolValue\":") + (raw ? "true}" : "false}");
} else if constexpr (std::is_same_v<T, double>) {
if (!std::isfinite(raw)) return "{\"doubleValue\":0}";
return std::string("{\"doubleValue\":") + molo::obs::format_double_finite(raw) + "}";
} else {
return "{\"intValue\":\"" + std::to_string(raw) + "\"}";
}
},
value);
}
void append_attributes(std::string& out, const std::vector<Attribute>& attributes) {
out += "\"attributes\":[";
bool first = true;
for (const Attribute& attr : attributes) {
if (!first) out += ",";
first = false;
out += "{\"key\":\"";
out += molo::log::escape_json(attr.key);
out += "\",\"value\":";
out += attribute_value_json(attr.value);
out += "}";
}
out += "]";
}
void append_span(std::string& out, const FinishedSpan& span) {
out += "{\"traceId\":\"";
out += span.context.trace_id.to_hex();
out += "\",\"spanId\":\"";
out += span.context.span_id.to_hex();
out += "\"";
if (span.parent_span_id.valid()) {
out += ",\"parentSpanId\":\"";
out += span.parent_span_id.to_hex();
out += "\"";
}
out += ",\"name\":\"";
out += molo::log::escape_json(span.name);
out += "\",\"kind\":";
out += std::to_string(kind_number(span.kind));
out += ",\"startTimeUnixNano\":";
out += nanos_json(span.start);
out += ",\"endTimeUnixNano\":";
out += nanos_json(span.end);
out += ",";
append_attributes(out, span.attributes);
out += ",\"events\":[";
bool first = true;
for (const SpanEvent& event : span.events) {
if (!first) out += ",";
first = false;
out += "{\"timeUnixNano\":";
out += nanos_json(event.timestamp);
out += ",\"name\":\"";
out += molo::log::escape_json(event.name);
out += "\",\"attributes\":[]}";
}
out += "],\"status\":{\"code\":";
out += std::to_string(status_number(span.status));
if (!span.status_desc.empty()) {
out += ",\"message\":\"";
out += molo::log::escape_json(span.status_desc);
out += "\"";
}
out += "}}";
}
} // namespace
OtlpHttpExporter::OtlpHttpExporter(std::string endpoint, std::shared_ptr<HttpSender> sender,
std::vector<std::chrono::milliseconds> backoff,
std::chrono::seconds timeout, bool gzip)
: endpoint_(std::move(endpoint)),
sender_(std::move(sender)),
backoff_(std::move(backoff)),
timeout_(timeout),
gzip_(gzip) {
if (!sender_) sender_ = std::make_shared<NullHttpSender>();
}
bool OtlpHttpExporter::export_spans(std::vector<FinishedSpan> batch) {
if (batch.empty()) return true;
std::string body = build_request_json(batch, "molo");
HttpSender::HeaderList headers = {{"Content-Type", "application/json"}};
if (gzip_ && compressor_) {
std::string compressed;
if (compressor_(body, compressed)) {
body = std::move(compressed);
headers.emplace_back("Content-Encoding", "gzip");
}
}
// First attempt plus at most kOtlpRetries retries with backoff.
const int max_attempts = 1 + molo::obs::Caps::kOtlpRetries;
for (int attempt = 0; attempt < max_attempts; ++attempt) {
requests_.fetch_add(1, std::memory_order_relaxed);
bool ok = false;
try {
ok = sender_->post(endpoint_, headers, body, timeout_);
} catch (...) {
ok = false;
}
if (ok) return true;
failed_.fetch_add(1, std::memory_order_relaxed);
if (attempt + 1 < max_attempts && !backoff_.empty()) {
std::size_t index = static_cast<std::size_t>(attempt);
if (index >= backoff_.size()) index = backoff_.size() - 1;
std::this_thread::sleep_for(backoff_[index]);
}
}
return false;
}
void OtlpHttpExporter::set_compressor(Compressor compressor) { compressor_ = std::move(compressor); }
std::string OtlpHttpExporter::build_request_json(const std::vector<FinishedSpan>& batch,
const std::string& scope_name) {
std::string out = "{\"resourceSpans\":[{\"resource\":{\"attributes\":[]},\"scopeSpans\":[{";
out += "\"scope\":{\"name\":\"";
out += molo::log::escape_json(scope_name);
out += "\"},\"spans\":[";
bool first = true;
for (const FinishedSpan& span : batch) {
if (!first) out += ",";
first = false;
append_span(out, span);
}
out += "]}]}]}";
return out;
}
std::uint64_t OtlpHttpExporter::requests_total() const {
return requests_.load(std::memory_order_relaxed);
}
std::uint64_t OtlpHttpExporter::requests_failed() const {
return failed_.load(std::memory_order_relaxed);
}
} // namespace molo::trace
+55
View File
@@ -0,0 +1,55 @@
#pragma once
#include <atomic>
#include <chrono>
#include <cstdint>
#include <functional>
#include <memory>
#include <string>
#include <vector>
#include "observability/config.h"
#include "observability/tracing/exporter.h"
#include "observability/tracing/http_sender.h"
namespace molo::trace {
// OTLP/HTTP JSON exporter: POSTs ExportTraceServiceRequest documents to a
// collector with a 10 s timeout and at most 3 retries with backoff.
// gzip is only applied when a compressor is injected (the gzip codec is
// owned by 01-runtime.md); otherwise identity is sent and documented.
class OtlpHttpExporter : public Exporter {
public:
using Compressor = std::function<bool(const std::string& input, std::string& output)>;
explicit OtlpHttpExporter(
std::string endpoint, std::shared_ptr<HttpSender> sender,
std::vector<std::chrono::milliseconds> backoff = {std::chrono::milliseconds(100),
std::chrono::milliseconds(500),
std::chrono::milliseconds(2000)},
std::chrono::seconds timeout = std::chrono::seconds(molo::obs::Caps::kOtlpTimeoutSec),
bool gzip = false);
bool export_spans(std::vector<FinishedSpan> batch) override;
std::string name() const override { return "otlp-http"; }
void set_compressor(Compressor compressor);
static std::string build_request_json(const std::vector<FinishedSpan>& batch,
const std::string& scope_name);
std::uint64_t requests_total() const;
std::uint64_t requests_failed() const;
private:
std::string endpoint_;
std::shared_ptr<HttpSender> sender_;
std::vector<std::chrono::milliseconds> backoff_;
std::chrono::seconds timeout_;
bool gzip_;
Compressor compressor_;
std::atomic<std::uint64_t> requests_{0};
std::atomic<std::uint64_t> failed_{0};
};
} // namespace molo::trace
+100
View File
@@ -0,0 +1,100 @@
#include "observability/tracing/propagator.h"
#include <cctype>
#include <vector>
#include "observability/config.h"
namespace molo::trace {
namespace {
Headers::const_iterator find_header_ci(const Headers& headers, const char* name) {
std::string wanted(name);
for (auto it = headers.begin(); it != headers.end(); ++it) {
const std::string& key = it->first;
if (key.size() != wanted.size()) continue;
bool same = true;
for (std::size_t i = 0; i < key.size(); ++i) {
if (std::tolower(static_cast<unsigned char>(key[i])) != wanted[i]) {
same = false;
break;
}
}
if (same) return it;
}
return headers.end();
}
std::vector<std::string> split_dash(std::string_view text) {
std::vector<std::string> parts;
std::string current;
for (char ch : text) {
if (ch == '-') {
parts.push_back(current);
current.clear();
} else {
current += ch;
}
}
parts.push_back(current);
return parts;
}
} // namespace
std::optional<SpanContext> W3CPropagator::extract(const Headers& headers,
std::function<void()> on_error) {
auto fail = [&]() -> std::optional<SpanContext> {
if (on_error) {
try {
on_error();
} catch (...) {
}
}
return std::nullopt;
};
auto it = find_header_ci(headers, "traceparent");
if (it == headers.end()) return std::nullopt;
std::vector<std::string> parts = split_dash(it->second);
if (parts.size() < 4) return fail();
if (parts[0].size() != 2 || parts[0] == "ff") return fail();
bool version_hex = true;
for (char ch : parts[0]) {
if (!std::isxdigit(static_cast<unsigned char>(ch))) version_hex = false;
}
if (!version_hex) return fail();
auto trace_id = TraceId::from_hex(parts[1]);
auto parent_id = SpanId::from_hex(parts[2]);
auto flags = TraceFlags::from_hex(parts[3]);
if (!trace_id.has_value() || !trace_id->valid()) return fail();
if (!parent_id.has_value() || !parent_id->valid()) return fail();
if (!flags.has_value()) return fail();
SpanContext context;
context.trace_id = *trace_id;
context.span_id = *parent_id;
context.flags = *flags;
auto state = find_header_ci(headers, "tracestate");
if (state != headers.end()) {
context.tracestate = state->second;
if (context.tracestate.size() > molo::obs::Caps::kMaxTracestate) {
context.tracestate.resize(molo::obs::Caps::kMaxTracestate);
}
}
return context;
}
void W3CPropagator::inject(const SpanContext& context, Headers& headers) {
if (!context.is_valid()) return;
headers["traceparent"] = render_traceparent(context);
if (!context.tracestate.empty()) headers["tracestate"] = context.tracestate;
}
std::string W3CPropagator::render_traceparent(const SpanContext& context) {
return "00-" + context.trace_id.to_hex() + "-" + context.span_id.to_hex() + "-" +
context.flags.to_hex();
}
} // namespace molo::trace
+26
View File
@@ -0,0 +1,26 @@
#pragma once
#include <functional>
#include <map>
#include <optional>
#include <string>
#include "observability/tracing/span_context.h"
namespace molo::trace {
using Headers = std::map<std::string, std::string>;
// W3C Trace Context propagation: parses inbound traceparent/tracestate
// headers and injects the current context into outbound headers.
// Malformed headers start a new trace and fire on_error (wired to
// molo_trace_propagator_errors_total).
class W3CPropagator {
public:
static std::optional<SpanContext> extract(const Headers& headers,
std::function<void()> on_error = nullptr);
static void inject(const SpanContext& context, Headers& headers);
static std::string render_traceparent(const SpanContext& context);
};
} // namespace molo::trace
+57
View File
@@ -0,0 +1,57 @@
#pragma once
#include <cstdint>
#include <limits>
#include <memory>
#include <string>
#include "observability/tracing/ids.h"
namespace molo::trace {
// Head-based sampler: the decision is made once per trace from the trace
// ID and stays immutable for the lifetime of that trace.
class Sampler {
public:
virtual ~Sampler() = default;
virtual bool should_sample(const TraceId& trace_id) = 0;
virtual std::string name() const = 0;
};
class AlwaysOnSampler : public Sampler {
public:
bool should_sample(const TraceId& /*trace_id*/) override { return true; }
std::string name() const override { return "always_on"; }
};
class AlwaysOffSampler : public Sampler {
public:
bool should_sample(const TraceId& /*trace_id*/) override { return false; }
std::string name() const override { return "always_off"; }
};
// Samples a deterministic fraction of traces. The same trace ID always
// gets the same decision.
class RatioSampler : public Sampler {
public:
explicit RatioSampler(double ratio) : ratio_(ratio) {
if (ratio_ < 0.0) ratio_ = 0.0;
if (ratio_ > 1.0) ratio_ = 1.0;
}
bool should_sample(const TraceId& trace_id) override {
if (ratio_ <= 0.0) return false;
if (ratio_ >= 1.0) return true;
const double threshold =
ratio_ * static_cast<double>(std::numeric_limits<std::uint64_t>::max());
return static_cast<double>(trace_id.prefix_u64()) < threshold;
}
std::string name() const override { return "ratio"; }
double ratio() const { return ratio_; }
private:
double ratio_;
};
} // namespace molo::trace
+120
View File
@@ -0,0 +1,120 @@
#include "observability/tracing/span.h"
#include "observability/config.h"
#include "observability/correlation/redaction.h"
namespace molo::trace {
std::string_view span_kind_name(SpanKind kind) {
switch (kind) {
case SpanKind::Internal:
return "internal";
case SpanKind::Server:
return "server";
case SpanKind::Client:
return "client";
case SpanKind::Producer:
return "producer";
case SpanKind::Consumer:
return "consumer";
}
return "internal";
}
struct Span::State {
std::string name;
SpanKind kind = SpanKind::Internal;
SpanId parent_span_id;
std::chrono::system_clock::time_point start = std::chrono::system_clock::now();
std::vector<Attribute> attributes;
std::vector<SpanEvent> events;
StatusCode status = StatusCode::Unset;
std::string status_desc;
bool attributes_truncated = false;
bool events_truncated = false;
bool ended = false;
EndHandler on_end;
};
Span::Span() = default;
Span Span::null_with_context(SpanContext context) {
Span span;
span.context_ = context;
return span;
}
Span::Span(std::string name, SpanKind kind, SpanContext context, SpanId parent_span_id,
EndHandler on_end)
: context_(context), state_(std::make_unique<State>()) {
state_->name = std::move(name);
state_->kind = kind;
state_->parent_span_id = parent_span_id;
state_->on_end = std::move(on_end);
}
Span::~Span() { end(); }
Span::Span(Span&& other) noexcept = default;
Span& Span::operator=(Span&& other) noexcept = default;
void Span::set_attribute(std::string key, AttributeValue value) {
if (state_ == nullptr || state_->ended) return;
if (molo::corr::is_sensitive_key(key)) {
value = std::string(molo::corr::kRedacted);
}
if (auto* text = std::get_if<std::string>(&value)) {
if (text->size() > molo::obs::Caps::kMaxAttrValue) {
text->resize(molo::obs::Caps::kMaxAttrValue);
state_->attributes_truncated = true;
}
}
if (state_->attributes.size() >= molo::obs::Caps::kMaxSpanAttrs) {
state_->attributes_truncated = true;
return;
}
state_->attributes.push_back(Attribute{std::move(key), std::move(value)});
}
void Span::add_event(std::string name) {
if (state_ == nullptr || state_->ended) return;
if (state_->events.size() >= molo::obs::Caps::kMaxSpanEvents) {
state_->events_truncated = true;
return;
}
SpanEvent event;
event.name = std::move(name);
state_->events.push_back(std::move(event));
}
void Span::set_status(StatusCode code, std::string description) {
if (state_ == nullptr || state_->ended) return;
state_->status = code;
state_->status_desc = std::move(description);
}
void Span::end() {
if (state_ == nullptr || state_->ended) return;
state_->ended = true;
FinishedSpan finished;
finished.name = std::move(state_->name);
finished.kind = state_->kind;
finished.context = context_;
finished.parent_span_id = state_->parent_span_id;
finished.start = state_->start;
finished.end = std::chrono::system_clock::now();
finished.attributes = std::move(state_->attributes);
finished.events = std::move(state_->events);
finished.status = state_->status;
finished.status_desc = std::move(state_->status_desc);
finished.attributes_truncated = state_->attributes_truncated;
finished.events_truncated = state_->events_truncated;
if (state_->on_end) {
try {
state_->on_end(std::move(finished));
} catch (...) {
}
}
}
} // namespace molo::trace
+85
View File
@@ -0,0 +1,85 @@
#pragma once
#include <chrono>
#include <cstdint>
#include <functional>
#include <memory>
#include <string>
#include <string_view>
#include <variant>
#include <vector>
#include "observability/tracing/ids.h"
#include "observability/tracing/span_context.h"
namespace molo::trace {
enum class SpanKind : std::uint8_t { Internal, Server, Client, Producer, Consumer };
std::string_view span_kind_name(SpanKind kind);
enum class StatusCode : std::uint8_t { Unset, Ok, Error };
using AttributeValue = std::variant<std::string, std::int64_t, double, bool>;
struct Attribute {
std::string key;
AttributeValue value;
};
struct SpanEvent {
std::chrono::system_clock::time_point timestamp = std::chrono::system_clock::now();
std::string name;
};
struct FinishedSpan {
std::string name;
SpanKind kind = SpanKind::Internal;
SpanContext context;
SpanId parent_span_id;
std::chrono::system_clock::time_point start;
std::chrono::system_clock::time_point end;
std::vector<Attribute> attributes;
std::vector<SpanEvent> events;
StatusCode status = StatusCode::Unset;
std::string status_desc;
bool attributes_truncated = false;
bool events_truncated = false;
};
// One distributed tracing span. RAII: ending happens on end() or on
// destruction. A null span (valid() == false) records nothing but still
// carries a usable context for propagation. Single-owner, thread-affine:
// a span is used from the thread that created it.
class Span {
public:
using EndHandler = std::function<void(FinishedSpan)>;
Span();
static Span null_with_context(SpanContext context);
Span(std::string name, SpanKind kind, SpanContext context, SpanId parent_span_id,
EndHandler on_end);
~Span();
Span(const Span&) = delete;
Span& operator=(const Span&) = delete;
Span(Span&& other) noexcept;
Span& operator=(Span&& other) noexcept;
bool valid() const { return state_ != nullptr; }
const SpanContext& context() const { return context_; }
void set_attribute(std::string key, AttributeValue value);
void add_event(std::string name);
void set_status(StatusCode code, std::string description = "");
void end();
private:
struct State;
SpanContext context_;
std::unique_ptr<State> state_;
};
} // namespace molo::trace
+22
View File
@@ -0,0 +1,22 @@
#pragma once
#include <string>
#include "observability/tracing/ids.h"
namespace molo::trace {
// Propagatable trace identity: trace ID, span ID, flags, vendor state.
struct SpanContext {
TraceId trace_id;
SpanId span_id;
TraceFlags flags;
std::string tracestate;
bool is_valid() const { return trace_id.valid() && span_id.valid(); }
bool is_sampled() const { return is_valid() && flags.is_sampled(); }
static SpanContext invalid() { return SpanContext{}; }
};
} // namespace molo::trace
+75
View File
@@ -0,0 +1,75 @@
#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
+47
View File
@@ -0,0 +1,47 @@
#pragma once
#include <atomic>
#include <cstdint>
#include <memory>
#include <string>
#include <string_view>
#include "observability/tracing/sampler.h"
#include "observability/tracing/span.h"
#include "observability/tracing/span_context.h"
namespace molo::trace {
class BatchProcessor;
// Creates spans. When tracing is disabled (compile time or runtime), when
// the sampler drops the trace, or when no processor is wired, start_span
// returns a null span: one branch, no allocation, no recording, but still
// carrying a valid context for propagation. Lifetime rule: every span must
// end before its tracer and batch processor are destroyed (tracers and
// processors are normally process-lifetime singletons).
class Tracer {
public:
Tracer(std::string name, std::shared_ptr<Sampler> sampler, BatchProcessor* processor);
explicit Tracer(std::string name);
Span start_span(std::string_view name, SpanKind kind = SpanKind::Internal,
const SpanContext& parent = SpanContext::invalid());
void set_processor(BatchProcessor* processor);
const std::string& name() const { return name_; }
std::uint64_t spans_started() const;
std::uint64_t spans_dropped() const;
static Tracer& global();
private:
std::string name_;
std::shared_ptr<Sampler> sampler_;
std::atomic<BatchProcessor*> processor_;
std::atomic<std::uint64_t> started_;
std::atomic<std::uint64_t> dropped_;
};
} // namespace molo::trace