Skip to content

Commit

Permalink
Add: HTTP Request ID Middleware
Browse files Browse the repository at this point in the history
Adds http request id in logs and response headers

Signed-off-by: jay-dee7 <jasdeepsingh.uppal@gmail.com>
  • Loading branch information
jay-dee7 committed Nov 19, 2021
1 parent a4b540f commit c52b23d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
10 changes: 9 additions & 1 deletion router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/containerish/OpenRegistry/registry/v2"
"github.com/containerish/OpenRegistry/telemetry"
fluentbit "github.com/containerish/OpenRegistry/telemetry/fluent-bit"
"github.com/google/uuid"
"github.com/labstack/echo-contrib/prometheus"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
Expand All @@ -28,7 +29,14 @@ func Register(cfg *config.RegistryConfig, e *echo.Echo, reg registry.Registry, a
e.Use(telemetry.ZerologMiddleware(telemetry.SetupLogger(), fbClient))
e.Use(middleware.Recover())
e.Use(middleware.CORS())
// JWT Auth Endpoint

e.Use(middleware.RequestIDWithConfig(middleware.RequestIDConfig{
Generator: func() string {
requestId := uuid.New()
return requestId.String()
},
}))

e.HideBanner = true

p := prometheus.NewPrometheus("OpenRegistry", nil)
Expand Down
2 changes: 1 addition & 1 deletion telemetry/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func ZerologMiddleware(baseLogger zerolog.Logger, fluentbitClient fluentbit.Flue
},
}

logFmt := `{"time":"${time_rfc3339}","request_id":"${request_id}","remote_ip":"${remote_ip}",` +
logFmt := `{"time":"${time_rfc3339}","x-request-id":"${request_id}","remote_ip":"${remote_ip}",` +
`"host":"${host}","method":"${method}","uri":"${uri}","user_agent":"${user_agent}",` +
`"status":${status},"error":"${error}","latency":${latency},"latency_human":"${latency_human}"` +
`,"bytes_in":${bytes_in},"bytes_out":${bytes_out}}` + "\n"
Expand Down

0 comments on commit c52b23d

Please sign in to comment.