Files
moloapp/example/fw/Observ.hpp
T

73 lines
1.6 KiB
C++
Raw Normal View History

#pragma once
// Observability skeletons: typed event bus, metrics registry, and tracer.
// All three compile to near-zero overhead when kEnabled is false.
// Gap: real 02-observability.md owns the log format, Prometheus and OTel
// export, and the zero-cost macro layer.
#include <functional>
#include <map>
#include <mutex>
#include <string>
#include <vector>
namespace molo::example::fw {
struct Event {
std::string type;
std::string payload;
std::string correlation_id;
};
class EventBus {
public:
static constexpr bool kEnabled = true;
void on(const std::string& type, std::function<void(const Event&)> listener);
void emit(const Event& event);
std::vector<Event> delivered() const;
private:
mutable std::mutex mu_;
std::map<std::string, std::vector<std::function<void(const Event&)>>>
listeners_;
std::vector<Event> delivered_;
};
class Metrics {
public:
static constexpr bool kEnabled = true;
void inc(const std::string& name, double value = 1.0);
void gauge(const std::string& name, double value);
double get(const std::string& name) const;
std::string render_prometheus() const;
private:
mutable std::mutex mu_;
std::map<std::string, double> values_;
};
struct Span {
std::string trace_id;
std::string span_id;
std::string name;
std::string correlation_id;
};
class Telemetry {
public:
static constexpr bool kEnabled = true;
Span start_span(const std::string& name, const std::string& correlation_id);
void finish_span(const Span& span);
std::vector<Span> spans() const;
private:
mutable std::mutex mu_;
std::vector<Span> spans_;
unsigned long next_id_ = 1;
};
} // namespace molo::example::fw