Skip to content

Commit

Permalink
feat(core): Add Info metric
Browse files Browse the repository at this point in the history
  • Loading branch information
petitlapin authored and gjasny committed Jun 18, 2022
1 parent d7b335a commit 2ce3bb0
Show file tree
Hide file tree
Showing 18 changed files with 197 additions and 6 deletions.
1 change: 1 addition & 0 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ add_library(core
src/family.cc
src/gauge.cc
src/histogram.cc
src/info.cc
src/registry.cc
src/serializer.cc
src/summary.cc
Expand Down
1 change: 1 addition & 0 deletions core/benchmarks/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ add_executable(benchmarks
counter_bench.cc
gauge_bench.cc
histogram_bench.cc
info_bench.cc
registry_bench.cc
summary_bench.cc
)
Expand Down
20 changes: 20 additions & 0 deletions core/benchmarks/info_bench.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <benchmark/benchmark.h>

#include "prometheus/family.h"
#include "prometheus/info.h"
#include "prometheus/registry.h"

static void BM_Info_Collect(benchmark::State& state) {
using prometheus::BuildInfo;
using prometheus::Info;
using prometheus::Registry;
Registry registry;
auto& info_family =
BuildInfo().Name("benchmark_info").Help("").Register(registry);
auto& info = info_family.Add({});

while (state.KeepRunning()) {
benchmark::DoNotOptimize(info.Collect());
};
}
BENCHMARK(BM_Info_Collect);
7 changes: 7 additions & 0 deletions core/include/prometheus/client_metric.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ struct PROMETHEUS_CPP_CORE_EXPORT ClientMetric {
};
Gauge gauge;

// Info

struct Info {
double value = 1.0;
};
Info info;

// Summary

struct Quantile {
Expand Down
1 change: 1 addition & 0 deletions core/include/prometheus/family.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// IWYU pragma: no_include "prometheus/counter.h"
// IWYU pragma: no_include "prometheus/gauge.h"
// IWYU pragma: no_include "prometheus/histogram.h"
// IWYU pragma: no_include "prometheus/info.h"
// IWYU pragma: no_include "prometheus/summary.h"

namespace prometheus {
Expand Down
68 changes: 68 additions & 0 deletions core/include/prometheus/info.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#pragma once

#include "prometheus/client_metric.h"
#include "prometheus/detail/builder.h" // IWYU pragma: export
#include "prometheus/detail/core_export.h"
#include "prometheus/metric_type.h"

namespace prometheus {

/// \brief A info metric to represent textual information which should not
/// change during process lifetime.
///
/// This class represents the metric type info:
/// https://github.com/OpenObservability/OpenMetrics/blob/98ae26c87b1c3bcf937909a880b32c8be643cc9b/specification/OpenMetrics.md#info

/// Prometheus does not provide this type directly, it is used by emulating a
/// gauge with value 1: https://prometheus.io/docs/concepts/metric_types/#gauge
///
/// The value of the info cannot change. Example of infos are:
/// - the application's version
/// - revision control commit
/// - version of the compiler
///
/// The class is thread-safe. No concurrent call to any API of this type causes
/// a data race.
class PROMETHEUS_CPP_CORE_EXPORT Info {
public:
static const MetricType metric_type{MetricType::Info};

/// \brief Create a info.
Info() = default;

/// \brief Get the current value of the info.
///
/// Collect is called by the Registry when collecting metrics.
ClientMetric Collect() const;
};

/// \brief Return a builder to configure and register a Info metric.
///
/// @copydetails Family<>::Family()
///
/// Example usage:
///
/// \code
/// auto registry = std::make_shared<Registry>();
/// auto& info_family = prometheus::BuildInfo()
/// .Name("some_name")
/// .Help("Additional description.")
/// .Labels({{"key", "value"}})
/// .Register(*registry);
///
/// ...
/// \endcode
///
/// \return An object of unspecified type T, i.e., an implementation detail
/// except that it has the following members:
///
/// - Name(const std::string&) to set the metric name,
/// - Help(const std::string&) to set an additional description.
/// - Labels(const Labels&) to assign a set of
/// key-value pairs (= labels) to the metric.
///
/// To finish the configuration of the Info metric, register it with
/// Register(Registry&).
PROMETHEUS_CPP_CORE_EXPORT detail::Builder<Info> BuildInfo();

} // namespace prometheus
1 change: 1 addition & 0 deletions core/include/prometheus/metric_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ enum class MetricType {
Summary,
Untyped,
Histogram,
Info,
};

} // namespace prometheus
4 changes: 3 additions & 1 deletion core/include/prometheus/registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace prometheus {
class Counter;
class Gauge;
class Histogram;
class Info;
class Summary;

namespace detail {
Expand All @@ -33,7 +34,7 @@ class Builder; // IWYU pragma: keep
/// that returns zero or more metrics and their samples. The metrics are
/// represented by the class Family<>, which implements the Collectable
/// interface. A new metric is registered with BuildCounter(), BuildGauge(),
/// BuildHistogram() or BuildSummary().
/// BuildHistogram(), BuildInfo() or BuildSummary().
///
/// The class is thread-safe. No concurrent call to any API of this type causes
/// a data race.
Expand Down Expand Up @@ -111,6 +112,7 @@ class PROMETHEUS_CPP_CORE_EXPORT Registry : public Collectable {
std::vector<std::unique_ptr<Family<Counter>>> counters_;
std::vector<std::unique_ptr<Family<Gauge>>> gauges_;
std::vector<std::unique_ptr<Family<Histogram>>> histograms_;
std::vector<std::unique_ptr<Family<Info>>> infos_;
std::vector<std::unique_ptr<Family<Summary>>> summaries_;
mutable std::mutex mutex_;
};
Expand Down
3 changes: 3 additions & 0 deletions core/src/detail/builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "prometheus/detail/core_export.h"
#include "prometheus/gauge.h"
#include "prometheus/histogram.h"
#include "prometheus/info.h"
#include "prometheus/registry.h"
#include "prometheus/summary.h"

Expand Down Expand Up @@ -37,13 +38,15 @@ Family<T>& Builder<T>::Register(Registry& registry) {
template class PROMETHEUS_CPP_CORE_EXPORT Builder<Counter>;
template class PROMETHEUS_CPP_CORE_EXPORT Builder<Gauge>;
template class PROMETHEUS_CPP_CORE_EXPORT Builder<Histogram>;
template class PROMETHEUS_CPP_CORE_EXPORT Builder<Info>;
template class PROMETHEUS_CPP_CORE_EXPORT Builder<Summary>;

} // namespace detail

detail::Builder<Counter> BuildCounter() { return {}; }
detail::Builder<Gauge> BuildGauge() { return {}; }
detail::Builder<Histogram> BuildHistogram() { return {}; }
detail::Builder<Info> BuildInfo() { return {}; }
detail::Builder<Summary> BuildSummary() { return {}; }

} // namespace prometheus
2 changes: 2 additions & 0 deletions core/src/family.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "prometheus/counter.h"
#include "prometheus/gauge.h"
#include "prometheus/histogram.h"
#include "prometheus/info.h"
#include "prometheus/summary.h"

namespace prometheus {
Expand Down Expand Up @@ -124,6 +125,7 @@ ClientMetric Family<T>::CollectMetric(const Labels& metric_labels,
template class PROMETHEUS_CPP_CORE_EXPORT Family<Counter>;
template class PROMETHEUS_CPP_CORE_EXPORT Family<Gauge>;
template class PROMETHEUS_CPP_CORE_EXPORT Family<Histogram>;
template class PROMETHEUS_CPP_CORE_EXPORT Family<Info>;
template class PROMETHEUS_CPP_CORE_EXPORT Family<Summary>;

} // namespace prometheus
11 changes: 11 additions & 0 deletions core/src/info.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "prometheus/info.h"

namespace prometheus {

ClientMetric Info::Collect() const {
ClientMetric metric;
metric.info.value = 1;
return metric;
}

} // namespace prometheus
27 changes: 23 additions & 4 deletions core/src/registry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "prometheus/counter.h"
#include "prometheus/gauge.h"
#include "prometheus/histogram.h"
#include "prometheus/info.h"
#include "prometheus/summary.h"

namespace prometheus {
Expand Down Expand Up @@ -49,6 +50,7 @@ std::vector<MetricFamily> Registry::Collect() const {
CollectAll(results, counters_);
CollectAll(results, gauges_);
CollectAll(results, histograms_);
CollectAll(results, infos_);
CollectAll(results, summaries_);

return results;
Expand All @@ -69,29 +71,39 @@ std::vector<std::unique_ptr<Family<Histogram>>>& Registry::GetFamilies() {
return histograms_;
}

template <>
std::vector<std::unique_ptr<Family<Info>>>& Registry::GetFamilies() {
return infos_;
}

template <>
std::vector<std::unique_ptr<Family<Summary>>>& Registry::GetFamilies() {
return summaries_;
}

template <>
bool Registry::NameExistsInOtherType<Counter>(const std::string& name) const {
return FamilyNameExists(name, gauges_, histograms_, summaries_);
return FamilyNameExists(name, gauges_, histograms_, infos_, summaries_);
}

template <>
bool Registry::NameExistsInOtherType<Gauge>(const std::string& name) const {
return FamilyNameExists(name, counters_, histograms_, summaries_);
return FamilyNameExists(name, counters_, histograms_, infos_, summaries_);
}

template <>
bool Registry::NameExistsInOtherType<Histogram>(const std::string& name) const {
return FamilyNameExists(name, counters_, gauges_, summaries_);
return FamilyNameExists(name, counters_, gauges_, infos_, summaries_);
}

template <>
bool Registry::NameExistsInOtherType<Info>(const std::string& name) const {
return FamilyNameExists(name, counters_, gauges_, histograms_, summaries_);
}

template <>
bool Registry::NameExistsInOtherType<Summary>(const std::string& name) const {
return FamilyNameExists(name, counters_, gauges_, histograms_);
return FamilyNameExists(name, counters_, gauges_, histograms_, infos_);
}

template <typename T>
Expand Down Expand Up @@ -143,6 +155,10 @@ template Family<Gauge>& Registry::Add(const std::string& name,
const std::string& help,
const Labels& labels);

template Family<Info>& Registry::Add(const std::string& name,
const std::string& help,
const Labels& labels);

template Family<Summary>& Registry::Add(const std::string& name,
const std::string& help,
const Labels& labels);
Expand Down Expand Up @@ -181,4 +197,7 @@ Registry::Remove(const Family<Summary>& family);
template bool PROMETHEUS_CPP_CORE_EXPORT
Registry::Remove(const Family<Histogram>& family);

template bool PROMETHEUS_CPP_CORE_EXPORT
Registry::Remove(const Family<Info>& family);

} // namespace prometheus
15 changes: 15 additions & 0 deletions core/src/text_serializer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ void SerializeGauge(std::ostream& out, const MetricFamily& family,
WriteTail(out, metric);
}

void SerializeInfo(std::ostream& out, const MetricFamily& family,
const ClientMetric& metric) {
WriteHead(out, family, metric, "_info");
WriteValue(out, metric.info.value);
WriteTail(out, metric);
}

void SerializeSummary(std::ostream& out, const MetricFamily& family,
const ClientMetric& metric) {
auto& sum = metric.summary;
Expand Down Expand Up @@ -164,6 +171,14 @@ void SerializeFamily(std::ostream& out, const MetricFamily& family) {
SerializeGauge(out, family, metric);
}
break;
// info is not handled by prometheus, we use gauge as workaround
// (https://github.com/OpenObservability/OpenMetrics/blob/98ae26c87b1c3bcf937909a880b32c8be643cc9b/specification/OpenMetrics.md#info-1)
case MetricType::Info:
out << "# TYPE " << family.name << " gauge\n";
for (auto& metric : family.metric) {
SerializeInfo(out, family, metric);
}
break;
case MetricType::Summary:
out << "# TYPE " << family.name << " summary\n";
for (auto& metric : family.metric) {
Expand Down
9 changes: 9 additions & 0 deletions core/tests/builder_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "prometheus/family.h"
#include "prometheus/gauge.h"
#include "prometheus/histogram.h"
#include "prometheus/info.h"
#include "prometheus/labels.h"
#include "prometheus/registry.h"
#include "prometheus/summary.h"
Expand Down Expand Up @@ -92,6 +93,14 @@ TEST_F(BuilderTest, build_histogram) {
verifyCollectedLabels();
}

TEST_F(BuilderTest, build_info) {
auto& family =
BuildInfo().Name(name).Help(help).Labels(const_labels).Register(registry);
family.Add(more_labels);

verifyCollectedLabels();
}

TEST_F(BuilderTest, build_summary) {
auto& family = BuildSummary()
.Name(name)
Expand Down
2 changes: 1 addition & 1 deletion core/tests/check_label_name_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ TEST_P(CheckLabelNameTest, reject_quantile_for_histogram) {
INSTANTIATE_TEST_SUITE_P(AllMetricTypes, CheckLabelNameTest,
testing::Values(MetricType::Counter, MetricType::Gauge,
MetricType::Histogram,
MetricType::Summary,
MetricType::Info, MetricType::Summary,
MetricType::Untyped));

} // namespace
Expand Down
Loading

0 comments on commit 2ce3bb0

Please sign in to comment.