Files
moloapp/example/fw/Observ.hpp
T
retoor 3c4334cec9 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.
2026-10-23 18:20:04 +02:00

73 lines
1.6 KiB
C++

#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