]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/opentelemetry-cpp/sdk/src/metrics/metric_reader.cc
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / jaegertracing / opentelemetry-cpp / sdk / src / metrics / metric_reader.cc
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 #ifndef ENABLE_METRICS_PREVIEW
5 # include "opentelemetry/sdk/metrics/metric_reader.h"
6 # include "opentelemetry/sdk/metrics/export/metric_producer.h"
7
8 # include <mutex>
9
10 OPENTELEMETRY_BEGIN_NAMESPACE
11 namespace sdk
12 {
13 namespace metrics
14 {
15
16 MetricReader::MetricReader(AggregationTemporality aggregation_temporality)
17 : aggregation_temporality_(aggregation_temporality), shutdown_(false), metric_producer_(nullptr)
18 {}
19
20 void MetricReader::SetMetricProducer(MetricProducer *metric_producer)
21 {
22 metric_producer_ = metric_producer;
23 OnInitialized();
24 }
25
26 AggregationTemporality MetricReader::GetAggregationTemporality() const noexcept
27 {
28 return aggregation_temporality_;
29 }
30
31 bool MetricReader::Collect(
32 nostd::function_ref<bool(ResourceMetrics &metric_data)> callback) noexcept
33 {
34 if (!metric_producer_)
35 {
36 OTEL_INTERNAL_LOG_WARN(
37 "MetricReader::Collect Cannot invoke Collect(). No MetricProducer registered for "
38 "collection!")
39 }
40 if (IsShutdown())
41 {
42 OTEL_INTERNAL_LOG_WARN("MetricReader::Collect Cannot invoke Collect(). Shutdown in progress!");
43 }
44
45 return metric_producer_->Collect(callback);
46 }
47
48 bool MetricReader::Shutdown(std::chrono::microseconds timeout) noexcept
49 {
50 bool status = true;
51 if (IsShutdown())
52 {
53 OTEL_INTERNAL_LOG_WARN("MetricReader::Shutdown - Cannot invoke shutdown twice!");
54 }
55
56 {
57 const std::lock_guard<opentelemetry::common::SpinLockMutex> locked(lock_);
58 shutdown_ = true;
59 }
60
61 if (!OnShutDown(timeout))
62 {
63 status = false;
64 OTEL_INTERNAL_LOG_WARN("MetricReader::OnShutDown Shutdown failed. Will not be tried again!");
65 }
66 return status;
67 }
68
69 /** Flush metric read by this reader **/
70 bool MetricReader::ForceFlush(std::chrono::microseconds timeout) noexcept
71 {
72 bool status = true;
73 if (shutdown_)
74 {
75 OTEL_INTERNAL_LOG_WARN("MetricReader::Shutdown Cannot invoke Force flush on shutdown reader!");
76 }
77 if (!OnForceFlush(timeout))
78 {
79 status = false;
80 OTEL_INTERNAL_LOG_ERROR("MetricReader::OnForceFlush failed!");
81 }
82 return status;
83 }
84
85 bool MetricReader::IsShutdown() const noexcept
86 {
87 const std::lock_guard<opentelemetry::common::SpinLockMutex> locked(lock_);
88 return shutdown_;
89 }
90
91 } // namespace metrics
92 } // namespace sdk
93 OPENTELEMETRY_END_NAMESPACE
94 #endif