From f1317f7c6cbde81837b053b8c419a0bfb2d3d3ea Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Tue, 11 May 2021 22:03:48 +0300 Subject: [PATCH] lib/httpserver: return X-Server-Hostname http header in all the responses for better debuggability --- docs/CHANGELOG.md | 1 + lib/httpserver/httpserver.go | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 806e77dcbd..32ea4b5dc5 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -7,6 +7,7 @@ sort: 15 * FEATURE: vminsert: add support for data ingestion via other `vminsert` nodes. This allows building multi-level data ingestion paths in VictoriaMetrics cluster by writing data from one level of `vminsert` nodes to another level of `vminsert` nodes. See [these docs](https://docs.victoriametrics.com/Cluster-VictoriaMetrics.html#multi-level-cluster-setup) and [this comment](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/541#issuecomment-835487858) for details. * FEATURE: vmalert: add flag to control behaviour on startup for state restore errors. Such errors were returned and logged before as well. But now user can specify whether to just log these errors (`-remoteRead.ignoreRestoreErrors=true`) or to stop the process (`-remoteRead.ignoreRestoreErrors=false`). The latter is important when VM isn't ready yet to serve queries from vmalert and it needs to wait. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/1252). * FEATURE: vmalert: add ability to pass `round_digits` query arg to datasource via `-datasource.roundDigits` command-line flag. This can be used for limiting the number of decimal digits after the point in recording rule results. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/525). +* FEATURE: return `X-Server-Hostname` header in http responses of all the VictoriaMetrics components. This should simplify tracing the origin server behind a load balancer or behind auth proxy during troubleshooting. * BUGFIX: vmagent: fix possible race when refreshing `role: endpoints` and `role: endpointslices` scrape targets in `kubernetes_sd_config`. Prevoiusly `pod` objects could be updated after the related `endpoints` object update. This could lead to missing scrape targets. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/1240). * BUGFIX: properly remove stale parts outside the configured retention if `-retentionPeriod` is smaller than one month. Previously stale parts could remain active for up to a month after they go outside the retention. diff --git a/lib/httpserver/httpserver.go b/lib/httpserver/httpserver.go index 022fe4d195..611a7a0ff5 100644 --- a/lib/httpserver/httpserver.go +++ b/lib/httpserver/httpserver.go @@ -8,6 +8,7 @@ import ( "flag" "fmt" "io" + "log" "net" "net/http" "net/http/pprof" @@ -190,6 +191,17 @@ func gzipHandler(s *server, rh RequestHandler) http.HandlerFunc { var metricsHandlerDuration = metrics.NewHistogram(`vm_http_request_duration_seconds{path="/metrics"}`) var connTimeoutClosedConns = metrics.NewCounter(`vm_http_conn_timeout_closed_conns_total`) +var hostname = func() string { + h, err := os.Hostname() + if err != nil { + // Cannot use logger.Errorf, since it isn't initialized yet. + // So use log.Printf instead. + log.Printf("ERROR: cannot determine hostname: %s", err) + return "unknown" + } + return h +}() + func handlerWrapper(s *server, w http.ResponseWriter, r *http.Request, rh RequestHandler) { // All the VictoriaMetrics code assumes that panic stops the process. // Unfortunately, the standard net/http.Server recovers from panics in request handlers, @@ -205,6 +217,7 @@ func handlerWrapper(s *server, w http.ResponseWriter, r *http.Request, rh Reques } }() + w.Header().Set("X-Server-Hostname", hostname) requestsTotal.Inc() if whetherToCloseConn(r) { connTimeoutClosedConns.Inc()