Repo-Init
 
Loading...
Searching...
No Matches
PrometheusServer.cpp
Go to the documentation of this file.
2
3#include "Version.h"
4
5#include <algorithm>
6
7#include <date/date.h>
8#include <prometheus/info.h>
9
10#include <limits>
11
12PrometheusServer::PrometheusServer(const std::string &serverAddr)
13 : _mainExposer(std::make_unique<prometheus::Exposer>(serverAddr, 1))
14{
15 auto reg = std::make_shared<prometheus::Registry>();
16
17 // Basic information
19 &prometheus::BuildInfo().Name(PROJECT_NAME).Help(std::string(PROJECT_NAME) + " information").Register(*reg);
20
21 _infoFamily->Add({{"init_time", date::format("%FT%TZ", date::floor<std::chrono::nanoseconds>(
22 std::chrono::high_resolution_clock::now()))}});
23 _infoFamily->Add({{"version", PROJECT_FULL_REVISION}});
24
25 _vRegister.emplace_back(std::numeric_limits<uint64_t>::max(), reg);
26 _mainExposer->RegisterCollectable(reg);
27}
28
29std::shared_ptr<prometheus::Registry> PrometheusServer::getRegistry(uint64_t regId)
30{
31 const std::scoped_lock guard(_guardLock);
32
33 if (auto iter =
34 std::ranges::find_if(_vRegister,
35 [regId](const std::pair<uint64_t, std::shared_ptr<prometheus::Registry>> &val) {
36 return regId == val.first;
37 });
38 iter != _vRegister.end())
39 {
40 return iter->second;
41 }
42 return nullptr;
43}
44
45std::shared_ptr<prometheus::Registry> PrometheusServer::createNewRegistry()
46{
47 uint64_t regId = 0;
48 return createNewRegistry(regId);
49}
50
51std::shared_ptr<prometheus::Registry> PrometheusServer::createNewRegistry(uint64_t &regId)
52{
53 const std::scoped_lock guard(_guardLock);
54
55 // Create registry
56 auto reg = std::make_shared<prometheus::Registry>();
57 _mainExposer->RegisterCollectable(reg);
58
59 // Push to vector (At least information registry always exist so back is valid)
60 regId = _vRegister.back().first + 1;
61 _vRegister.emplace_back(_vRegister.back().first + 1, reg);
62 return reg;
63}
64
66{
67 if (regId == std::numeric_limits<uint64_t>::max())
68 {
69 return false;
70 }
71
72 const std::scoped_lock guard(_guardLock);
73 std::erase_if(_vRegister, [regId](const std::pair<uint64_t, std::shared_ptr<prometheus::Registry>> &val) {
74 return regId == val.first;
75 });
76
77 return true;
78}
std::shared_ptr< prometheus::Registry > createNewRegistry()
std::vector< std::pair< uint64_t, std::shared_ptr< prometheus::Registry > > > _vRegister
All tracker registries.
bool deleteRegistry(uint64_t regId)
std::shared_ptr< prometheus::Registry > getRegistry(uint64_t regId)
std::unique_ptr< prometheus::Exposer > _mainExposer
Main HTTP Server.
std::mutex _guardLock
Mutex for concurrent add tracker calls.
PrometheusServer(const std::string &serverAddr)
prometheus::Family< prometheus::Info > * _infoFamily
General application information.