diff --git a/src/metric.cpp b/src/metric.cpp index 0689aca..4fa03ad 100644 --- a/src/metric.cpp +++ b/src/metric.cpp @@ -4,8 +4,35 @@ #include #include #include +namespace metric { +struct MetricKey { + std::string_view name; + std::vector> labels; + bool operator==(const MetricKey &other) const { + return name == other.name && labels == other.labels; + } +}; +} // namespace metric + +namespace std { +template <> struct hash { + std::size_t operator()(const metric::MetricKey &k) const { + thread_local std::vector parts; + parts.clear(); + parts.push_back(std::hash{}(k.name)); + for (const auto &p : k.labels) { + parts.push_back(std::hash{}(p.first)); + parts.push_back(std::hash{}(p.second)); + } + return std::hash{}( + std::string_view{reinterpret_cast(parts.data()), + parts.size() * sizeof(size_t)}); + } +}; +} // namespace std namespace metric { + struct Counter::State {}; struct Gauge::State {}; struct Histogram::State {}; @@ -13,11 +40,6 @@ struct Metric { static std::mutex mutex; - struct MetricKey { - std::string_view name; - std::vector> labels; - }; - struct PerThreadState { std::unordered_map counters; std::unordered_map histograms; diff --git a/src/metric.hpp b/src/metric.hpp index 25ae7fa..3be5949 100644 --- a/src/metric.hpp +++ b/src/metric.hpp @@ -3,8 +3,10 @@ #include "arena_allocator.hpp" #include #include +#include #include #include +#include namespace metric {