@@ -89,6 +89,14 @@ func (p *TavilyProxy) Do(ctx context.Context, req ProxyRequest) (ProxyResponse,
8989 const maxLogBytes = 32 * 1024
9090
9191 proxyReqID := uuid .NewString ()
92+ startTime := time .Now ()
93+
94+ p .logger .Info ("proxy request started" ,
95+ "request_id" , proxyReqID ,
96+ "method" , req .Method ,
97+ "path" , req .Path ,
98+ "client_ip" , req .ClientIP ,
99+ )
92100
93101 loggingEnabled := p .logs != nil && p .isRequestLoggingEnabled (ctx )
94102 captureBodies := strings .EqualFold (req .Method , http .MethodPost ) && req .Path == "/search"
@@ -148,6 +156,10 @@ func (p *TavilyProxy) Do(ctx context.Context, req ProxyRequest) (ProxyResponse,
148156 }
149157
150158 if len (candidates ) == 0 {
159+ p .logger .Warn ("no available keys" ,
160+ "request_id" , proxyReqID ,
161+ "path" , req .Path ,
162+ )
151163 if captureBodies {
152164 createdAt := time .Now ()
153165 if loggingEnabled {
@@ -178,15 +190,32 @@ func (p *TavilyProxy) Do(ctx context.Context, req ProxyRequest) (ProxyResponse,
178190 resp , status , latencyMs , tavilyReqID , err := p .tryKey (ctx , key .ID , key .Key , req , proxyReqID )
179191
180192 if err != nil {
193+ p .logger .Warn ("upstream request failed" ,
194+ "request_id" , proxyReqID ,
195+ "key_id" , key .ID ,
196+ "key_alias" , key .Alias ,
197+ "err" , err ,
198+ )
181199 lastErr = err
182200 continue
183201 }
184202
185203 switch status {
186204 case http .StatusUnauthorized :
205+ p .logger .Warn ("key marked invalid" ,
206+ "request_id" , proxyReqID ,
207+ "key_id" , key .ID ,
208+ "key_alias" , key .Alias ,
209+ )
187210 _ = p .keys .MarkInvalid (ctx , key .ID )
188211 continue
189212 case http .StatusTooManyRequests , 432 , 433 :
213+ p .logger .Warn ("key quota exhausted" ,
214+ "request_id" , proxyReqID ,
215+ "key_id" , key .ID ,
216+ "key_alias" , key .Alias ,
217+ "status" , status ,
218+ )
190219 _ = p .keys .MarkExhausted (ctx , key .ID )
191220 continue
192221 }
@@ -241,10 +270,21 @@ func (p *TavilyProxy) Do(ctx context.Context, req ProxyRequest) (ProxyResponse,
241270
242271 resp .ProxyRequestID = proxyReqID
243272 resp .TavilyRequestID = tavilyReqID
273+ p .logger .Info ("proxy request completed" ,
274+ "request_id" , proxyReqID ,
275+ "key_id" , key .ID ,
276+ "status" , status ,
277+ "latency_ms" , time .Since (startTime ).Milliseconds (),
278+ )
244279 return resp , nil
245280 }
246281
247282 if captureBodies && lastErr != nil {
283+ p .logger .Error ("all keys exhausted" ,
284+ "request_id" , proxyReqID ,
285+ "path" , req .Path ,
286+ "last_err" , lastErr ,
287+ )
248288 createdAt := time .Now ()
249289 if loggingEnabled {
250290 _ = p .logs .Create (ctx , & models.RequestLog {
@@ -265,6 +305,11 @@ func (p *TavilyProxy) Do(ctx context.Context, req ProxyRequest) (ProxyResponse,
265305 if p .stats != nil {
266306 _ = p .stats .RecordRequest (ctx , req .Path , createdAt )
267307 }
308+ } else if lastErr == nil {
309+ p .logger .Error ("all keys exhausted" ,
310+ "request_id" , proxyReqID ,
311+ "path" , req .Path ,
312+ )
268313 }
269314
270315 return ProxyResponse {}, ErrNoAvailableKeys
@@ -278,12 +323,12 @@ func truncateForLog(data []byte, maxBytes int) (string, bool) {
278323}
279324
280325func (p * TavilyProxy ) tryKey (ctx context.Context , keyID uint , tavilyKey string , req ProxyRequest , proxyReqID string ) (ProxyResponse , int , int64 , string , error ) {
281- url := p .baseURL + req .Path
326+ targetURL := p .baseURL + req .Path
282327 if req .RawQuery != "" {
283- url += "?" + req .RawQuery
328+ targetURL += "?" + req .RawQuery
284329 }
285330
286- upstreamReq , err := http .NewRequestWithContext (ctx , req .Method , url , bytes .NewReader (req .Body ))
331+ upstreamReq , err := http .NewRequestWithContext (ctx , req .Method , targetURL , bytes .NewReader (req .Body ))
287332 if err != nil {
288333 return ProxyResponse {}, 0 , 0 , "" , err
289334 }
@@ -296,16 +341,43 @@ func (p *TavilyProxy) tryKey(ctx context.Context, keyID uint, tavilyKey string,
296341 }
297342 upstreamReq .Header .Set ("X-Proxy-Request-Id" , proxyReqID )
298343
344+ p .logger .Debug ("sending upstream request" ,
345+ "request_id" , proxyReqID ,
346+ "method" , req .Method ,
347+ "url" , targetURL ,
348+ "key_id" , keyID ,
349+ )
350+
299351 start := time .Now ()
300352 upstreamResp , err := p .client .Do (upstreamReq )
301353 latencyMs := time .Since (start ).Milliseconds ()
302354 if err != nil {
355+ p .logger .Warn ("upstream call error" ,
356+ "request_id" , proxyReqID ,
357+ "url" , targetURL ,
358+ "key_id" , keyID ,
359+ "latency_ms" , latencyMs ,
360+ "err" , err ,
361+ )
303362 return ProxyResponse {}, 0 , latencyMs , "" , err
304363 }
305364 defer upstreamResp .Body .Close ()
306365
366+ p .logger .Debug ("upstream response received" ,
367+ "request_id" , proxyReqID ,
368+ "status" , upstreamResp .StatusCode ,
369+ "latency_ms" , latencyMs ,
370+ "key_id" , keyID ,
371+ )
372+
307373 body , err := io .ReadAll (upstreamResp .Body )
308374 if err != nil {
375+ p .logger .Warn ("upstream response read error" ,
376+ "request_id" , proxyReqID ,
377+ "key_id" , keyID ,
378+ "status" , upstreamResp .StatusCode ,
379+ "err" , err ,
380+ )
309381 return ProxyResponse {}, upstreamResp .StatusCode , latencyMs , "" , err
310382 }
311383
@@ -396,6 +468,7 @@ func (p *TavilyProxy) GetUsage(ctx context.Context, tavilyKey string) (int, *int
396468
397469 resp , err := p .client .Do (req )
398470 if err != nil {
471+ p .logger .Warn ("upstream usage request failed" , "err" , err )
399472 return 0 , nil , err
400473 }
401474 defer resp .Body .Close ()
0 commit comments