From: Ensar Sarajčić Date: Thu, 12 Dec 2024 16:23:05 +0000 (+0100) Subject: dnsdist: use template function to simplify metrics mutation functions X-Git-Tag: dnsdist-2.0.0-alpha0^2~10 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=657caa55a4c3ae37a039c7de24762553b08ed8f3;p=thirdparty%2Fpdns.git dnsdist: use template function to simplify metrics mutation functions --- diff --git a/pdns/dnsdistdist/dnsdist-metrics.cc b/pdns/dnsdistdist/dnsdist-metrics.cc index 4f9c92b94f..10d18dc292 100644 --- a/pdns/dnsdistdist/dnsdist-metrics.cc +++ b/pdns/dnsdistdist/dnsdist-metrics.cc @@ -22,6 +22,7 @@ #include #include #include +#include #include "dnsdist-metrics.hh" #include "dnsdist.hh" @@ -233,19 +234,26 @@ static std::string generateCombinationOfLabels(const std::unordered_map +static T& initializeOrGetMetric(const std::string_view& name, std::map& metricEntries, const std::unordered_map& labels) +{ + auto combinationOfLabels = generateCombinationOfLabels(labels); + auto metricEntry = metricEntries.find(combinationOfLabels); + if (metricEntry == metricEntries.end()) { + metricEntry = metricEntries.emplace(std::piecewise_construct, std::forward_as_tuple(combinationOfLabels), std::forward_as_tuple()).first; + g_stats.entries.write_lock()->emplace_back(Stats::EntryTriple{std::string(name), combinationOfLabels, &metricEntry->second.d_value}); + } + return metricEntry->second; +} + std::variant incrementCustomCounter(const std::string_view& name, uint64_t step, const std::unordered_map& labels) { auto customCounters = s_customCounters.write_lock(); auto metric = customCounters->find(name); if (metric != customCounters->end()) { - auto combinationOfLabels = generateCombinationOfLabels(labels); - auto metricEntry = metric->second.find(combinationOfLabels); - if (metricEntry == metric->second.end()) { - metricEntry = metric->second.emplace(combinationOfLabels, MutableCounter()).first; - g_stats.entries.write_lock()->emplace_back(Stats::EntryTriple{std::string(name), combinationOfLabels, &metricEntry->second.d_value}); - } - metricEntry->second.d_value += step; - return metricEntry->second.d_value.load(); + auto& metricEntry = initializeOrGetMetric(name, metric->second, labels); + metricEntry.d_value += step; + return metricEntry.d_value.load(); } return std::string("Unable to increment custom metric '") + std::string(name) + "': no such counter"; } @@ -255,14 +263,9 @@ std::variant decrementCustomCounter(const std::string_view& nam auto customCounters = s_customCounters.write_lock(); auto metric = customCounters->find(name); if (metric != customCounters->end()) { - auto combinationOfLabels = generateCombinationOfLabels(labels); - auto metricEntry = metric->second.find(combinationOfLabels); - if (metricEntry == metric->second.end()) { - metricEntry = metric->second.emplace(combinationOfLabels, MutableCounter()).first; - g_stats.entries.write_lock()->emplace_back(Stats::EntryTriple{std::string(name), combinationOfLabels, &metricEntry->second.d_value}); - } - metricEntry->second.d_value -= step; - return metricEntry->second.d_value.load(); + auto& metricEntry = initializeOrGetMetric(name, metric->second, labels); + metricEntry.d_value -= step; + return metricEntry.d_value.load(); } return std::string("Unable to decrement custom metric '") + std::string(name) + "': no such counter"; } @@ -272,13 +275,8 @@ std::variant setCustomGauge(const std::string_view& name, const d auto customGauges = s_customGauges.write_lock(); auto metric = customGauges->find(name); if (metric != customGauges->end()) { - auto combinationOfLabels = generateCombinationOfLabels(labels); - auto metricEntry = metric->second.find(combinationOfLabels); - if (metricEntry == metric->second.end()) { - metricEntry = metric->second.emplace(combinationOfLabels, MutableGauge()).first; - g_stats.entries.write_lock()->emplace_back(Stats::EntryTriple{std::string(name), combinationOfLabels, &metricEntry->second.d_value}); - } - metricEntry->second.d_value = value; + auto& metricEntry = initializeOrGetMetric(name, metric->second, labels); + metricEntry.d_value = value; return value; }