-
Notifications
You must be signed in to change notification settings - Fork 438
/
meter_context.cc
148 lines (128 loc) · 4.27 KB
/
meter_context.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
#ifndef ENABLE_METRICS_PREVIEW
# include "opentelemetry/sdk/metrics/meter_context.h"
# include "opentelemetry/sdk/common/global_log_handler.h"
# include "opentelemetry/sdk/metrics/metric_reader.h"
# include "opentelemetry/sdk_config.h"
# include "opentelemetry/version.h"
OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
{
namespace metrics
{
MeterContext::MeterContext(std::unique_ptr<ViewRegistry> views,
opentelemetry::sdk::resource::Resource resource) noexcept
: resource_{resource}, views_(std::move(views)), sdk_start_ts_{std::chrono::system_clock::now()}
{}
const resource::Resource &MeterContext::GetResource() const noexcept
{
return resource_;
}
ViewRegistry *MeterContext::GetViewRegistry() const noexcept
{
return views_.get();
}
nostd::span<std::shared_ptr<Meter>> MeterContext::GetMeters() noexcept
{
std::lock_guard<opentelemetry::common::SpinLockMutex> guard(storage_lock_);
return nostd::span<std::shared_ptr<Meter>>{meters_};
}
nostd::span<std::shared_ptr<CollectorHandle>> MeterContext::GetCollectors() noexcept
{
return nostd::span<std::shared_ptr<CollectorHandle>>(collectors_);
}
opentelemetry::common::SystemTimestamp MeterContext::GetSDKStartTime() noexcept
{
return sdk_start_ts_;
}
void MeterContext::AddMetricReader(std::shared_ptr<MetricReader> reader) noexcept
{
auto collector = std::shared_ptr<MetricCollector>{new MetricCollector(this, reader)};
collectors_.push_back(collector);
}
void MeterContext::AddView(std::unique_ptr<InstrumentSelector> instrument_selector,
std::unique_ptr<MeterSelector> meter_selector,
std::unique_ptr<View> view) noexcept
{
views_->AddView(std::move(instrument_selector), std::move(meter_selector), std::move(view));
}
void MeterContext::AddMeter(std::shared_ptr<Meter> meter)
{
std::lock_guard<opentelemetry::common::SpinLockMutex> guard(storage_lock_);
meters_.push_back(meter);
}
bool MeterContext::Shutdown() noexcept
{
bool result = true;
// Shutdown only once.
if (!shutdown_latch_.test_and_set(std::memory_order_acquire))
{
for (auto &collector : collectors_)
{
bool status = std::static_pointer_cast<MetricCollector>(collector)->Shutdown();
result = result && status;
}
if (!result)
{
OTEL_INTERNAL_LOG_WARN("[MeterContext::Shutdown] Unable to shutdown all metric readers");
}
}
else
{
OTEL_INTERNAL_LOG_WARN("[MeterContext::Shutdown] Shutdown can be invoked only once.");
}
return result;
}
bool MeterContext::ForceFlush(std::chrono::microseconds timeout) noexcept
{
bool result = true;
// Simultaneous flush not allowed.
const std::lock_guard<opentelemetry::common::SpinLockMutex> locked(forceflush_lock_);
// Convert to nanos to prevent overflow
auto timeout_ns = std::chrono::nanoseconds::max();
if (std::chrono::duration_cast<std::chrono::microseconds>(timeout_ns) > timeout)
{
timeout_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(timeout);
}
auto current_time = std::chrono::system_clock::now();
std::chrono::system_clock::time_point expire_time;
auto overflow_checker = std::chrono::system_clock::time_point::max();
// check if the expected expire time doesn't overflow.
if (overflow_checker - current_time > timeout_ns)
{
expire_time =
current_time + std::chrono::duration_cast<std::chrono::system_clock::duration>(timeout_ns);
}
else
{
// overflow happens, reset expire time to max.
expire_time = overflow_checker;
}
for (auto &collector : collectors_)
{
if (!std::static_pointer_cast<MetricCollector>(collector)->ForceFlush(
std::chrono::duration_cast<std::chrono::microseconds>(timeout_ns)))
{
result = false;
}
current_time = std::chrono::system_clock::now();
if (expire_time >= current_time)
{
timeout_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(expire_time - current_time);
}
else
{
timeout_ns = std::chrono::nanoseconds::zero();
}
}
if (!result)
{
OTEL_INTERNAL_LOG_WARN("[MeterContext::ForceFlush] Unable to ForceFlush all metric readers");
}
return result;
}
} // namespace metrics
} // namespace sdk
OPENTELEMETRY_END_NAMESPACE
#endif