From f25a6baedb9724269ea1ea792f3b1641fc18a9c6 Mon Sep 17 00:00:00 2001 From: Daniele Sluijters Date: Fri, 23 Nov 2018 15:49:49 +0100 Subject: [PATCH] remote: Set User-Agent header in requests (#4891) Currently Prometheus requests show up with a UA of Go-http-client/1.1 which isn't super helpful. Though the X-Prometheus-Remote-* headers exist they need to be explicitly configured when logging the request in order to be able to deduce this is a request originating from Prometheus. By setting the header we remove this ambiguity and make default server logs just a bit more useful. This also updates a few other places to consistently capitalize the 'P' in the user agent, as well as ensure we set a UA to begin with. Signed-off-by: Daniele Sluijters --- discovery/kubernetes/kubernetes.go | 2 +- notifier/notifier.go | 4 ++++ storage/remote/client.go | 5 +++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/discovery/kubernetes/kubernetes.go b/discovery/kubernetes/kubernetes.go index 5315badd03..1e063bc715 100644 --- a/discovery/kubernetes/kubernetes.go +++ b/discovery/kubernetes/kubernetes.go @@ -238,7 +238,7 @@ func New(l log.Logger, conf *SDConfig) (*Discovery, error) { } } - kcfg.UserAgent = "prometheus/discovery" + kcfg.UserAgent = "Prometheus/discovery" c, err := kubernetes.NewForConfig(kcfg) if err != nil { diff --git a/notifier/notifier.go b/notifier/notifier.go index 8604fe28d9..3b31c8fb47 100644 --- a/notifier/notifier.go +++ b/notifier/notifier.go @@ -33,6 +33,7 @@ import ( "github.com/prometheus/client_golang/prometheus" config_util "github.com/prometheus/common/config" "github.com/prometheus/common/model" + "github.com/prometheus/common/version" "github.com/prometheus/prometheus/config" "github.com/prometheus/prometheus/discovery/targetgroup" @@ -52,6 +53,8 @@ const ( alertmanagerLabel = "alertmanager" ) +var userAgent = fmt.Sprintf("Prometheus/%s", version.Version) + // Alert is a generic representation of an alert in the Prometheus eco-system. type Alert struct { // Label value pairs for purpose of aggregation, matching, and disposition @@ -498,6 +501,7 @@ func (n *Manager) sendOne(ctx context.Context, c *http.Client, url string, b []b if err != nil { return err } + req.Header.Set("User-Agent", userAgent) req.Header.Set("Content-Type", contentTypeJSON) resp, err := n.opts.Do(ctx, c, req) if err != nil { diff --git a/storage/remote/client.go b/storage/remote/client.go index 6e1f8f0fc7..2679f42b6b 100644 --- a/storage/remote/client.go +++ b/storage/remote/client.go @@ -26,6 +26,7 @@ import ( "github.com/gogo/protobuf/proto" "github.com/golang/snappy" "github.com/prometheus/common/model" + "github.com/prometheus/common/version" config_util "github.com/prometheus/common/config" "github.com/prometheus/prometheus/prompb" @@ -33,6 +34,8 @@ import ( const maxErrMsgLen = 256 +var userAgent = fmt.Sprintf("Prometheus/%s", version.Version) + // Client allows reading and writing from/to a remote HTTP endpoint. type Client struct { index int // Used to differentiate clients in metrics. @@ -83,6 +86,7 @@ func (c *Client) Store(ctx context.Context, req *prompb.WriteRequest) error { } httpReq.Header.Add("Content-Encoding", "snappy") httpReq.Header.Set("Content-Type", "application/x-protobuf") + httpReq.Header.Set("User-Agent", userAgent) httpReq.Header.Set("X-Prometheus-Remote-Write-Version", "0.1.0") httpReq = httpReq.WithContext(ctx) @@ -138,6 +142,7 @@ func (c *Client) Read(ctx context.Context, query *prompb.Query) (*prompb.QueryRe httpReq.Header.Add("Content-Encoding", "snappy") httpReq.Header.Add("Accept-Encoding", "snappy") httpReq.Header.Set("Content-Type", "application/x-protobuf") + httpReq.Header.Set("User-Agent", userAgent) httpReq.Header.Set("X-Prometheus-Remote-Read-Version", "0.1.0") ctx, cancel := context.WithTimeout(ctx, c.timeout)