Skip to content

[SDK] Optimize PeriodicExportingMetricReader Thread Usage #3383

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 6, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 30 additions & 55 deletions sdk/src/metrics/export/periodic_exporting_metric_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ void PeriodicExportingMetricReader::DoBackgroundWork()
worker_thread_instrumentation_->OnStart();
}
#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */

do
std::unique_lock<std::mutex> lk(cv_m_);
while(true)
{
auto start = std::chrono::steady_clock::now();

Expand Down Expand Up @@ -134,8 +134,6 @@ void PeriodicExportingMetricReader::DoBackgroundWork()
worker_thread_instrumentation_->BeforeWait();
}
#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */

std::unique_lock<std::mutex> lk(cv_m_);
cv_.wait_for(lk, remaining_wait_interval_ms, [this]() {
if (is_force_wakeup_background_worker_.load(std::memory_order_acquire))
{
Expand All @@ -151,8 +149,11 @@ void PeriodicExportingMetricReader::DoBackgroundWork()
worker_thread_instrumentation_->AfterWait();
}
#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */

} while (IsShutdown() != true);
if(IsShutdown())
{
return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we need call worker_thread_instrumentation_->OnEnd() below?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1, probably we can just break from here instead of return.

}
}

#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW
if (worker_thread_instrumentation_ != nullptr)
Expand All @@ -164,61 +165,40 @@ void PeriodicExportingMetricReader::DoBackgroundWork()

bool PeriodicExportingMetricReader::CollectAndExportOnce()
{
std::atomic<bool> cancel_export_for_timeout{false};

std::uint64_t notify_force_flush = force_flush_pending_sequence_.load(std::memory_order_acquire);
std::unique_ptr<std::thread> task_thread;

#if OPENTELEMETRY_HAVE_EXCEPTIONS
try
{
#endif
std::promise<void> sender;
auto receiver = sender.get_future();

task_thread.reset(
new std::thread([this, &cancel_export_for_timeout, sender = std::move(sender)] {
#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW
if (collect_thread_instrumentation_ != nullptr)
{
collect_thread_instrumentation_->OnStart();
collect_thread_instrumentation_->BeforeLoad();
}
if (collect_thread_instrumentation_ != nullptr)
{
collect_thread_instrumentation_->OnStart();
collect_thread_instrumentation_->BeforeLoad();
}
#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */
auto start = std::chrono::steady_clock::now();
this->Collect([this, &start](ResourceMetrics &metric_data) {
auto end = std::chrono::steady_clock::now();
if ((end - start) > this->export_timeout_millis_)
{
OTEL_INTERNAL_LOG_ERROR(
"[Periodic Exporting Metric Reader] Collect took longer configured time: "
<< this->export_timeout_millis_.count() << " ms, and timed out");
return false;
}
this->exporter_->Export(metric_data);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to self - the timeout interval export_timeout_millis_ is checked at best effort basis between collect and export. This means, if the collect and/or export takes indefinite time, we can be in blocked state. This is specifically relevant to export method, as its execution time is not in the control of the SDK. The export operation runs to completion once started, regardless of how long it takes.

Nothing for this PR, as this is the existing behavior, but something to discuss further.

return true;
});

this->Collect([this, &cancel_export_for_timeout](ResourceMetrics &metric_data) {
if (cancel_export_for_timeout.load(std::memory_order_acquire))
{
OTEL_INTERNAL_LOG_ERROR(
"[Periodic Exporting Metric Reader] Collect took longer configured time: "
<< this->export_timeout_millis_.count() << " ms, and timed out");
return false;
}
this->exporter_->Export(metric_data);
return true;
});

const_cast<std::promise<void> &>(sender).set_value();

#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW
if (collect_thread_instrumentation_ != nullptr)
{
collect_thread_instrumentation_->AfterLoad();
collect_thread_instrumentation_->OnEnd();
}
if (collect_thread_instrumentation_ != nullptr)
{
collect_thread_instrumentation_->AfterLoad();
collect_thread_instrumentation_->OnEnd();
}
#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */
}));

std::future_status status;
do
{
status = receiver.wait_for(std::chrono::milliseconds(export_timeout_millis_));
if (status == std::future_status::timeout)
{
cancel_export_for_timeout.store(true, std::memory_order_release);
break;
}
} while (status != std::future_status::ready);
#if OPENTELEMETRY_HAVE_EXCEPTIONS
}
catch (std::exception &e)
Expand All @@ -235,11 +215,6 @@ bool PeriodicExportingMetricReader::CollectAndExportOnce()
}
#endif

if (task_thread && task_thread->joinable())
{
task_thread->join();
}

std::uint64_t notified_sequence = force_flush_notified_sequence_.load(std::memory_order_acquire);
while (notify_force_flush > notified_sequence)
{
Expand Down