1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
5 #ifndef ENABLE_METRICS_PREVIEW
10 # include <prometheus/exposer.h>
11 # include "opentelemetry/common/spin_lock_mutex.h"
12 # include "opentelemetry/exporters/prometheus/collector.h"
13 # include "opentelemetry/nostd/span.h"
14 # include "opentelemetry/sdk/common/env_variables.h"
15 # include "opentelemetry/sdk/metrics/metric_exporter.h"
16 # include "opentelemetry/version.h"
19 * This class is an implementation of the MetricsExporter interface and
20 * exports Prometheus metrics data. Functions in this class should be
21 * called by the Controller in our data pipeline.
24 OPENTELEMETRY_BEGIN_NAMESPACE
31 inline const std::string
GetPrometheusDefaultHttpEndpoint()
33 constexpr char kPrometheusEndpointEnv
[] = "PROMETHEUS_EXPORTER_ENDPOINT";
34 constexpr char kPrometheusEndpointDefault
[] = "localhost:9464";
36 auto endpoint
= opentelemetry::sdk::common::GetEnvironmentVariable(kPrometheusEndpointEnv
);
37 return endpoint
.size() ? endpoint
: kPrometheusEndpointDefault
;
41 * Struct to hold Prometheus exporter options.
43 struct PrometheusExporterOptions
45 // The endpoint the Prometheus backend can collect metrics from
46 std::string url
= GetPrometheusDefaultHttpEndpoint();
49 class PrometheusExporter
: public sdk::metrics::MetricExporter
53 * Constructor - binds an exposer and collector to the exporter
54 * @param options: options for an exposer that exposes
55 * an HTTP endpoint for the exporter to connect to
57 PrometheusExporter(const PrometheusExporterOptions
&options
);
60 * Exports a batch of Metric Records.
61 * @param records: a collection of records to export
62 * @return: returns a ReturnCode detailing a success, or type of failure
64 sdk::common::ExportResult
Export(const sdk::metrics::ResourceMetrics
&data
) noexcept override
;
67 * Force flush the exporter.
70 std::chrono::microseconds timeout
= (std::chrono::microseconds::max
)()) noexcept override
;
73 * Shuts down the exporter and does cleanup.
74 * Since Prometheus is a pull based interface,
75 * we cannot serve data remaining in the intermediate
76 * collection to to client an HTTP request being sent,
77 * so we flush the data.
79 bool Shutdown(std::chrono::microseconds timeout
= std::chrono::microseconds(0)) noexcept override
;
82 * @return: returns a shared_ptr to
83 * the PrometheusCollector instance
85 std::shared_ptr
<PrometheusCollector
> &GetCollector();
88 * @return: Gets the shutdown status of the exporter
90 bool IsShutdown() const;
93 // The configuration options associated with this exporter.
94 const PrometheusExporterOptions options_
;
96 * exporter shutdown status
102 * PrometheusCollector instance
104 std::shared_ptr
<PrometheusCollector
> collector_
;
110 std::unique_ptr
<::prometheus::Exposer
> exposer_
;
113 * friend class for testing
115 friend class PrometheusExporterTest
;
118 * PrometheusExporter constructor with no parameters
119 * Used for testing only
121 PrometheusExporter();
123 } // namespace metrics
124 } // namespace exporter
125 OPENTELEMETRY_END_NAMESPACE
126 #endif // ENABLE_METRICS_PREVIEW