Make MetricKey hashable
This commit is contained in:
@@ -4,8 +4,35 @@
|
|||||||
#include <thread>
|
#include <thread>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
namespace metric {
|
||||||
|
struct MetricKey {
|
||||||
|
std::string_view name;
|
||||||
|
std::vector<std::pair<std::string, std::string>> labels;
|
||||||
|
bool operator==(const MetricKey &other) const {
|
||||||
|
return name == other.name && labels == other.labels;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} // namespace metric
|
||||||
|
|
||||||
|
namespace std {
|
||||||
|
template <> struct hash<metric::MetricKey> {
|
||||||
|
std::size_t operator()(const metric::MetricKey &k) const {
|
||||||
|
thread_local std::vector<size_t> parts;
|
||||||
|
parts.clear();
|
||||||
|
parts.push_back(std::hash<std::string_view>{}(k.name));
|
||||||
|
for (const auto &p : k.labels) {
|
||||||
|
parts.push_back(std::hash<std::string>{}(p.first));
|
||||||
|
parts.push_back(std::hash<std::string>{}(p.second));
|
||||||
|
}
|
||||||
|
return std::hash<std::string_view>{}(
|
||||||
|
std::string_view{reinterpret_cast<const char *>(parts.data()),
|
||||||
|
parts.size() * sizeof(size_t)});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} // namespace std
|
||||||
|
|
||||||
namespace metric {
|
namespace metric {
|
||||||
|
|
||||||
struct Counter::State {};
|
struct Counter::State {};
|
||||||
struct Gauge::State {};
|
struct Gauge::State {};
|
||||||
struct Histogram::State {};
|
struct Histogram::State {};
|
||||||
@@ -13,11 +40,6 @@ struct Metric {
|
|||||||
|
|
||||||
static std::mutex mutex;
|
static std::mutex mutex;
|
||||||
|
|
||||||
struct MetricKey {
|
|
||||||
std::string_view name;
|
|
||||||
std::vector<std::pair<std::string, std::string>> labels;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct PerThreadState {
|
struct PerThreadState {
|
||||||
std::unordered_map<MetricKey, Counter::State> counters;
|
std::unordered_map<MetricKey, Counter::State> counters;
|
||||||
std::unordered_map<MetricKey, Histogram::State> histograms;
|
std::unordered_map<MetricKey, Histogram::State> histograms;
|
||||||
|
|||||||
@@ -3,8 +3,10 @@
|
|||||||
#include "arena_allocator.hpp"
|
#include "arena_allocator.hpp"
|
||||||
#include <initializer_list>
|
#include <initializer_list>
|
||||||
#include <span>
|
#include <span>
|
||||||
|
#include <string>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
namespace metric {
|
namespace metric {
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user