]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/opentelemetry-cpp/exporters/prometheus/src/prometheus_exporter.cc
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / jaegertracing / opentelemetry-cpp / exporters / prometheus / src / prometheus_exporter.cc
1 /*
2 * Copyright The OpenTelemetry Authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifdef ENABLE_METRICS_PREVIEW
18 # include "opentelemetry/exporters/prometheus/prometheus_exporter.h"
19
20 OPENTELEMETRY_BEGIN_NAMESPACE
21
22 namespace exporter
23 {
24 namespace prometheus
25 {
26 /**
27 * Constructor - binds an exposer and collector to the exporter
28 * @param address: an address for an exposer that exposes
29 * an HTTP endpoint for the exporter to connect to
30 */
31 PrometheusExporter::PrometheusExporter(std::string &address) : is_shutdown_(false)
32 {
33 exposer_ = std::unique_ptr<::prometheus::Exposer>(new ::prometheus::Exposer{address});
34 collector_ = std::shared_ptr<PrometheusCollector>(new PrometheusCollector);
35
36 exposer_->RegisterCollectable(collector_);
37 }
38
39 /**
40 * PrometheusExporter constructor with no parameters
41 * Used for testing only
42 */
43 PrometheusExporter::PrometheusExporter() : is_shutdown_(false)
44 {
45 collector_ = std::unique_ptr<PrometheusCollector>(new PrometheusCollector);
46 }
47
48 /**
49 * Exports a batch of Metric Records.
50 * @param records: a collection of records to export
51 * @return: returns a ReturnCode detailing a success, or type of failure
52 */
53 sdk::common::ExportResult PrometheusExporter::Export(
54 const std::vector<sdk::metrics::Record> &records) noexcept
55 {
56 if (is_shutdown_)
57 {
58 return sdk::common::ExportResult::kFailure;
59 }
60 else if (records.empty())
61 {
62 return sdk::common::ExportResult::kFailureInvalidArgument;
63 }
64 else if (collector_->GetCollection().size() + records.size() >
65 (size_t)collector_->GetMaxCollectionSize())
66 {
67 return sdk::common::ExportResult::kFailureFull;
68 }
69 else
70 {
71 collector_->AddMetricData(records);
72 return sdk::common::ExportResult::kSuccess;
73 }
74 }
75
76 /**
77 * Shuts down the exporter and does cleanup.
78 * Since Prometheus is a pull based interface,
79 * we cannot serve data remaining in the intermediate
80 * collection to to client an HTTP request being sent,
81 * so we flush the data.
82 */
83 void PrometheusExporter::Shutdown() noexcept
84 {
85 is_shutdown_ = true;
86
87 collector_->GetCollection().clear();
88 }
89
90 /**
91 * @return: returns a shared_ptr to
92 * the PrometheusCollector instance
93 */
94 std::shared_ptr<PrometheusCollector> &PrometheusExporter::GetCollector()
95 {
96 return collector_;
97 }
98
99 /**
100 * @return: Gets the shutdown status of the exporter
101 */
102 bool PrometheusExporter::IsShutdown() const
103 {
104 return is_shutdown_;
105 }
106
107 } // namespace prometheus
108 } // namespace exporter
109 OPENTELEMETRY_END_NAMESPACE
110 #endif // ENABLE_METRICS_PREVIEW