Skip to content

Commit

Permalink
feat: disable healthz and metrics logging (#968)
Browse files Browse the repository at this point in the history
  • Loading branch information
morremeyer authored Feb 13, 2024
1 parent 07fd286 commit 3c27609
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 26 deletions.
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@ See [docs/upgrading.md](docs/upgrading.md).

The backend can be configured with the following environment variables.

| Name | Type | Default | Description |
| -------------------- | ------------------------- | ---------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `API_URL` | `string` | _none, must be set_ | The URL of the API, e.g. `https://ez.example.com/api` |
| `GIN_MODE` | One of `release`, `debug` | `release` | The mode that gin runs in. Only set this to `debug` on your development environment! |
| `PORT` | `number` | `8080` | The port the backend listens on |
| `LOG_FORMAT` | One of `json`, `human` | `json` if `GIN_MODE` is `release`, otherwise `human` | If log output is written human readable or as JSON. |
| `CORS_ALLOW_ORIGINS` | `string` | `""` | :information_source: This is only needed for frontend development. Defines hosts that are allowed to use cross origin requests, separated by spaces. |
| `ENABLE_PPROF` | `bool` | `false` | If set to `true`, pprof profiles for application profiling are made available at `/debug/pprof`. :warning: If you do not know what this means, do not turn this on. |
| Name | Type | Default | Description |
| ---------------------- | ------------------------- | ---------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `API_URL` | `string` | _none, must be set_ | The URL of the API, e.g. `https://ez.example.com/api` |
| `GIN_MODE` | One of `release`, `debug` | `release` | The mode that gin runs in. Only set this to `debug` on your development environment! |
| `PORT` | `number` | `8080` | The port the backend listens on |
| `LOG_FORMAT` | One of `json`, `human` | `json` if `GIN_MODE` is `release`, otherwise `human` | If log output is written human readable or as JSON. |
| `CORS_ALLOW_ORIGINS` | `string` | `""` | :information_source: This is only needed for frontend development. Defines hosts that are allowed to use cross origin requests, separated by spaces. |
| `ENABLE_PPROF` | `bool` | `false` | If set to `true`, pprof profiles for application profiling are made available at `/debug/pprof`. :warning: If you do not know what this means, do not turn this on. |
| `DISABLE_METRICS_LOGS` | `bool` | `false` | Set to `true` to disable logs for the `/metrics` endpoint |
| `DISABLE_HEALTHZ_LOGS` | `bool` | `false` | Set to `true` to disable logs for the `/healthz` endpoint |

### Deployment methods

Expand Down
59 changes: 41 additions & 18 deletions pkg/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,6 @@ func Config(url *url.URL) (*gin.Engine, func(), error) {
"error": "This HTTP method is not allowed for the endpoint you called",
})
})
r.Use(logger.SetLogger(
logger.WithDefaultLevel(zerolog.InfoLevel),
logger.WithClientErrorLevel(zerolog.InfoLevel),
logger.WithServerErrorLevel(zerolog.ErrorLevel),
logger.WithLogger(func(c *gin.Context, logger zerolog.Logger) zerolog.Logger {
return logger.With().
Str("request-id", requestid.Get(c)).
Str("method", c.Request.Method).
Str("path", c.Request.URL.Path).
Int("status", c.Writer.Status()).
Int("size", c.Writer.Size()).
Str("user-agent", c.Request.UserAgent()).
Logger()
})))

// CORS settings
allowOrigins, ok := os.LookupEnv("CORS_ALLOW_ORIGINS")
Expand Down Expand Up @@ -108,8 +94,45 @@ func Config(url *url.URL) (*gin.Engine, func(), error) {
// Separating this from RouterConfig() allows us to attach it to different
// paths for different use cases, e.g. the standalone version.
func AttachRoutes(group *gin.RouterGroup) {
// Register metrics
group.GET("/metrics", gin.WrapH(promhttp.Handler()))
ezLogger := logger.SetLogger(
logger.WithDefaultLevel(zerolog.InfoLevel),
logger.WithClientErrorLevel(zerolog.InfoLevel),
logger.WithServerErrorLevel(zerolog.ErrorLevel),
logger.WithLogger(func(c *gin.Context, logger zerolog.Logger) zerolog.Logger {
return logger.With().
Str("request-id", requestid.Get(c)).
Str("method", c.Request.Method).
Str("path", c.Request.URL.Path).
Int("status", c.Writer.Status()).
Int("size", c.Writer.Size()).
Str("user-agent", c.Request.UserAgent()).
Logger()
}),
)

// skipLogger returns the correct logger for the route
//
// This is either a logger that skips logging or the ezLogger
skipLogger := func(path, envVar string) gin.HandlerFunc {
disable, ok := os.LookupEnv(envVar)
if ok && disable == "true" {
return logger.SetLogger(
logger.WithSkipPath([]string{path}),
)
}

return ezLogger
}

// metrics
group.GET("/metrics", skipLogger("/metrics", "DISABLE_METRICS_LOGS"), gin.WrapH(promhttp.Handler()))

// healthz
healthzGroup := group.Group("/healthz", skipLogger("/healthz", "DISABLE_HEALTHZ_LOGS"))
healthz.RegisterRoutes(healthzGroup.Group(""))

// All groups that can disable logs are registered, register logger by default
group.Use(ezLogger)

// pprof performance profiles
enablePprof, ok := os.LookupEnv("ENABLE_PPROF")
Expand All @@ -120,13 +143,13 @@ func AttachRoutes(group *gin.RouterGroup) {
// Swagger API docs
group.GET("/docs/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))

// Unversioned global endpoints
{
// Unversioned global endpoints
root.RegisterRoutes(group.Group(""))
healthz.RegisterRoutes(group.Group("/healthz"))
version_controller.RegisterRoutes(group.Group("/version"), version)
}

// v4
{
v4Group := group.Group("/v4")
v4.RegisterRootRoutes(v4Group.Group(""))
Expand Down

0 comments on commit 3c27609

Please sign in to comment.