66 "net/http"
77 "time"
88
9+ "github.com/thushan/olla/internal/app/middleware"
910 "github.com/thushan/olla/internal/core/constants"
1011 "github.com/thushan/olla/internal/core/domain"
1112 "github.com/thushan/olla/internal/core/ports"
@@ -14,16 +15,17 @@ import (
1415)
1516
1617type proxyRequest struct {
17- stats * ports.RequestStats
1818 requestLogger logger.StyledLogger
19+ stats * ports.RequestStats
20+ profile * domain.RequestProfile
1921 clientIP string
2022 targetPath string
21- profile * domain.RequestProfile
2223 model string
2324 contentType string
2425 method string
2526 path string
2627 query string
28+ userAgent string
2729 contentLength int64
2830}
2931
@@ -52,8 +54,19 @@ func (a *Application) proxyHandler(w http.ResponseWriter, r *http.Request) {
5254}
5355
5456func (a * Application ) initializeProxyRequest (r * http.Request ) * proxyRequest {
57+ // get the requestID from the middleware context first
58+ requestID := ""
59+ if id , ok := r .Context ().Value (middleware .RequestIDKey ).(string ); ok {
60+ requestID = id
61+ }
62+
63+ // fallback to generating a new one otherwise
64+ if requestID == "" {
65+ requestID = util .GenerateRequestID ()
66+ }
67+
5568 stats := & ports.RequestStats {
56- RequestID : util . GenerateRequestID () ,
69+ RequestID : requestID ,
5770 StartTime : time .Now (),
5871 }
5972
@@ -65,6 +78,7 @@ func (a *Application) initializeProxyRequest(r *http.Request) *proxyRequest {
6578 path : r .URL .Path ,
6679 query : r .URL .RawQuery ,
6780 contentLength : r .ContentLength ,
81+ userAgent : r .UserAgent (),
6882 }
6983}
7084
@@ -121,34 +135,69 @@ func (a *Application) executeProxyRequest(ctx context.Context, w http.ResponseWr
121135}
122136
123137func (a * Application ) logRequestStart (pr * proxyRequest , endpointCount int ) {
138+ // Log essential operational info at INFO level
124139 logFields := []any {
125140 "client_ip" , pr .clientIP ,
126141 "method" , pr .method ,
127142 "path" , pr .path ,
128- "target_path" , pr .targetPath ,
129143 "compatible_endpoints" , endpointCount ,
130- "path_resolution_ms" , pr .stats .PathResolutionMs ,
131- "query" , pr .query ,
132- "content_type" , pr .contentType ,
133- "content_length" , pr .contentLength ,
134144 }
135145
146+ // Add user agent if present
147+ if pr .userAgent != "" {
148+ logFields = append (logFields , "user_agent" , pr .userAgent )
149+ }
150+
151+ // Add model if identified
136152 if pr .model != "" {
137153 logFields = append (logFields , "model" , pr .model )
138154 }
139155
140- pr .requestLogger .Info ("Request started" , logFields ... )
156+ // Add content length if it's a POST/PUT with body
157+ if pr .contentLength > 0 {
158+ logFields = append (logFields , "content_length" , pr .contentLength )
159+ }
160+
161+ pr .requestLogger .Info ("Request received" , logFields ... )
162+
163+ // Log additional details at DEBUG level
164+ debugFields := []any {
165+ "target_path" , pr .targetPath ,
166+ "path_resolution_ms" , pr .stats .PathResolutionMs ,
167+ "query" , pr .query ,
168+ "content_type" , pr .contentType ,
169+ }
170+
171+ pr .requestLogger .Debug ("Request details" , debugFields ... )
141172}
142173
143174func (a * Application ) logRequestResult (pr * proxyRequest , err error ) {
144175 duration := time .Since (pr .stats .StartTime )
145176
146- logFields := a .buildLogFields (pr , duration )
147-
148177 if err != nil {
178+ logFields := a .buildLogFields (pr , duration )
149179 pr .requestLogger .Error ("Request failed" , append ([]any {"error" , err }, logFields ... )... )
150180 } else {
151- pr .requestLogger .Info ("Request completed" , logFields ... )
181+ // Log essential completion info at INFO level
182+ infoFields := []any {
183+ "endpoint" , pr .stats .EndpointName ,
184+ "duration_ms" , duration .Milliseconds (),
185+ "status" , "completed" ,
186+ }
187+
188+ if pr .model != "" {
189+ infoFields = append (infoFields , "model" , pr .model )
190+ }
191+
192+ if pr .stats .TotalBytes > 0 {
193+ infoFields = append (infoFields , "total_bytes" , pr .stats .TotalBytes )
194+ }
195+
196+ pr .requestLogger .Info ("Request completed" , infoFields ... )
197+
198+ // Log detailed metrics at DEBUG level
199+ debugFields := a .buildLogFields (pr , duration )
200+ pr .requestLogger .Debug ("Request metrics" , debugFields ... )
152201 }
153202}
154203
0 commit comments