Prometheus Client Library for Modern C++
gateway.h
1 #pragma once
2 
3 #include <future>
4 #include <memory>
5 #include <mutex>
6 #include <string>
7 #include <utility>
8 #include <vector>
9 
10 #include "prometheus/collectable.h"
11 #include "prometheus/detail/http_method.h"
12 #include "prometheus/detail/push_export.h"
13 #include "prometheus/labels.h"
14 
15 namespace prometheus {
16 
17 namespace detail {
18 class CurlWrapper;
19 }
20 
21 class PROMETHEUS_CPP_PUSH_EXPORT Gateway {
22  public:
23  Gateway(const std::string& host, const std::string& port,
24  const std::string& jobname, const Labels& labels = {},
25  const std::string& username = {}, const std::string& password = {});
26 
27  Gateway(const Gateway&) = delete;
28  Gateway(Gateway&&) = delete;
29  Gateway& operator=(const Gateway&) = delete;
30  Gateway& operator=(Gateway&&) = delete;
31 
32  ~Gateway();
33 
34  void RegisterCollectable(const std::weak_ptr<Collectable>& collectable,
35  const Labels* labels = nullptr);
36 
37  static Labels GetInstanceLabel(std::string hostname);
38 
39  // Push metrics to the given pushgateway.
40  int Push();
41 
42  std::future<int> AsyncPush();
43 
44  // PushAdd metrics to the given pushgateway.
45  int PushAdd();
46 
47  std::future<int> AsyncPushAdd();
48 
49  // Delete metrics from the given pushgateway.
50  int Delete();
51 
52  // Delete metrics from the given pushgateway.
53  std::future<int> AsyncDelete();
54 
55  // Delete metrics from the given pushgateway (for configured instance labels).
56  int DeleteForInstance();
57 
58  // Delete metrics from the given pushgateway (for configured instance labels).
59  std::future<int> AsyncDeleteForInstance();
60 
61  private:
62  std::string jobUri_;
63  std::string labels_;
64  std::unique_ptr<detail::CurlWrapper> curlWrapper_;
65  std::mutex mutex_;
66 
67  using CollectableEntry = std::pair<std::weak_ptr<Collectable>, std::string>;
68  std::vector<CollectableEntry> collectables_;
69 
70  std::string getUri(const CollectableEntry& collectable) const;
71 
72  int push(detail::HttpMethod method);
73 
74  std::future<int> async_push(detail::HttpMethod method);
75 
76  static void CleanupStalePointers(std::vector<CollectableEntry>& collectables);
77 };
78 
79 } // namespace prometheus
Definition: gateway.h:21