Skip to content

Commit

Permalink
Set user agent when scraping prom metrics (influxdata#9271)
Browse files Browse the repository at this point in the history
  • Loading branch information
russorat authored May 20, 2021
1 parent 3a1a44d commit e8ae019
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions plugins/inputs/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/plugins/common/tls"
"github.com/influxdata/telegraf/plugins/inputs"
parser_v2 "github.com/influxdata/telegraf/plugins/parsers/prometheus"
Expand Down Expand Up @@ -58,7 +59,8 @@ type Prometheus struct {

Log telegraf.Logger

client *http.Client
client *http.Client
headers map[string]string

// Should we scrape Kubernetes services for prometheus annotations
MonitorPods bool `toml:"monitor_kubernetes_pods"`
Expand Down Expand Up @@ -273,6 +275,10 @@ func (p *Prometheus) Gather(acc telegraf.Accumulator) error {
return err
}
p.client = client
p.headers = map[string]string{
"User-Agent": internal.ProductToken(),
"Accept": acceptHeader,
}
}

var wg sync.WaitGroup
Expand Down Expand Up @@ -350,7 +356,7 @@ func (p *Prometheus) gatherURL(u URLAndAddress, acc telegraf.Accumulator) error
}
}

req.Header.Add("Accept", acceptHeader)
p.addHeaders(req)

if p.BearerToken != "" {
token, err := ioutil.ReadFile(p.BearerToken)
Expand Down Expand Up @@ -427,6 +433,12 @@ func (p *Prometheus) gatherURL(u URLAndAddress, acc telegraf.Accumulator) error
return nil
}

func (p *Prometheus) addHeaders(req *http.Request) {
for header, value := range p.headers {
req.Header.Add(header, value)
}
}

/* Check if the field selector specified is valid.
* See ToSelectableFields() for list of fields that are selectable:
* https://github.com/kubernetes/kubernetes/release-1.20/pkg/registry/core/pod/strategy.go
Expand Down

0 comments on commit e8ae019

Please sign in to comment.