Skip to content

Commit

Permalink
add name label for prometheus
Browse files Browse the repository at this point in the history
  • Loading branch information
marenz2569 committed May 25, 2024
1 parent 7e201d1 commit 4c1c085
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
3 changes: 2 additions & 1 deletion include/prometheus.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ class PrometheusExporter {
private:
std::shared_ptr<prometheus::Registry> registry_;
std::unique_ptr<prometheus::Exposer> exposer_;
const std::string prometheus_name_;

public:
PrometheusExporter(const std::string& host) noexcept;
PrometheusExporter(const std::string& prometheus_host, const std::string& prometheus_name) noexcept;
~PrometheusExporter() noexcept = default;

auto burst_received_count() noexcept -> prometheus::Family<prometheus::Counter>&;
Expand Down
5 changes: 4 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ auto main(int argc, char** argv) -> int {
std::optional<std::string> input_file, output_file;
std::optional<unsigned> uplink_scrambling_code;
std::optional<std::string> prometheus_address;
std::optional<std::string> prometheus_name;

std::shared_ptr<PrometheusExporter> prometheus_exporter;

Expand All @@ -42,6 +43,7 @@ auto main(int argc, char** argv) -> int {
("iq", "Receive IQ instead of bitstream", cxxopts::value<bool>()->default_value("false"))
("uplink", "<scrambling code> enable uplink parsing with predefined scrambilng code", cxxopts::value<std::optional<unsigned>>(uplink_scrambling_code))
("prometheus-address", "<prometheus-address> on which ip and port the webserver for prometheus should listen. example: 127.0.0.1:9010", cxxopts::value<std::optional<std::string>>(prometheus_address))
("prometheus-name", "<prometheus-name> the name which is included in the prometheus metrics", cxxopts::value<std::optional<std::string>>(prometheus_name))
;
// clang-format on

Expand All @@ -61,7 +63,8 @@ auto main(int argc, char** argv) -> int {
iq_or_bit_stream = result["iq"].as<bool>();

if (prometheus_address) {
prometheus_exporter = std::make_shared<PrometheusExporter>(*prometheus_address);
prometheus_exporter = std::make_shared<PrometheusExporter>(
*prometheus_address, prometheus_name.value_or("Unnamed Tetra Decoder"));
}
} catch (std::exception& e) {
std::cout << "error parsing options: " << e.what() << std::endl;
Expand Down
5 changes: 4 additions & 1 deletion src/prometheus.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "prometheus.h"

PrometheusExporter::PrometheusExporter(const std::string& prometheus_host) noexcept {
PrometheusExporter::PrometheusExporter(const std::string& prometheus_host, const std::string& prometheus_name) noexcept
: prometheus_name_(std::move(prometheus_name)) {
exposer_ = std::make_unique<prometheus::Exposer>(prometheus_host);
registry_ = std::make_shared<prometheus::Registry>();

Expand All @@ -11,12 +12,14 @@ auto PrometheusExporter::burst_received_count() noexcept -> prometheus::Family<p
return prometheus::BuildCounter()
.Name("burst_received_count")
.Help("Incrementing counter of the received bursts")
.Labels({{"name", prometheus_name_}})
.Register(*registry_);
}

auto PrometheusExporter::burst_decode_error_count() noexcept -> prometheus::Family<prometheus::Counter>& {
return prometheus::BuildCounter()
.Name("burst_decode_error_count")
.Help("Incrementing counter of the decoding errors on received bursts")
.Labels({{"name", prometheus_name_}})
.Register(*registry_);
}

0 comments on commit 4c1c085

Please sign in to comment.