]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/opentelemetry-cpp/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter.h
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / jaegertracing / opentelemetry-cpp / exporters / prometheus / include / opentelemetry / exporters / prometheus / exporter.h
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 #pragma once
5 #ifndef ENABLE_METRICS_PREVIEW
6 # include <memory>
7 # include <string>
8 # include <vector>
9
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"
17
18 /**
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.
22 */
23
24 OPENTELEMETRY_BEGIN_NAMESPACE
25
26 namespace exporter
27 {
28 namespace metrics
29 {
30
31 inline const std::string GetPrometheusDefaultHttpEndpoint()
32 {
33 constexpr char kPrometheusEndpointEnv[] = "PROMETHEUS_EXPORTER_ENDPOINT";
34 constexpr char kPrometheusEndpointDefault[] = "localhost:9464";
35
36 auto endpoint = opentelemetry::sdk::common::GetEnvironmentVariable(kPrometheusEndpointEnv);
37 return endpoint.size() ? endpoint : kPrometheusEndpointDefault;
38 }
39
40 /**
41 * Struct to hold Prometheus exporter options.
42 */
43 struct PrometheusExporterOptions
44 {
45 // The endpoint the Prometheus backend can collect metrics from
46 std::string url = GetPrometheusDefaultHttpEndpoint();
47 };
48
49 class PrometheusExporter : public sdk::metrics::MetricExporter
50 {
51 public:
52 /**
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
56 */
57 PrometheusExporter(const PrometheusExporterOptions &options);
58
59 /**
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
63 */
64 sdk::common::ExportResult Export(const sdk::metrics::ResourceMetrics &data) noexcept override;
65
66 /**
67 * Force flush the exporter.
68 */
69 bool ForceFlush(
70 std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept override;
71
72 /**
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.
78 */
79 bool Shutdown(std::chrono::microseconds timeout = std::chrono::microseconds(0)) noexcept override;
80
81 /**
82 * @return: returns a shared_ptr to
83 * the PrometheusCollector instance
84 */
85 std::shared_ptr<PrometheusCollector> &GetCollector();
86
87 /**
88 * @return: Gets the shutdown status of the exporter
89 */
90 bool IsShutdown() const;
91
92 private:
93 // The configuration options associated with this exporter.
94 const PrometheusExporterOptions options_;
95 /**
96 * exporter shutdown status
97 */
98 bool is_shutdown_;
99
100 /**
101 * Pointer to a
102 * PrometheusCollector instance
103 */
104 std::shared_ptr<PrometheusCollector> collector_;
105
106 /**
107 * Pointer to an
108 * Exposer instance
109 */
110 std::unique_ptr<::prometheus::Exposer> exposer_;
111
112 /**
113 * friend class for testing
114 */
115 friend class PrometheusExporterTest;
116
117 /**
118 * PrometheusExporter constructor with no parameters
119 * Used for testing only
120 */
121 PrometheusExporter();
122 };
123 } // namespace metrics
124 } // namespace exporter
125 OPENTELEMETRY_END_NAMESPACE
126 #endif // ENABLE_METRICS_PREVIEW