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