10
10
#include < ostream>
11
11
#include < ratio>
12
12
#include < thread>
13
- #include < type_traits>
14
13
#include < utility>
15
14
16
15
#include " opentelemetry/common/timestamp.h"
23
22
#include " opentelemetry/sdk/metrics/instruments.h"
24
23
#include " opentelemetry/sdk/metrics/push_metric_exporter.h"
25
24
26
- #if defined(_MSC_VER)
27
- # pragma warning(suppress : 5204)
28
- # include < future>
29
- #else
30
- # include < future>
31
- #endif
32
-
33
25
#if OPENTELEMETRY_HAVE_EXCEPTIONS
34
26
# include < exception>
35
27
#endif
@@ -97,11 +89,9 @@ void PeriodicExportingMetricReader::DoBackgroundWork()
97
89
worker_thread_instrumentation_->OnStart ();
98
90
}
99
91
#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */
100
-
101
92
do
102
93
{
103
94
auto start = std::chrono::steady_clock::now ();
104
-
105
95
#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW
106
96
if (worker_thread_instrumentation_ != nullptr )
107
97
{
@@ -133,7 +123,6 @@ void PeriodicExportingMetricReader::DoBackgroundWork()
133
123
worker_thread_instrumentation_->BeforeWait ();
134
124
}
135
125
#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */
136
-
137
126
std::unique_lock<std::mutex> lk (cv_m_);
138
127
cv_.wait_for (lk, remaining_wait_interval_ms, [this ]() {
139
128
if (is_force_wakeup_background_worker_.load (std::memory_order_acquire))
@@ -150,7 +139,6 @@ void PeriodicExportingMetricReader::DoBackgroundWork()
150
139
worker_thread_instrumentation_->AfterWait ();
151
140
}
152
141
#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */
153
-
154
142
} while (IsShutdown () != true );
155
143
156
144
#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW
@@ -163,61 +151,39 @@ void PeriodicExportingMetricReader::DoBackgroundWork()
163
151
164
152
bool PeriodicExportingMetricReader::CollectAndExportOnce ()
165
153
{
166
- std::atomic<bool > cancel_export_for_timeout{false };
167
-
168
154
std::uint64_t notify_force_flush = force_flush_pending_sequence_.load (std::memory_order_acquire);
169
- std::unique_ptr<std::thread> task_thread;
170
-
171
155
#if OPENTELEMETRY_HAVE_EXCEPTIONS
172
156
try
173
157
{
174
158
#endif
175
- std::promise<void > sender;
176
- auto receiver = sender.get_future ();
177
-
178
- task_thread.reset (
179
- new std::thread ([this , &cancel_export_for_timeout, sender = std::move (sender)] {
180
159
#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW
181
- if (collect_thread_instrumentation_ != nullptr )
182
- {
183
- collect_thread_instrumentation_->OnStart ();
184
- collect_thread_instrumentation_->BeforeLoad ();
185
- }
160
+ if (collect_thread_instrumentation_ != nullptr )
161
+ {
162
+ collect_thread_instrumentation_->OnStart ();
163
+ collect_thread_instrumentation_->BeforeLoad ();
164
+ }
186
165
#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */
187
-
188
- this ->Collect ([this , &cancel_export_for_timeout](ResourceMetrics &metric_data) {
189
- if (cancel_export_for_timeout.load (std::memory_order_acquire))
190
- {
191
- OTEL_INTERNAL_LOG_ERROR (
192
- " [Periodic Exporting Metric Reader] Collect took longer configured time: "
193
- << this ->export_timeout_millis_ .count () << " ms, and timed out" );
194
- return false ;
195
- }
196
- this ->exporter_ ->Export (metric_data);
197
- return true ;
198
- });
199
-
200
- const_cast <std::promise<void > &>(sender).set_value ();
166
+ auto start = std::chrono::steady_clock::now ();
167
+ this ->Collect ([this , &start](ResourceMetrics &metric_data) {
168
+ auto end = std::chrono::steady_clock::now ();
169
+ if ((end - start) > this ->export_timeout_millis_ )
170
+ {
171
+ OTEL_INTERNAL_LOG_ERROR (
172
+ " [Periodic Exporting Metric Reader] Collect took longer configured time: "
173
+ << this ->export_timeout_millis_ .count () << " ms, and timed out" );
174
+ return false ;
175
+ }
176
+ this ->exporter_ ->Export (metric_data);
177
+ return true ;
178
+ });
201
179
202
180
#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW
203
- if (collect_thread_instrumentation_ != nullptr )
204
- {
205
- collect_thread_instrumentation_->AfterLoad ();
206
- collect_thread_instrumentation_->OnEnd ();
207
- }
208
- #endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */
209
- }));
210
-
211
- std::future_status status;
212
- do
181
+ if (collect_thread_instrumentation_ != nullptr )
213
182
{
214
- status = receiver.wait_for (std::chrono::milliseconds (export_timeout_millis_));
215
- if (status == std::future_status::timeout)
216
- {
217
- cancel_export_for_timeout.store (true , std::memory_order_release);
218
- break ;
219
- }
220
- } while (status != std::future_status::ready);
183
+ collect_thread_instrumentation_->AfterLoad ();
184
+ collect_thread_instrumentation_->OnEnd ();
185
+ }
186
+ #endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */
221
187
#if OPENTELEMETRY_HAVE_EXCEPTIONS
222
188
}
223
189
catch (std::exception &e)
@@ -234,11 +200,6 @@ bool PeriodicExportingMetricReader::CollectAndExportOnce()
234
200
}
235
201
#endif
236
202
237
- if (task_thread && task_thread->joinable ())
238
- {
239
- task_thread->join ();
240
- }
241
-
242
203
std::uint64_t notified_sequence = force_flush_notified_sequence_.load (std::memory_order_acquire);
243
204
while (notify_force_flush > notified_sequence)
244
205
{
0 commit comments