-
Notifications
You must be signed in to change notification settings - Fork 220
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
Update echo middleware to get Hub instance from context #217
Update echo middleware to get Hub instance from context #217
Conversation
…o include extra fields defined in the parent context
@j--wong would you have a short example of how you're using this? I would like to understand what use case this is solving, because if we change the echo middleware we may want to consider the same change for other frameworks. |
Hi @rhcarvalho, thanks for the reply. We are building rest apis in AWS using We configure logging, tracing (honeycomb) and error handling/monitoring (sentry) at the The key use case for us is we want that same set of extra information to flow to We configure a hub instance at Without this PR, those extra information is not preserved in We configure honeycomb the same way. Honeycomb version of echo middleware looks for existing trace from parent context first, and create a new one only if there is no existing trace in parent context. As seen here: https://github.com/honeycombio/beeline-go/blob/d24142d664/wrappers/common/common.go#L45-L57 I believe this should be applicable to other web frameworks as well. I think it's sensible to use existing hub from context as default. Maybe you could introduce this behind an option like Sorry it was a bit long, hope this all makes sense 😄 |
@j--wong thanks for the description. Do you have a short piece of code to demonstrate how you're putting things together? Particularly interested in the part related to sentry-go. I'd like to see precisely how you're inserting a hub in the request context before the sentry middleware. In this example code there is a demonstration of how to add a tag to all Sentry events: sentry-go/example/echo/main.go Lines 35 to 46 in af3076c
With the same pattern you can configure the request's hub/scope arbitrarily. I am imagining you have a middleware that is running before Sentry's middleware, that's why you are trying to add a hub to the Echo context manually?! |
Hi @rhcarvalho, in my case, I already have a Yes, I got a middleware running before sentry-echo middleware (it's lambda middlware, not echo middleware). The code looks like this: return func(next lambda.Handler) lambda.Handler {
...
...
return func(ctx context.Context, payload []byte) ([]byte, error) {
lc, _ := lambdacontext.FromContext(ctx)
hub := sentry.CurrentHub().Clone()
hub.Scope().SetExtras(cfg.Fields) // extra information
hub.Scope().SetExtra("aws_request_id", lc.AwsRequestID)
hub.Scope().SetExtra("...",...) // more lines omitted
ctx = sentry.SetHubOnContext(ctx, hub)
...
...
// invoke next lambda middleware; from this point forward, we have hub instance on context
return next.Invoke(ctx, payload)
}
} The reason I configured sentry this way in The other approach I could take is, like in the example you shared, duplicate above code in another echo middleware func. So I add whatever fields I want to add after sentry middleware. I guess the key use-case question I'd like to ask is -- do you believe this is a valid/reasonable scenario for sentry/echo users to have Hub instance configured higher up in the chain before echo middleware run? To me, that's exactly my use case 😄. Maybe not many people are doing |
This is a reasonable use case. I'll follow up with adding similar behavior to the other integrations. Thanks, @j--wong! This will be part of the upcoming next release. |
This allows users to setup a request-scoped Hub with relevant data in a middleware that runs before the web frameworks we integrate with. A typical case is AWS Lambda + Web Framework. See [1] for an example. [1]: getsentry#217 (comment)
This allows users to setup a request-scoped Hub with relevant data in a middleware that runs before the web frameworks we integrate with. A typical case is AWS Lambda + Web Framework. See [1] for an example. [1]: #217 (comment)
Great stuff @rhcarvalho! Thanks for that 👍 |
Modified to support getting
sentry.Hub
instance from parent Context to include extra fields defined in the parent context.Background -- we use sentry with echo web framework in a AWS lambda environment behind AWS API Gateway. We have top-level sentry instance defined with extra fields.
The purpose of this change is to use the existing hub on context by default.