Prometheus Exporter with boost::beast Webserver #2554
-
I'm adding a bunch of metrics to an application that uses Has anyone tried something similar? What changes would need to happen for this? I guess I'd still need a
I could then attach this function to a particular boost::beast view. Does this sound reasonable? Am I missing something obvious? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
One thing I missed in my original post is the link between an Exporter / MetricReader & a MeterProvider. So I still need a dummy exporter/reader that has a collector and can be passed into Not sure yet exactly how that interaction will look like. |
Beta Was this translation helpful? Give feedback.
-
This is what I finally ended up with: #pragma once
#include <memory>
#include <mutex>
#include <sstream>
#include <string>
#include <vector>
#include <opentelemetry/exporters/prometheus/exporter_utils.h>
#include <opentelemetry/sdk/metrics/metric_reader.h>
#include <prometheus/collectable.h>
#include <prometheus/text_serializer.h>
class PrometheusExporter : public opentelemetry::sdk::metrics::MetricReader
{
public:
opentelemetry::sdk::metrics::AggregationTemporality GetAggregationTemporality(
opentelemetry::sdk::metrics::InstrumentType /*instrument_type*/) const noexcept override
{
return opentelemetry::sdk::metrics::AggregationTemporality::kCumulative;
};
std::vector<prometheus::MetricFamily> CollectPrometheus()
{
if (IsShutdown())
{
return {};
}
collection_lock_.lock();
std::vector<prometheus::MetricFamily> result;
auto status = Collect([&result](opentelemetry::sdk::metrics::ResourceMetrics& metric_data) {
auto prometheus_metric_data
= opentelemetry::exporter::metrics::PrometheusExporterUtils::TranslateToPrometheus(
metric_data, true, true);
for (auto& data : prometheus_metric_data)
result.emplace_back(data);
return true;
});
collection_lock_.unlock();
return result;
}
std::string serialize()
{
// TODO: Do we need to allocate this stream here?
// What if we took the boost stream itself as an arg & wrote to it?
std::ostringstream output;
prometheus::TextSerializer().Serialize(output, CollectPrometheus());
return output.str();
};
private:
mutable std::mutex collection_lock_;
bool OnForceFlush(std::chrono::microseconds /*timeout*/) noexcept override { return true; };
bool OnShutDown(std::chrono::microseconds /*timeout*/) noexcept override { return true; };
}; |
Beta Was this translation helpful? Give feedback.
-
I have a similar use case where, i don't need an endpoint exposed through the prometheus exporter since I have my own http server. But on this endpoint i do want metrics in prometheus output format. |
Beta Was this translation helpful? Give feedback.
This is what I finally ended up with: