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 10 of file PrometheusServer.cpp.

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}
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 42 of file PrometheusServer.cpp.

43{
44 uint64_t regId = 0;
45 return createNewRegistry(regId);
46}
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 48 of file PrometheusServer.cpp.

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}
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 62 of file PrometheusServer.cpp.

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}

◆ 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 27 of file PrometheusServer.cpp.

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}

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: