open-telemetry / open-telemetry/opentelemetry-cpp
Provide a CMake prometheus_exporter_utils library target
@lalitb is already working on this.
Since Mar 6, 2024.
- Dominant language
- C++
- Stars
- 1.4k
- Forks
- 632
- Avg merge
- 1d 13h
- Merged PRs (30d)
- 75
Description
Is your feature request related to a problem?
I'm trying to use opentelemetry prometheus exporter WITHOUT the CivetWeb Webserver that is baked into prometheus-cpp.
The current target provided by this package is prometheus_exporter which links against prometheus-cpp::pull & prometheus-cpp::core
The pull dependency brings in the civetweb server in as well. Since our application already has a webserver (boost::beast) we'd like to avoid this.
Describe the solution you'd like
If this library provided a separate target for the prometheus/exporter_utils.cc file - which would only link against prometheus-cpp::core, I'd be able to use it.
Describe alternatives you've considered
Currently, I'm using a local copy of the source and I've added this separate target for 1 cc file and it all works locally.
Additional context
Here's the code that I have for my PrometheusExporter - it differs from the one present in lib as it has no PrometheusCollector & no Exposer:
#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; };
};
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.