-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Add context to ReporterV2 #11451
Closed
Closed
Add context to ReporterV2 #11451
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c52b025
ReporterV2 implement context
jsoriano 8480a9e
Add a Context() method instead of implementing context
jsoriano f58eb8d
Fix godoc
jsoriano 364cc62
Merge remote-tracking branch 'origin/master' into mb-reporter-v2-context
jsoriano 280fbf6
Update changelog
jsoriano c11efbc
Merge remote-tracking branch 'origin/master' into mb-reporter-v2-context
jsoriano File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm afraid that I believe that we shouldn't add context at all 😅 There are two main rules when implementing context:
context.Context
must be propagated from the initial call sofunc ReportingFetchV2(metricSet mb.ReportingMetricSetV2) ([]mb.Event, []error)
must befunc ReportingFetchV2(ctx context.Context, metricSet mb.ReportingMetricSetV2) ([]mb.Event, []error)
. Propagation is key when using context.context.Context
must not be stored anyhow. It's a continuation of the previous, if you follow the rule of propagation, you won't store it at all.Finally, I don't see a real benefit of using context here. I mean that it looks like it's a "we can use Context here, as many other approaches" instead of a "it's a clear use case of Context". Also, I feel that we are replacing a piece with a explicit implementation which was already working. Explicit code is idiomatic in Go :)
A not so old post about this: https://dave.cheney.net/2017/08/20/context-isnt-for-cancellation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general I agree with that, but I'd like to avoid adding yet another interface (two of them if adding the one returning an error, plus its testing related code). We'd have to evaluate if it worths to add more interfaces for this.
Also this is a pattern also used in the standard library, for example HTTP handlers receive a Request that has a
Context()
.The context is only being stored where it is created, in the wrapper. We can continue with future refactorizations to replace
Start(done <-chan struct{})
withStart(ctx context.Context)
so we have a context that can be propagated from there.Well, most of our modules make requests to other services and many client libraries accept contexts.
The main benefit is that we can start using a context that makes sense in the current module lifecycle instead of using
Background()
,TODO()
or avoiding APIs with contexts.It is not using it "as many other approaches", it is using it as the approach that afaik is more widely used in the standard and third party libraries (even if we can discuss if it is the best approach).
Nice read, but I think that start using something like
tomb
would require a huge effort in the case of Metricbeat, while getting a context from a done channel we already have is quite straightforward. Also many libraries we use expect contexts, if we use other things we still need to convert them.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the first version of Beats was written, Context did not exist yet and we started with the
done
channel. Would we build it again I would assume Context would play quite a role in it. Long term I think more and more parts should be switched over to context but we need to go baby steps here (like in this PR). At least that is my thinking.