open-telemetry / opentelemetry-cpp

The OpenTelemetry C++ Client
https://opentelemetry.io/
Apache License 2.0
890 stars 423 forks source link

Provide a CMake prometheus_exporter_utils library target #2565

Open dufferzafar opened 8 months ago

dufferzafar commented 8 months ago

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; };
};
github-actions[bot] commented 6 months ago

This issue was marked as stale due to lack of activity.