@@ -20,10 +20,11 @@ import (
2020)
2121
2222const (
23- apiAttemptsKey = "API_UPSTREAM_ATTEMPTS"
24- apiRequestKey = "API_REQUEST"
25- apiResponseKey = "API_RESPONSE"
26- apiWebsocketTimelineKey = "API_WEBSOCKET_TIMELINE"
23+ apiAttemptsKey = "API_UPSTREAM_ATTEMPTS"
24+ apiRequestKey = "API_REQUEST"
25+ apiResponseKey = "API_RESPONSE"
26+ apiWebsocketTimelineKey = "API_WEBSOCKET_TIMELINE"
27+ apiAttemptResponseTimestampKey = "API_ATTEMPT_RESPONSE_TIMESTAMP"
2728)
2829
2930// UpstreamRequestLog captures the outbound upstream request details for logging.
@@ -54,10 +55,13 @@ type upstreamAttempt struct {
5455
5556// RecordAPIRequest stores the upstream request metadata in Gin context for request logging.
5657func RecordAPIRequest (ctx context.Context , cfg * config.Config , info UpstreamRequestLog ) {
58+ ginCtx := ginContextFrom (ctx )
59+ if ginCtx != nil {
60+ ginCtx .Set (apiAttemptResponseTimestampKey , time.Time {})
61+ }
5762 if cfg == nil || ! cfg .RequestLog {
5863 return
5964 }
60- ginCtx := ginContextFrom (ctx )
6165 if ginCtx == nil {
6266 return
6367 }
@@ -206,10 +210,13 @@ func AppendAPIResponseChunk(ctx context.Context, cfg *config.Config, chunk []byt
206210
207211// RecordAPIWebsocketRequest stores an upstream websocket request event in Gin context.
208212func RecordAPIWebsocketRequest (ctx context.Context , cfg * config.Config , info UpstreamRequestLog ) {
213+ ginCtx := ginContextFrom (ctx )
214+ if ginCtx != nil {
215+ ginCtx .Set (apiAttemptResponseTimestampKey , time.Time {})
216+ }
209217 if cfg == nil || ! cfg .RequestLog {
210218 return
211219 }
212- ginCtx := ginContextFrom (ctx )
213220 if ginCtx == nil {
214221 return
215222 }
@@ -451,10 +458,16 @@ func markAPIResponseTimestamp(ginCtx *gin.Context) {
451458 if ginCtx == nil {
452459 return
453460 }
461+ now := time .Now ()
462+ if value , exists := ginCtx .Get (apiAttemptResponseTimestampKey ); ! exists {
463+ ginCtx .Set (apiAttemptResponseTimestampKey , now )
464+ } else if timestamp , ok := value .(time.Time ); ! ok || timestamp .IsZero () {
465+ ginCtx .Set (apiAttemptResponseTimestampKey , now )
466+ }
454467 if _ , exists := ginCtx .Get ("API_RESPONSE_TIMESTAMP" ); exists {
455468 return
456469 }
457- ginCtx .Set ("API_RESPONSE_TIMESTAMP" , time . Now () )
470+ ginCtx .Set ("API_RESPONSE_TIMESTAMP" , now )
458471}
459472
460473func writeHeaders (builder * strings.Builder , headers http.Header ) {
0 commit comments