Skip to content

Conversation

@alecsammon
Copy link

In our usage of this we make use of slog context methods - i.e. slog.InfoContext

As the periodic reader uses a context.Background() this is causing issues with our logging as we are missing required data from the context.

I think that ideally NewPeriodicReader would take a context.Context as it's first parameter - i.e.
func NewPeriodicReader(ctx context.Context, exporter Exporter, options ...PeriodicReaderOption) *PeriodicReader {
However this would be a breaking change which may be undesirable.

There are few of option I believe.

  1. Add a functional option to add a context. This is probably the cleanest option at the moment, but does break the standard go convention of not including contexts in structs.
  2. Add a new method of NewPeriodicReaderContext that receives the context - this would be the least code change
  3. Make a breaking change to the NewPeriodicReader

This PR shows the code required for option 1.
If option 2 of NewPeriodicReaderContext is preferred then I'm happy to create that PR instead.

Thanks!

@dmathieu
Copy link
Member

A functional option is IMHO the best way indeed.
In this PR, you're setting the context in every test (though it's not necessary, so setting it is a bit of clutter). But nothing actually tests that the option actually uses the passed context.

@codecov
Copy link

codecov bot commented Jun 30, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 83.0%. Comparing base (4458a00) to head (ff69e1b).
Report is 5 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@          Coverage Diff          @@
##            main   #6936   +/-   ##
=====================================
  Coverage   83.0%   83.0%           
=====================================
  Files        261     261           
  Lines      24256   24262    +6     
=====================================
+ Hits       20143   20148    +5     
- Misses      3738    3739    +1     
  Partials     375     375           
Files with missing lines Coverage Δ
sdk/metric/periodic_reader.go 84.4% <100.0%> (-0.6%) ⬇️

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@alecsammon
Copy link
Author

Thanks @dmathieu

Agree the tests on this PR are not good enough! I wanted to get clarity on way forward before investing too much time.

I'll get the PR ready with tests and changelog.

@alecsammon alecsammon marked this pull request as draft June 30, 2025 11:09
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