Repo-Init
 
Loading...
Searching...
No Matches
PrometheusServer Class Reference

#include <PrometheusServer.hpp>

Public Member Functions

 PrometheusServer (const std::string &serverAddr)
 
std::shared_ptr< prometheus::Registry > getRegistry (uint64_t regId)
 
std::shared_ptr< prometheus::Registry > createNewRegistry ()
 
std::shared_ptr< prometheus::Registry > createNewRegistry (uint64_t &regId)
 
bool deleteRegistry (uint64_t regId)
 

Private Attributes

std::mutex _guardLock
 Mutex for concurrent add tracker calls.
 
std::unique_ptr< prometheus::Exposer > _mainExposer
 Main HTTP Server.
 
std::vector< std::pair< uint64_t, std::shared_ptr< prometheus::Registry > > > _vRegister
 All tracker registries.
 
prometheus::Family< prometheus::Info > * _infoFamily
 General application information.
 

Detailed Description

Class representing a Prometheus server for collecting and exposing metrics.

Definition at line 10 of file PrometheusServer.hpp.

Constructor & Destructor Documentation

◆ PrometheusServer()

PrometheusServer::PrometheusServer ( const std::string & serverAddr)
explicit

Construct a new Prometheus Server

Parameters
[in]serverAddrServer address in <IP>:<Port> format

Definition at line 12 of file PrometheusServer.cpp.

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}
std::vector< std::pair< uint64_t, std::shared_ptr< prometheus::Registry > > > _vRegister
All tracker registries.
std::unique_ptr< prometheus::Exposer > _mainExposer
Main HTTP Server.
prometheus::Family< prometheus::Info > * _infoFamily
General application information.

Member Function Documentation

◆ createNewRegistry() [1/2]

std::shared_ptr< prometheus::Registry > PrometheusServer::createNewRegistry ( )

Create a registry for prometheus

Returns
std::shared_ptr<prometheus::Registry> Registry pointer

Definition at line 45 of file PrometheusServer.cpp.

46{
47 uint64_t regId = 0;
48 return createNewRegistry(regId);
49}
std::shared_ptr< prometheus::Registry > createNewRegistry()
Here is the call graph for this function:
Here is the caller graph for this function:

◆ createNewRegistry() [2/2]

std::shared_ptr< prometheus::Registry > PrometheusServer::createNewRegistry ( uint64_t & regId)

Create a registry for prometheus and returns id of the registry

Parameters
[out]regIdRegistry id
Returns
std::shared_ptr<prometheus::Registry> Registry pointer

Definition at line 51 of file PrometheusServer.cpp.

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}
std::mutex _guardLock
Mutex for concurrent add tracker calls.

◆ deleteRegistry()

bool PrometheusServer::deleteRegistry ( uint64_t regId)

Deletes the registry with id

Parameters
[in]regIdRegistry id
Returns
true If successfully removed
false otherwise

Definition at line 65 of file PrometheusServer.cpp.

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}

◆ getRegistry()

std::shared_ptr< prometheus::Registry > PrometheusServer::getRegistry ( uint64_t regId)

Get the registry with id

Parameters
[in]regIdRegistry id
Returns
std::shared_ptr<prometheus::Registry> Registry pointer if found, nullptr otherwise

Definition at line 29 of file PrometheusServer.cpp.

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}

Member Data Documentation

◆ _guardLock

std::mutex PrometheusServer::_guardLock
private

Mutex for concurrent add tracker calls.

Definition at line 13 of file PrometheusServer.hpp.

◆ _infoFamily

prometheus::Family<prometheus::Info>* PrometheusServer::_infoFamily
private

General application information.

Definition at line 20 of file PrometheusServer.hpp.

◆ _mainExposer

std::unique_ptr<prometheus::Exposer> PrometheusServer::_mainExposer
private

Main HTTP Server.

Definition at line 15 of file PrometheusServer.hpp.

◆ _vRegister

std::vector<std::pair<uint64_t, std::shared_ptr<prometheus::Registry> > > PrometheusServer::_vRegister
private

All tracker registries.

Definition at line 17 of file PrometheusServer.hpp.


The documentation for this class was generated from the following files: