8 #include "prometheus/collectable.h"
9 #include "prometheus/detail/core_export.h"
10 #include "prometheus/family.h"
11 #include "prometheus/labels.h"
12 #include "prometheus/metric_family.h"
14 namespace prometheus
{
24 class Builder
; // IWYU pragma: keep
27 /// \brief Manages the collection of a number of metrics.
29 /// The Registry is responsible to expose data to a class/method/function
30 /// "bridge", which returns the metrics in a format Prometheus supports.
32 /// The key class is the Collectable. This has a method - called Collect() -
33 /// that returns zero or more metrics and their samples. The metrics are
34 /// represented by the class Family<>, which implements the Collectable
35 /// interface. A new metric is registered with BuildCounter(), BuildGauge(),
36 /// BuildHistogram() or BuildSummary().
38 /// The class is thread-safe. No concurrent call to any API of this type causes
40 class PROMETHEUS_CPP_CORE_EXPORT Registry
: public Collectable
{
42 /// \brief How to deal with repeatedly added family names for a type.
44 /// Adding a family with the same name but different types is always an error
45 /// and will lead to an exception.
46 enum class InsertBehavior
{
47 /// \brief If a family with the same name and labels already exists return
48 /// the existing one. If no family with that name exists create it.
51 /// \brief Throws if a family with the same name already exists.
55 /// \brief name Create a new registry.
57 /// \param insert_behavior How to handle families with the same name.
58 explicit Registry(InsertBehavior insert_behavior
= InsertBehavior::Merge
);
60 /// \brief Deleted copy constructor.
61 Registry(const Registry
&) = delete;
63 /// \brief Deleted copy assignment.
64 Registry
& operator=(const Registry
&) = delete;
66 /// \brief Deleted move constructor.
67 Registry(Registry
&&) = delete;
69 /// \brief Deleted move assignment.
70 Registry
& operator=(Registry
&&) = delete;
72 /// \brief name Destroys a registry.
75 /// \brief Returns a list of metrics and their samples.
77 /// Every time the Registry is scraped it calls each of the metrics Collect
80 /// \return Zero or more metrics and their samples.
81 std::vector
<MetricFamily
> Collect() const override
;
83 /// \brief Removes a metrics family from the registry.
85 /// Please note that this operation invalidates the previously
86 /// returned reference to the Family and all of their added
89 /// \tparam T One of the metric types Counter, Gauge, Histogram or Summary.
90 /// \param family The family to remove
92 /// \return True if the family was found and removed.
94 bool Remove(const Family
<T
>& family
);
98 friend class detail::Builder
;
100 template <typename T
>
101 std::vector
<std::unique_ptr
<Family
<T
>>>& GetFamilies();
103 template <typename T
>
104 bool NameExistsInOtherType(const std::string
& name
) const;
106 template <typename T
>
107 Family
<T
>& Add(const std::string
& name
, const std::string
& help
,
108 const Labels
& labels
);
110 const InsertBehavior insert_behavior_
;
111 std::vector
<std::unique_ptr
<Family
<Counter
>>> counters_
;
112 std::vector
<std::unique_ptr
<Family
<Gauge
>>> gauges_
;
113 std::vector
<std::unique_ptr
<Family
<Histogram
>>> histograms_
;
114 std::vector
<std::unique_ptr
<Family
<Summary
>>> summaries_
;
115 mutable std::mutex mutex_
;
118 } // namespace prometheus