// See the file "COPYING" in the main distribution directory for copyright. #include "zeek/telemetry/Gauge.h" #include using namespace zeek::telemetry; double Gauge::Value() const noexcept { if ( callback ) return callback(); return handle.Value(); } Gauge::Gauge(FamilyType* family, const prometheus::Labels& labels, detail::CollectCallbackPtr callback) noexcept : family(family), handle(family->Add(labels)), labels(labels), callback(std::move(callback)) {} std::shared_ptr GaugeFamily::GetOrAdd(Span labels, detail::CollectCallbackPtr callback) { prometheus::Labels p_labels = detail::BuildPrometheusLabels(labels); auto check = [&](const std::shared_ptr& gauge) { return gauge->CompareLabels(p_labels); }; if ( auto it = std::find_if(gauges.begin(), gauges.end(), check); it != gauges.end() ) return *it; auto gauge = std::make_shared(family, p_labels, callback); gauges.push_back(gauge); return gauge; } std::shared_ptr GaugeFamily::GetOrAdd(std::initializer_list labels, detail::CollectCallbackPtr callback) { return GetOrAdd(Span{labels.begin(), labels.size()}, std::move(callback)); } void GaugeFamily::RunCallbacks() { for ( const auto& g : gauges ) { if ( g->HasCallback() ) g->Set(g->RunCallback()); } }