chore(gateway): debug logging for the http requests (#8518)
* chore(gateway): better logging for the http requests * chore(gateway): removed defer and add more data to the final log * chore(gateway): debug logging refactor * chore(gateway): use debug w/o context when only msg * doc: add cmd for log level * chore: add more logs and address fedback * chore(gateway): log subdomains and from=requestURI, refactor * chore(gateway): fix debug redirect
Manuel Alonso committed
Feb 15, 2022 at 23:13 UTC
edb32ac3d743404118834f8c371a3fdf45c2ea66
2 files changed
+42
-9
core/corehttp/gateway_handler.go
+36
-9
@@ -82,6 +82,7 @@ func (sw *statusResponseWriter) WriteHeader(code int) {
82
redirect := sw.ResponseWriter.Header().Get("Location")
83
if redirect != "" && code == http.StatusOK {
84
code = http.StatusMovedPermanently
85
+ log.Debugw("subdomain redirect", "location", redirect, "status", code)
86
}
87
sw.ResponseWriter.WriteHeader(code)
88
}
@@ -198,6 +199,9 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
199
urlPath := r.URL.Path
200
escapedURLPath := r.URL.EscapedPath()
201
202
+ logger := log.With("from", r.RequestURI)
203
+ logger.Debug("http request received")
204
+
205
// If the gateway is behind a reverse proxy and mounted at a sub-path,
206
// the prefix header can be set to signal this sub-path.
207
// It will be prepended to links in directory listings and the index.html redirect.
@@ -210,6 +214,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
214
break
215
}
216
}
217
+ logger.Debugw("sub-path (deprecrated)", "prefix", prefix)
218
}
219
220
// HostnameOption might have constructed an IPNS/IPFS path using the Host header.
@@ -242,7 +247,10 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
247
if u.RawQuery != "" { // preserve query if present
248
path = path + "?" + u.RawQuery
249
}
245
- http.Redirect(w, r, gopath.Join("/", prefix, u.Scheme, u.Host, path), http.StatusMovedPermanently)
250
+
251
+ redirectURL := gopath.Join("/", prefix, u.Scheme, u.Host, path)
252
+ logger.Debugw("uri param, redirect", "to", redirectURL, "status", http.StatusMovedPermanently)
253
+ http.Redirect(w, r, redirectURL, http.StatusMovedPermanently)
254
return
255
}
256
@@ -263,6 +271,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
271
if prefix == "" && fixupSuperfluousNamespace(w, urlPath, r.URL.RawQuery) {
272
// the error was due to redundant namespace, which we were able to fix
273
// by returning error/redirect page, nothing left to do here
274
+ logger.Debugw("redundant namespace; noop")
275
return
276
}
277
// unable to fix path, returning error
@@ -279,6 +288,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
288
return
289
default:
290
if i.servePretty404IfPresent(w, r, parsedPath) {
291
+ logger.Debugw("serve pretty 404 if present")
292
return
293
}
294
@@ -345,6 +355,8 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
355
} else {
356
name = getFilename(urlPath)
357
}
358
+
359
+ logger.Debugw("serving file", "name", name)
360
i.serveFile(w, r, name, modtime, f)
361
return
362
}
@@ -354,7 +366,8 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
366
return
367
}
368
357
- idx, err := i.api.Unixfs().Get(r.Context(), ipath.Join(resolvedPath, "index.html"))
369
+ idxPath := ipath.Join(resolvedPath, "index.html")
370
+ idx, err := i.api.Unixfs().Get(r.Context(), idxPath)
371
switch err.(type) {
372
case nil:
373
dirwithoutslash := urlPath[len(urlPath)-1] != '/'
@@ -366,7 +379,10 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
379
// preserve query parameters
380
suffix = suffix + "?" + r.URL.RawQuery
381
}
369
- http.Redirect(w, r, originalUrlPath+suffix, 302)
382
+
383
+ redirectURL := originalUrlPath + suffix
384
+ logger.Debugw("serving index.html file", "to", redirectURL, "status", http.StatusFound, "path", idxPath)
385
+ http.Redirect(w, r, redirectURL, http.StatusFound)
386
return
387
}
388
@@ -376,11 +392,12 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
392
return
393
}
394
395
+ logger.Debugw("serving index.html file", "path", idxPath)
396
// write to request
397
i.serveFile(w, r, "index.html", modtime, f)
398
return
399
case resolver.ErrNoLink:
383
- // no index.html; noop
400
+ logger.Debugw("no index.html; noop", "path", idxPath)
401
default:
402
internalWebError(w, err)
403
return
@@ -391,6 +408,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
408
// Note: this needs to occur before listingTemplate.Execute otherwise we get
409
// superfluous response.WriteHeader call from prometheus/client_golang
410
if w.Header().Get("Location") != "" {
411
+ logger.Debugw("location moved permanently", "status", http.StatusMovedPermanently)
412
w.WriteHeader(http.StatusMovedPermanently)
413
return
414
}
@@ -399,6 +417,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
417
// type instead of relying on autodetection (which may fail).
418
w.Header().Set("Content-Type", "text/html")
419
if r.Method == http.MethodHead {
420
+ logger.Debug("return as request's HTTP method is HEAD")
421
return
422
}
423
@@ -490,8 +509,9 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
509
Hash: hash,
510
}
511
493
- err = listingTemplate.Execute(w, tplData)
494
- if err != nil {
512
+ logger.Debugw("request processed", "tplDataDNSLink", dnslink, "tplDataSize", size, "tplDataBackLink", backLink, "tplDataHash", hash, "duration", time.Since(begin))
513
+
514
+ if err := listingTemplate.Execute(w, tplData); err != nil {
515
internalWebError(w, err)
516
return
517
}
@@ -568,7 +588,7 @@ func (i *gatewayHandler) servePretty404IfPresent(w http.ResponseWriter, r *http.
588
return false
589
}
590
571
- log.Debugf("using pretty 404 file for %s", parsedPath.String())
591
+ log.Debugw("using pretty 404 file", "path", parsedPath)
592
w.Header().Set("Content-Type", ctype)
593
w.Header().Set("Content-Length", strconv.FormatInt(size, 10))
594
w.WriteHeader(http.StatusNotFound)
@@ -585,6 +605,7 @@ func (i *gatewayHandler) postHandler(w http.ResponseWriter, r *http.Request) {
605
606
i.addUserHeaders(w) // ok, _now_ write user's headers.
607
w.Header().Set("IPFS-Hash", p.Cid().String())
608
+ log.Debugw("CID created, http redirect", "from", r.URL, "to", p, "status", http.StatusCreated)
609
http.Redirect(w, r, p.String(), http.StatusCreated)
610
}
611
@@ -677,7 +698,10 @@ func (i *gatewayHandler) putHandler(w http.ResponseWriter, r *http.Request) {
698
699
i.addUserHeaders(w) // ok, _now_ write user's headers.
700
w.Header().Set("IPFS-Hash", newcid.String())
680
- http.Redirect(w, r, gopath.Join(ipfsPathPrefix, newcid.String(), newPath), http.StatusCreated)
701
+
702
+ redirectURL := gopath.Join(ipfsPathPrefix, newcid.String(), newPath)
703
+ log.Debugw("CID replaced, redirect", "from", r.URL, "to", redirectURL, "status", http.StatusCreated)
704
+ http.Redirect(w, r, redirectURL, http.StatusCreated)
705
}
706
707
func (i *gatewayHandler) deleteHandler(w http.ResponseWriter, r *http.Request) {
@@ -748,8 +772,11 @@ func (i *gatewayHandler) deleteHandler(w http.ResponseWriter, r *http.Request) {
772
773
i.addUserHeaders(w) // ok, _now_ write user's headers.
774
w.Header().Set("IPFS-Hash", ncid.String())
775
+
776
+ redirectURL := gopath.Join(ipfsPathPrefix+ncid.String(), directory)
777
// note: StatusCreated is technically correct here as we created a new resource.
752
- http.Redirect(w, r, gopath.Join(ipfsPathPrefix+ncid.String(), directory), http.StatusCreated)
778
+ log.Debugw("CID deleted, redirect", "from", r.RequestURI, "to", redirectURL, "status", http.StatusCreated)
779
+ http.Redirect(w, r, redirectURL, http.StatusCreated)
780
}
781
782
func (i *gatewayHandler) addUserHeaders(w http.ResponseWriter) {
docs/gateway.md
+6
@@ -16,6 +16,12 @@ The gateway's configuration options are (briefly) described in the
16
[config](https://github.com/ipfs/go-ipfs/blob/master/docs/config.md#gateway)
17
documentation.
18
19
+### Debug
20
+The gateway's log level can be changed with this command:
21
+```
22
+> ipfs log level core/server debug
23
+```
24
+
25
## Directories
26
27
For convenience, the gateway (mostly) acts like a normal web-server when serving