@@ -101,10 +101,13 @@ func RecordAPIRequest(ctx context.Context, cfg *config.Config, info UpstreamRequ
101101
102102// RecordAPIResponseMetadata captures upstream response status/header information for the latest attempt.
103103func RecordAPIResponseMetadata (ctx context.Context , cfg * config.Config , status int , headers http.Header ) {
104+ ginCtx := ginContextFrom (ctx )
105+ if ginCtx != nil && ! isStreamingResponseHeaders (headers ) {
106+ markAPIResponseTimestamp (ginCtx )
107+ }
104108 if cfg == nil || ! cfg .RequestLog {
105109 return
106110 }
107- ginCtx := ginContextFrom (ctx )
108111 if ginCtx == nil {
109112 return
110113 }
@@ -125,7 +128,13 @@ func RecordAPIResponseMetadata(ctx context.Context, cfg *config.Config, status i
125128 updateAggregatedResponse (ginCtx , attempts )
126129}
127130
128- // RecordAPIResponseError adds an error entry for the latest attempt when no HTTP response is available.
131+ func isStreamingResponseHeaders (headers http.Header ) bool {
132+ if len (headers ) == 0 {
133+ return false
134+ }
135+ return strings .Contains (strings .ToLower (strings .TrimSpace (headers .Get ("Content-Type" ))), "text/event-stream" )
136+ }
137+
129138func RecordAPIResponseError (ctx context.Context , cfg * config.Config , err error ) {
130139 if cfg == nil || ! cfg .RequestLog || err == nil {
131140 return
@@ -152,14 +161,17 @@ func RecordAPIResponseError(ctx context.Context, cfg *config.Config, err error)
152161
153162// AppendAPIResponseChunk appends an upstream response chunk to Gin context for request logging.
154163func AppendAPIResponseChunk (ctx context.Context , cfg * config.Config , chunk []byte ) {
155- if cfg == nil || ! cfg .RequestLog {
156- return
157- }
158164 data := bytes .TrimSpace (chunk )
159165 if len (data ) == 0 {
160166 return
161167 }
162168 ginCtx := ginContextFrom (ctx )
169+ if ginCtx != nil {
170+ markAPIResponseTimestamp (ginCtx )
171+ }
172+ if cfg == nil || ! cfg .RequestLog {
173+ return
174+ }
163175 if ginCtx == nil {
164176 return
165177 }
@@ -283,18 +295,20 @@ func WebsocketUpgradeRequestURL(rawURL string) string {
283295
284296// AppendAPIWebsocketResponse stores an upstream websocket response frame in Gin context.
285297func AppendAPIWebsocketResponse (ctx context.Context , cfg * config.Config , payload []byte ) {
286- if cfg == nil || ! cfg .RequestLog {
287- return
288- }
289298 data := bytes .TrimSpace (payload )
290299 if len (data ) == 0 {
291300 return
292301 }
293302 ginCtx := ginContextFrom (ctx )
303+ if ginCtx != nil {
304+ markAPIResponseTimestamp (ginCtx )
305+ }
306+ if cfg == nil || ! cfg .RequestLog {
307+ return
308+ }
294309 if ginCtx == nil {
295310 return
296311 }
297- markAPIResponseTimestamp (ginCtx )
298312
299313 builder := & strings.Builder {}
300314 builder .WriteString (fmt .Sprintf ("Timestamp: %s\n " , time .Now ().Format (time .RFC3339Nano )))
@@ -307,14 +321,16 @@ func AppendAPIWebsocketResponse(ctx context.Context, cfg *config.Config, payload
307321
308322// RecordAPIWebsocketError stores an upstream websocket error event in Gin context.
309323func RecordAPIWebsocketError (ctx context.Context , cfg * config.Config , stage string , err error ) {
324+ ginCtx := ginContextFrom (ctx )
325+ if ginCtx != nil {
326+ markAPIResponseTimestamp (ginCtx )
327+ }
310328 if cfg == nil || ! cfg .RequestLog || err == nil {
311329 return
312330 }
313- ginCtx := ginContextFrom (ctx )
314331 if ginCtx == nil {
315332 return
316333 }
317- markAPIResponseTimestamp (ginCtx )
318334
319335 builder := & strings.Builder {}
320336 builder .WriteString (fmt .Sprintf ("Timestamp: %s\n " , time .Now ().Format (time .RFC3339Nano )))
@@ -328,6 +344,9 @@ func RecordAPIWebsocketError(ctx context.Context, cfg *config.Config, stage stri
328344}
329345
330346func ginContextFrom (ctx context.Context ) * gin.Context {
347+ if ctx == nil {
348+ return nil
349+ }
331350 ginCtx , _ := ctx .Value ("gin" ).(* gin.Context )
332351 return ginCtx
333352}
0 commit comments