Commit 052e3b60 authored by Dominik Charousset's avatar Dominik Charousset

Fix instance getters on the metric registry

parent 1ea3128e
...@@ -16,6 +16,8 @@ is based on [Keep a Changelog](https://keepachangelog.com). ...@@ -16,6 +16,8 @@ is based on [Keep a Changelog](https://keepachangelog.com).
- The JSON reader now automatically widens integers to doubles as necessary. - The JSON reader now automatically widens integers to doubles as necessary.
- Module options (e.g. for the `middleman`) now show up in `--long-help` output. - Module options (e.g. for the `middleman`) now show up in `--long-help` output.
- Fix undefined behavior in the Qt group chat example (#1336). - Fix undefined behavior in the Qt group chat example (#1336).
- The `..._instance` convenience functions on the registry metric now properly
support `double` metrics and histograms.
### Changed ### Changed
......
...@@ -136,8 +136,9 @@ public: ...@@ -136,8 +136,9 @@ public:
gauge_instance(std::string_view prefix, std::string_view name, gauge_instance(std::string_view prefix, std::string_view name,
span_t<label_view> labels, std::string_view helptext, span_t<label_view> labels, std::string_view helptext,
std::string_view unit = "1", bool is_sum = false) { std::string_view unit = "1", bool is_sum = false) {
auto fptr = gauge_family<ValueType>(prefix, name, labels, helptext, unit, auto label_names = get_label_names(labels);
is_sum); auto fptr = gauge_family<ValueType>(prefix, name, label_names, helptext,
unit, is_sum);
return fptr->get_or_add(labels); return fptr->get_or_add(labels);
} }
...@@ -228,27 +229,6 @@ public: ...@@ -228,27 +229,6 @@ public:
is_sum); is_sum);
} }
/// @copydoc counter_family
template <class ValueType = int64_t>
metric_family_impl<counter<ValueType>>*
counter_family(std::string_view prefix, std::string_view name,
span_t<label_view> labels, std::string_view helptext,
std::string_view unit = "1", bool is_sum = false) {
using counter_type = counter<ValueType>;
using family_type = metric_family_impl<counter_type>;
std::unique_lock<std::mutex> guard{families_mx_};
if (auto ptr = fetch(prefix, name)) {
assert_properties(ptr, counter_type::runtime_type, labels, unit, is_sum);
return static_cast<family_type*>(ptr);
}
auto ptr = std::make_unique<family_type>(
std::string{prefix}, std::string{name}, to_sorted_vec(labels),
std::string{helptext}, std::string{unit}, is_sum);
auto result = ptr.get();
families_.emplace_back(std::move(ptr));
return result;
}
/// Returns a counter. Creates all objects lazily if necessary, but fails /// Returns a counter. Creates all objects lazily if necessary, but fails
/// if the full name already belongs to a different family. /// if the full name already belongs to a different family.
/// @param prefix The prefix (namespace) this family belongs to. Usually the /// @param prefix The prefix (namespace) this family belongs to. Usually the
...@@ -269,8 +249,9 @@ public: ...@@ -269,8 +249,9 @@ public:
counter_instance(std::string_view prefix, std::string_view name, counter_instance(std::string_view prefix, std::string_view name,
span_t<label_view> labels, std::string_view helptext, span_t<label_view> labels, std::string_view helptext,
std::string_view unit = "1", bool is_sum = false) { std::string_view unit = "1", bool is_sum = false) {
auto fptr = counter_family<ValueType>(prefix, name, labels, helptext, unit, auto label_names = get_label_names(labels);
is_sum); auto fptr = counter_family<ValueType>(prefix, name, label_names, helptext,
unit, is_sum);
return fptr->get_or_add(labels); return fptr->get_or_add(labels);
} }
...@@ -419,8 +400,10 @@ public: ...@@ -419,8 +400,10 @@ public:
span_t<label_view> labels, span_t<ValueType> upper_bounds, span_t<label_view> labels, span_t<ValueType> upper_bounds,
std::string_view helptext, std::string_view unit = "1", std::string_view helptext, std::string_view unit = "1",
bool is_sum = false) { bool is_sum = false) {
auto fptr = histogram_family<ValueType>(prefix, name, labels, upper_bounds, auto label_names = get_label_names(labels);
helptext, unit, is_sum); auto fptr = histogram_family<ValueType>(prefix, name, label_names,
upper_bounds, helptext, unit,
is_sum);
return fptr->get_or_add(labels); return fptr->get_or_add(labels);
} }
...@@ -432,8 +415,8 @@ public: ...@@ -432,8 +415,8 @@ public:
span_t<ValueType> upper_bounds, std::string_view helptext, span_t<ValueType> upper_bounds, std::string_view helptext,
std::string_view unit = "1", bool is_sum = false) { std::string_view unit = "1", bool is_sum = false) {
span_t<label_view> lbls{labels.begin(), labels.size()}; span_t<label_view> lbls{labels.begin(), labels.size()};
return histogram_instance(prefix, name, lbls, upper_bounds, helptext, unit, return histogram_instance<ValueType>(prefix, name, lbls, upper_bounds,
is_sum); helptext, unit, is_sum);
} }
/// Returns a histogram metric singleton, i.e., the single instance of a /// Returns a histogram metric singleton, i.e., the single instance of a
...@@ -489,6 +472,8 @@ private: ...@@ -489,6 +472,8 @@ private:
metric_family* fetch(const std::string_view& prefix, metric_family* fetch(const std::string_view& prefix,
const std::string_view& name); const std::string_view& name);
static std::vector<std::string_view> get_label_names(span_t<label_view> xs);
static std::vector<std::string> to_sorted_vec(span_t<std::string_view> xs); static std::vector<std::string> to_sorted_vec(span_t<std::string_view> xs);
static std::vector<std::string> to_sorted_vec(span_t<label_view> xs); static std::vector<std::string> to_sorted_vec(span_t<label_view> xs);
......
...@@ -63,6 +63,15 @@ metric_family* metric_registry::fetch(const std::string_view& prefix, ...@@ -63,6 +63,15 @@ metric_family* metric_registry::fetch(const std::string_view& prefix,
return nullptr; return nullptr;
} }
std::vector<std::string_view>
metric_registry::get_label_names(span_t<label_view> xs) {
std::vector<std::string_view> result;
result.reserve(xs.size());
for (auto& x : xs)
result.push_back(x.name());
return result;
}
std::vector<std::string> std::vector<std::string>
metric_registry::to_sorted_vec(span<const std::string_view> xs) { metric_registry::to_sorted_vec(span<const std::string_view> xs) {
std::vector<std::string> result; std::vector<std::string> result;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment