Skip to content

Commit

Permalink
refactor: simplify handler build functions
Browse files Browse the repository at this point in the history
  • Loading branch information
lidel committed Jun 25, 2024
1 parent ec34d2e commit 103335f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
15 changes: 4 additions & 11 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,8 @@ func setupGatewayHandler(cfg Config, nd *Node) (http.Handler, error) {
}
gwHandler := gateway.NewHandler(gwConf, backend)

var ipfsHandler, ipnsHandler http.Handler
if cfg.disableMetrics {
ipfsHandler = gwHandler
ipnsHandler = gwHandler
} else {
ipfsHandler = withHTTPMetrics(gwHandler, "ipfs")
ipnsHandler = withHTTPMetrics(gwHandler, "ipns")
}
ipfsHandler := withHTTPMetrics(gwHandler, "ipfs", cfg.disableMetrics)
ipnsHandler := withHTTPMetrics(gwHandler, "ipns", cfg.disableMetrics)

topMux := http.NewServeMux()
topMux.Handle("/ipfs/", ipfsHandler)
Expand Down Expand Up @@ -217,14 +211,13 @@ func setupGatewayHandler(cfg Config, nd *Node) (http.Handler, error) {
handler = withRequestLogger(handler)

// Add tracing.
handler = withTracingAndDebug(handler, cfg)
handler = withTracingAndDebug(handler, cfg.TracingAuthToken)

return handler, nil
}

func withTracingAndDebug(next http.Handler, cfg Config) http.Handler {
func withTracingAndDebug(next http.Handler, authToken string) http.Handler {
next = otelhttp.NewHandler(next, "Gateway")
authToken := cfg.TracingAuthToken

// Remove tracing and cache skipping headers if not authorized
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
Expand Down
5 changes: 4 additions & 1 deletion metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ var defaultDurationHistogramBuckets = []float64{0.05, 0.1, 0.25, 0.5, 1, 2, 5, 1

// withHTTPMetrics collects metrics around HTTP request/response count, duration, and size
// per specific handler. Allows us to track them separately for /ipns and /ipfs.
func withHTTPMetrics(handler http.Handler, handlerName string) http.Handler {
func withHTTPMetrics(handler http.Handler, handlerName string, disableMetrics bool) http.Handler {
if disableMetrics {
return handler
}

opts := prometheus.HistogramOpts{
Namespace: "ipfs",
Expand Down

0 comments on commit 103335f

Please sign in to comment.