From 1a97f42cd8120f65bfc1303a783d327ac6222464 Mon Sep 17 00:00:00 2001 From: sneak Date: Thu, 8 Jan 2026 05:04:08 -0800 Subject: [PATCH] Add detailed logging for image requests with cache status and timing --- internal/handlers/image.go | 15 +++++++++++++++ internal/imgcache/imgcache.go | 2 ++ internal/imgcache/service.go | 1 + 3 files changed, 18 insertions(+) diff --git a/internal/handlers/image.go b/internal/handlers/image.go index 08a9076..89960e3 100644 --- a/internal/handlers/image.go +++ b/internal/handlers/image.go @@ -78,7 +78,11 @@ func (s *Handlers) HandleImage() http.HandlerFunc { return } + // Get cache key for logging + cacheKey := imgcache.CacheKey(req) + // Get the image (from cache or fetch/process) + startTime := time.Now() resp, err := s.imgSvc.Get(ctx, req) if err != nil { s.log.Error("failed to get image", @@ -120,6 +124,17 @@ func (s *Handlers) HandleImage() http.HandlerFunc { w.Header().Set("ETag", resp.ETag) } + // Log cache status and timing + duration := time.Since(startTime) + s.log.Info("image served", + "cache_key", cacheKey, + "cache_status", resp.CacheStatus, + "duration_ms", duration.Milliseconds(), + "format", req.Format, + "served_bytes", resp.ContentLength, + "fetched_bytes", resp.FetchedBytes, + ) + // Stream the response w.WriteHeader(http.StatusOK) diff --git a/internal/imgcache/imgcache.go b/internal/imgcache/imgcache.go index 7e78f17..8a55723 100644 --- a/internal/imgcache/imgcache.go +++ b/internal/imgcache/imgcache.go @@ -90,6 +90,8 @@ type ImageResponse struct { LastModified time.Time // CacheStatus indicates HIT, MISS, or STALE CacheStatus CacheStatus + // FetchedBytes is the number of bytes fetched from upstream (0 if cache hit) + FetchedBytes int64 } // CacheStatus indicates whether the response was served from cache. diff --git a/internal/imgcache/service.go b/internal/imgcache/service.go index 6bc8641..283c3de 100644 --- a/internal/imgcache/service.go +++ b/internal/imgcache/service.go @@ -172,6 +172,7 @@ func (s *Service) fetchAndProcess(ctx context.Context, req *ImageRequest) (*Imag Content: io.NopCloser(bytes.NewReader(processedData)), ContentLength: int64(len(processedData)), ContentType: processResult.ContentType, + FetchedBytes: int64(len(sourceData)), }, nil }