From bbf77baec3ddacb90a3e4eba6a84dbde38b87444 Mon Sep 17 00:00:00 2001 From: VnPower Date: Wed, 2 Oct 2024 19:31:59 +0700 Subject: [PATCH] Fix responses logging without any content --- core/requests.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/core/requests.go b/core/requests.go index 835e850..53edd6f 100644 --- a/core/requests.go +++ b/core/requests.go @@ -67,17 +67,25 @@ func retryRequest(ctx context.Context, reqFunc func(context.Context, string) (*r return nil, err } + start := time.Now() resp, err := retryClient.Do(req) end := time.Now() + // Unwrap the body here so that we could log stuff correctly + defer resp.Body.Close() + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, err + } + audit.LogAPIRoundTrip(audit.APIRequestSpan{ RequestId: request_context.GetFromContext(ctx).RequestId, Response: resp, Error: err, Method: req.Method, Token: tokenValue, - Body: "", + Body: string(body), StartTime: start, EndTime: end, }) @@ -86,11 +94,6 @@ func retryRequest(ctx context.Context, reqFunc func(context.Context, string) (*r if userToken == "" && !isPost { tokenManager.MarkTokenStatus(token, token_manager.Good) } - defer resp.Body.Close() - body, err := io.ReadAll(resp.Body) - if err != nil { - return nil, err - } return &SimpleHTTPResponse{ StatusCode: resp.StatusCode, Body: string(body),