#include "observability/metrics/registry.h" #include #include namespace molo::metrics { namespace { bool valid_name_char(char ch, bool first) { if (ch >= 'a' && ch <= 'z') return true; if (ch >= 'A' && ch <= 'Z') return true; if (ch == '_' || ch == ':') return true; if (!first && ch >= '0' && ch <= '9') return true; return false; } bool valid_metric_name(std::string_view name) { if (name.empty()) return false; for (std::size_t i = 0; i < name.size(); ++i) { if (!valid_name_char(name[i], i == 0)) return false; } return true; } bool ends_with_total(const std::string& name) { return name.size() >= 6 && name.compare(name.size() - 6, 6, "_total") == 0; } } // namespace Registry::Registry() : overflow_counter_("molo_metrics_cardinality_overflows_total", "Series whose label value overflowed into other", Labels{}), blackhole_counter_("molo_blackhole_total", "Overflow sink, never exported", Labels{}), blackhole_gauge_("molo_blackhole", "Overflow sink, never exported", Labels{}), blackhole_histogram_("molo_blackhole_hist", "Overflow sink, never exported", Labels{}, Histogram::default_http_buckets()), overflows_(0) {} Counter& Registry::counter(const std::string& name, const std::string& help, Labels labels) { std::string full = name; if (!ends_with_total(full)) full += "_total"; if (!valid_metric_name(full)) throw std::invalid_argument("invalid counter name: " + full); std::lock_guard lock(mutex_); Labels mapped = apply_cardinality(full, std::move(labels)); std::string key = "c\x01" + full + "\x01" + mapped.key(); auto it = counters_.find(key); if (it != counters_.end()) return *it->second; if (total_series_locked() >= molo::obs::Caps::kMaxSeries) { overflows_.fetch_add(1, std::memory_order_relaxed); overflow_counter_.inc(); return blackhole_counter_; } auto inserted = counters_.emplace(key, std::make_unique(full, help, std::move(mapped))); return *inserted.first->second; } Gauge& Registry::gauge(const std::string& name, const std::string& help, Labels labels) { if (!valid_metric_name(name)) throw std::invalid_argument("invalid gauge name: " + name); std::lock_guard lock(mutex_); Labels mapped = apply_cardinality(name, std::move(labels)); std::string key = "g\x01" + name + "\x01" + mapped.key(); auto it = gauges_.find(key); if (it != gauges_.end()) return *it->second; if (total_series_locked() >= molo::obs::Caps::kMaxSeries) { overflows_.fetch_add(1, std::memory_order_relaxed); overflow_counter_.inc(); return blackhole_gauge_; } auto inserted = gauges_.emplace(key, std::make_unique(name, help, std::move(mapped))); return *inserted.first->second; } Histogram& Registry::histogram(const std::string& name, const std::string& help, Labels labels, std::vector bounds) { if (!valid_metric_name(name)) throw std::invalid_argument("invalid histogram name: " + name); std::lock_guard lock(mutex_); Labels mapped = apply_cardinality(name, std::move(labels)); std::string key = "h\x01" + name + "\x01" + mapped.key(); auto it = histograms_.find(key); if (it != histograms_.end()) return *it->second; if (total_series_locked() >= molo::obs::Caps::kMaxSeries) { overflows_.fetch_add(1, std::memory_order_relaxed); overflow_counter_.inc(); return blackhole_histogram_; } auto inserted = histograms_.emplace( key, std::make_unique(name, help, std::move(mapped), std::move(bounds))); return *inserted.first->second; } void Registry::ensure_framework_metrics() { gauge("molo_http_requests_in_flight", "HTTP requests currently in flight"); counter("molo_log_dropped_total", "Log records dropped under saturation"); counter("molo_spans_dropped_total", "Spans dropped under saturation"); counter("molo_trace_propagator_errors_total", "Malformed traceparent headers"); for (const char* level : {"trace", "debug", "info", "warn", "error", "critical"}) { Labels labels; labels.add("level", level); counter("molo_log_records_total", "Log records emitted", labels); } for (const char* kind : {"server", "client", "internal", "producer", "consumer"}) { Labels labels; labels.add("kind", kind); counter("molo_spans_total", "Spans started", labels); } } std::size_t Registry::series_count() const { std::lock_guard lock(mutex_); return total_series_locked(); } std::uint64_t Registry::cardinality_overflows() const { return overflows_.load(std::memory_order_relaxed); } Registry::Snapshot Registry::snapshot() const { Snapshot out; std::lock_guard lock(mutex_); for (const auto& entry : counters_) { const Counter& metric = *entry.second; CounterSnap snap; snap.name = metric.name(); snap.help = metric.help(); snap.labels = metric.labels(); snap.value = metric.value(); out.counters.push_back(std::move(snap)); } CounterSnap overflow; overflow.name = overflow_counter_.name(); overflow.help = overflow_counter_.help(); overflow.labels = overflow_counter_.labels(); overflow.value = overflow_counter_.value(); out.counters.push_back(std::move(overflow)); for (const auto& entry : gauges_) { const Gauge& metric = *entry.second; GaugeSnap snap; snap.name = metric.name(); snap.help = metric.help(); snap.labels = metric.labels(); snap.value = metric.value(); out.gauges.push_back(std::move(snap)); } for (const auto& entry : histograms_) { const Histogram& metric = *entry.second; HistogramSnap snap; snap.name = metric.name(); snap.help = metric.help(); snap.labels = metric.labels(); snap.bounds = metric.bounds(); Histogram::Snapshot inner = metric.snapshot(); snap.cumulative = std::move(inner.cumulative); snap.sum = inner.sum; snap.count = inner.count; out.histograms.push_back(std::move(snap)); } return out; } Registry& Registry::global() { static Registry instance; return instance; } Labels Registry::apply_cardinality(const std::string& metric, Labels labels) { Labels mapped; for (const auto& [key, value] : labels.items()) { std::string track = metric + "\x00" + key; auto& seen = distinct_[track]; if (value == "other" || seen.count(value) > 0) { mapped.add(key, value); continue; } if (seen.size() >= molo::obs::Caps::kMaxLabelValues) { seen.insert("other"); mapped.add(key, "other"); overflows_.fetch_add(1, std::memory_order_relaxed); overflow_counter_.inc(); } else { seen.insert(value); mapped.add(key, value); } } return mapped; } std::size_t Registry::total_series_locked() const { return counters_.size() + gauges_.size() + histograms_.size(); } } // namespace molo::metrics