|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <atomic> |
| 4 | +#include <string> |
| 5 | +#include <sstream> |
| 6 | +#include <map> |
| 7 | +#include <mutex> |
| 8 | +#include <shared_mutex> |
| 9 | + |
| 10 | +// Simple thread-safe Prometheus metrics implementation |
| 11 | +// Supports counters with labels |
| 12 | + |
| 13 | +class PrometheusMetrics { |
| 14 | +public: |
| 15 | + // Counter for tracking cumulative values |
| 16 | + class Counter { |
| 17 | + private: |
| 18 | + std::atomic<uint64_t> value{0}; |
| 19 | + |
| 20 | + public: |
| 21 | + void inc(uint64_t n = 1) { |
| 22 | + value.fetch_add(n, std::memory_order_relaxed); |
| 23 | + } |
| 24 | + |
| 25 | + uint64_t get() const { |
| 26 | + return value.load(std::memory_order_relaxed); |
| 27 | + } |
| 28 | + }; |
| 29 | + |
| 30 | + // Labeled counter - allows multiple counters with different label values |
| 31 | + class LabeledCounter { |
| 32 | + private: |
| 33 | + mutable std::shared_mutex mutex; |
| 34 | + std::map<std::string, Counter> counters; |
| 35 | + |
| 36 | + public: |
| 37 | + void inc(const std::string& label, uint64_t n = 1) { |
| 38 | + // Try read lock first for common case |
| 39 | + { |
| 40 | + std::shared_lock<std::shared_mutex> lock(mutex); |
| 41 | + auto it = counters.find(label); |
| 42 | + if (it != counters.end()) { |
| 43 | + it->second.inc(n); |
| 44 | + return; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + // Need to create new counter |
| 49 | + std::unique_lock<std::shared_mutex> lock(mutex); |
| 50 | + counters[label].inc(n); |
| 51 | + } |
| 52 | + |
| 53 | + std::map<std::string, uint64_t> getAll() const { |
| 54 | + std::shared_lock<std::shared_mutex> lock(mutex); |
| 55 | + std::map<std::string, uint64_t> result; |
| 56 | + for (const auto& [label, counter] : counters) { |
| 57 | + result[label] = counter.get(); |
| 58 | + } |
| 59 | + return result; |
| 60 | + } |
| 61 | + }; |
| 62 | + |
| 63 | + // Singleton instance |
| 64 | + static PrometheusMetrics& getInstance() { |
| 65 | + static PrometheusMetrics instance; |
| 66 | + return instance; |
| 67 | + } |
| 68 | + |
| 69 | + // Nostr client message counters (messages FROM clients TO relay) |
| 70 | + LabeledCounter nostrClientMessages; |
| 71 | + |
| 72 | + // Nostr relay message counters (messages FROM relay TO clients) |
| 73 | + LabeledCounter nostrRelayMessages; |
| 74 | + |
| 75 | + // Nostr event counters (by kind) |
| 76 | + LabeledCounter nostrEventsByKind; |
| 77 | + |
| 78 | + // Generate Prometheus text format output |
| 79 | + std::string render() const { |
| 80 | + std::ostringstream out; |
| 81 | + |
| 82 | + // Client messages |
| 83 | + out << "# HELP nostr_client_messages_total Total number of Nostr client messages by verb\n"; |
| 84 | + out << "# TYPE nostr_client_messages_total counter\n"; |
| 85 | + auto clientMsgs = nostrClientMessages.getAll(); |
| 86 | + for (const auto& [verb, count] : clientMsgs) { |
| 87 | + out << "nostr_client_messages_total{verb=\"" << verb << "\"} " << count << "\n"; |
| 88 | + } |
| 89 | + |
| 90 | + // Relay messages |
| 91 | + out << "# HELP nostr_relay_messages_total Total number of Nostr relay messages by verb\n"; |
| 92 | + out << "# TYPE nostr_relay_messages_total counter\n"; |
| 93 | + auto relayMsgs = nostrRelayMessages.getAll(); |
| 94 | + for (const auto& [verb, count] : relayMsgs) { |
| 95 | + out << "nostr_relay_messages_total{verb=\"" << verb << "\"} " << count << "\n"; |
| 96 | + } |
| 97 | + |
| 98 | + // Events by kind |
| 99 | + out << "# HELP nostr_events_total Total number of Nostr events by kind\n"; |
| 100 | + out << "# TYPE nostr_events_total counter\n"; |
| 101 | + auto events = nostrEventsByKind.getAll(); |
| 102 | + for (const auto& [kind, count] : events) { |
| 103 | + out << "nostr_events_total{kind=\"" << kind << "\"} " << count << "\n"; |
| 104 | + } |
| 105 | + |
| 106 | + return out.str(); |
| 107 | + } |
| 108 | + |
| 109 | +private: |
| 110 | + PrometheusMetrics() = default; |
| 111 | + PrometheusMetrics(const PrometheusMetrics&) = delete; |
| 112 | + PrometheusMetrics& operator=(const PrometheusMetrics&) = delete; |
| 113 | +}; |
| 114 | + |
| 115 | +// Convenience macros for incrementing metrics |
| 116 | +#define PROM_INC_CLIENT_MSG(verb) PrometheusMetrics::getInstance().nostrClientMessages.inc(verb) |
| 117 | +#define PROM_INC_RELAY_MSG(verb) PrometheusMetrics::getInstance().nostrRelayMessages.inc(verb) |
| 118 | +#define PROM_INC_EVENT_KIND(kind) PrometheusMetrics::getInstance().nostrEventsByKind.inc(kind) |
0 commit comments