Skip to content

Commit 3c27609

Browse files
authored
feat: disable healthz and metrics logging (#968)
1 parent 07fd286 commit 3c27609

File tree

2 files changed

+51
-26
lines changed

2 files changed

+51
-26
lines changed

README.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,16 @@ See [docs/upgrading.md](docs/upgrading.md).
2222

2323
The backend can be configured with the following environment variables.
2424

25-
| Name | Type | Default | Description |
26-
| -------------------- | ------------------------- | ---------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
27-
| `API_URL` | `string` | _none, must be set_ | The URL of the API, e.g. `https://ez.example.com/api` |
28-
| `GIN_MODE` | One of `release`, `debug` | `release` | The mode that gin runs in. Only set this to `debug` on your development environment! |
29-
| `PORT` | `number` | `8080` | The port the backend listens on |
30-
| `LOG_FORMAT` | One of `json`, `human` | `json` if `GIN_MODE` is `release`, otherwise `human` | If log output is written human readable or as JSON. |
31-
| `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. |
32-
| `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. |
25+
| Name | Type | Default | Description |
26+
| ---------------------- | ------------------------- | ---------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
27+
| `API_URL` | `string` | _none, must be set_ | The URL of the API, e.g. `https://ez.example.com/api` |
28+
| `GIN_MODE` | One of `release`, `debug` | `release` | The mode that gin runs in. Only set this to `debug` on your development environment! |
29+
| `PORT` | `number` | `8080` | The port the backend listens on |
30+
| `LOG_FORMAT` | One of `json`, `human` | `json` if `GIN_MODE` is `release`, otherwise `human` | If log output is written human readable or as JSON. |
31+
| `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. |
32+
| `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. |
33+
| `DISABLE_METRICS_LOGS` | `bool` | `false` | Set to `true` to disable logs for the `/metrics` endpoint |
34+
| `DISABLE_HEALTHZ_LOGS` | `bool` | `false` | Set to `true` to disable logs for the `/healthz` endpoint |
3335

3436
### Deployment methods
3537

pkg/router/router.go

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,6 @@ func Config(url *url.URL) (*gin.Engine, func(), error) {
5757
"error": "This HTTP method is not allowed for the endpoint you called",
5858
})
5959
})
60-
r.Use(logger.SetLogger(
61-
logger.WithDefaultLevel(zerolog.InfoLevel),
62-
logger.WithClientErrorLevel(zerolog.InfoLevel),
63-
logger.WithServerErrorLevel(zerolog.ErrorLevel),
64-
logger.WithLogger(func(c *gin.Context, logger zerolog.Logger) zerolog.Logger {
65-
return logger.With().
66-
Str("request-id", requestid.Get(c)).
67-
Str("method", c.Request.Method).
68-
Str("path", c.Request.URL.Path).
69-
Int("status", c.Writer.Status()).
70-
Int("size", c.Writer.Size()).
71-
Str("user-agent", c.Request.UserAgent()).
72-
Logger()
73-
})))
7460

7561
// CORS settings
7662
allowOrigins, ok := os.LookupEnv("CORS_ALLOW_ORIGINS")
@@ -108,8 +94,45 @@ func Config(url *url.URL) (*gin.Engine, func(), error) {
10894
// Separating this from RouterConfig() allows us to attach it to different
10995
// paths for different use cases, e.g. the standalone version.
11096
func AttachRoutes(group *gin.RouterGroup) {
111-
// Register metrics
112-
group.GET("/metrics", gin.WrapH(promhttp.Handler()))
97+
ezLogger := logger.SetLogger(
98+
logger.WithDefaultLevel(zerolog.InfoLevel),
99+
logger.WithClientErrorLevel(zerolog.InfoLevel),
100+
logger.WithServerErrorLevel(zerolog.ErrorLevel),
101+
logger.WithLogger(func(c *gin.Context, logger zerolog.Logger) zerolog.Logger {
102+
return logger.With().
103+
Str("request-id", requestid.Get(c)).
104+
Str("method", c.Request.Method).
105+
Str("path", c.Request.URL.Path).
106+
Int("status", c.Writer.Status()).
107+
Int("size", c.Writer.Size()).
108+
Str("user-agent", c.Request.UserAgent()).
109+
Logger()
110+
}),
111+
)
112+
113+
// skipLogger returns the correct logger for the route
114+
//
115+
// This is either a logger that skips logging or the ezLogger
116+
skipLogger := func(path, envVar string) gin.HandlerFunc {
117+
disable, ok := os.LookupEnv(envVar)
118+
if ok && disable == "true" {
119+
return logger.SetLogger(
120+
logger.WithSkipPath([]string{path}),
121+
)
122+
}
123+
124+
return ezLogger
125+
}
126+
127+
// metrics
128+
group.GET("/metrics", skipLogger("/metrics", "DISABLE_METRICS_LOGS"), gin.WrapH(promhttp.Handler()))
129+
130+
// healthz
131+
healthzGroup := group.Group("/healthz", skipLogger("/healthz", "DISABLE_HEALTHZ_LOGS"))
132+
healthz.RegisterRoutes(healthzGroup.Group(""))
133+
134+
// All groups that can disable logs are registered, register logger by default
135+
group.Use(ezLogger)
113136

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

146+
// Unversioned global endpoints
123147
{
124-
// Unversioned global endpoints
125148
root.RegisterRoutes(group.Group(""))
126-
healthz.RegisterRoutes(group.Group("/healthz"))
127149
version_controller.RegisterRoutes(group.Group("/version"), version)
128150
}
129151

152+
// v4
130153
{
131154
v4Group := group.Group("/v4")
132155
v4.RegisterRootRoutes(v4Group.Group(""))

0 commit comments

Comments
 (0)