]>
Commit | Line | Data |
---|---|---|
1e59de90 TL |
1 | // Copyright The OpenTelemetry Authors |
2 | // SPDX-License-Identifier: Apache-2.0 | |
3 | ||
4 | #pragma once | |
5 | #ifndef ENABLE_METRICS_PREVIEW | |
6 | ||
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" | |
14 | ||
15 | # include <chrono> | |
16 | # include <memory> | |
17 | # include <vector> | |
18 | ||
19 | OPENTELEMETRY_BEGIN_NAMESPACE | |
20 | namespace sdk | |
21 | { | |
22 | namespace metrics | |
23 | { | |
24 | ||
25 | // forward declaration | |
26 | class Meter; | |
27 | class MetricReader; | |
28 | ||
29 | /** | |
30 | * A class which stores the MeterProvider context. | |
31 | ||
32 | */ | |
33 | class MeterContext : public std::enable_shared_from_this<MeterContext> | |
34 | { | |
35 | public: | |
36 | /** | |
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. | |
41 | */ | |
42 | MeterContext( | |
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; | |
46 | ||
47 | /** | |
48 | * Obtain the resource associated with this meter context. | |
49 | * @return The resource for this meter context | |
50 | */ | |
51 | const opentelemetry::sdk::resource::Resource &GetResource() const noexcept; | |
52 | ||
53 | /** | |
54 | * Obtain the View Registry configured | |
55 | * @return The reference to view registry | |
56 | */ | |
57 | ViewRegistry *GetViewRegistry() const noexcept; | |
58 | ||
59 | /** | |
60 | * Obtain the configured meters. | |
61 | * | |
62 | */ | |
63 | nostd::span<std::shared_ptr<Meter>> GetMeters() noexcept; | |
64 | ||
65 | /** | |
66 | * Obtain the configured collectors. | |
67 | * | |
68 | */ | |
69 | nostd::span<std::shared_ptr<CollectorHandle>> GetCollectors() noexcept; | |
70 | ||
71 | /** | |
72 | * GET SDK Start time | |
73 | * | |
74 | */ | |
75 | opentelemetry::common::SystemTimestamp GetSDKStartTime() noexcept; | |
76 | ||
77 | /** | |
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. | |
81 | * | |
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. | |
84 | */ | |
85 | void AddMetricReader(std::unique_ptr<MetricReader> reader) noexcept; | |
86 | ||
87 | /** | |
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. | |
91 | * | |
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. | |
94 | */ | |
95 | void AddView(std::unique_ptr<InstrumentSelector> instrument_selector, | |
96 | std::unique_ptr<MeterSelector> meter_selector, | |
97 | std::unique_ptr<View> view) noexcept; | |
98 | ||
99 | /** | |
100 | * Adds a meter to the list of configured meters. | |
101 | * Note: This method is INTERNAL to sdk not thread safe. | |
102 | * | |
103 | * @param meter | |
104 | */ | |
105 | void AddMeter(std::shared_ptr<Meter> meter); | |
106 | ||
107 | /** | |
108 | * Force all active Collectors to flush any buffered meter data | |
109 | * within the given timeout. | |
110 | */ | |
111 | ||
112 | bool ForceFlush(std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept; | |
113 | ||
114 | /** | |
115 | * Shutdown the Collectors associated with this meter provider. | |
116 | */ | |
117 | bool Shutdown() noexcept; | |
118 | ||
119 | private: | |
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_; | |
125 | ||
126 | std::atomic_flag shutdown_latch_ = ATOMIC_FLAG_INIT; | |
127 | opentelemetry::common::SpinLockMutex forceflush_lock_; | |
128 | }; | |
129 | ||
130 | } // namespace metrics | |
131 | } // namespace sdk | |
132 | OPENTELEMETRY_END_NAMESPACE | |
133 | #endif |