@@ -98,8 +98,8 @@ void PeriodicExportingMetricReader::DoBackgroundWork()
98
98
worker_thread_instrumentation_->OnStart ();
99
99
}
100
100
#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */
101
-
102
- do
101
+ std::unique_lock<std::mutex> lk (cv_m_);
102
+ while ( true )
103
103
{
104
104
auto start = std::chrono::steady_clock::now ();
105
105
@@ -134,8 +134,6 @@ void PeriodicExportingMetricReader::DoBackgroundWork()
134
134
worker_thread_instrumentation_->BeforeWait ();
135
135
}
136
136
#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */
137
-
138
- std::unique_lock<std::mutex> lk (cv_m_);
139
137
cv_.wait_for (lk, remaining_wait_interval_ms, [this ]() {
140
138
if (is_force_wakeup_background_worker_.load (std::memory_order_acquire))
141
139
{
@@ -151,8 +149,11 @@ void PeriodicExportingMetricReader::DoBackgroundWork()
151
149
worker_thread_instrumentation_->AfterWait ();
152
150
}
153
151
#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */
154
-
155
- } while (IsShutdown () != true );
152
+ if (IsShutdown ())
153
+ {
154
+ return ;
155
+ }
156
+ }
156
157
157
158
#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW
158
159
if (worker_thread_instrumentation_ != nullptr )
@@ -164,61 +165,40 @@ void PeriodicExportingMetricReader::DoBackgroundWork()
164
165
165
166
bool PeriodicExportingMetricReader::CollectAndExportOnce ()
166
167
{
167
- std::atomic<bool > cancel_export_for_timeout{false };
168
-
169
168
std::uint64_t notify_force_flush = force_flush_pending_sequence_.load (std::memory_order_acquire);
170
- std::unique_ptr<std::thread> task_thread;
171
-
172
169
#if OPENTELEMETRY_HAVE_EXCEPTIONS
173
170
try
174
171
{
175
172
#endif
176
- std::promise<void > sender;
177
- auto receiver = sender.get_future ();
178
-
179
- task_thread.reset (
180
- new std::thread ([this , &cancel_export_for_timeout, sender = std::move (sender)] {
181
173
#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW
182
- if (collect_thread_instrumentation_ != nullptr )
183
- {
184
- collect_thread_instrumentation_->OnStart ();
185
- collect_thread_instrumentation_->BeforeLoad ();
186
- }
174
+ if (collect_thread_instrumentation_ != nullptr )
175
+ {
176
+ collect_thread_instrumentation_->OnStart ();
177
+ collect_thread_instrumentation_->BeforeLoad ();
178
+ }
187
179
#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */
180
+ auto start = std::chrono::steady_clock::now ();
181
+ this ->Collect ([this , &start](ResourceMetrics &metric_data) {
182
+ auto end = std::chrono::steady_clock::now ();
183
+ if ((end - start) > this ->export_timeout_millis_ )
184
+ {
185
+ OTEL_INTERNAL_LOG_ERROR (
186
+ " [Periodic Exporting Metric Reader] Collect took longer configured time: "
187
+ << this ->export_timeout_millis_ .count () << " ms, and timed out" );
188
+ return false ;
189
+ }
190
+ this ->exporter_ ->Export (metric_data);
191
+ return true ;
192
+ });
188
193
189
- this ->Collect ([this , &cancel_export_for_timeout](ResourceMetrics &metric_data) {
190
- if (cancel_export_for_timeout.load (std::memory_order_acquire))
191
- {
192
- OTEL_INTERNAL_LOG_ERROR (
193
- " [Periodic Exporting Metric Reader] Collect took longer configured time: "
194
- << this ->export_timeout_millis_ .count () << " ms, and timed out" );
195
- return false ;
196
- }
197
- this ->exporter_ ->Export (metric_data);
198
- return true ;
199
- });
200
-
201
- const_cast <std::promise<void > &>(sender).set_value ();
202
194
203
195
#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW
204
- if (collect_thread_instrumentation_ != nullptr )
205
- {
206
- collect_thread_instrumentation_->AfterLoad ();
207
- collect_thread_instrumentation_->OnEnd ();
208
- }
196
+ if (collect_thread_instrumentation_ != nullptr )
197
+ {
198
+ collect_thread_instrumentation_->AfterLoad ();
199
+ collect_thread_instrumentation_->OnEnd ();
200
+ }
209
201
#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */
210
- }));
211
-
212
- std::future_status status;
213
- do
214
- {
215
- status = receiver.wait_for (std::chrono::milliseconds (export_timeout_millis_));
216
- if (status == std::future_status::timeout)
217
- {
218
- cancel_export_for_timeout.store (true , std::memory_order_release);
219
- break ;
220
- }
221
- } while (status != std::future_status::ready);
222
202
#if OPENTELEMETRY_HAVE_EXCEPTIONS
223
203
}
224
204
catch (std::exception &e)
@@ -235,11 +215,6 @@ bool PeriodicExportingMetricReader::CollectAndExportOnce()
235
215
}
236
216
#endif
237
217
238
- if (task_thread && task_thread->joinable ())
239
- {
240
- task_thread->join ();
241
- }
242
-
243
218
std::uint64_t notified_sequence = force_flush_notified_sequence_.load (std::memory_order_acquire);
244
219
while (notify_force_flush > notified_sequence)
245
220
{
0 commit comments