Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a CMake prometheus_exporter_utils library target #2565

Open
dufferzafar opened this issue Feb 28, 2024 · 1 comment
Open

Provide a CMake prometheus_exporter_utils library target #2565

dufferzafar opened this issue Feb 28, 2024 · 1 comment
Assignees
Labels
Stale triage/accepted Indicates an issue or PR is ready to be actively worked on.

Comments

@dufferzafar
Copy link

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 github-actions bot added the needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. label Feb 28, 2024
dufferzafar added a commit to dufferzafar/opentelemetry-cpp that referenced this issue Feb 28, 2024
@marcalff marcalff added triage/accepted Indicates an issue or PR is ready to be actively worked on. and removed needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Mar 6, 2024
dufferzafar added a commit to dufferzafar/opentelemetry-cpp that referenced this issue Apr 23, 2024
Copy link

github-actions bot commented May 8, 2024

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

@github-actions github-actions bot added the Stale label May 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Stale triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants