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

Conversation

ColeVanOphem
Copy link
Contributor

@ColeVanOphem ColeVanOphem commented Apr 26, 2025

Fixes #3230

Changes

Optimizes thread usage in PeriodicExportingMetricReader by:

  • Removing the separate thread previously used for CollectAndExportOnce. Collection and export now run synchronously in the worker thread.
  • Simplifying the timeout logic within the collection callback, replacing the previous std::future-based mechanism.
  • Refactoring the worker thread's wait loop using std::condition_variable.

For significant contributions please make sure you have completed the following items:

  • CHANGELOG.md updated for non-trivial changes
  • Unit tests have been added
  • Changes in public API reviewed

@ColeVanOphem ColeVanOphem requested a review from a team as a code owner April 26, 2025 01:29
Copy link

linux-foundation-easycla bot commented Apr 26, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

Copy link

netlify bot commented Apr 26, 2025

Deploy Preview for opentelemetry-cpp-api-docs canceled.

Name Link
🔨 Latest commit f8c3b5f
🔍 Latest deploy log https://app.netlify.com/sites/opentelemetry-cpp-api-docs/deploys/68198759135b050008925b1e

@ColeVanOphem ColeVanOphem changed the title Optimize PeriodicExportingMetricReader Thread Usage 3230 Optimize PeriodicExportingMetricReader Thread Usage Apr 26, 2025
} 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.

Replaced early returns with breaks in PeriodicExportingMetricReader::DoBackgroundWork to fix worker_thread_instrumentation_ behavior.
@marcalff marcalff changed the title 3230 Optimize PeriodicExportingMetricReader Thread Usage [SDK] Optimize PeriodicExportingMetricReader Thread Usage Apr 28, 2025
Copy link

codecov bot commented Apr 28, 2025

Codecov Report

Attention: Patch coverage is 75.00000% with 2 lines in your changes missing coverage. Please review.

Project coverage is 89.98%. Comparing base (ed91e71) to head (f8c3b5f).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...metrics/export/periodic_exporting_metric_reader.cc 75.00% 2 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #3383      +/-   ##
==========================================
+ Coverage   89.91%   89.98%   +0.08%     
==========================================
  Files         211      211              
  Lines        6832     6812      -20     
==========================================
- Hits         6142     6129      -13     
+ Misses        690      683       -7     
Files with missing lines Coverage Δ
...metrics/export/periodic_exporting_metric_reader.cc 76.60% <75.00%> (+2.04%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

pr1nceray and others added 2 commits April 28, 2025 05:37
Fixed formatting so that it aligns with the clangformat
Removed unnecessary future includes to address errors raised by include-what-you-use
@@ -151,8 +141,11 @@ void PeriodicExportingMetricReader::DoBackgroundWork()
worker_thread_instrumentation_->AfterWait();
}
#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */

} while (IsShutdown() != true);
if (IsShutdown())
Copy link
Contributor

Choose a reason for hiding this comment

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

With the return changed back to break, is it the same as the original do...while loop? If so, just restore it to the do...while loop which seems more concise?

pr1nceray and others added 2 commits May 3, 2025 01:21
Improved scoping of the unique lock that is used in PeriodicExportingMetricReader::DoBackgroundWork

Changed while loop logic from while(true)to be similar to original code (do-while)
Copy link
Contributor

@dbarker dbarker left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution @ColeVanOphem @pr1nceray. The change looks good. Please add a changelog entry and consider adding test cases/checks for the PeriodicExportingMetricReader if missing or impacted by this PR.

<< 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.

Copy link
Member

@lalitb lalitb left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks for the PR :)

@ThomsonTan
Copy link
Contributor

@ColeVanOphem please let us know if could add an entry to CHANGELOG as @dbarker mentioned, then the PR is ready for merging.

@ThomsonTan ThomsonTan merged commit 9451f0e into open-telemetry:main May 6, 2025
66 checks passed
malkia added a commit to malkia/opentelemetry-cpp that referenced this pull request May 6, 2025
[SDK] Optimize PeriodicExportingMetricReader Thread Usage (open-telemetry#3383)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Metrics SDK] Optimize Thread Usage in PeriodicExportingMetricReader
6 participants