Change default user agent
authornikosmonaut <nikosmonaut@protonmail.ch>
Thu, 12 Dec 2019 12:01:15 +0000 (13:01 +0100)
committernikosmonaut <nikosmonaut@protonmail.ch>
Thu, 12 Dec 2019 12:33:47 +0000 (13:33 +0100)
According to prometheus scrape behavior. Change default go http client user-agent
from "Go-http-client/1.1" to "{{ project_name }}/{{ version }}"

Signed-off-by: nikosmonaut <nikosmonaut@protonmail.ch>
prober/http.go

index f4abdbe119e3c678151be186a6f2099a5cee13eb..8df0d2213860f94745c9e7533749cd2723749154 100644 (file)
@@ -35,6 +35,7 @@ import (
        "github.com/go-kit/kit/log/level"
        "github.com/prometheus/client_golang/prometheus"
        pconfig "github.com/prometheus/common/config"
+       "github.com/prometheus/common/version"
        "golang.org/x/net/publicsuffix"
 
        "github.com/prometheus/blackbox_exporter/config"
@@ -226,6 +227,8 @@ func (t *transport) GotFirstResponseByte() {
        t.current.responseStart = time.Now()
 }
 
+var userAgentDefaultHeader = fmt.Sprintf("BlackBox Exporter/%s", version.Version)
+
 func ProbeHTTP(ctx context.Context, target string, module config.Module, registry *prometheus.Registry, logger log.Logger) (success bool) {
        var redirects int
        var (
@@ -403,6 +406,10 @@ func ProbeHTTP(ctx context.Context, target string, module config.Module, registr
                request.Header.Set(key, value)
        }
 
+       if request.Header.Get("User-Agent") == "" {
+               request.Header.Set("User-Agent", userAgentDefaultHeader)
+       }
+
        trace := &httptrace.ClientTrace{
                DNSStart:             tt.DNSStart,
                DNSDone:              tt.DNSDone,