1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
5 #ifndef ENABLE_METRICS_PREVIEW
7 # include "opentelemetry/common/spin_lock_mutex.h"
8 # include "opentelemetry/sdk/metrics/state/metric_collector.h"
9 # include "opentelemetry/sdk/metrics/view/instrument_selector.h"
10 # include "opentelemetry/sdk/metrics/view/meter_selector.h"
11 # include "opentelemetry/sdk/metrics/view/view_registry.h"
12 # include "opentelemetry/sdk/resource/resource.h"
13 # include "opentelemetry/version.h"
19 OPENTELEMETRY_BEGIN_NAMESPACE
25 // forward declaration
30 * A class which stores the MeterProvider context.
33 class MeterContext
: public std::enable_shared_from_this
<MeterContext
>
37 * Initialize a new meter provider
38 * @param readers The readers to be configured with meter context.
39 * @param views The views to be configured with meter context.
40 * @param resource The resource for this meter context.
43 std::unique_ptr
<ViewRegistry
> views
= std::unique_ptr
<ViewRegistry
>(new ViewRegistry()),
44 opentelemetry::sdk::resource::Resource resource
=
45 opentelemetry::sdk::resource::Resource::Create({})) noexcept
;
48 * Obtain the resource associated with this meter context.
49 * @return The resource for this meter context
51 const opentelemetry::sdk::resource::Resource
&GetResource() const noexcept
;
54 * Obtain the View Registry configured
55 * @return The reference to view registry
57 ViewRegistry
*GetViewRegistry() const noexcept
;
60 * Obtain the configured meters.
63 nostd::span
<std::shared_ptr
<Meter
>> GetMeters() noexcept
;
66 * Obtain the configured collectors.
69 nostd::span
<std::shared_ptr
<CollectorHandle
>> GetCollectors() noexcept
;
75 opentelemetry::common::SystemTimestamp
GetSDKStartTime() noexcept
;
78 * Attaches a metric reader to list of configured readers for this Meter context.
79 * @param reader The metric reader for this meter context. This
80 * must not be a nullptr.
82 * Note: This reader may not receive any in-flight meter data, but will get newly created meter
83 * data. Note: This method is not thread safe, and should ideally be called from main thread.
85 void AddMetricReader(std::unique_ptr
<MetricReader
> reader
) noexcept
;
88 * Attaches a View to list of configured Views for this Meter context.
89 * @param view The Views for this meter context. This
90 * must not be a nullptr.
92 * Note: This view may not receive any in-flight meter data, but will get newly created meter
93 * data. Note: This method is not thread safe, and should ideally be called from main thread.
95 void AddView(std::unique_ptr
<InstrumentSelector
> instrument_selector
,
96 std::unique_ptr
<MeterSelector
> meter_selector
,
97 std::unique_ptr
<View
> view
) noexcept
;
100 * Adds a meter to the list of configured meters.
101 * Note: This method is INTERNAL to sdk not thread safe.
105 void AddMeter(std::shared_ptr
<Meter
> meter
);
108 * Force all active Collectors to flush any buffered meter data
109 * within the given timeout.
112 bool ForceFlush(std::chrono::microseconds timeout
= (std::chrono::microseconds::max
)()) noexcept
;
115 * Shutdown the Collectors associated with this meter provider.
117 bool Shutdown() noexcept
;
120 opentelemetry::sdk::resource::Resource resource_
;
121 std::vector
<std::shared_ptr
<CollectorHandle
>> collectors_
;
122 std::unique_ptr
<ViewRegistry
> views_
;
123 opentelemetry::common::SystemTimestamp sdk_start_ts_
;
124 std::vector
<std::shared_ptr
<Meter
>> meters_
;
126 std::atomic_flag shutdown_latch_
= ATOMIC_FLAG_INIT
;
127 opentelemetry::common::SpinLockMutex forceflush_lock_
;
130 } // namespace metrics
132 OPENTELEMETRY_END_NAMESPACE