Skip to content

Commit 7ce0424

Browse files
committed
lib/revproxy: add debug logging for the reverse proxy
1 parent 0f35a80 commit 7ce0424

File tree

2 files changed

+52
-6
lines changed

2 files changed

+52
-6
lines changed

cmd/go-cache-plugin/setup.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ func initModProxy(env *command.Env, s3c *s3util.Client) (_ http.Handler, cleanup
110110
S3Client: s3c,
111111
KeyPrefix: path.Join(flags.KeyPrefix, "module"),
112112
MaxTasks: flags.S3Concurrency,
113-
LogRequests: flags.DebugLog,
114113
Logf: vprintf,
114+
LogRequests: flags.DebugLog,
115115
}
116116
cleanup = func() { vprintf("close cacher (err=%v)", cacher.Close()) }
117117
proxy := &goproxy.Goproxy{
@@ -158,11 +158,12 @@ func initRevProxy(env *command.Env, s3c *s3util.Client, g *taskgroup.Group) (htt
158158
}
159159

160160
proxy := &revproxy.Server{
161-
Targets: hosts,
162-
Local: revCachePath,
163-
S3Client: s3c,
164-
KeyPrefix: path.Join(flags.KeyPrefix, "revproxy"),
165-
Logf: vprintf,
161+
Targets: hosts,
162+
Local: revCachePath,
163+
S3Client: s3c,
164+
KeyPrefix: path.Join(flags.KeyPrefix, "revproxy"),
165+
Logf: vprintf,
166+
LogRequests: flags.DebugLog,
166167
}
167168
bridge := &proxyconn.Bridge{
168169
Addrs: hosts,

lib/revproxy/revproxy.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,36 @@ type Server struct {
8787
// discarded.
8888
Logf func(string, ...any)
8989

90+
// LogRequests, if true, enables detailed (but noisy) debug logging of all
91+
// requests handled by the reverse proxy. Logs are written to Logf.
92+
//
93+
// Each request is presented in the format:
94+
//
95+
// B U:"<url>" H:<digest> C:<bool>
96+
// E H:<digest> <disposition> B:<bytes> (<time> elapsed)
97+
// - H:<digest> miss
98+
//
99+
// The "B" line is when the request began, and "E" when it was finished.
100+
// The abbreviated fields are:
101+
//
102+
// U: -- request URL
103+
// H: -- request URL digest (cache key)
104+
// C: -- whether the request is cacheable (true/false)
105+
// B: -- body size in bytes (for hits)
106+
//
107+
// The dispositions of a request are:
108+
//
109+
// hit mem -- cache hit in memory (volatile)
110+
// hit disk -- cache hit in local disk
111+
// hit S3 -- cache hit in S3 (faulted to disk)
112+
// fetch -- fetched from the origin server
113+
//
114+
// On fetches, the "RC" tag indicates whether the response is cacheable,
115+
// with "no" meaning it was not cached at all, "mem" meaning it was cached
116+
// as a short-lived volatile response in memory, and "yes" meaning it was
117+
// cached on disk (and S3).
118+
LogRequests bool
119+
90120
initOnce sync.Once
91121
tasks *taskgroup.Group
92122
start func(taskgroup.Task) *taskgroup.Group
@@ -157,12 +187,15 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
157187

158188
hash := hashRequestURL(r.URL)
159189
canCache := s.canCacheRequest(r)
190+
s.vlogf("B U:%q H:%s C:%v", r.URL, hash, canCache)
191+
start := time.Now()
160192
if canCache {
161193
// Check for a hit on this object in the memory cache.
162194
if data, hdr, err := s.cacheLoadMemory(hash); err == nil {
163195
s.reqMemoryHit.Add(1)
164196
setXCacheInfo(hdr, "hit, memory", hash)
165197
writeCachedResponse(w, hdr, data)
198+
s.vlogf("E H:%s hit mem B:%d (%v elapsed)", hash, len(data), time.Since(start))
166199
return
167200
}
168201

@@ -171,6 +204,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
171204
s.reqLocalHit.Add(1)
172205
setXCacheInfo(hdr, "hit, local", hash)
173206
writeCachedResponse(w, hdr, data)
207+
s.vlogf("E H:%s hit disk B:%d (%v elapsed)", hash, len(data), time.Since(start))
174208
return
175209
}
176210
s.reqLocalMiss.Add(1)
@@ -183,9 +217,11 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
183217
}
184218
setXCacheInfo(hdr, "hit, remote", hash)
185219
writeCachedResponse(w, hdr, data)
220+
s.vlogf("E H:%s hit S3 B:%d (%v elapsed)", hash, len(data), time.Since(start))
186221
return
187222
}
188223
s.reqFaultMiss.Add(1)
224+
s.vlogf("- H:%s miss", hash)
189225
}
190226

191227
// Reaching here, the object is not already cached locally so we have to
@@ -202,6 +238,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
202238
// A response we cannot cache at all.
203239
setXCacheInfo(rsp.Header, "fetch, uncached", "")
204240
s.rspNotCached.Add(1)
241+
s.vlogf("E H:%s fetch RC:no (%v elapsed)", hash, time.Since(start))
205242
return nil
206243
}
207244

@@ -220,6 +257,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
220257
s.rspSaveMem.Add(1)
221258

222259
// N.B. Don't persist on disk or in S3.
260+
s.vlogf("E H:%s fetch RC:mem B:%d (%v elapsed)", hash, len(body), time.Since(start))
223261
}
224262
} else {
225263
setXCacheInfo(rsp.Header, "fetch, cached", hash)
@@ -235,6 +273,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
235273
s.rspSaveBytes.Add(int64(len(body)))
236274
s.start(s.cacheStoreS3(hash, rsp.Header, body))
237275
}
276+
s.vlogf("E H:%s fetch RC:yes B:%d (%v elapsed)", hash, len(body), time.Since(start))
238277
}
239278
}
240279
return nil
@@ -272,6 +311,12 @@ func (s *Server) logf(msg string, args ...any) {
272311
}
273312
}
274313

314+
func (s *Server) vlogf(msg string, args ...any) {
315+
if s.LogRequests {
316+
s.logf(msg, args...)
317+
}
318+
}
319+
275320
func hostMatchesTarget(host string, targets []string) bool {
276321
return slices.Contains(targets, host)
277322
}

0 commit comments

Comments
 (0)