-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
proposal: context: add WithValues #26691
Comments
See also #19643. /cc @Sajmani @zombiezen @bcmills |
Some values in the In such cases, you need something more than just to carry over the values: you need to filter out values that can no longer be used, or perhaps extend their lifetimes. That sort of use-case doesn't seem to be addressed in this proposal. |
I don't think WithValues needs to encode that filtering; that can be done
outside the context package. I think WithValues should have a somewhat more
restricted signature than is in the proposal: the second parameter should
have type context.Values, an interface that has only the Value method. Then
users can define their own filter as a Values implementation that passes
through only the allowed Value calls.
…On Fri, Aug 10, 2018 at 2:00 PM Bryan C. Mills ***@***.***> wrote:
Some values in the Context may have finite lifetimes after which they can
no longer be used. (For example, when the End method of an OpenCensus Span
s <https://godoc.org/go.opencensus.io/trace#Span> is called, it is
flushed to the Exporters, and future annotations to the span will be
lost.)
In such cases, you need something more than just to carry over the values:
you need to filter out values that can no longer be used, or perhaps extend
their lifetimes.
That sort of use-case doesn't seem to be addressed in this proposal.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#26691 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AJSK3Sr2PqPMk867ewvacDteeUmAK9vVks5uPcpJgaJpZM4VmQEQ>
.
|
@bcmills can you elaborate on the given example, for those who are not familiar with the code - as it seems to me, if there is an explicit call to And generally, what sort of cases need filtering out values from the context? |
Overrides of
Right, that's the problem: when |
I'm sorry, I don't understand how spans are bad example here:
For long running tasks that were created from HTTP handlers - I think it is bad practice to create a new context for them, and it is important for them to know the context that they came from. The examples I gave demonstrate the need, and they don't have a solution currently in the standard library. |
The middleware is exactly the point: at some point, that middleware needs to know when to flush the span, and at the moment the only reasonable signal it gets is “the next handler down the chain has returned”. |
Maybe asynchronous task trace should not be the same as the handler itself trace. Maybe if you want to send spans from asynchronous task, you should create a new trace and send it when you are done? |
Yes, that's exactly my point: if the trace may be stored and accessed in the |
This need can be satisfied outside the standard library, as you have. (https://github.com/posener/ctxutil/blob/master/values.go) There are various things you might want to do to mix parts of two contexts. It's not clear which one is "right" if we're going to have a blessed mixer. Better for users who need a specific kind of mix to implement it themselves. This is why context.Context is an interface. |
Note that any If we're going to press for folks to address these sorts of issues outside the standard library, we should probably make the standard library play more nicely with external implementations. |
What version of Go are you using (
go version
)?1.10
Does this issue reproduce with the latest release?
yes
What operating system and processor architecture are you using (
go env
)?linux
What did you do?
Consider the following standard HTTP Go server stack:
http.Request
context as values.http.Handler
launches an asynchronous goroutin task which needs those values from the context.Problem Statement
http.Handler
returns.Proposal
I suggest to add a function that combines values from two contexts -
context.WithValues(ctx, values)
or so.An initial implementation is available here.
Discussion is available in golang-nuts forum
The text was updated successfully, but these errors were encountered: