-
-
Notifications
You must be signed in to change notification settings - Fork 167
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
Huma middleware can't access some information comming from the http.Request #590
Comments
@aureliar8 this seems like a missing feature in the adapter interface. I'll see if I can get it added to enable this use case. Thanks for reporting! |
@danielgtaylor any reason why It would be nice to also allow overriding the underlying |
@george-rogers not all routers use the standard library for HTTP, for example Fiber has its own HTTP library with its own request type, so we couldn't just return an |
@danielgtaylor - just exporting the ChiContext would be sufficient for my use case. e.g. type chiContext struct { -> type ChiContext struct { as I could just cast it in the middleware. No? |
On a similar observability topic, in a Huma middleware how can we get access to the Request and Response sizes. Chi's logging middleware does this by wrapping the ResponseWriter in a multiwriter so it can see the response size after it's been sent and log it. |
It's a bit of a hack, but I figured out how to create a router-level middleware that can get operation id by passing in a closure that returns an instance of the EDIT: This doesn't work for paths with parameters, so nevermind. 🫤 func OperationID(apiFn func() huma.API) func(http.Handler) http.Handler {
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
method := r.Method
path := r.URL.Path
l := log.With().Str("path", path).Str("method", method).Logger()
api := apiFn()
if api == nil || api.OpenAPI() == nil {
l.Warn().Msg("Huma API is unexpectedly nil!")
h.ServeHTTP(w, r)
return
}
item, found := api.OpenAPI().Paths[path]
if !found {
l.Warn().Msg("Path not found in OpenAPI spec")
h.ServeHTTP(w, r)
return
}
var operationID string
switch method {
case http.MethodGet:
operationID = item.Get.OperationID
case http.MethodPut:
operationID = item.Put.OperationID
case http.MethodPost:
operationID = item.Post.OperationID
case http.MethodDelete:
operationID = item.Delete.OperationID
case http.MethodOptions:
operationID = item.Options.OperationID
case http.MethodHead:
operationID = item.Head.OperationID
case http.MethodPatch:
operationID = item.Patch.OperationID
case http.MethodTrace:
operationID = item.Trace.OperationID
}
l.Info().Str("operation_id", operationID).Msg("Operation ID")
h.ServeHTTP(w, r)
})
}
} Initialization is done like: router := chi.NewMux()
var api huma.API
router.Use(mw.OperationID(func() huma.API { return api }))
api = humachi.New(router, MyConfig()) |
I'm in the process of adding opentelemetry support to one of our project that uses huma.
As I want to set the
operationID
that was matched to this request as a span name/attribute I need to make that at the huma middleware. (An router-level middleware wouldn't have this information)In this middleware I need to crate a new opentelemetry span and then attach some attribute to it that describe the request.
This list of attribute is standardized. There are two attributes that are
required
by the standard and that can't be accessed from ahuma.Context
(unless I missed something)In other opentelemetry interceptors & middleware this information is extracted from the
http.Request
struct and specifically ther.TLS
,r.Proto
,r.ProtoMajor
andr.ProtoMinor
fields.The text was updated successfully, but these errors were encountered: