diff --git a/README.md b/README.md index c0003c5..24b8dca 100644 --- a/README.md +++ b/README.md @@ -125,6 +125,7 @@ In this case, both proxy and websockets will be available under the `/v1/graphql | `BANNED_USERS_FILE` | The path to the file with banned users | `/go/src/app/banned_users.json` | | `PROXIED_CLIENT_TIMEOUT` | The timeout for the proxied client in seconds | `120` | | `PURGE_METRICS_ON_CRAWL` | Purge metrics on each /metrics crawl | `false` | +| `PURGE_METRICS_ON_TIMER` | Purge metrics every x seconds. `0` - disabled | `0` | ### Speed @@ -235,6 +236,8 @@ You can always enable `PURGE_METRICS_ON_CRAWL` environment variable to purge the With the `PURGE_METRICS_ON_CRAWL` enabled, the `graphql_proxy_requests_failed`, `graphql_proxy_requests_skipped` and `graphql_proxy_requests_succesful` metrics will remain between resets. +If you prefer more control over the metrics purging - you can enable `PURGE_METRICS_ON_TIMER` environment variable and set the interval in seconds. This will allow you to purge the metrics on a regular basis, for example every 90 seconds. It could be better solution if you have multiple crawlers checking the metrics endpoints and you want to avoid the situation when metrics are purged by for example healthcheck. + #### Healthcheck If you'd like the `/healthz` endpoint to perform actual check for the connectivity to the graphql endpoint - set the `HEALTHCHECK_GRAPHQL_URL` environment variable to the exact URL of the graphql endpoint. The query executed will be `query { __typename }` and if the response is not `200 OK` - the healthcheck will fail. Remember that the endpoint is a full URL which you'd like to check, so it should include the protocol, host and path - for example `http://localhost:8080/v1/graphql` and it's NOT the same as value of `HOST_GRAPHQL` environment variable which should provide only the host, without path, ending with slash. diff --git a/main.go b/main.go index f305781..b91c785 100644 --- a/main.go +++ b/main.go @@ -56,6 +56,7 @@ func parseConfig() { c.Server.ApiPort = envutil.GetInt("API_PORT", 9090) c.Api.BannedUsersFile = envutil.Getenv("BANNED_USERS_FILE", "/go/src/app/banned_users.json") c.Server.PurgeOnCrawl = envutil.GetBool("PURGE_METRICS_ON_CRAWL", false) + c.Server.PurgeEvery = envutil.GetInt("PURGE_METRICS_ON_TIMER", 0) cfg = &c enableCache() // takes close to no resources, but can be used with dynamic query cache loadRatelimitConfig() diff --git a/monitoring.go b/monitoring.go index fc21d4e..e115fd8 100644 --- a/monitoring.go +++ b/monitoring.go @@ -5,7 +5,7 @@ import ( ) func StartMonitoringServer() { - cfg.Monitoring = libpack_monitoring.NewMonitoring(cfg.Server.PurgeOnCrawl) + cfg.Monitoring = libpack_monitoring.NewMonitoring(cfg.Server.PurgeOnCrawl, cfg.Server.PurgeEvery) cfg.Monitoring.AddMetricsPrefix("graphql_proxy") cfg.Monitoring.RegisterDefaultMetrics() } diff --git a/monitoring/monitoring.go b/monitoring/monitoring.go index 8b2cd6e..0819d29 100644 --- a/monitoring/monitoring.go +++ b/monitoring/monitoring.go @@ -23,15 +23,26 @@ type MetricsSetup struct { var ( log *logging.LogConfig purgeMetricsOnCrawl bool + purgeMetricsEvery int ) -func NewMonitoring(purgeOnCrawl bool) *MetricsSetup { +func NewMonitoring(purgeOnCrawl bool, purgeEvery int) *MetricsSetup { purgeMetricsOnCrawl = purgeOnCrawl log = logging.NewLogger() ms := &MetricsSetup{} ms.metrics_set = metrics.NewSet() ms.metrics_set_custom = metrics.NewSet() go ms.startPrometheusEndpoint() + + if purgeMetricsEvery > 0 { + ticker := time.NewTicker(time.Duration(purgeMetricsEvery) * time.Second) + go func() { + for range ticker.C { + ms.PurgeMetrics() + } + }() + } + return ms } @@ -50,7 +61,8 @@ func (ms *MetricsSetup) startPrometheusEndpoint() { func (ms *MetricsSetup) metricsEndpoint(c *fiber.Ctx) error { ms.metrics_set.WritePrometheus(c.Response().BodyWriter()) ms.metrics_set_custom.WritePrometheus(c.Response().BodyWriter()) - if purgeMetricsOnCrawl { + + if purgeMetricsOnCrawl && purgeMetricsEvery == 0 { ms.PurgeMetrics() } return nil diff --git a/struct_config.go b/struct_config.go index d0bf35a..da98ad3 100644 --- a/struct_config.go +++ b/struct_config.go @@ -25,6 +25,7 @@ type config struct { EnableApi bool ApiPort int PurgeOnCrawl bool + PurgeEvery int } Client struct {