Skip to content
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

feat(sdk-metrics): PeriodicExportingMetricReader now flushes pending tasks at shutdown #5242

Merged

Conversation

macno
Copy link
Contributor

@macno macno commented Dec 8, 2024

Which problem is this PR solving?

Metric provider behave differently from traces and logs providers during shutdown process, being the only one that does not flush pending tasks.

Although it's not explicitly required (yet) by the specs for metrics and logs as it is for traces, this PR adds flushing metrics on shutdown, with the intent of eliminating a not infrequent confusion and wrong assumption among users.
This way we consistently ensure the flush for all supported signals upon shutdown.

The same approach has already been taken by java and python SDKs.

Short description of the changes

now MeterProvider#shutdown() calls MeterProvider#forceFlush()

Type of change

  • New feature (non-breaking change which adds functionality)

Checklist:

  • Followed the style guidelines of this project
  • Unit tests have been added updated

macno added 2 commits December 7, 2024 10:41
…cription, calling forceFlush()

this also aligns the behavior with the other signals providers:
calling sdk.shutdown() will now flush all pending signals
@macno macno requested a review from a team as a code owner December 8, 2024 15:18
Copy link
Member

@pichlermarc pichlermarc left a comment

Choose a reason for hiding this comment

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

Hi @macno thanks for working on this 🙂

I think the agreement on open-telemetry/opentelemetry-specification#2983 was on the PeriodicExporingMetricReader - not the MeterProvider. Would you mind adapting the code accordingly? 🙂

@macno
Copy link
Contributor Author

macno commented Dec 9, 2024

Hi @macno thanks for working on this 🙂

I think the agreement on open-telemetry/opentelemetry-specification#2983 was on the PeriodicExporingMetricReader - not the MeterProvider. Would you mind adapting the code accordingly? 🙂

Hi @pichlermarc thanks for taking time to look at the PR.

I'm not sure if I read the same agreement, to me seems like an open point.
From my understanding, this is the shutdown flow:

graph TD;
    D@{ shape: procs, label: "metricCollector"}
    SDK-- shutdown() ---meterProvider;
    meterProvider-- shutdown() ---D
    D-- shutdown() ---metricReader
Loading

so calling forceFlush in the meterProvider will cause all the registered metricReader to execute forceFlush, meaning you don't have to implement the same logic in all the metricReader implementations to achieve the same behaviour.

What's the reason to move it to PeriodicExporingMetricReader ?

@pichlermarc
Copy link
Member

[...]
so calling forceFlush in the meterProvider will cause all the registered metricReader to execute forceFlush, meaning you don't have to implement the same logic in all the metricReader implementations to achieve the same behaviour.

What's the reason to move it to PeriodicExporingMetricReader ?

The equivalent of PeriodicExportingMetricReader is BatchSpanProcessor and BatchLogRecordProcessor in the other SDKs and that behavior is specified for their base interfaces. Looking into what the Java and Python SIGs implemented, their MeterProviders don't apply the effects forceFlush() on shutdown(), but the PeriodicExportingMetricReader does it.

While it is true that adding it to MeterProvider would be the easiest, it seems that this intentionally split. Though I don't know what the original reasoning behind that, the spec does not elaborate on it. Anyway, that why I'm not very confident about going against the grain on this.

I asked here again, but I'm fairly certain that my assumption is correct. 🤔

Some links:

@macno
Copy link
Contributor Author

macno commented Dec 9, 2024

Ok, I will proceed as requested

@macno macno requested a review from pichlermarc December 9, 2024 14:30
…hutdown()' only after 'meterProvider.forceFlush()' resolves
Copy link

codecov bot commented Dec 10, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.65%. Comparing base (484af40) to head (a2d7810).
Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5242      +/-   ##
==========================================
+ Coverage   94.60%   94.65%   +0.05%     
==========================================
  Files         315      315              
  Lines        8011     8012       +1     
  Branches     1617     1617              
==========================================
+ Hits         7579     7584       +5     
+ Misses        432      428       -4     
Files with missing lines Coverage Δ
packages/sdk-metrics/src/MeterProvider.ts 100.00% <ø> (ø)
...etrics/src/export/PeriodicExportingMetricReader.ts 98.21% <100.00%> (+0.03%) ⬆️

... and 4 files with indirect coverage changes

@pichlermarc
Copy link
Member

Please also add a changelog entry in ./CHANGELOG.md, then we can merge this. 🙂

@macno macno changed the title sdk-metrics: forceFlush on shutdown feat(sdk-metrics): PeriodicExportingMetricReader now flushes pending tasks at shutdown Dec 10, 2024
Copy link
Member

@pichlermarc pichlermarc left a comment

Choose a reason for hiding this comment

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

Looks good, thanks 🙂

@pichlermarc pichlermarc added this pull request to the merge queue Dec 10, 2024
Merged via the queue into open-telemetry:main with commit 79e2875 Dec 10, 2024
21 checks passed
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.

2 participants