family_factory.cc
Go to the documentation of this file.
1 /*
2  * Copyright 2018 The Cartographer Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
20 #include "prometheus/counter.h"
21 #include "prometheus/family.h"
22 #include "prometheus/gauge.h"
23 #include "prometheus/histogram.h"
24 
25 namespace cartographer {
26 namespace cloud {
27 namespace metrics {
28 namespace prometheus {
29 namespace {
30 
32 
33 class Counter : public ::cartographer::metrics::Counter {
34  public:
35  explicit Counter(::prometheus::Counter* prometheus)
36  : prometheus_(prometheus) {}
37 
38  void Increment() override { prometheus_->Increment(); }
39  void Increment(double by_value) override { prometheus_->Increment(by_value); }
40 
41  private:
42  ::prometheus::Counter* prometheus_;
43 };
44 
45 class CounterFamily
46  : public ::cartographer::metrics::Family<::cartographer::metrics::Counter> {
47  public:
48  explicit CounterFamily(
49  ::prometheus::Family<::prometheus::Counter>* prometheus)
50  : prometheus_(prometheus) {}
51 
52  Counter* Add(const std::map<std::string, std::string>& labels) override {
53  ::prometheus::Counter* counter = &prometheus_->Add(labels);
54  auto wrapper = common::make_unique<Counter>(counter);
55  auto* ptr = wrapper.get();
56  wrappers_.emplace_back(std::move(wrapper));
57  return ptr;
58  }
59 
60  private:
61  ::prometheus::Family<::prometheus::Counter>* prometheus_;
62  std::vector<std::unique_ptr<Counter>> wrappers_;
63 };
64 
65 class Gauge : public ::cartographer::metrics::Gauge {
66  public:
67  explicit Gauge(::prometheus::Gauge* prometheus) : prometheus_(prometheus) {}
68 
69  void Decrement() override { prometheus_->Decrement(); }
70  void Decrement(double by_value) override { prometheus_->Decrement(by_value); }
71  void Increment() override { prometheus_->Increment(); }
72  void Increment(double by_value) override { prometheus_->Increment(by_value); }
73  void Set(double value) override { prometheus_->Set(value); }
74 
75  private:
76  ::prometheus::Gauge* prometheus_;
77 };
78 
79 class GaugeFamily
80  : public ::cartographer::metrics::Family<::cartographer::metrics::Gauge> {
81  public:
82  explicit GaugeFamily(::prometheus::Family<::prometheus::Gauge>* prometheus)
83  : prometheus_(prometheus) {}
84 
85  Gauge* Add(const std::map<std::string, std::string>& labels) override {
86  ::prometheus::Gauge* gauge = &prometheus_->Add(labels);
87  auto wrapper = common::make_unique<Gauge>(gauge);
88  auto* ptr = wrapper.get();
89  wrappers_.emplace_back(std::move(wrapper));
90  return ptr;
91  }
92 
93  private:
94  ::prometheus::Family<::prometheus::Gauge>* prometheus_;
95  std::vector<std::unique_ptr<Gauge>> wrappers_;
96 };
97 
98 class Histogram : public ::cartographer::metrics::Histogram {
99  public:
100  explicit Histogram(::prometheus::Histogram* prometheus)
101  : prometheus_(prometheus) {}
102 
103  void Observe(double value) override { prometheus_->Observe(value); }
104 
105  private:
106  ::prometheus::Histogram* prometheus_;
107 };
108 
109 class HistogramFamily : public ::cartographer::metrics::Family<
110  ::cartographer::metrics::Histogram> {
111  public:
112  HistogramFamily(::prometheus::Family<::prometheus::Histogram>* prometheus,
113  const BucketBoundaries& boundaries)
114  : prometheus_(prometheus), boundaries_(boundaries) {}
115 
116  Histogram* Add(const std::map<std::string, std::string>& labels) override {
117  ::prometheus::Histogram* histogram = &prometheus_->Add(labels, boundaries_);
118  auto wrapper = common::make_unique<Histogram>(histogram);
119  auto* ptr = wrapper.get();
120  wrappers_.emplace_back(std::move(wrapper));
121  return ptr;
122  }
123 
124  private:
125  ::prometheus::Family<::prometheus::Histogram>* prometheus_;
126  std::vector<std::unique_ptr<Histogram>> wrappers_;
127  const BucketBoundaries boundaries_;
128 };
129 
130 } // namespace
131 
133  : registry_(std::make_shared<::prometheus::Registry>()) {}
134 
136 FamilyFactory::NewCounterFamily(const std::string& name,
137  const std::string& description) {
138  auto& family = ::prometheus::BuildCounter()
139  .Name(name)
140  .Help(description)
141  .Register(*registry_);
142  auto wrapper = common::make_unique<CounterFamily>(&family);
143  auto* ptr = wrapper.get();
144  counters_.emplace_back(std::move(wrapper));
145  return ptr;
146 }
147 
149 FamilyFactory::NewGaugeFamily(const std::string& name,
150  const std::string& description) {
151  auto& family = ::prometheus::BuildGauge()
152  .Name(name)
153  .Help(description)
154  .Register(*registry_);
155  auto wrapper = common::make_unique<GaugeFamily>(&family);
156  auto* ptr = wrapper.get();
157  gauges_.emplace_back(std::move(wrapper));
158  return ptr;
159 }
160 
162 FamilyFactory::NewHistogramFamily(const std::string& name,
163  const std::string& description,
164  const BucketBoundaries& boundaries) {
165  auto& family = ::prometheus::BuildHistogram()
166  .Name(name)
167  .Help(description)
168  .Register(*registry_);
169  auto wrapper = common::make_unique<HistogramFamily>(&family, boundaries);
170  auto* ptr = wrapper.get();
171  histograms_.emplace_back(std::move(wrapper));
172  return ptr;
173 }
174 
175 std::weak_ptr<::prometheus::Collectable> FamilyFactory::GetCollectable() const {
176  return registry_;
177 }
178 
179 } // namespace prometheus
180 } // namespace metrics
181 } // namespace cloud
182 } // namespace cartographer
std::weak_ptr<::prometheus::Collectable > GetCollectable() const
::cartographer::metrics::Family<::cartographer::metrics::Gauge > * NewGaugeFamily(const std::string &name, const std::string &description) override
const BucketBoundaries boundaries_
std::vector< std::unique_ptr< ::cartographer::metrics::Family<::cartographer::metrics::Counter > > > counters_
std::vector< double > BucketBoundaries
::cartographer::metrics::Family<::cartographer::metrics::Histogram > * NewHistogramFamily(const std::string &name, const std::string &description, const ::cartographer::metrics::Histogram::BucketBoundaries &boundaries) override
std::vector< std::unique_ptr< ::cartographer::metrics::Family<::cartographer::metrics::Histogram > > > histograms_
::cartographer::metrics::Family<::cartographer::metrics::Counter > * NewCounterFamily(const std::string &name, const std::string &description) override
std::vector< std::unique_ptr< ::cartographer::metrics::Family<::cartographer::metrics::Gauge > > > gauges_
::prometheus::Counter * prometheus_
std::vector< std::unique_ptr< Counter > > wrappers_


cartographer
Author(s): The Cartographer Authors
autogenerated on Mon Feb 28 2022 22:00:58