From e41bdef9779aae6bba3ac52b39f7dd545241e4ce Mon Sep 17 00:00:00 2001 From: df-wg Date: Mon, 16 Dec 2024 09:24:08 +0200 Subject: [PATCH] fix: set request in hook context before send request, in case of error (#1016) This will be added to https://github.com/wundergraph/cosmo/pull/1445, to ensure that it's more resilient Previously, we weren't setting the request (even when we really should have had access), because in error cases we were exiting before setting the request/response context. This ensures that we can pass the correct information to the access logs --- .../engine/datasource/httpclient/nethttpclient.go | 14 +++++++++++++- v2/pkg/engine/resolve/loader.go | 13 ++++++++++--- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/v2/pkg/engine/datasource/httpclient/nethttpclient.go b/v2/pkg/engine/datasource/httpclient/nethttpclient.go index 64761d728..51ff7bd23 100644 --- a/v2/pkg/engine/datasource/httpclient/nethttpclient.go +++ b/v2/pkg/engine/datasource/httpclient/nethttpclient.go @@ -79,9 +79,19 @@ func InjectResponseContext(ctx context.Context) (context.Context, *ResponseConte return context.WithValue(ctx, responseContextKey{}, value), value } +func setRequest(ctx context.Context, request *http.Request) { + if value, ok := ctx.Value(responseContextKey{}).(*ResponseContext); ok { + value.Request = request + } +} + func setResponseStatus(ctx context.Context, request *http.Request, response *http.Response) { if value, ok := ctx.Value(responseContextKey{}).(*ResponseContext); ok { - value.StatusCode = response.StatusCode + if response != nil { + value.StatusCode = response.StatusCode + } else { + value.StatusCode = 0 + } value.Request = request value.Response = response } @@ -189,6 +199,8 @@ func makeHTTPRequest(client *http.Client, ctx context.Context, url, method, head request.Header.Set(AcceptEncodingHeader, EncodingGzip) request.Header.Add(AcceptEncodingHeader, EncodingDeflate) + setRequest(ctx, request) + response, err := client.Do(request) if err != nil { return err diff --git a/v2/pkg/engine/resolve/loader.go b/v2/pkg/engine/resolve/loader.go index f2545db97..9ca6f332e 100644 --- a/v2/pkg/engine/resolve/loader.go +++ b/v2/pkg/engine/resolve/loader.go @@ -62,9 +62,16 @@ func newResponseInfo(res *result, subgraphError error) *ResponseInfo { // We're using the response.Request here, because the body will be nil (since the response was read) and won't // cause a memory leak. if res.httpResponseContext.Response != nil { - responseInfo.Request = res.httpResponseContext.Response.Request - responseInfo.ResponseHeaders = res.httpResponseContext.Response.Header.Clone() - } else { + if res.httpResponseContext.Response.Request != nil { + responseInfo.Request = res.httpResponseContext.Response.Request + } + + if res.httpResponseContext.Response.Header != nil { + responseInfo.ResponseHeaders = res.httpResponseContext.Response.Header.Clone() + } + } + + if responseInfo.Request == nil { // In cases where the request errors, the response will be nil, and so we need to get the original request responseInfo.Request = res.httpResponseContext.Request }