Advise to cache Metric instance

This commit is contained in:
2025-08-31 14:43:34 -04:00
parent 58649103e5
commit 8b828be0a9

View File

@@ -123,10 +123,14 @@ template <class T> struct Family {
static_assert(std::is_same_v<T, Counter> || std::is_same_v<T, Gauge> ||
std::is_same_v<T, Histogram>);
// Create metric instance with specific labels
// Labels are sorted by key for Prometheus compatibility
// ERROR: Will abort if labels already registered via register_callback()
// OK: Multiple calls with same labels return same instance (idempotent)
// Create metric instance with specific labels.
// For performance, it is recommended to create instances once and cache them
// for reuse, rather than calling .create() repeatedly in
// performance-critical paths.
//
// Labels are sorted by key for Prometheus compatibility.
// ERROR: Will abort if labels already registered via register_callback().
// OK: Multiple calls with same labels return same instance (idempotent).
T create(std::initializer_list<std::pair<std::string_view, std::string_view>>
labels) {
return create(