//------------------------------------------------------------------------------ /* This file is part of clio: https://github.com/XRPLF/clio Copyright (c) 2023, the clio developers. Permission to use, copy, modify, and distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ //============================================================================== #include "util/prometheus/Prometheus.hpp" #include "util/Assert.hpp" #include "util/newconfig/ConfigDefinition.hpp" #include "util/prometheus/Bool.hpp" #include "util/prometheus/Counter.hpp" #include "util/prometheus/Gauge.hpp" #include "util/prometheus/Histogram.hpp" #include "util/prometheus/Label.hpp" #include "util/prometheus/MetricBase.hpp" #include "util/prometheus/MetricsFamily.hpp" #include "util/prometheus/OStream.hpp" #include #include #include #include #include #include #include #include namespace util::prometheus { namespace { template MetricType& convertBaseTo(MetricBase& metricBase) { auto result = dynamic_cast(&metricBase); ASSERT(result != nullptr, "Failed to cast metric {} to the requested type", metricBase.name()); return *result; } } // namespace Bool PrometheusImpl::boolMetric(std::string name, Labels labels, std::optional description) { auto& metric = gaugeInt(std::move(name), std::move(labels), std::move(description)); return Bool{metric}; } CounterInt& PrometheusImpl::counterInt(std::string name, Labels labels, std::optional description) { MetricBase& metricBase = getMetric(std::move(name), std::move(labels), std::move(description), MetricType::COUNTER_INT); return convertBaseTo(metricBase); } CounterDouble& PrometheusImpl::counterDouble(std::string name, Labels labels, std::optional description) { MetricBase& metricBase = getMetric(std::move(name), std::move(labels), std::move(description), MetricType::COUNTER_DOUBLE); return convertBaseTo(metricBase); } GaugeInt& PrometheusImpl::gaugeInt(std::string name, Labels labels, std::optional description) { MetricBase& metricBase = getMetric(std::move(name), std::move(labels), std::move(description), MetricType::GAUGE_INT); return convertBaseTo(metricBase); } GaugeDouble& PrometheusImpl::gaugeDouble(std::string name, Labels labels, std::optional description) { MetricBase& metricBase = getMetric(std::move(name), std::move(labels), std::move(description), MetricType::GAUGE_DOUBLE); return convertBaseTo(metricBase); } HistogramInt& PrometheusImpl::histogramInt( std::string name, Labels labels, std::vector const& buckets, std::optional description ) { MetricBase& metricBase = getMetric(std::move(name), std::move(labels), std::move(description), MetricType::HISTOGRAM_INT, buckets); return convertBaseTo(metricBase); } HistogramDouble& PrometheusImpl::histogramDouble( std::string name, Labels labels, std::vector const& buckets, std::optional description ) { MetricBase& metricBase = getMetric(std::move(name), std::move(labels), std::move(description), MetricType::HISTOGRAM_DOUBLE, buckets); return convertBaseTo(metricBase); } std::string PrometheusImpl::collectMetrics() { if (!isEnabled()) return {}; OStream stream{compressReplyEnabled()}; for (auto const& [name, family] : metrics_) { stream << family; } return std::move(stream).data(); } MetricsFamily& PrometheusImpl::getMetricsFamily(std::string name, std::optional description, MetricType type) { auto it = metrics_.find(name); if (it == metrics_.end()) { auto nameCopy = name; it = metrics_.emplace(std::move(nameCopy), MetricsFamily(std::move(name), std::move(description), type)).first; } else if (it->second.type() != type) { throw std::runtime_error("Metrics of different type can't have the same name: " + name); } return it->second; } MetricBase& PrometheusImpl::getMetric( std::string name, Labels labels, std::optional description, MetricType const type ) { auto& metricFamily = getMetricsFamily(std::move(name), std::move(description), type); return metricFamily.getMetric(std::move(labels)); } template requires std::same_as || std::same_as MetricBase& PrometheusImpl::getMetric( std::string name, Labels labels, std::optional description, MetricType type, std::vector const& buckets ) { auto& metricFamily = getMetricsFamily(std::move(name), std::move(description), type); return metricFamily.getMetric(std::move(labels), buckets); } } // namespace util::prometheus void PrometheusService::init(util::config::ClioConfigDefinition const& config) { bool const enabled = config.get("prometheus.enabled"); bool const compressReply = config.get("prometheus.compress_reply"); instance_ = std::make_unique(enabled, compressReply); } util::prometheus::Bool PrometheusService::boolMetric(std::string name, util::prometheus::Labels labels, std::optional description) { return instance().boolMetric(std::move(name), std::move(labels), std::move(description)); } util::prometheus::CounterInt& PrometheusService::counterInt(std::string name, util::prometheus::Labels labels, std::optional description) { return instance().counterInt(std::move(name), std::move(labels), std::move(description)); } util::prometheus::CounterDouble& PrometheusService::counterDouble( std::string name, util::prometheus::Labels labels, std::optional description ) { return instance().counterDouble(std::move(name), std::move(labels), std::move(description)); } util::prometheus::GaugeInt& PrometheusService::gaugeInt(std::string name, util::prometheus::Labels labels, std::optional description) { return instance().gaugeInt(std::move(name), std::move(labels), std::move(description)); } util::prometheus::GaugeDouble& PrometheusService::gaugeDouble( std::string name, util::prometheus::Labels labels, std::optional description ) { return instance().gaugeDouble(std::move(name), std::move(labels), std::move(description)); } util::prometheus::HistogramInt& PrometheusService::histogramInt( std::string name, util::prometheus::Labels labels, std::vector const& buckets, std::optional description ) { return instance().histogramInt(std::move(name), std::move(labels), buckets, std::move(description)); } util::prometheus::HistogramDouble& PrometheusService::histogramDouble( std::string name, util::prometheus::Labels labels, std::vector const& buckets, std::optional description ) { return instance().histogramDouble(std::move(name), std::move(labels), buckets, std::move(description)); } std::string PrometheusService::collectMetrics() { return instance().collectMetrics(); } bool PrometheusService::isEnabled() { return instance().isEnabled(); } bool PrometheusService::compressReplyEnabled() { return instance().compressReplyEnabled(); } void PrometheusService::replaceInstance(std::unique_ptr instance) { instance_ = std::move(instance); } util::prometheus::PrometheusInterface& PrometheusService::instance() { ASSERT(instance_ != nullptr, "PrometheusService::instance() called before init()"); return *instance_; } std::unique_ptr PrometheusService::instance_;