1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
4 #ifndef ENABLE_METRICS_PREVIEW
5 # include "opentelemetry/sdk/metrics/metric_reader.h"
6 # include "opentelemetry/sdk/metrics/export/metric_producer.h"
10 OPENTELEMETRY_BEGIN_NAMESPACE
16 MetricReader::MetricReader(AggregationTemporality aggregation_temporality
)
17 : aggregation_temporality_(aggregation_temporality
), shutdown_(false), metric_producer_(nullptr)
20 void MetricReader::SetMetricProducer(MetricProducer
*metric_producer
)
22 metric_producer_
= metric_producer
;
26 AggregationTemporality
MetricReader::GetAggregationTemporality() const noexcept
28 return aggregation_temporality_
;
31 bool MetricReader::Collect(
32 nostd::function_ref
<bool(ResourceMetrics
&metric_data
)> callback
) noexcept
34 if (!metric_producer_
)
36 OTEL_INTERNAL_LOG_WARN(
37 "MetricReader::Collect Cannot invoke Collect(). No MetricProducer registered for "
42 OTEL_INTERNAL_LOG_WARN("MetricReader::Collect Cannot invoke Collect(). Shutdown in progress!");
45 return metric_producer_
->Collect(callback
);
48 bool MetricReader::Shutdown(std::chrono::microseconds timeout
) noexcept
53 OTEL_INTERNAL_LOG_WARN("MetricReader::Shutdown - Cannot invoke shutdown twice!");
57 const std::lock_guard
<opentelemetry::common::SpinLockMutex
> locked(lock_
);
61 if (!OnShutDown(timeout
))
64 OTEL_INTERNAL_LOG_WARN("MetricReader::OnShutDown Shutdown failed. Will not be tried again!");
69 /** Flush metric read by this reader **/
70 bool MetricReader::ForceFlush(std::chrono::microseconds timeout
) noexcept
75 OTEL_INTERNAL_LOG_WARN("MetricReader::Shutdown Cannot invoke Force flush on shutdown reader!");
77 if (!OnForceFlush(timeout
))
80 OTEL_INTERNAL_LOG_ERROR("MetricReader::OnForceFlush failed!");
85 bool MetricReader::IsShutdown() const noexcept
87 const std::lock_guard
<opentelemetry::common::SpinLockMutex
> locked(lock_
);
91 } // namespace metrics
93 OPENTELEMETRY_END_NAMESPACE