Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
kcenon::network::metrics::histogram_snapshot Struct Reference

Immutable snapshot of histogram state for export. More...

#include <histogram.h>

Collaboration diagram for kcenon::network::metrics::histogram_snapshot:
Collaboration graph

Public Member Functions

auto to_prometheus (const std::string &name) const -> std::string
 Export histogram in Prometheus format.
 
auto to_json () const -> std::string
 Export histogram as JSON.
 

Public Attributes

uint64_t count
 Total number of observations.
 
double sum
 Sum of all observed values.
 
double min_value
 Minimum observed value.
 
double max_value
 Maximum observed value.
 
std::map< double, double > percentiles
 Percentile -> value mapping.
 
std::vector< std::pair< double, uint64_t > > buckets
 Boundary -> cumulative count.
 
std::map< std::string, std::string > labels
 Additional metric labels.
 

Detailed Description

Immutable snapshot of histogram state for export.

Definition at line 59 of file histogram.h.

Member Function Documentation

◆ to_json()

auto kcenon::network::metrics::histogram_snapshot::to_json ( ) const -> std::string
nodiscard

Export histogram as JSON.

Returns
JSON-formatted string

Definition at line 82 of file histogram.cpp.

83{
84 std::ostringstream oss;
85 oss << std::fixed << std::setprecision(6);
86
87 oss << "{";
88 oss << "\"count\":" << count << ",";
89 oss << "\"sum\":" << sum << ",";
90 oss << "\"min\":" << min_value << ",";
91 oss << "\"max\":" << max_value << ",";
92
93 // Percentiles
94 oss << "\"percentiles\":{";
95 bool first = true;
96 for (const auto& [p, v] : percentiles)
97 {
98 if (!first)
99 {
100 oss << ",";
101 }
102 oss << "\"" << p << "\":" << v;
103 first = false;
104 }
105 oss << "},";
106
107 // Buckets
108 oss << "\"buckets\":[";
109 first = true;
110 for (const auto& [boundary, bucket_count] : buckets)
111 {
112 if (!first)
113 {
114 oss << ",";
115 }
116 oss << "{\"le\":";
117 if (boundary == std::numeric_limits<double>::infinity())
118 {
119 oss << "\"+Inf\"";
120 }
121 else
122 {
123 oss << boundary;
124 }
125 oss << ",\"count\":" << bucket_count << "}";
126 first = false;
127 }
128 oss << "],";
129
130 // Labels
131 oss << "\"labels\":{";
132 first = true;
133 for (const auto& [key, value] : labels)
134 {
135 if (!first)
136 {
137 oss << ",";
138 }
139 oss << "\"" << key << "\":\"" << value << "\"";
140 first = false;
141 }
142 oss << "}";
143
144 oss << "}";
145
146 return oss.str();
147}
std::map< double, double > percentiles
Percentile -> value mapping.
Definition histogram.h:65
std::map< std::string, std::string > labels
Additional metric labels.
Definition histogram.h:67
uint64_t count
Total number of observations.
Definition histogram.h:61
double sum
Sum of all observed values.
Definition histogram.h:62
double min_value
Minimum observed value.
Definition histogram.h:63
std::vector< std::pair< double, uint64_t > > buckets
Boundary -> cumulative count.
Definition histogram.h:66
double max_value
Maximum observed value.
Definition histogram.h:64

References buckets, count, labels, max_value, min_value, percentiles, and sum.

◆ to_prometheus()

auto kcenon::network::metrics::histogram_snapshot::to_prometheus ( const std::string & name) const -> std::string
nodiscard

Export histogram in Prometheus format.

Parameters
nameMetric name
Returns
Prometheus-formatted string

Definition at line 19 of file histogram.cpp.

20{
21 std::ostringstream oss;
22 oss << std::fixed << std::setprecision(6);
23
24 // Build label string
25 std::string label_str;
26 if (!labels.empty())
27 {
28 std::ostringstream label_oss;
29 label_oss << "{";
30 bool first = true;
31 for (const auto& [key, value] : labels)
32 {
33 if (!first)
34 {
35 label_oss << ",";
36 }
37 label_oss << key << "=\"" << value << "\"";
38 first = false;
39 }
40 label_oss << "}";
41 label_str = label_oss.str();
42 }
43
44 // Output bucket counts (cumulative)
45 for (const auto& [boundary, bucket_count] : buckets)
46 {
47 oss << name << "_bucket{le=\"";
48 if (boundary == std::numeric_limits<double>::infinity())
49 {
50 oss << "+Inf";
51 }
52 else
53 {
54 oss << boundary;
55 }
56 oss << "\"";
57 if (!labels.empty())
58 {
59 oss << "," << label_str.substr(1, label_str.length() - 2);
60 }
61 oss << "} " << bucket_count << "\n";
62 }
63
64 // Output sum and count
65 oss << name << "_sum";
66 if (!labels.empty())
67 {
68 oss << label_str;
69 }
70 oss << " " << sum << "\n";
71
72 oss << name << "_count";
73 if (!labels.empty())
74 {
75 oss << label_str;
76 }
77 oss << " " << count << "\n";
78
79 return oss.str();
80}

Member Data Documentation

◆ buckets

std::vector<std::pair<double, uint64_t> > kcenon::network::metrics::histogram_snapshot::buckets

Boundary -> cumulative count.

Definition at line 66 of file histogram.h.

Referenced by kcenon::network::metrics::sliding_histogram::aggregate(), kcenon::network::metrics::histogram::snapshot(), and to_json().

◆ count

uint64_t kcenon::network::metrics::histogram_snapshot::count

Total number of observations.

Definition at line 61 of file histogram.h.

Referenced by kcenon::network::metrics::sliding_histogram::aggregate(), kcenon::network::metrics::histogram::snapshot(), and to_json().

◆ labels

std::map<std::string, std::string> kcenon::network::metrics::histogram_snapshot::labels

Additional metric labels.

Definition at line 67 of file histogram.h.

Referenced by kcenon::network::metrics::histogram::snapshot(), and to_json().

◆ max_value

double kcenon::network::metrics::histogram_snapshot::max_value

◆ min_value

double kcenon::network::metrics::histogram_snapshot::min_value

◆ percentiles

std::map<double, double> kcenon::network::metrics::histogram_snapshot::percentiles

Percentile -> value mapping.

Definition at line 65 of file histogram.h.

Referenced by kcenon::network::metrics::sliding_histogram::aggregate(), kcenon::network::metrics::histogram::snapshot(), and to_json().

◆ sum

double kcenon::network::metrics::histogram_snapshot::sum

The documentation for this struct was generated from the following files: