Update http header User-Agent in k8s and swarm discovery. (#7614)

* discovery: update k8s and swarm user agent.

Signed-off-by: johncming <johncming@yahoo.com>
This commit is contained in:
johncming 2020-07-31 06:18:38 +08:00 committed by GitHub
parent f482c7bdd7
commit 3c5ff27959
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 1 deletions

View file

@ -24,6 +24,7 @@ import (
"github.com/go-kit/kit/log" "github.com/go-kit/kit/log"
config_util "github.com/prometheus/common/config" config_util "github.com/prometheus/common/config"
"github.com/prometheus/common/model" "github.com/prometheus/common/model"
"github.com/prometheus/common/version"
"github.com/prometheus/prometheus/discovery/refresh" "github.com/prometheus/prometheus/discovery/refresh"
"github.com/prometheus/prometheus/discovery/targetgroup" "github.com/prometheus/prometheus/discovery/targetgroup"
@ -33,6 +34,8 @@ const (
swarmLabel = model.MetaLabelPrefix + "dockerswarm_" swarmLabel = model.MetaLabelPrefix + "dockerswarm_"
) )
var userAgent = fmt.Sprintf("Prometheus/%s", version.Version)
// DefaultSDConfig is the default Docker Swarm SD configuration. // DefaultSDConfig is the default Docker Swarm SD configuration.
var DefaultSDConfig = SDConfig{ var DefaultSDConfig = SDConfig{
RefreshInterval: model.Duration(60 * time.Second), RefreshInterval: model.Duration(60 * time.Second),
@ -116,6 +119,9 @@ func NewDiscovery(conf *SDConfig, logger log.Logger) (*Discovery, error) {
Timeout: time.Duration(conf.RefreshInterval), Timeout: time.Duration(conf.RefreshInterval),
}), }),
client.WithScheme(hostURL.Scheme), client.WithScheme(hostURL.Scheme),
client.WithHTTPHeaders(map[string]string{
"User-Agent": userAgent,
}),
) )
} }

View file

@ -15,6 +15,7 @@ package kubernetes
import ( import (
"context" "context"
"fmt"
"reflect" "reflect"
"strings" "strings"
"sync" "sync"
@ -36,6 +37,7 @@ import (
"k8s.io/client-go/rest" "k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache" "k8s.io/client-go/tools/cache"
"github.com/prometheus/common/version"
"github.com/prometheus/prometheus/discovery/targetgroup" "github.com/prometheus/prometheus/discovery/targetgroup"
) )
@ -49,6 +51,8 @@ const (
) )
var ( var (
// Http header
userAgent = fmt.Sprintf("Prometheus/%s", version.Version)
// Custom events metric // Custom events metric
eventCount = prometheus.NewCounterVec( eventCount = prometheus.NewCounterVec(
prometheus.CounterOpts{ prometheus.CounterOpts{
@ -262,7 +266,7 @@ func New(l log.Logger, conf *SDConfig) (*Discovery, error) {
} }
} }
kcfg.UserAgent = "Prometheus/discovery" kcfg.UserAgent = userAgent
c, err := kubernetes.NewForConfig(kcfg) c, err := kubernetes.NewForConfig(kcfg)
if err != nil { if err != nil {