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:
@@ -0,0 +1,140 @@
|
||||
// Correlation: request ID generation and validation, RequestContext
|
||||
// flow into logs, events and hooks (mirrors X-Request-ID behavior).
|
||||
|
||||
#include <set>
|
||||
#include <string>
|
||||
|
||||
#include "observability/correlation/redaction.h"
|
||||
#include "observability/correlation/request_context.h"
|
||||
#include "observability/correlation/request_id.h"
|
||||
#include "observability/events/event.h"
|
||||
#include "observability/hooks.h"
|
||||
#include "observability/logging/logger.h"
|
||||
#include "observability/logging/registry.h"
|
||||
#include "observability/logging/ring_sink.h"
|
||||
#include "observability/tracing/ids.h"
|
||||
#include "observability/tracing/span_context.h"
|
||||
#include "test_helper.h"
|
||||
|
||||
int main() {
|
||||
TestCtx ctx;
|
||||
|
||||
// Request IDs are unique 128-bit hex strings.
|
||||
{
|
||||
std::set<std::string> seen;
|
||||
for (int i = 0; i < 1000; ++i) {
|
||||
std::string id = molo::corr::generate_request_id();
|
||||
CHECK(ctx, id.size() == 32);
|
||||
seen.insert(id);
|
||||
}
|
||||
CHECK(ctx, seen.size() == 1000);
|
||||
}
|
||||
|
||||
// Validation and inbound normalization.
|
||||
CHECK(ctx, molo::corr::is_valid_request_id("abc-123_XYZ"));
|
||||
CHECK(ctx, !molo::corr::is_valid_request_id(""));
|
||||
CHECK(ctx, !molo::corr::is_valid_request_id("has space"));
|
||||
CHECK(ctx, !molo::corr::is_valid_request_id(std::string(65, 'a')));
|
||||
CHECK(ctx, molo::corr::normalize_request_id("good-id") == "good-id");
|
||||
CHECK(ctx, molo::corr::is_valid_request_id(molo::corr::normalize_request_id("bad id!")));
|
||||
|
||||
// RequestContext defaults and span attachment.
|
||||
{
|
||||
molo::corr::RequestContext fresh;
|
||||
CHECK(ctx, molo::corr::is_valid_request_id(fresh.request_id()));
|
||||
CHECK(ctx, !fresh.span_context().is_valid());
|
||||
molo::corr::RequestContext named("req-ctx-1");
|
||||
CHECK(ctx, named.request_id() == "req-ctx-1");
|
||||
molo::corr::RequestContext bad("bad id!");
|
||||
CHECK(ctx, bad.request_id() != "bad id!");
|
||||
|
||||
molo::trace::SpanContext span;
|
||||
span.trace_id = molo::trace::TraceId::generate();
|
||||
span.span_id = molo::trace::SpanId::generate();
|
||||
named.set_span_context(span);
|
||||
CHECK(ctx, named.span_context().trace_id == span.trace_id);
|
||||
molo::corr::RequestContext grown = named.with_span(molo::trace::SpanContext::invalid());
|
||||
CHECK(ctx, grown.request_id() == "req-ctx-1");
|
||||
CHECK(ctx, !grown.span_context().is_valid());
|
||||
}
|
||||
|
||||
// Context flows into log records.
|
||||
{
|
||||
molo::log::Registry registry;
|
||||
auto ring = std::make_shared<molo::log::RingSink>(16);
|
||||
registry.add_sink(ring);
|
||||
auto logger = registry.get("molo.http");
|
||||
molo::corr::RequestContext req("req-log-1");
|
||||
molo::trace::SpanContext span;
|
||||
span.trace_id = molo::trace::TraceId::generate();
|
||||
span.span_id = molo::trace::SpanId::generate();
|
||||
req.set_span_context(span);
|
||||
logger->set_request_id(req.request_id());
|
||||
logger->set_span(span.trace_id.to_hex(), span.span_id.to_hex());
|
||||
logger->log(molo::log::Level::Info, "request.finished");
|
||||
auto records = ring->snapshot();
|
||||
CHECK(ctx, records.size() == 1);
|
||||
CHECK(ctx, records[0].request_id == "req-log-1");
|
||||
CHECK(ctx, records[0].trace_id == span.trace_id.to_hex());
|
||||
CHECK(ctx, records[0].span_id == span.span_id.to_hex());
|
||||
}
|
||||
|
||||
// Context flows into events.
|
||||
{
|
||||
molo::corr::RequestContext req("req-evt-1");
|
||||
molo::trace::SpanContext span;
|
||||
span.trace_id = molo::trace::TraceId::generate();
|
||||
span.span_id = molo::trace::SpanId::generate();
|
||||
req.set_span_context(span);
|
||||
molo::events::Event event = molo::events::Event::from_context("custom", req);
|
||||
CHECK(ctx, event.request_id() == "req-evt-1");
|
||||
CHECK(ctx, event.trace_id() == span.trace_id.to_hex());
|
||||
CHECK(ctx, event.span_id() == span.span_id.to_hex());
|
||||
}
|
||||
|
||||
// Redaction helper behavior.
|
||||
CHECK(ctx, molo::corr::is_sensitive_key("password"));
|
||||
CHECK(ctx, molo::corr::is_sensitive_key("X-Auth-Token"));
|
||||
CHECK(ctx, molo::corr::is_sensitive_key("session_state"));
|
||||
CHECK(ctx, molo::corr::is_sensitive_key("request_body"));
|
||||
CHECK(ctx, !molo::corr::is_sensitive_key("table"));
|
||||
CHECK(ctx, !molo::corr::is_sensitive_key("monkey"));
|
||||
CHECK(ctx, !molo::corr::is_sensitive_key("keyboard"));
|
||||
CHECK(ctx, molo::corr::is_sensitive_key("api_key"));
|
||||
CHECK(ctx, molo::corr::is_sensitive_key("key"));
|
||||
CHECK(ctx, molo::corr::is_sensitive_key("key-id"));
|
||||
CHECK(ctx, molo::corr::is_sensitive_key("cacheKey"));
|
||||
CHECK(ctx, molo::corr::redact_value("token", "abc") == "***");
|
||||
CHECK(ctx, molo::corr::redact_value("table", "users") == "users");
|
||||
|
||||
// Lifecycle hooks are safe unset and fire when set.
|
||||
{
|
||||
molo::corr::RequestContext req("req-hook-1");
|
||||
molo::obs::fire_server_start();
|
||||
molo::obs::fire_server_stop();
|
||||
molo::obs::fire_request_start(req);
|
||||
molo::obs::fire_request_finish(req);
|
||||
int starts = 0;
|
||||
int finishes = 0;
|
||||
int server_starts = 0;
|
||||
molo::obs::LifecycleHooks::global().on_server_start = [&server_starts] { ++server_starts; };
|
||||
molo::obs::LifecycleHooks::global().on_request_start =
|
||||
[&](const molo::corr::RequestContext& context) {
|
||||
++starts;
|
||||
CHECK(ctx, context.request_id() == "req-hook-1");
|
||||
};
|
||||
molo::obs::LifecycleHooks::global().on_request_finish =
|
||||
[&](const molo::corr::RequestContext&) { ++finishes; };
|
||||
molo::obs::fire_server_start();
|
||||
molo::obs::fire_request_start(req);
|
||||
molo::obs::fire_request_finish(req);
|
||||
CHECK(ctx, server_starts == 1);
|
||||
CHECK(ctx, starts == 1);
|
||||
CHECK(ctx, finishes == 1);
|
||||
molo::obs::LifecycleHooks::global() = molo::obs::LifecycleHooks{};
|
||||
molo::obs::fire_server_start();
|
||||
CHECK(ctx, server_starts == 1);
|
||||
}
|
||||
|
||||
return test_summary(ctx, "correlation");
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
// Events pillar: dispatch, zero-listener fast path, async delivery,
|
||||
// lifecycle event set, pubsub bridge hook.
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <cstdio>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
|
||||
#include "observability/correlation/request_context.h"
|
||||
#include "observability/events/dispatcher.h"
|
||||
#include "observability/events/event.h"
|
||||
#include "observability/events/lifecycle_events.h"
|
||||
#include "test_helper.h"
|
||||
|
||||
int main() {
|
||||
TestCtx ctx;
|
||||
molo::events::Dispatcher dispatcher;
|
||||
|
||||
// Sync dispatch to multiple listeners.
|
||||
int calls_a = 0;
|
||||
int calls_b = 0;
|
||||
std::uint64_t id_a = dispatcher.on("request.started", [&](const molo::events::Event& event) {
|
||||
++calls_a;
|
||||
CHECK(ctx, event.type() == "request.started");
|
||||
});
|
||||
dispatcher.on("request.started", [&](const molo::events::Event&) { ++calls_b; });
|
||||
CHECK(ctx, dispatcher.has_listeners("request.started"));
|
||||
CHECK(ctx, !dispatcher.has_listeners("nope"));
|
||||
molo::corr::RequestContext req("req-events-1");
|
||||
molo::events::RequestStartedEvent started("GET", "/x", req);
|
||||
dispatcher.emit(started);
|
||||
CHECK(ctx, calls_a == 1);
|
||||
CHECK(ctx, calls_b == 1);
|
||||
CHECK(ctx, dispatcher.emitted_total() == 1);
|
||||
|
||||
// Removal.
|
||||
CHECK(ctx, dispatcher.off(id_a));
|
||||
CHECK(ctx, !dispatcher.off(id_a));
|
||||
dispatcher.emit(started);
|
||||
CHECK(ctx, calls_a == 1);
|
||||
CHECK(ctx, calls_b == 2);
|
||||
|
||||
// Zero-listener fast path: emits without listeners are cheap and safe.
|
||||
{
|
||||
molo::events::Event lonely("nothing.listens");
|
||||
auto begin = std::chrono::steady_clock::now();
|
||||
const int iters = 100000;
|
||||
for (int i = 0; i < iters; ++i) dispatcher.emit(lonely);
|
||||
auto end = std::chrono::steady_clock::now();
|
||||
double ns =
|
||||
std::chrono::duration<double, std::nano>(end - begin).count() / static_cast<double>(iters);
|
||||
std::printf("info: zero-listener dispatch %.1f ns/call\n", ns);
|
||||
CHECK(ctx, ns < 5000.0);
|
||||
}
|
||||
|
||||
// Publisher bridge into the pubsub core.
|
||||
int published = 0;
|
||||
dispatcher.set_publisher([&](const molo::events::Event&) { ++published; });
|
||||
dispatcher.emit(started);
|
||||
CHECK(ctx, published == 1);
|
||||
dispatcher.set_publisher(nullptr);
|
||||
dispatcher.emit(started);
|
||||
CHECK(ctx, published == 1);
|
||||
|
||||
// Async delivery on the background worker.
|
||||
dispatcher.start();
|
||||
std::atomic<int> async_calls{0};
|
||||
std::string seen_task;
|
||||
dispatcher.on_async("task.finished", [&](std::shared_ptr<const molo::events::Event> event) {
|
||||
async_calls.fetch_add(1);
|
||||
auto typed = std::dynamic_pointer_cast<const molo::events::TaskFinishedEvent>(event);
|
||||
if (typed) seen_task = typed->task;
|
||||
});
|
||||
molo::events::TaskFinishedEvent done("backup", "t-1", 42);
|
||||
dispatcher.emit(done);
|
||||
auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(2);
|
||||
while (async_calls.load() == 0 && std::chrono::steady_clock::now() < deadline) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(5));
|
||||
}
|
||||
CHECK(ctx, async_calls.load() == 1);
|
||||
CHECK(ctx, seen_task == "backup");
|
||||
CHECK(ctx, dispatcher.dropped_async() == 0);
|
||||
|
||||
// Async drops when the worker was never started.
|
||||
{
|
||||
molo::events::Dispatcher unstated;
|
||||
unstated.on_async("x", [](std::shared_ptr<const molo::events::Event>) {});
|
||||
molo::events::Event event("x");
|
||||
unstated.emit(event);
|
||||
CHECK(ctx, unstated.dropped_async() == 1);
|
||||
}
|
||||
dispatcher.stop();
|
||||
|
||||
// Lifecycle event set: types and clone without slicing.
|
||||
CHECK(ctx, std::string(molo::events::ServerStartedEvent::kType) == "server.started");
|
||||
CHECK(ctx, std::string(molo::events::ServerStoppedEvent::kType) == "server.stopped");
|
||||
CHECK(ctx, std::string(molo::events::ConnectionOpenedEvent::kType) == "connection.opened");
|
||||
CHECK(ctx, std::string(molo::events::ConnectionClosedEvent::kType) == "connection.closed");
|
||||
CHECK(ctx, std::string(molo::events::RequestStartedEvent::kType) == "request.started");
|
||||
CHECK(ctx, std::string(molo::events::RequestFinishedEvent::kType) == "request.finished");
|
||||
CHECK(ctx, std::string(molo::events::TaskEnqueuedEvent::kType) == "task.enqueued");
|
||||
CHECK(ctx, std::string(molo::events::TaskStartedEvent::kType) == "task.started");
|
||||
CHECK(ctx, std::string(molo::events::TaskFinishedEvent::kType) == "task.finished");
|
||||
CHECK(ctx, std::string(molo::events::TaskFailedEvent::kType) == "task.failed");
|
||||
|
||||
molo::events::RequestFinishedEvent finished("POST", "/items", 201, 12, req);
|
||||
CHECK(ctx, finished.request_id() == "req-events-1");
|
||||
CHECK(ctx, finished.status == 201);
|
||||
auto cloned = finished.clone();
|
||||
auto typed = std::dynamic_pointer_cast<const molo::events::RequestFinishedEvent>(cloned);
|
||||
CHECK(ctx, typed != nullptr);
|
||||
CHECK(ctx, typed->route == "/items");
|
||||
|
||||
molo::events::TaskFailedEvent failed("backup", "t-2", "timeout");
|
||||
CHECK(ctx, failed.error_kind == "timeout");
|
||||
|
||||
return test_summary(ctx, "events");
|
||||
}
|
||||
@@ -0,0 +1,208 @@
|
||||
// Logging pillar: levels, registry, loggers, sinks, JSON schema, macros,
|
||||
// redaction, truncation, async queue drops, file rotation.
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include "observability/logging/async_sink.h"
|
||||
#include "observability/logging/console_sink.h"
|
||||
#include "observability/logging/file_sink.h"
|
||||
#include "observability/logging/json_format.h"
|
||||
#include "observability/logging/level.h"
|
||||
#include "observability/logging/logger.h"
|
||||
#include "observability/logging/macros.h"
|
||||
#include "observability/logging/null_sink.h"
|
||||
#include "observability/logging/registry.h"
|
||||
#include "observability/logging/ring_sink.h"
|
||||
#include "test_helper.h"
|
||||
|
||||
namespace {
|
||||
|
||||
bool contains(const std::string& haystack, const std::string& needle) {
|
||||
return haystack.find(needle) != std::string::npos;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main() {
|
||||
TestCtx ctx;
|
||||
|
||||
// Levels.
|
||||
CHECK(ctx, molo::log::to_string(molo::log::Level::Info) == "info");
|
||||
CHECK(ctx, molo::log::from_string("warn") == molo::log::Level::Warn);
|
||||
CHECK(ctx, molo::log::from_string("warning") == molo::log::Level::Warn);
|
||||
CHECK(ctx, !molo::log::from_string("bogus").has_value());
|
||||
|
||||
// Registry and named loggers.
|
||||
molo::log::Registry registry;
|
||||
auto ring = std::make_shared<molo::log::RingSink>(64);
|
||||
registry.add_sink(ring);
|
||||
registry.add_sink(std::make_shared<molo::log::NullSink>());
|
||||
CHECK(ctx, registry.sink_count() == 2);
|
||||
auto logger = registry.get("molo.http");
|
||||
auto same = registry.get("molo.http");
|
||||
CHECK(ctx, logger.get() == same.get());
|
||||
CHECK(ctx, logger->name() == "molo.http");
|
||||
|
||||
// Basic record flow with correlation context.
|
||||
logger->set_request_id("req-1");
|
||||
logger->set_span("trace-1", "span-1");
|
||||
MOLO_LOG_INFO(*logger, "request.finished");
|
||||
auto records = ring->snapshot();
|
||||
CHECK(ctx, records.size() == 1);
|
||||
CHECK(ctx, records[0].logger == "molo.http");
|
||||
CHECK(ctx, records[0].level == molo::log::Level::Info);
|
||||
CHECK(ctx, records[0].message == "request.finished");
|
||||
CHECK(ctx, records[0].request_id == "req-1");
|
||||
CHECK(ctx, records[0].trace_id == "trace-1");
|
||||
CHECK(ctx, records[0].span_id == "span-1");
|
||||
CHECK(ctx, !records[0].file.empty());
|
||||
CHECK(ctx, records[0].line > 0);
|
||||
CHECK(ctx, registry.records_total(molo::log::Level::Info) == 1);
|
||||
|
||||
// Structured fields over string interpolation.
|
||||
std::vector<molo::log::Field> fields = {{"table", std::string("users")},
|
||||
{"rows", std::int64_t(3)},
|
||||
{"ratio", 0.5},
|
||||
{"cached", true}};
|
||||
MOLO_LOG_INFO(logger, "db.query", fields);
|
||||
records = ring->snapshot();
|
||||
CHECK(ctx, records.size() == 2);
|
||||
CHECK(ctx, records[1].fields.size() == 4);
|
||||
|
||||
// JSON schema shape.
|
||||
std::string json = molo::log::to_json(records[1]);
|
||||
CHECK(ctx, contains(json, "\"level\":\"info\""));
|
||||
CHECK(ctx, contains(json, "\"logger\":\"molo.http\""));
|
||||
CHECK(ctx, contains(json, "\"msg\":\"db.query\""));
|
||||
CHECK(ctx, contains(json, "\"request_id\":\"req-1\""));
|
||||
CHECK(ctx, contains(json, "\"trace_id\":\"trace-1\""));
|
||||
CHECK(ctx, contains(json, "\"span_id\":\"span-1\""));
|
||||
CHECK(ctx, contains(json, "\"table\":\"users\""));
|
||||
CHECK(ctx, contains(json, "\"rows\":3"));
|
||||
CHECK(ctx, contains(json, "\"cached\":true"));
|
||||
CHECK(ctx, contains(json, "\"ts\":\""));
|
||||
CHECK(ctx, contains(json, "Z\""));
|
||||
|
||||
// Redaction of secrets in fields.
|
||||
ring->clear();
|
||||
MOLO_LOG_INFO(*logger, "auth.login",
|
||||
std::vector<molo::log::Field>{{"user", std::string("u")},
|
||||
{"password", std::string("hunter2")},
|
||||
{"token", std::string("abc")},
|
||||
{"session_state", std::string("x")}});
|
||||
records = ring->snapshot();
|
||||
CHECK(ctx, records.size() == 1);
|
||||
std::string redacted = molo::log::to_json(records[0]);
|
||||
CHECK(ctx, contains(redacted, "\"password\":\"***\""));
|
||||
CHECK(ctx, contains(redacted, "\"token\":\"***\""));
|
||||
CHECK(ctx, contains(redacted, "\"session_state\":\"***\""));
|
||||
CHECK(ctx, !contains(redacted, "hunter2"));
|
||||
|
||||
// Truncation of oversized lines.
|
||||
ring->clear();
|
||||
std::string big(9000, 'x');
|
||||
MOLO_LOG_INFO(*logger, big);
|
||||
records = ring->snapshot();
|
||||
CHECK(ctx, records.size() == 1);
|
||||
CHECK(ctx, records[0].truncated);
|
||||
CHECK(ctx, records[0].message.size() == molo::obs::Caps::kMaxLogLine);
|
||||
CHECK(ctx, contains(molo::log::to_json(records[0]), "\"truncated\":true"));
|
||||
|
||||
// Runtime level gating.
|
||||
ring->clear();
|
||||
registry.set_global_level(molo::log::Level::Error);
|
||||
MOLO_LOG_INFO(*logger, "suppressed");
|
||||
MOLO_LOG_ERROR(*logger, "kept");
|
||||
records = ring->snapshot();
|
||||
CHECK(ctx, records.size() == 1);
|
||||
CHECK(ctx, records[0].message == "kept");
|
||||
registry.set_global_level(molo::log::Level::Debug);
|
||||
|
||||
// Child loggers inherit context.
|
||||
auto kid = logger->child("router");
|
||||
CHECK(ctx, kid->name() == "molo.http.router");
|
||||
CHECK(ctx, kid->request_id() == "req-1");
|
||||
|
||||
// Async sink delivers without blocking and drops under saturation.
|
||||
{
|
||||
auto inner = std::make_shared<molo::log::RingSink>(4096);
|
||||
molo::log::AsyncSink async(inner, 1024);
|
||||
for (int i = 0; i < 200; ++i) {
|
||||
molo::log::Record record;
|
||||
record.logger = "async";
|
||||
record.message = "m" + std::to_string(i);
|
||||
async.write(record);
|
||||
}
|
||||
async.flush();
|
||||
CHECK(ctx, inner->size() == 200);
|
||||
CHECK(ctx, async.dropped() == 0);
|
||||
}
|
||||
{
|
||||
molo::log::AsyncQueue queue([](const molo::log::Record&) {}, 8);
|
||||
queue.stop();
|
||||
for (int i = 0; i < 100; ++i) {
|
||||
molo::log::Record record;
|
||||
CHECK(ctx, !queue.try_push(record));
|
||||
}
|
||||
CHECK(ctx, queue.dropped() == 100);
|
||||
}
|
||||
|
||||
// Flood through a live small queue: bounded memory, drops counted.
|
||||
{
|
||||
std::atomic<int> slow{0};
|
||||
molo::log::AsyncQueue queue(
|
||||
[&slow](const molo::log::Record&) {
|
||||
slow.fetch_add(1);
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(5));
|
||||
},
|
||||
8);
|
||||
int accepted = 0;
|
||||
for (int i = 0; i < 200; ++i) {
|
||||
molo::log::Record record;
|
||||
if (queue.try_push(record)) ++accepted;
|
||||
}
|
||||
queue.stop();
|
||||
CHECK(ctx, queue.dropped() > 0);
|
||||
CHECK(ctx, accepted + static_cast<int>(queue.dropped()) == 200);
|
||||
}
|
||||
|
||||
// File sink rotation.
|
||||
{
|
||||
std::filesystem::create_directories("build");
|
||||
std::string path = "build/obs_file_sink_test.log";
|
||||
std::remove(path.c_str());
|
||||
molo::log::FileSink sink(path, 1024, 3);
|
||||
for (int i = 0; i < 100; ++i) {
|
||||
molo::log::Record record;
|
||||
record.logger = "file";
|
||||
record.message = "line-" + std::to_string(i) + "-" + std::string(60, 'y');
|
||||
sink.write(record);
|
||||
}
|
||||
sink.flush();
|
||||
CHECK(ctx, std::filesystem::exists(path));
|
||||
bool rotated = std::filesystem::exists(path + ".1") || std::filesystem::exists(path + ".2");
|
||||
CHECK(ctx, rotated);
|
||||
}
|
||||
|
||||
// Console sink does not throw.
|
||||
{
|
||||
molo::log::ConsoleSink text(false);
|
||||
molo::log::ConsoleSink as_json(true);
|
||||
molo::log::Record record;
|
||||
record.logger = "console";
|
||||
record.message = "hello";
|
||||
text.write(record);
|
||||
as_json.write(record);
|
||||
text.flush();
|
||||
CHECK(ctx, true);
|
||||
}
|
||||
|
||||
return test_summary(ctx, "logging");
|
||||
}
|
||||
@@ -0,0 +1,203 @@
|
||||
// Metrics pillar, mirroring GET /metrics: instruments, cardinality caps,
|
||||
// series caps, Prometheus exposition shape, framework metrics, render cost.
|
||||
|
||||
#include <chrono>
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
|
||||
#include "observability/metrics/counter.h"
|
||||
#include "observability/metrics/gauge.h"
|
||||
#include "observability/metrics/histogram.h"
|
||||
#include "observability/metrics/labels.h"
|
||||
#include "observability/metrics/prometheus.h"
|
||||
#include "observability/metrics/registry.h"
|
||||
#include "observability/metrics/timer.h"
|
||||
#include "test_helper.h"
|
||||
|
||||
namespace {
|
||||
|
||||
bool contains(const std::string& haystack, const std::string& needle) {
|
||||
return haystack.find(needle) != std::string::npos;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main() {
|
||||
TestCtx ctx;
|
||||
|
||||
// Counter basics and automatic _total suffix.
|
||||
{
|
||||
molo::metrics::Registry registry;
|
||||
auto& counter = registry.counter("molo_http_requests", "Requests");
|
||||
CHECK(ctx, counter.name() == "molo_http_requests_total");
|
||||
counter.inc();
|
||||
counter.inc(2.0);
|
||||
counter.inc(-5.0);
|
||||
CHECK(ctx, counter.value() == 3.0);
|
||||
auto& same = registry.counter("molo_http_requests_total", "Requests");
|
||||
CHECK(ctx, &same == &counter);
|
||||
bool threw = false;
|
||||
try {
|
||||
registry.counter("bad name!", "x");
|
||||
} catch (const std::invalid_argument&) {
|
||||
threw = true;
|
||||
}
|
||||
CHECK(ctx, threw);
|
||||
}
|
||||
|
||||
// Gauge up and down.
|
||||
{
|
||||
molo::metrics::Gauge gauge("g", "h");
|
||||
gauge.set(2.0);
|
||||
gauge.inc();
|
||||
gauge.dec(0.5);
|
||||
CHECK(ctx, gauge.value() == 2.5);
|
||||
}
|
||||
|
||||
// Labels: caps and truncation.
|
||||
{
|
||||
molo::metrics::Labels labels;
|
||||
labels.add("method", "GET").add("route", "/items");
|
||||
CHECK(ctx, labels.size() == 2);
|
||||
molo::metrics::Labels fat;
|
||||
for (int i = 0; i < 16; ++i) fat.add("k" + std::to_string(i), "v");
|
||||
bool threw = false;
|
||||
try {
|
||||
fat.add("overflow", "v");
|
||||
} catch (const std::invalid_argument&) {
|
||||
threw = true;
|
||||
}
|
||||
CHECK(ctx, threw);
|
||||
molo::metrics::Labels trunc;
|
||||
trunc.add("k", std::string(200, 'a'));
|
||||
CHECK(ctx, trunc.items()[0].second.size() == molo::obs::Caps::kMaxLabelValueLen);
|
||||
}
|
||||
|
||||
// Cardinality cap: 65th distinct value maps to "other" and counts.
|
||||
{
|
||||
molo::metrics::Registry registry;
|
||||
for (int i = 0; i < 70; ++i) {
|
||||
molo::metrics::Labels labels;
|
||||
labels.add("route", "/r" + std::to_string(i));
|
||||
registry.counter("molo_card", "card", labels).inc();
|
||||
}
|
||||
CHECK(ctx, registry.cardinality_overflows() == 6);
|
||||
std::string body = molo::metrics::render(registry);
|
||||
CHECK(ctx, contains(body, "molo_card_total{route=\"other\"} 6"));
|
||||
CHECK(ctx, contains(body, "molo_metrics_cardinality_overflows_total 6"));
|
||||
}
|
||||
|
||||
// Series cap: 65536 series max, excess hits the blackhole and counts.
|
||||
{
|
||||
molo::metrics::Registry registry;
|
||||
for (int i = 0; i < 65537; ++i) {
|
||||
registry.gauge("fill_" + std::to_string(i), "filler").set(1.0);
|
||||
}
|
||||
CHECK(ctx, registry.series_count() == 65536);
|
||||
CHECK(ctx, registry.cardinality_overflows() >= 1);
|
||||
std::string body = molo::metrics::render(registry);
|
||||
CHECK(ctx, contains(body, "fill_0 1"));
|
||||
CHECK(ctx, !contains(body, "fill_65536"));
|
||||
}
|
||||
|
||||
// Histogram cumulative buckets, sum and count.
|
||||
{
|
||||
molo::metrics::Registry registry;
|
||||
molo::metrics::Labels labels;
|
||||
labels.add("method", "GET");
|
||||
auto& hist = registry.histogram("molo_http_request_duration_seconds", "Latency", labels,
|
||||
{0.1, 0.5, 1.0});
|
||||
hist.observe(0.05);
|
||||
hist.observe(0.3);
|
||||
hist.observe(5.0);
|
||||
auto snap = hist.snapshot();
|
||||
CHECK(ctx, snap.count == 3);
|
||||
CHECK(ctx, snap.cumulative.size() == 3);
|
||||
CHECK(ctx, snap.cumulative[0] == 1);
|
||||
CHECK(ctx, snap.cumulative[1] == 2);
|
||||
CHECK(ctx, snap.cumulative[2] == 2);
|
||||
CHECK(ctx, snap.sum > 5.3 && snap.sum < 5.4);
|
||||
std::string body = molo::metrics::render(registry);
|
||||
CHECK(ctx, contains(body, "# TYPE molo_http_request_duration_seconds histogram"));
|
||||
CHECK(ctx, contains(body, "molo_http_request_duration_seconds_bucket{method=\"GET\",le=\"0.1\"} 1"));
|
||||
CHECK(ctx, contains(body, "molo_http_request_duration_seconds_bucket{method=\"GET\",le=\"+Inf\"} 3"));
|
||||
CHECK(ctx, contains(body, "molo_http_request_duration_seconds_sum{method=\"GET\"}"));
|
||||
CHECK(ctx, contains(body, "molo_http_request_duration_seconds_count{method=\"GET\"} 3"));
|
||||
}
|
||||
|
||||
// Render shape and escaping.
|
||||
{
|
||||
molo::metrics::Registry registry;
|
||||
molo::metrics::Labels labels;
|
||||
labels.add("route", "/a\"b\\c\nd");
|
||||
registry.counter("molo_esc", "help\nline\\x", labels).inc(3.0);
|
||||
std::string body = molo::metrics::render(registry);
|
||||
CHECK(ctx, contains(body, "# HELP molo_esc_total help\\nline\\\\x"));
|
||||
CHECK(ctx, contains(body, "# TYPE molo_esc_total counter"));
|
||||
CHECK(ctx, contains(body, "molo_esc_total{route=\"/a\\\"b\\\\c\\nd\"} 3"));
|
||||
CHECK(ctx, molo::metrics::format_sample_double(0.5) == "0.5");
|
||||
CHECK(ctx, molo::metrics::format_sample_double(3.0) == "3");
|
||||
CHECK(ctx, molo::metrics::format_sample_double(1.0 / 0.0) == "+Inf");
|
||||
}
|
||||
|
||||
// Framework metrics shipped by default.
|
||||
{
|
||||
molo::metrics::Registry registry;
|
||||
registry.ensure_framework_metrics();
|
||||
std::string body = molo::metrics::render(registry);
|
||||
CHECK(ctx, contains(body, "molo_http_requests_in_flight 0"));
|
||||
CHECK(ctx, contains(body, "molo_log_records_total{level=\"info\"} 0"));
|
||||
CHECK(ctx, contains(body, "molo_log_records_total{level=\"critical\"} 0"));
|
||||
CHECK(ctx, contains(body, "molo_spans_total{kind=\"server\"} 0"));
|
||||
CHECK(ctx, contains(body, "molo_log_dropped_total 0"));
|
||||
CHECK(ctx, contains(body, "molo_spans_dropped_total 0"));
|
||||
CHECK(ctx, contains(body, "molo_trace_propagator_errors_total 0"));
|
||||
CHECK(ctx, contains(body, "molo_metrics_cardinality_overflows_total 0"));
|
||||
}
|
||||
|
||||
// RAII timer records, cancel skips.
|
||||
{
|
||||
molo::metrics::Histogram hist("t", "h", molo::metrics::Labels{}, {1.0, 10.0});
|
||||
{
|
||||
molo::metrics::Timer timer(hist);
|
||||
}
|
||||
CHECK(ctx, hist.snapshot().count == 1);
|
||||
{
|
||||
molo::metrics::Timer timer(hist);
|
||||
timer.cancel();
|
||||
}
|
||||
CHECK(ctx, hist.snapshot().count == 1);
|
||||
{
|
||||
MOLO_TIMER(hist);
|
||||
}
|
||||
CHECK(ctx, hist.snapshot().count == 2);
|
||||
}
|
||||
|
||||
// Runtime toggle disables updates with one branch.
|
||||
{
|
||||
molo::metrics::Counter counter("rt_total", "h");
|
||||
counter.inc();
|
||||
molo::obs::RuntimeToggles::instance().metrics.store(false);
|
||||
counter.inc();
|
||||
molo::obs::RuntimeToggles::instance().metrics.store(true);
|
||||
counter.inc();
|
||||
CHECK(ctx, counter.value() == 2.0);
|
||||
}
|
||||
|
||||
// Render cost: full exposition of 10k series well under the 100 ms budget.
|
||||
{
|
||||
molo::metrics::Registry registry;
|
||||
for (int i = 0; i < 10000; ++i) {
|
||||
registry.gauge("perf_gauge_" + std::to_string(i), "perf").set(static_cast<double>(i));
|
||||
}
|
||||
auto begin = std::chrono::steady_clock::now();
|
||||
std::string body = molo::metrics::render(registry);
|
||||
auto end = std::chrono::steady_clock::now();
|
||||
double ms = std::chrono::duration<double, std::milli>(end - begin).count();
|
||||
std::printf("info: render 10k series %.1f ms (%zu bytes)\n", ms, body.size());
|
||||
CHECK(ctx, !body.empty());
|
||||
CHECK(ctx, ms < 1000.0);
|
||||
}
|
||||
|
||||
return test_summary(ctx, "metrics-endpoint");
|
||||
}
|
||||
@@ -0,0 +1,217 @@
|
||||
// Export path: OTLP/HTTP JSON shape, retry with backoff, batch processor
|
||||
// behavior and bounds.
|
||||
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "observability/tracing/batch_processor.h"
|
||||
#include "observability/tracing/exporter.h"
|
||||
#include "observability/tracing/http_sender.h"
|
||||
#include "observability/tracing/ids.h"
|
||||
#include "observability/tracing/memory_exporter.h"
|
||||
#include "observability/tracing/otlp_http.h"
|
||||
#include "observability/tracing/span.h"
|
||||
#include "test_helper.h"
|
||||
|
||||
namespace {
|
||||
|
||||
bool contains(const std::string& haystack, const std::string& needle) {
|
||||
return haystack.find(needle) != std::string::npos;
|
||||
}
|
||||
|
||||
molo::trace::FinishedSpan make_span(const std::string& name, bool with_parent) {
|
||||
molo::trace::FinishedSpan span;
|
||||
span.name = name;
|
||||
span.kind = molo::trace::SpanKind::Server;
|
||||
span.context.trace_id = molo::trace::TraceId::generate();
|
||||
span.context.span_id = molo::trace::SpanId::generate();
|
||||
span.context.flags.value = molo::trace::TraceFlags::kSampled;
|
||||
if (with_parent) span.parent_span_id = molo::trace::SpanId::generate();
|
||||
span.start = std::chrono::system_clock::now();
|
||||
span.end = span.start + std::chrono::milliseconds(5);
|
||||
span.attributes.push_back({"http.method", std::string("GET")});
|
||||
span.attributes.push_back({"http.status", std::int64_t(200)});
|
||||
span.attributes.push_back({"ratio", 0.5});
|
||||
span.attributes.push_back({"cached", true});
|
||||
molo::trace::SpanEvent event;
|
||||
event.timestamp = span.start;
|
||||
event.name = "db.start";
|
||||
span.events.push_back(event);
|
||||
span.status = molo::trace::StatusCode::Ok;
|
||||
return span;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main() {
|
||||
TestCtx ctx;
|
||||
|
||||
// OTLP JSON document shape.
|
||||
{
|
||||
std::vector<molo::trace::FinishedSpan> batch;
|
||||
batch.push_back(make_span("GET /root", false));
|
||||
batch.push_back(make_span("db.query", true));
|
||||
std::string body = molo::trace::OtlpHttpExporter::build_request_json(batch, "molo");
|
||||
CHECK(ctx, contains(body, "\"resourceSpans\":[{"));
|
||||
CHECK(ctx, contains(body, "\"scope\":{\"name\":\"molo\"}"));
|
||||
CHECK(ctx, contains(body, "\"traceId\":\"" + batch[0].context.trace_id.to_hex() + "\""));
|
||||
CHECK(ctx, contains(body, "\"spanId\":\"" + batch[0].context.span_id.to_hex() + "\""));
|
||||
CHECK(ctx, contains(body, "\"parentSpanId\":\"" + batch[1].parent_span_id.to_hex() + "\""));
|
||||
CHECK(ctx, contains(body, "\"name\":\"GET /root\""));
|
||||
CHECK(ctx, contains(body, "\"kind\":2"));
|
||||
CHECK(ctx, contains(body, "\"startTimeUnixNano\":\""));
|
||||
CHECK(ctx, contains(body, "\"key\":\"http.method\",\"value\":{\"stringValue\":\"GET\"}"));
|
||||
CHECK(ctx, contains(body, "\"key\":\"http.status\",\"value\":{\"intValue\":\"200\"}"));
|
||||
CHECK(ctx, contains(body, "\"doubleValue\":0.5"));
|
||||
CHECK(ctx, contains(body, "\"boolValue\":true"));
|
||||
CHECK(ctx, contains(body, "\"status\":{\"code\":1}"));
|
||||
CHECK(ctx, contains(body, "\"name\":\"db.start\""));
|
||||
// Root span carries no parentSpanId field.
|
||||
std::size_t first_span = body.find("\"name\":\"GET /root\"");
|
||||
std::size_t parent_key = body.find("\"parentSpanId\"");
|
||||
CHECK(ctx, first_span != std::string::npos && parent_key != std::string::npos);
|
||||
CHECK(ctx, parent_key > first_span);
|
||||
}
|
||||
|
||||
// Successful export posts JSON with the right headers.
|
||||
{
|
||||
std::string seen_url;
|
||||
std::string seen_body;
|
||||
std::string seen_content_type;
|
||||
auto sender = std::make_shared<molo::trace::FunctionHttpSender>(
|
||||
[&](const std::string& url, const molo::trace::HttpSender::HeaderList& headers,
|
||||
const std::string& body, std::chrono::seconds timeout) {
|
||||
seen_url = url;
|
||||
seen_body = body;
|
||||
for (const auto& [key, value] : headers) {
|
||||
if (key == "Content-Type") seen_content_type = value;
|
||||
}
|
||||
CHECK(ctx, timeout == std::chrono::seconds(10));
|
||||
return true;
|
||||
});
|
||||
molo::trace::OtlpHttpExporter exporter("http://collector:4318/v1/traces", sender);
|
||||
std::vector<molo::trace::FinishedSpan> batch;
|
||||
batch.push_back(make_span("GET /ok", false));
|
||||
CHECK(ctx, exporter.export_spans(batch));
|
||||
CHECK(ctx, seen_url == "http://collector:4318/v1/traces");
|
||||
CHECK(ctx, seen_content_type == "application/json");
|
||||
CHECK(ctx, contains(seen_body, "\"resourceSpans\""));
|
||||
CHECK(ctx, exporter.requests_total() == 1);
|
||||
CHECK(ctx, exporter.requests_failed() == 0);
|
||||
CHECK(ctx, exporter.name() == "otlp-http");
|
||||
}
|
||||
|
||||
// Retry: two failures then success.
|
||||
{
|
||||
int calls = 0;
|
||||
auto sender = std::make_shared<molo::trace::FunctionHttpSender>(
|
||||
[&](const std::string&, const molo::trace::HttpSender::HeaderList&,
|
||||
const std::string&, std::chrono::seconds) { return ++calls >= 3; });
|
||||
molo::trace::OtlpHttpExporter exporter(
|
||||
"http://collector:4318/v1/traces", sender,
|
||||
{std::chrono::milliseconds(0), std::chrono::milliseconds(0),
|
||||
std::chrono::milliseconds(0)});
|
||||
std::vector<molo::trace::FinishedSpan> batch;
|
||||
batch.push_back(make_span("GET /retry", false));
|
||||
CHECK(ctx, exporter.export_spans(batch));
|
||||
CHECK(ctx, calls == 3);
|
||||
CHECK(ctx, exporter.requests_total() == 3);
|
||||
CHECK(ctx, exporter.requests_failed() == 2);
|
||||
}
|
||||
|
||||
// Persistent failure: first attempt plus 3 retries, then give up.
|
||||
{
|
||||
int calls = 0;
|
||||
auto sender = std::make_shared<molo::trace::FunctionHttpSender>(
|
||||
[&](const std::string&, const molo::trace::HttpSender::HeaderList&,
|
||||
const std::string&, std::chrono::seconds) {
|
||||
++calls;
|
||||
return false;
|
||||
});
|
||||
molo::trace::OtlpHttpExporter exporter(
|
||||
"http://collector:4318/v1/traces", sender,
|
||||
{std::chrono::milliseconds(0), std::chrono::milliseconds(0),
|
||||
std::chrono::milliseconds(0)});
|
||||
std::vector<molo::trace::FinishedSpan> batch;
|
||||
batch.push_back(make_span("GET /fail", false));
|
||||
CHECK(ctx, !exporter.export_spans(batch));
|
||||
CHECK(ctx, calls == 4);
|
||||
}
|
||||
|
||||
// Empty batches are free.
|
||||
{
|
||||
auto sender = std::make_shared<molo::trace::FunctionHttpSender>(
|
||||
[](const std::string&, const molo::trace::HttpSender::HeaderList&,
|
||||
const std::string&, std::chrono::seconds) { return false; });
|
||||
molo::trace::OtlpHttpExporter exporter("http://x", sender);
|
||||
CHECK(ctx, exporter.export_spans({}));
|
||||
CHECK(ctx, exporter.requests_total() == 0);
|
||||
}
|
||||
|
||||
// The marked stub drops and reports failure.
|
||||
{
|
||||
molo::trace::NullHttpSender stub;
|
||||
molo::trace::OtlpHttpExporter exporter(
|
||||
"http://x", std::make_shared<molo::trace::NullHttpSender>());
|
||||
std::vector<molo::trace::FinishedSpan> batch;
|
||||
batch.push_back(make_span("GET /stub", false));
|
||||
CHECK(ctx, !exporter.export_spans(batch));
|
||||
CHECK(ctx, !stub.post("http://x", {}, "{}", std::chrono::seconds(10)));
|
||||
CHECK(ctx, stub.attempts() == 1);
|
||||
}
|
||||
|
||||
// gzip without an injected codec sends identity (documented fallback).
|
||||
{
|
||||
std::string encoding;
|
||||
auto sender = std::make_shared<molo::trace::FunctionHttpSender>(
|
||||
[&](const std::string&, const molo::trace::HttpSender::HeaderList& headers,
|
||||
const std::string&, std::chrono::seconds) {
|
||||
for (const auto& [key, value] : headers) {
|
||||
if (key == "Content-Encoding") encoding = value;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
molo::trace::OtlpHttpExporter exporter("http://x", sender,
|
||||
{std::chrono::milliseconds(0)}, std::chrono::seconds(10),
|
||||
true);
|
||||
std::vector<molo::trace::FinishedSpan> batch;
|
||||
batch.push_back(make_span("GET /gzip", false));
|
||||
CHECK(ctx, exporter.export_spans(batch));
|
||||
CHECK(ctx, encoding.empty());
|
||||
exporter.set_compressor([](const std::string& input, std::string& output) {
|
||||
output = "COMPRESSED:" + input;
|
||||
return true;
|
||||
});
|
||||
CHECK(ctx, exporter.export_spans(batch));
|
||||
CHECK(ctx, encoding == "gzip");
|
||||
}
|
||||
|
||||
// Batch processor: batches of 512 or 5 s, bounded buffer with drops.
|
||||
{
|
||||
auto exporter = std::make_shared<molo::trace::MemoryExporter>();
|
||||
molo::trace::BatchProcessor processor(exporter);
|
||||
for (int i = 0; i < 600; ++i) processor.add(make_span("s" + std::to_string(i), false));
|
||||
processor.flush();
|
||||
CHECK(ctx, exporter->spans().size() == 600);
|
||||
CHECK(ctx, processor.dropped() == 0);
|
||||
CHECK(ctx, processor.exported_total() >= 1);
|
||||
CHECK(ctx, processor.failed_batches() == 0);
|
||||
}
|
||||
{
|
||||
auto exporter = std::make_shared<molo::trace::MemoryExporter>();
|
||||
molo::trace::BatchProcessor processor(exporter, 8, 1000000, std::chrono::seconds(3600));
|
||||
for (int i = 0; i < 20; ++i) processor.add(make_span("o" + std::to_string(i), false));
|
||||
CHECK(ctx, processor.dropped() == 12);
|
||||
}
|
||||
{
|
||||
auto exporter = std::make_shared<molo::trace::MemoryExporter>();
|
||||
CHECK(ctx, exporter->name() == "memory");
|
||||
exporter->clear();
|
||||
CHECK(ctx, exporter->spans().empty());
|
||||
}
|
||||
|
||||
return test_summary(ctx, "otlp-export");
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
// W3C propagation: spec vectors round trip, malformed headers start a
|
||||
// new trace and count an error, outbound injection always emits context.
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "observability/config.h"
|
||||
#include "observability/tracing/ids.h"
|
||||
#include "observability/tracing/propagator.h"
|
||||
#include "observability/tracing/span_context.h"
|
||||
#include "test_helper.h"
|
||||
|
||||
int main() {
|
||||
TestCtx ctx;
|
||||
|
||||
// Spec-style vector parses and round trips exactly.
|
||||
{
|
||||
molo::trace::Headers headers = {
|
||||
{"traceparent", "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01"},
|
||||
{"tracestate", "rojo=00f067aa0ba902b7,congo=t61rcWkgMzE"}};
|
||||
int errors = 0;
|
||||
auto parsed =
|
||||
molo::trace::W3CPropagator::extract(headers, [&errors] { ++errors; });
|
||||
CHECK(ctx, parsed.has_value());
|
||||
CHECK(ctx, errors == 0);
|
||||
CHECK(ctx, parsed->trace_id.to_hex() == "4bf92f3577b34da6a3ce929d0e0e4736");
|
||||
CHECK(ctx, parsed->span_id.to_hex() == "00f067aa0ba902b7");
|
||||
CHECK(ctx, parsed->is_sampled());
|
||||
CHECK(ctx, parsed->tracestate == "rojo=00f067aa0ba902b7,congo=t61rcWkgMzE");
|
||||
|
||||
molo::trace::Headers outbound;
|
||||
molo::trace::W3CPropagator::inject(*parsed, outbound);
|
||||
CHECK(ctx, outbound["traceparent"] ==
|
||||
"00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01");
|
||||
CHECK(ctx, outbound["tracestate"] == "rojo=00f067aa0ba902b7,congo=t61rcWkgMzE");
|
||||
}
|
||||
|
||||
// Header names are case-insensitive.
|
||||
{
|
||||
molo::trace::Headers headers = {
|
||||
{"TraceParent", "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-00"}};
|
||||
auto parsed = molo::trace::W3CPropagator::extract(headers);
|
||||
CHECK(ctx, parsed.has_value());
|
||||
CHECK(ctx, !parsed->is_sampled());
|
||||
}
|
||||
|
||||
// Missing header means no context, and no error.
|
||||
{
|
||||
molo::trace::Headers headers;
|
||||
int errors = 0;
|
||||
auto parsed = molo::trace::W3CPropagator::extract(headers, [&errors] { ++errors; });
|
||||
CHECK(ctx, !parsed.has_value());
|
||||
CHECK(ctx, errors == 0);
|
||||
}
|
||||
|
||||
// Malformed headers fail and count exactly one error each.
|
||||
const char* malformed[] = {
|
||||
"bogus",
|
||||
"00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7",
|
||||
"ff-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01",
|
||||
"00-00000000000000000000000000000000-00f067aa0ba902b7-01",
|
||||
"00-4bf92f3577b34da6a3ce929d0e0e4736-0000000000000000-01",
|
||||
"00-zzzzzf3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01",
|
||||
"00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-zz",
|
||||
"00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-0100",
|
||||
};
|
||||
for (const char* value : malformed) {
|
||||
molo::trace::Headers headers = {{"traceparent", value}};
|
||||
int errors = 0;
|
||||
auto parsed = molo::trace::W3CPropagator::extract(headers, [&errors] { ++errors; });
|
||||
CHECK(ctx, !parsed.has_value());
|
||||
CHECK(ctx, errors == 1);
|
||||
}
|
||||
|
||||
// Overlong tracestate is truncated to the cap.
|
||||
{
|
||||
molo::trace::Headers headers = {
|
||||
{"traceparent", "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01"},
|
||||
{"tracestate", std::string(2000, 's')}};
|
||||
auto parsed = molo::trace::W3CPropagator::extract(headers);
|
||||
CHECK(ctx, parsed.has_value());
|
||||
CHECK(ctx, parsed->tracestate.size() == molo::obs::Caps::kMaxTracestate);
|
||||
}
|
||||
|
||||
// Invalid contexts are never injected.
|
||||
{
|
||||
molo::trace::Headers headers;
|
||||
molo::trace::W3CPropagator::inject(molo::trace::SpanContext::invalid(), headers);
|
||||
CHECK(ctx, headers.empty());
|
||||
}
|
||||
|
||||
// Fresh contexts render valid version 00 headers.
|
||||
{
|
||||
molo::trace::SpanContext context;
|
||||
context.trace_id = molo::trace::TraceId::generate();
|
||||
context.span_id = molo::trace::SpanId::generate();
|
||||
context.flags.value = 0;
|
||||
std::string rendered = molo::trace::W3CPropagator::render_traceparent(context);
|
||||
CHECK(ctx, rendered.size() == 55);
|
||||
CHECK(ctx, rendered.substr(0, 3) == "00-");
|
||||
molo::trace::Headers headers = {{"traceparent", rendered}};
|
||||
auto back = molo::trace::W3CPropagator::extract(headers);
|
||||
CHECK(ctx, back.has_value());
|
||||
CHECK(ctx, back->trace_id == context.trace_id);
|
||||
CHECK(ctx, back->span_id == context.span_id);
|
||||
}
|
||||
|
||||
return test_summary(ctx, "propagation");
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
// Minimal test helper. Tests print FAIL lines per broken check and a PASS
|
||||
// line with counts, and return nonzero when any check fails.
|
||||
|
||||
#include <cstdio>
|
||||
|
||||
struct TestCtx {
|
||||
int checks = 0;
|
||||
int failures = 0;
|
||||
};
|
||||
|
||||
#define CHECK(ctx, cond) \
|
||||
do { \
|
||||
++(ctx).checks; \
|
||||
if (!(cond)) { \
|
||||
++(ctx).failures; \
|
||||
std::printf("FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
inline int test_summary(TestCtx& ctx, const char* name) {
|
||||
if (ctx.failures == 0) {
|
||||
std::printf("PASS %s (%d checks)\n", name, ctx.checks);
|
||||
return 0;
|
||||
}
|
||||
std::printf("FAIL %s (%d of %d checks failed)\n", name, ctx.failures, ctx.checks);
|
||||
return 1;
|
||||
}
|
||||
@@ -0,0 +1,234 @@
|
||||
// Tracing core: IDs, samplers, span lifecycle, attribute caps,
|
||||
// null-span fast path, redaction.
|
||||
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "observability/config.h"
|
||||
#include "observability/tracing/batch_processor.h"
|
||||
#include "observability/tracing/ids.h"
|
||||
#include "observability/tracing/memory_exporter.h"
|
||||
#include "observability/tracing/sampler.h"
|
||||
#include "observability/tracing/span.h"
|
||||
#include "observability/tracing/span_context.h"
|
||||
#include "observability/tracing/tracer.h"
|
||||
#include "test_helper.h"
|
||||
|
||||
int main() {
|
||||
TestCtx ctx;
|
||||
|
||||
// IDs: generation, hex round trip, strict parsing.
|
||||
{
|
||||
auto trace = molo::trace::TraceId::generate();
|
||||
auto span = molo::trace::SpanId::generate();
|
||||
CHECK(ctx, trace.valid());
|
||||
CHECK(ctx, span.valid());
|
||||
CHECK(ctx, trace.to_hex().size() == 32);
|
||||
CHECK(ctx, span.to_hex().size() == 16);
|
||||
auto back = molo::trace::TraceId::from_hex(trace.to_hex());
|
||||
CHECK(ctx, back.has_value() && *back == trace);
|
||||
auto back_span = molo::trace::SpanId::from_hex(span.to_hex());
|
||||
CHECK(ctx, back_span.has_value() && *back_span == span);
|
||||
CHECK(ctx, !molo::trace::TraceId::from_hex("short").has_value());
|
||||
CHECK(ctx, !molo::trace::TraceId::from_hex(std::string(32, 'z')).has_value());
|
||||
CHECK(ctx, !molo::trace::SpanId::from_hex("short").has_value());
|
||||
auto zero = molo::trace::TraceId::from_hex("00000000000000000000000000000000");
|
||||
CHECK(ctx, zero.has_value() && !zero->valid());
|
||||
|
||||
auto flags = molo::trace::TraceFlags::from_hex("01");
|
||||
CHECK(ctx, flags.has_value() && flags->is_sampled());
|
||||
auto flags0 = molo::trace::TraceFlags::from_hex("00");
|
||||
CHECK(ctx, flags0.has_value() && !flags0->is_sampled());
|
||||
CHECK(ctx, !molo::trace::TraceFlags::from_hex("0").has_value());
|
||||
CHECK(ctx, !molo::trace::TraceFlags::from_hex("zz").has_value());
|
||||
|
||||
molo::trace::SpanContext invalid = molo::trace::SpanContext::invalid();
|
||||
CHECK(ctx, !invalid.is_valid());
|
||||
CHECK(ctx, !invalid.is_sampled());
|
||||
}
|
||||
|
||||
// Samplers.
|
||||
{
|
||||
molo::trace::AlwaysOnSampler on;
|
||||
molo::trace::AlwaysOffSampler off;
|
||||
auto id = molo::trace::TraceId::generate();
|
||||
CHECK(ctx, on.should_sample(id));
|
||||
CHECK(ctx, !off.should_sample(id));
|
||||
CHECK(ctx, on.name() == "always_on");
|
||||
|
||||
molo::trace::RatioSampler half(0.5);
|
||||
const int total = 100000;
|
||||
int kept = 0;
|
||||
for (int i = 0; i < total; ++i) {
|
||||
if (half.should_sample(molo::trace::TraceId::generate())) ++kept;
|
||||
}
|
||||
double rate = static_cast<double>(kept) / static_cast<double>(total);
|
||||
CHECK(ctx, rate > 0.45 && rate < 0.55);
|
||||
auto fixed = molo::trace::TraceId::generate();
|
||||
CHECK(ctx, half.should_sample(fixed) == half.should_sample(fixed));
|
||||
molo::trace::RatioSampler none(0.0);
|
||||
molo::trace::RatioSampler all(1.0);
|
||||
CHECK(ctx, !none.should_sample(fixed));
|
||||
CHECK(ctx, all.should_sample(fixed));
|
||||
}
|
||||
|
||||
// Span lifecycle with parent and child linkage.
|
||||
{
|
||||
auto exporter = std::make_shared<molo::trace::MemoryExporter>();
|
||||
molo::trace::BatchProcessor processor(exporter);
|
||||
molo::trace::Tracer tracer("test", std::make_shared<molo::trace::AlwaysOnSampler>(),
|
||||
&processor);
|
||||
{
|
||||
molo::trace::Span parent =
|
||||
tracer.start_span("GET /items/{id}", molo::trace::SpanKind::Server);
|
||||
CHECK(ctx, parent.valid());
|
||||
parent.set_attribute("http.request.method", std::string("GET"));
|
||||
parent.set_attribute("http.response.status_code", std::int64_t(200));
|
||||
parent.set_attribute("ratio", 0.25);
|
||||
parent.set_attribute("cached", true);
|
||||
parent.add_event("db.start");
|
||||
{
|
||||
molo::trace::Span child =
|
||||
tracer.start_span("db.query", molo::trace::SpanKind::Client, parent.context());
|
||||
CHECK(ctx, child.valid());
|
||||
CHECK(ctx, child.context().trace_id == parent.context().trace_id);
|
||||
CHECK(ctx, child.context().span_id != parent.context().span_id);
|
||||
child.set_status(molo::trace::StatusCode::Ok);
|
||||
}
|
||||
parent.set_status(molo::trace::StatusCode::Ok);
|
||||
parent.end();
|
||||
parent.end();
|
||||
}
|
||||
processor.flush();
|
||||
auto spans = exporter->spans();
|
||||
CHECK(ctx, spans.size() == 2);
|
||||
const molo::trace::FinishedSpan* server = nullptr;
|
||||
const molo::trace::FinishedSpan* client = nullptr;
|
||||
for (const auto& span : spans) {
|
||||
if (span.kind == molo::trace::SpanKind::Server) server = &span;
|
||||
if (span.kind == molo::trace::SpanKind::Client) client = &span;
|
||||
}
|
||||
CHECK(ctx, server != nullptr && client != nullptr);
|
||||
CHECK(ctx, server->name == "GET /items/{id}");
|
||||
CHECK(ctx, client->parent_span_id == server->context.span_id);
|
||||
CHECK(ctx, !server->parent_span_id.valid());
|
||||
CHECK(ctx, server->attributes.size() == 4);
|
||||
CHECK(ctx, server->events.size() == 1);
|
||||
CHECK(ctx, server->status == molo::trace::StatusCode::Ok);
|
||||
CHECK(ctx, server->end >= server->start);
|
||||
CHECK(ctx, !server->attributes_truncated);
|
||||
CHECK(ctx, tracer.spans_started() == 2);
|
||||
}
|
||||
|
||||
// Attribute and event caps with truncation flags.
|
||||
{
|
||||
auto exporter = std::make_shared<molo::trace::MemoryExporter>();
|
||||
molo::trace::BatchProcessor processor(exporter);
|
||||
molo::trace::Tracer tracer("caps", std::make_shared<molo::trace::AlwaysOnSampler>(),
|
||||
&processor);
|
||||
molo::trace::Span span = tracer.start_span("caps");
|
||||
for (int i = 0; i < 70; ++i) {
|
||||
span.set_attribute("k" + std::to_string(i), std::int64_t(i));
|
||||
}
|
||||
span.set_attribute("big", std::string(2000, 'v'));
|
||||
for (int i = 0; i < 40; ++i) span.add_event("e" + std::to_string(i));
|
||||
span.end();
|
||||
processor.flush();
|
||||
auto spans = exporter->spans();
|
||||
CHECK(ctx, spans.size() == 1);
|
||||
CHECK(ctx, spans[0].attributes.size() == 64);
|
||||
CHECK(ctx, spans[0].attributes_truncated);
|
||||
CHECK(ctx, spans[0].events.size() == 32);
|
||||
CHECK(ctx, spans[0].events_truncated);
|
||||
}
|
||||
|
||||
// Attribute value truncation at 1024 bytes.
|
||||
{
|
||||
auto exporter = std::make_shared<molo::trace::MemoryExporter>();
|
||||
molo::trace::BatchProcessor processor(exporter);
|
||||
molo::trace::Tracer tracer("trunc", std::make_shared<molo::trace::AlwaysOnSampler>(),
|
||||
&processor);
|
||||
molo::trace::Span span = tracer.start_span("trunc");
|
||||
span.set_attribute("long", std::string(2000, 'w'));
|
||||
span.end();
|
||||
processor.flush();
|
||||
auto spans = exporter->spans();
|
||||
CHECK(ctx, spans.size() == 1);
|
||||
auto* text = std::get_if<std::string>(&spans[0].attributes[0].value);
|
||||
CHECK(ctx, text != nullptr && text->size() == 1024);
|
||||
CHECK(ctx, spans[0].attributes_truncated);
|
||||
}
|
||||
|
||||
// Unsampled spans are null but carry a propagatable context.
|
||||
{
|
||||
auto exporter = std::make_shared<molo::trace::MemoryExporter>();
|
||||
molo::trace::BatchProcessor processor(exporter);
|
||||
molo::trace::Tracer tracer("unsampled", std::make_shared<molo::trace::AlwaysOffSampler>(),
|
||||
&processor);
|
||||
molo::trace::Span span = tracer.start_span("dropped");
|
||||
CHECK(ctx, !span.valid());
|
||||
CHECK(ctx, span.context().is_valid());
|
||||
CHECK(ctx, !span.context().is_sampled());
|
||||
span.set_attribute("k", std::int64_t(1));
|
||||
span.end();
|
||||
processor.flush();
|
||||
CHECK(ctx, exporter->spans().empty());
|
||||
}
|
||||
|
||||
// Sampling decision is inherited from the parent (immutable per trace).
|
||||
{
|
||||
auto exporter = std::make_shared<molo::trace::MemoryExporter>();
|
||||
molo::trace::BatchProcessor processor(exporter);
|
||||
molo::trace::Tracer tracer("inherit", std::make_shared<molo::trace::AlwaysOffSampler>(),
|
||||
&processor);
|
||||
molo::trace::SpanContext sampled_parent;
|
||||
sampled_parent.trace_id = molo::trace::TraceId::generate();
|
||||
sampled_parent.span_id = molo::trace::SpanId::generate();
|
||||
sampled_parent.flags.value = molo::trace::TraceFlags::kSampled;
|
||||
molo::trace::Span child =
|
||||
tracer.start_span("child", molo::trace::SpanKind::Internal, sampled_parent);
|
||||
CHECK(ctx, child.valid());
|
||||
CHECK(ctx, child.context().is_sampled());
|
||||
child.end();
|
||||
}
|
||||
|
||||
// Secrets in attributes are redacted.
|
||||
{
|
||||
auto exporter = std::make_shared<molo::trace::MemoryExporter>();
|
||||
molo::trace::BatchProcessor processor(exporter);
|
||||
molo::trace::Tracer tracer("redact", std::make_shared<molo::trace::AlwaysOnSampler>(),
|
||||
&processor);
|
||||
molo::trace::Span span = tracer.start_span("redact");
|
||||
span.set_attribute("auth_token", std::string("abc"));
|
||||
span.end();
|
||||
processor.flush();
|
||||
auto spans = exporter->spans();
|
||||
CHECK(ctx, spans.size() == 1);
|
||||
auto* text = std::get_if<std::string>(&spans[0].attributes[0].value);
|
||||
CHECK(ctx, text != nullptr && *text == "***");
|
||||
}
|
||||
|
||||
// Disabled runtime and missing processor yield null spans and count drops.
|
||||
{
|
||||
molo::trace::Tracer tracer("off");
|
||||
molo::trace::Span span = tracer.start_span("x");
|
||||
CHECK(ctx, !span.valid());
|
||||
CHECK(ctx, tracer.spans_dropped() == 1);
|
||||
|
||||
auto exporter = std::make_shared<molo::trace::MemoryExporter>();
|
||||
molo::trace::BatchProcessor processor(exporter);
|
||||
tracer.set_processor(&processor);
|
||||
molo::obs::RuntimeToggles::instance().tracing.store(false);
|
||||
molo::trace::Span span2 = tracer.start_span("y");
|
||||
molo::obs::RuntimeToggles::instance().tracing.store(true);
|
||||
CHECK(ctx, !span2.valid());
|
||||
molo::trace::Span span3 = tracer.start_span("z");
|
||||
CHECK(ctx, span3.valid());
|
||||
span3.end();
|
||||
}
|
||||
|
||||
CHECK(ctx, molo::trace::span_kind_name(molo::trace::SpanKind::Server) == "server");
|
||||
|
||||
return test_summary(ctx, "tracing");
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
// Zero-cost probe: one TU touching every pillar. Must compile both with
|
||||
// full instrumentation and with MOLO_OBSERVABILITY=0 plus all ENABLE
|
||||
// flags at 0, where every call below must vanish (macros expand to
|
||||
// ((void)0), instruments guard on if constexpr).
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include "observability/events/dispatcher.h"
|
||||
#include "observability/events/event.h"
|
||||
#include "observability/events/lifecycle_events.h"
|
||||
#include "observability/logging/logger.h"
|
||||
#include "observability/logging/macros.h"
|
||||
#include "observability/logging/registry.h"
|
||||
#include "observability/metrics/counter.h"
|
||||
#include "observability/metrics/histogram.h"
|
||||
#include "observability/metrics/registry.h"
|
||||
#include "observability/metrics/timer.h"
|
||||
#include "observability/tracing/span.h"
|
||||
#include "observability/tracing/tracer.h"
|
||||
|
||||
void probe_hot_path(molo::log::Logger& logger, molo::metrics::Counter& counter,
|
||||
molo::metrics::Histogram& histogram, molo::trace::Tracer& tracer,
|
||||
molo::events::Dispatcher& dispatcher) {
|
||||
MOLO_LOG_DEBUG(logger, "hot.path.tick");
|
||||
MOLO_LOG_INFO(logger, "hot.path.tick", {{"n", std::int64_t(1)}});
|
||||
counter.inc();
|
||||
{
|
||||
MOLO_TIMER(histogram);
|
||||
molo::trace::Span span =
|
||||
tracer.start_span("hot", molo::trace::SpanKind::Internal);
|
||||
span.set_attribute("k", std::int64_t(1));
|
||||
}
|
||||
molo::events::Event event("hot.tick");
|
||||
dispatcher.emit(event);
|
||||
}
|
||||
|
||||
int main() {
|
||||
molo::log::Registry logs;
|
||||
auto logger = logs.get("probe");
|
||||
molo::metrics::Registry metrics;
|
||||
auto& counter = metrics.counter("probe_ticks", "probe");
|
||||
auto& histogram = metrics.histogram("probe_latency", "probe");
|
||||
molo::trace::Tracer tracer("probe");
|
||||
molo::events::Dispatcher dispatcher;
|
||||
probe_hot_path(*logger, counter, histogram, tracer, dispatcher);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user