Files
AI-on-the-edge-device/code/components/openmetrics/openmetrics.cpp
Henry Thasler 1a76ae121c add Prometheus/OpenMetrics exporter (#3081)
* add prometheus endpoint

* refine metrics implementation

* move metrics generator to ClassFlowControll

* add more metrics
align prefix

* add more metrics
clean up

* refine documentation

* revert dependencies change

* sanitize labels

* create separate module for openmetrics

* move openmetrics to separate folder

* clean up

* add basic unit-tests

* work with const numbers
add replaceAll for string replacement
avoid opening std namespace
adapt unit-tests

* Update code/main/server_main.cpp

---------

Co-authored-by: CaCO3 <caco3@ruinelli.ch>
2024-06-02 21:13:15 +02:00

44 lines
1.6 KiB
C++

#include "openmetrics.h"
/**
* create a singe metric from the given input
**/
std::string createMetric(const std::string &metricName, const std::string &help, const std::string &type, const std::string &value)
{
return "# HELP " + metricName + " " + help + "\n" +
"# TYPE " + metricName + " " + type + "\n" +
metricName + " " + value + "\n";
}
/**
* Generate the MetricFamily from all available sequences
* @returns the string containing the text wire format of the MetricFamily
**/
std::string createSequenceMetrics(std::string prefix, const std::vector<NumberPost *> &numbers)
{
std::string res;
for (const auto &number : numbers)
{
// only valid data is reported (https://github.com/OpenObservability/OpenMetrics/blob/main/specification/OpenMetrics.md#missing-data)
if (number->ReturnValue.length() > 0)
{
auto label = number->name;
// except newline, double quote, and backslash (https://github.com/OpenObservability/OpenMetrics/blob/main/specification/OpenMetrics.md#abnf)
// to keep it simple, these characters are just removed from the label
replaceAll(label, "\\", "");
replaceAll(label, "\"", "");
replaceAll(label, "\n", "");
res += prefix + "_flow_value{sequence=\"" + label + "\"} " + number->ReturnValue + "\n";
}
}
// prepend metadata if a valid metric was created
if (res.length() > 0)
{
res = "# HELP " + prefix + "_flow_value current value of meter readout\n# TYPE " + prefix + "_flow_value gauge\n" + res;
}
return res;
}