2 * Copyright The OpenTelemetry Authors
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifdef ENABLE_METRICS_PREVIEW
18 # include "opentelemetry/exporters/prometheus/prometheus_exporter.h"
20 OPENTELEMETRY_BEGIN_NAMESPACE
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
31 PrometheusExporter::PrometheusExporter(std::string
&address
) : is_shutdown_(false)
33 exposer_
= std::unique_ptr
<::prometheus::Exposer
>(new ::prometheus::Exposer
{address
});
34 collector_
= std::shared_ptr
<PrometheusCollector
>(new PrometheusCollector
);
36 exposer_
->RegisterCollectable(collector_
);
40 * PrometheusExporter constructor with no parameters
41 * Used for testing only
43 PrometheusExporter::PrometheusExporter() : is_shutdown_(false)
45 collector_
= std::unique_ptr
<PrometheusCollector
>(new PrometheusCollector
);
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
53 sdk::common::ExportResult
PrometheusExporter::Export(
54 const std::vector
<sdk::metrics::Record
> &records
) noexcept
58 return sdk::common::ExportResult::kFailure
;
60 else if (records
.empty())
62 return sdk::common::ExportResult::kFailureInvalidArgument
;
64 else if (collector_
->GetCollection().size() + records
.size() >
65 (size_t)collector_
->GetMaxCollectionSize())
67 return sdk::common::ExportResult::kFailureFull
;
71 collector_
->AddMetricData(records
);
72 return sdk::common::ExportResult::kSuccess
;
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.
83 void PrometheusExporter::Shutdown() noexcept
87 collector_
->GetCollection().clear();
91 * @return: returns a shared_ptr to
92 * the PrometheusCollector instance
94 std::shared_ptr
<PrometheusCollector
> &PrometheusExporter::GetCollector()
100 * @return: Gets the shutdown status of the exporter
102 bool PrometheusExporter::IsShutdown() const
107 } // namespace prometheus
108 } // namespace exporter
109 OPENTELEMETRY_END_NAMESPACE
110 #endif // ENABLE_METRICS_PREVIEW