Skip to content

Commit edb32ac

Browse files
author
Manuel Alonso
authored
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
1 parent 0216bae commit edb32ac

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

core/corehttp/gateway_handler.go

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ func (sw *statusResponseWriter) WriteHeader(code int) {
8282
redirect := sw.ResponseWriter.Header().Get("Location")
8383
if redirect != "" && code == http.StatusOK {
8484
code = http.StatusMovedPermanently
85+
log.Debugw("subdomain redirect", "location", redirect, "status", code)
8586
}
8687
sw.ResponseWriter.WriteHeader(code)
8788
}
@@ -198,6 +199,9 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
198199
urlPath := r.URL.Path
199200
escapedURLPath := r.URL.EscapedPath()
200201

202+
logger := log.With("from", r.RequestURI)
203+
logger.Debug("http request received")
204+
201205
// If the gateway is behind a reverse proxy and mounted at a sub-path,
202206
// the prefix header can be set to signal this sub-path.
203207
// 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
210214
break
211215
}
212216
}
217+
logger.Debugw("sub-path (deprecrated)", "prefix", prefix)
213218
}
214219

215220
// 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
242247
if u.RawQuery != "" { // preserve query if present
243248
path = path + "?" + u.RawQuery
244249
}
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)
246254
return
247255
}
248256

@@ -263,6 +271,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
263271
if prefix == "" && fixupSuperfluousNamespace(w, urlPath, r.URL.RawQuery) {
264272
// the error was due to redundant namespace, which we were able to fix
265273
// by returning error/redirect page, nothing left to do here
274+
logger.Debugw("redundant namespace; noop")
266275
return
267276
}
268277
// unable to fix path, returning error
@@ -279,6 +288,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
279288
return
280289
default:
281290
if i.servePretty404IfPresent(w, r, parsedPath) {
291+
logger.Debugw("serve pretty 404 if present")
282292
return
283293
}
284294

@@ -345,6 +355,8 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
345355
} else {
346356
name = getFilename(urlPath)
347357
}
358+
359+
logger.Debugw("serving file", "name", name)
348360
i.serveFile(w, r, name, modtime, f)
349361
return
350362
}
@@ -354,7 +366,8 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
354366
return
355367
}
356368

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)
358371
switch err.(type) {
359372
case nil:
360373
dirwithoutslash := urlPath[len(urlPath)-1] != '/'
@@ -366,7 +379,10 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
366379
// preserve query parameters
367380
suffix = suffix + "?" + r.URL.RawQuery
368381
}
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)
370386
return
371387
}
372388

@@ -376,11 +392,12 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
376392
return
377393
}
378394

395+
logger.Debugw("serving index.html file", "path", idxPath)
379396
// write to request
380397
i.serveFile(w, r, "index.html", modtime, f)
381398
return
382399
case resolver.ErrNoLink:
383-
// no index.html; noop
400+
logger.Debugw("no index.html; noop", "path", idxPath)
384401
default:
385402
internalWebError(w, err)
386403
return
@@ -391,6 +408,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
391408
// Note: this needs to occur before listingTemplate.Execute otherwise we get
392409
// superfluous response.WriteHeader call from prometheus/client_golang
393410
if w.Header().Get("Location") != "" {
411+
logger.Debugw("location moved permanently", "status", http.StatusMovedPermanently)
394412
w.WriteHeader(http.StatusMovedPermanently)
395413
return
396414
}
@@ -399,6 +417,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
399417
// type instead of relying on autodetection (which may fail).
400418
w.Header().Set("Content-Type", "text/html")
401419
if r.Method == http.MethodHead {
420+
logger.Debug("return as request's HTTP method is HEAD")
402421
return
403422
}
404423

@@ -490,8 +509,9 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
490509
Hash: hash,
491510
}
492511

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 {
495515
internalWebError(w, err)
496516
return
497517
}
@@ -568,7 +588,7 @@ func (i *gatewayHandler) servePretty404IfPresent(w http.ResponseWriter, r *http.
568588
return false
569589
}
570590

571-
log.Debugf("using pretty 404 file for %s", parsedPath.String())
591+
log.Debugw("using pretty 404 file", "path", parsedPath)
572592
w.Header().Set("Content-Type", ctype)
573593
w.Header().Set("Content-Length", strconv.FormatInt(size, 10))
574594
w.WriteHeader(http.StatusNotFound)
@@ -585,6 +605,7 @@ func (i *gatewayHandler) postHandler(w http.ResponseWriter, r *http.Request) {
585605

586606
i.addUserHeaders(w) // ok, _now_ write user's headers.
587607
w.Header().Set("IPFS-Hash", p.Cid().String())
608+
log.Debugw("CID created, http redirect", "from", r.URL, "to", p, "status", http.StatusCreated)
588609
http.Redirect(w, r, p.String(), http.StatusCreated)
589610
}
590611

@@ -677,7 +698,10 @@ func (i *gatewayHandler) putHandler(w http.ResponseWriter, r *http.Request) {
677698

678699
i.addUserHeaders(w) // ok, _now_ write user's headers.
679700
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)
681705
}
682706

683707
func (i *gatewayHandler) deleteHandler(w http.ResponseWriter, r *http.Request) {
@@ -748,8 +772,11 @@ func (i *gatewayHandler) deleteHandler(w http.ResponseWriter, r *http.Request) {
748772

749773
i.addUserHeaders(w) // ok, _now_ write user's headers.
750774
w.Header().Set("IPFS-Hash", ncid.String())
775+
776+
redirectURL := gopath.Join(ipfsPathPrefix+ncid.String(), directory)
751777
// 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)
753780
}
754781

755782
func (i *gatewayHandler) addUserHeaders(w http.ResponseWriter) {

docs/gateway.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ The gateway's configuration options are (briefly) described in the
1616
[config](https://github.com/ipfs/go-ipfs/blob/master/docs/config.md#gateway)
1717
documentation.
1818

19+
### Debug
20+
The gateway's log level can be changed with this command:
21+
```
22+
> ipfs log level core/server debug
23+
```
24+
1925
## Directories
2026

2127
For convenience, the gateway (mostly) acts like a normal web-server when serving

0 commit comments

Comments
 (0)