]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/opentelemetry-cpp/third_party/prometheus-cpp/pull/src/exposer.cc
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / jaegertracing / opentelemetry-cpp / third_party / prometheus-cpp / pull / src / exposer.cc
1 #include "prometheus/exposer.h"
2
3 #include <algorithm>
4 #include <iterator>
5 #include <string>
6 #include <utility>
7
8 #include "CivetServer.h"
9 #include "endpoint.h"
10 #include "prometheus/detail/future_std.h"
11
12 namespace prometheus {
13
14 Exposer::Exposer(const std::string& bind_address, const std::size_t num_threads,
15 const CivetCallbacks* callbacks)
16 : Exposer(
17 std::vector<std::string>{"listening_ports", bind_address,
18 "num_threads", std::to_string(num_threads)},
19 callbacks) {}
20
21 Exposer::Exposer(std::vector<std::string> options,
22 const CivetCallbacks* callbacks)
23 : server_(detail::make_unique<CivetServer>(std::move(options), callbacks)) {
24 }
25
26 Exposer::~Exposer() = default;
27
28 void Exposer::RegisterCollectable(const std::weak_ptr<Collectable>& collectable,
29 const std::string& uri) {
30 std::lock_guard<std::mutex> lock{mutex_};
31 auto& endpoint = GetEndpointForUri(uri);
32 endpoint.RegisterCollectable(collectable);
33 }
34
35 void Exposer::RegisterAuth(
36 std::function<bool(const std::string&, const std::string&)> authCB,
37 const std::string& realm, const std::string& uri) {
38 std::lock_guard<std::mutex> lock{mutex_};
39 auto& endpoint = GetEndpointForUri(uri);
40 endpoint.RegisterAuth(std::move(authCB), realm);
41 }
42
43 void Exposer::RemoveCollectable(const std::weak_ptr<Collectable>& collectable,
44 const std::string& uri) {
45 std::lock_guard<std::mutex> lock{mutex_};
46 auto& endpoint = GetEndpointForUri(uri);
47 endpoint.RemoveCollectable(collectable);
48 }
49
50 std::vector<int> Exposer::GetListeningPorts() const {
51 return server_->getListeningPorts();
52 }
53
54 detail::Endpoint& Exposer::GetEndpointForUri(const std::string& uri) {
55 auto sameUri = [uri](const std::unique_ptr<detail::Endpoint>& endpoint) {
56 return endpoint->GetURI() == uri;
57 };
58 auto it = std::find_if(std::begin(endpoints_), std::end(endpoints_), sameUri);
59 if (it != std::end(endpoints_)) {
60 return *it->get();
61 }
62
63 endpoints_.emplace_back(detail::make_unique<detail::Endpoint>(*server_, uri));
64 return *endpoints_.back().get();
65 }
66
67 } // namespace prometheus