mirror of
https://github.com/netdata/netdata.git
synced 2025-04-26 05:47:20 +00:00
go.d: set User-Agent automatically when creating HTTP req (#17286)
This commit is contained in:
parent
416b95ee6f
commit
ec01c7665c
6 changed files with 21 additions and 10 deletions
packaging/cmake/Modules
src/go/collectors/go.d.plugin
|
@ -5,9 +5,9 @@
|
||||||
# SPDX-License-Identifier: GPL
|
# SPDX-License-Identifier: GPL
|
||||||
|
|
||||||
if(CMAKE_BUILD_TYPE STREQUAL Debug OR CMAKE_BUILD_TYPE STREQUAL RelWithDebInfo)
|
if(CMAKE_BUILD_TYPE STREQUAL Debug OR CMAKE_BUILD_TYPE STREQUAL RelWithDebInfo)
|
||||||
set(GO_LDFLAGS "-X main.version=${NETDATA_VERSION}")
|
set(GO_LDFLAGS "-X github.com/netdata/netdata/go/go.d.plugin/pkg/buildinfo.Version=${NETDATA_VERSION}")
|
||||||
else()
|
else()
|
||||||
set(GO_LDFLAGS "-w -s -X main.version=${NETDATA_VERSION}")
|
set(GO_LDFLAGS "-w -s -X github.com/netdata/netdata/go/go.d.plugin/pkg/buildinfo.Version=${NETDATA_VERSION}")
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
# add_go_target: Add a new target that needs to be built using the Go toolchain.
|
# add_go_target: Add a new target that needs to be built using the Go toolchain.
|
||||||
|
|
|
@ -15,6 +15,7 @@ import (
|
||||||
"github.com/netdata/netdata/go/go.d.plugin/agent/executable"
|
"github.com/netdata/netdata/go/go.d.plugin/agent/executable"
|
||||||
"github.com/netdata/netdata/go/go.d.plugin/cli"
|
"github.com/netdata/netdata/go/go.d.plugin/cli"
|
||||||
"github.com/netdata/netdata/go/go.d.plugin/logger"
|
"github.com/netdata/netdata/go/go.d.plugin/logger"
|
||||||
|
"github.com/netdata/netdata/go/go.d.plugin/pkg/buildinfo"
|
||||||
"github.com/netdata/netdata/go/go.d.plugin/pkg/multipath"
|
"github.com/netdata/netdata/go/go.d.plugin/pkg/multipath"
|
||||||
|
|
||||||
"github.com/jessevdk/go-flags"
|
"github.com/jessevdk/go-flags"
|
||||||
|
@ -32,8 +33,6 @@ var (
|
||||||
lockDir = os.Getenv("NETDATA_LOCK_DIR")
|
lockDir = os.Getenv("NETDATA_LOCK_DIR")
|
||||||
watchPath = os.Getenv("NETDATA_PLUGINS_GOD_WATCH_PATH")
|
watchPath = os.Getenv("NETDATA_PLUGINS_GOD_WATCH_PATH")
|
||||||
envLogLevel = os.Getenv("NETDATA_LOG_LEVEL")
|
envLogLevel = os.Getenv("NETDATA_LOG_LEVEL")
|
||||||
|
|
||||||
version = "unknown"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func confDir(opts *cli.Option) multipath.MultiPath {
|
func confDir(opts *cli.Option) multipath.MultiPath {
|
||||||
|
@ -115,7 +114,7 @@ func main() {
|
||||||
opts := parseCLI()
|
opts := parseCLI()
|
||||||
|
|
||||||
if opts.Version {
|
if opts.Version {
|
||||||
fmt.Printf("go.d.plugin, version: %s\n", version)
|
fmt.Printf("go.d.plugin, version: %s\n", buildinfo.Version)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -142,7 +141,7 @@ func main() {
|
||||||
MinUpdateEvery: opts.UpdateEvery,
|
MinUpdateEvery: opts.UpdateEvery,
|
||||||
})
|
})
|
||||||
|
|
||||||
a.Debugf("plugin: name=%s, version=%s", a.Name, version)
|
a.Debugf("plugin: name=%s, version=%s", a.Name, buildinfo.Version)
|
||||||
if u, err := user.Current(); err == nil {
|
if u, err := user.Current(); err == nil {
|
||||||
a.Debugf("current user: name=%s, uid=%s", u.Username, u.Uid)
|
a.Debugf("current user: name=%s, uid=%s", u.Username, u.Uid)
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,7 +36,7 @@ WHICH="$1"
|
||||||
VERSION="${TRAVIS_TAG:-$(git describe --tags --always --dirty)}"
|
VERSION="${TRAVIS_TAG:-$(git describe --tags --always --dirty)}"
|
||||||
|
|
||||||
GOLDFLAGS=${GLDFLAGS:-}
|
GOLDFLAGS=${GLDFLAGS:-}
|
||||||
GOLDFLAGS="$GOLDFLAGS -w -s -X main.version=$VERSION"
|
GOLDFLAGS="$GOLDFLAGS -w -s -X github.com/netdata/netdata/go/go.d.plugin/pkg/buildinfo.Version=$VERSION"
|
||||||
|
|
||||||
build() {
|
build() {
|
||||||
echo "Building ${GOOS}/${GOARCH}"
|
echo "Building ${GOOS}/${GOARCH}"
|
||||||
|
|
6
src/go/collectors/go.d.plugin/pkg/buildinfo/version.go
Normal file
6
src/go/collectors/go.d.plugin/pkg/buildinfo/version.go
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
|
||||||
|
package buildinfo
|
||||||
|
|
||||||
|
// Version stores the agent's version number. It's set during the build process using build flags.
|
||||||
|
var Version = "v0.0.0"
|
|
@ -42,8 +42,7 @@ type (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
acceptHeader = `text/plain;version=0.0.4;q=1,*/*;q=0.1`
|
acceptHeader = `text/plain;version=0.0.4;q=1,*/*;q=0.1`
|
||||||
userAgentHeader = `netdata/go.d.plugin`
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// New creates a Prometheus instance.
|
// New creates a Prometheus instance.
|
||||||
|
@ -118,7 +117,6 @@ func (p *prometheus) fetch(w io.Writer) error {
|
||||||
|
|
||||||
req.Header.Add("Accept", acceptHeader)
|
req.Header.Add("Accept", acceptHeader)
|
||||||
req.Header.Add("Accept-Encoding", "gzip")
|
req.Header.Add("Accept-Encoding", "gzip")
|
||||||
req.Header.Set("User-Agent", userAgentHeader)
|
|
||||||
|
|
||||||
resp, err := p.client.Do(req)
|
resp, err := p.client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -4,9 +4,13 @@ package web
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/netdata/netdata/go/go.d.plugin/agent/executable"
|
||||||
|
"github.com/netdata/netdata/go/go.d.plugin/pkg/buildinfo"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Request is the configuration of the HTTP request.
|
// Request is the configuration of the HTTP request.
|
||||||
|
@ -50,6 +54,8 @@ func (r Request) Copy() Request {
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var userAgent = fmt.Sprintf("Netdata %s.plugin/%s", executable.Name, buildinfo.Version)
|
||||||
|
|
||||||
// NewHTTPRequest returns a new *http.Requests given a Request configuration and an error if any.
|
// NewHTTPRequest returns a new *http.Requests given a Request configuration and an error if any.
|
||||||
func NewHTTPRequest(cfg Request) (*http.Request, error) {
|
func NewHTTPRequest(cfg Request) (*http.Request, error) {
|
||||||
var body io.Reader
|
var body io.Reader
|
||||||
|
@ -62,6 +68,8 @@ func NewHTTPRequest(cfg Request) (*http.Request, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
req.Header.Set("User-Agent", userAgent)
|
||||||
|
|
||||||
if cfg.Username != "" || cfg.Password != "" {
|
if cfg.Username != "" || cfg.Password != "" {
|
||||||
req.SetBasicAuth(cfg.Username, cfg.Password)
|
req.SetBasicAuth(cfg.Username, cfg.Password)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue