Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
metric_types_adapter.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2021-2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
5#pragma once
6
16#include <string>
17#include <unordered_map>
18#include <variant>
19
20namespace kcenon { namespace monitoring {
21
53struct metric {
54 std::string name;
55 std::variant<double, int64_t, std::string> value;
56 std::unordered_map<std::string, std::string> tags;
58 std::chrono::system_clock::time_point timestamp;
59
60 metric() : timestamp(std::chrono::system_clock::now()) {}
61
62 metric(const std::string& n, const std::variant<double, int64_t, std::string>& v,
63 const std::unordered_map<std::string, std::string>& t,
65 : name(n), value(v), tags(t), type(mt),
66 timestamp(std::chrono::system_clock::now()) {}
67
68 // Convert to compact representation
70 // Simple hash function for name
71 uint32_t hash = 0;
72 for (char c : name) {
73 hash = hash * 31 + static_cast<uint32_t>(c);
74 }
75
76 metric_metadata meta(hash, type, static_cast<uint8_t>(tags.size()));
77
78 if (std::holds_alternative<double>(value)) {
79 return compact_metric_value(meta, std::get<double>(value));
80 } else if (std::holds_alternative<int64_t>(value)) {
81 return compact_metric_value(meta, std::get<int64_t>(value));
82 } else {
83 return compact_metric_value(meta, std::get<std::string>(value));
84 }
85 }
86};
87
118 uint64_t total_collected{0};
119 uint64_t total_errors{0};
120 uint64_t total_dropped{0};
121 std::chrono::milliseconds avg_collection_time{0};
122 std::chrono::system_clock::time_point last_collection;
123
124 double success_rate() const {
125 if (total_collected == 0) return 0.0;
126 return 1.0 - (static_cast<double>(total_errors) / total_collected);
127 }
128
129 void reset() {
130 total_collected = 0;
131 total_errors = 0;
132 total_dropped = 0;
133 avg_collection_time = std::chrono::milliseconds{0};
134 }
135};
136
137} } // namespace kcenon::monitoring
Common metric type definitions for efficient storage.
metric_type
Metric types supported by exporters.
@ gauge
Instantaneous value that can go up and down.
Memory-efficient metric value storage.
Compact metadata for metrics.
Statistics about metric collection.
std::chrono::milliseconds avg_collection_time
std::chrono::system_clock::time_point last_collection
Basic metric structure for interface compatibility.
metric(const std::string &n, const std::variant< double, int64_t, std::string > &v, const std::unordered_map< std::string, std::string > &t, metric_type mt=metric_type::gauge)
compact_metric_value to_compact() const
std::chrono::system_clock::time_point timestamp
std::variant< double, int64_t, std::string > value
std::unordered_map< std::string, std::string > tags