diff options
author | Ilya Mashchenko <ilya@netdata.cloud> | 2024-03-29 09:27:51 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-29 09:27:51 +0200 |
commit | ec01c7665c212e8fa3e6e73bffc96448be62acc7 (patch) | |
tree | afa05bc1c4ca57126bee64b5ac570b71652b1d54 | |
parent | 416b95ee6fad0e026e440f6f9459ab6a2aaf6547 (diff) |
go.d: set User-Agent automatically when creating HTTP req (#17286)
6 files changed, 21 insertions, 10 deletions
diff --git a/packaging/cmake/Modules/NetdataGoTools.cmake b/packaging/cmake/Modules/NetdataGoTools.cmake index 12785aeb54..9bb09cdc55 100644 --- a/packaging/cmake/Modules/NetdataGoTools.cmake +++ b/packaging/cmake/Modules/NetdataGoTools.cmake @@ -5,9 +5,9 @@ # SPDX-License-Identifier: GPL 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() - 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() # add_go_target: Add a new target that needs to be built using the Go toolchain. diff --git a/src/go/collectors/go.d.plugin/cmd/godplugin/main.go b/src/go/collectors/go.d.plugin/cmd/godplugin/main.go index 587cc41315..3e043b6603 100644 --- a/src/go/collectors/go.d.plugin/cmd/godplugin/main.go +++ b/src/go/collectors/go.d.plugin/cmd/godplugin/main.go @@ -15,6 +15,7 @@ import ( "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/logger" + "github.com/netdata/netdata/go/go.d.plugin/pkg/buildinfo" "github.com/netdata/netdata/go/go.d.plugin/pkg/multipath" "github.com/jessevdk/go-flags" @@ -32,8 +33,6 @@ var ( lockDir = os.Getenv("NETDATA_LOCK_DIR") watchPath = os.Getenv("NETDATA_PLUGINS_GOD_WATCH_PATH") envLogLevel = os.Getenv("NETDATA_LOG_LEVEL") - - version = "unknown" ) func confDir(opts *cli.Option) multipath.MultiPath { @@ -115,7 +114,7 @@ func main() { opts := parseCLI() if opts.Version { - fmt.Printf("go.d.plugin, version: %s\n", version) + fmt.Printf("go.d.plugin, version: %s\n", buildinfo.Version) return } @@ -142,7 +141,7 @@ func main() { 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 { a.Debugf("current user: name=%s, uid=%s", u.Username, u.Uid) } diff --git a/src/go/collectors/go.d.plugin/hack/go-build.sh b/src/go/collectors/go.d.plugin/hack/go-build.sh index 960eaa20ba..c211cff4d8 100755 --- a/src/go/collectors/go.d.plugin/hack/go-build.sh +++ b/src/go/collectors/go.d.plugin/hack/go-build.sh @@ -36,7 +36,7 @@ WHICH="$1" VERSION="${TRAVIS_TAG:-$(git describe --tags --always --dirty)}" 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() { echo "Building ${GOOS}/${GOARCH}" diff --git a/src/go/collectors/go.d.plugin/pkg/buildinfo/version.go b/src/go/collectors/go.d.plugin/pkg/buildinfo/version.go new file mode 100644 index 0000000000..55977a5927 --- /dev/null +++ b/src/go/collectors/go.d.plugin/pkg/buildinfo/version.go @@ -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" diff --git a/src/go/collectors/go.d.plugin/pkg/prometheus/client.go b/src/go/collectors/go.d.plugin/pkg/prometheus/client.go index 51f596595a..3365b270ce 100644 --- a/src/go/collectors/go.d.plugin/pkg/prometheus/client.go +++ b/src/go/collectors/go.d.plugin/pkg/prometheus/client.go @@ -42,8 +42,7 @@ type ( ) const ( - acceptHeader = `text/plain;version=0.0.4;q=1,*/*;q=0.1` - userAgentHeader = `netdata/go.d.plugin` + acceptHeader = `text/plain;version=0.0.4;q=1,*/*;q=0.1` ) // 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-Encoding", "gzip") - req.Header.Set("User-Agent", userAgentHeader) resp, err := p.client.Do(req) if err != nil { diff --git a/src/go/collectors/go.d.plugin/pkg/web/request.go b/src/go/collectors/go.d.plugin/pkg/web/request.go index 3db08f734f..655b3c7d0b 100644 --- a/src/go/collectors/go.d.plugin/pkg/web/request.go +++ b/src/go/collectors/go.d.plugin/pkg/web/request.go @@ -4,9 +4,13 @@ package web import ( "encoding/base64" + "fmt" "io" "net/http" "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. @@ -50,6 +54,8 @@ func (r Request) Copy() Request { 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. func NewHTTPRequest(cfg Request) (*http.Request, error) { var body io.Reader @@ -62,6 +68,8 @@ func NewHTTPRequest(cfg Request) (*http.Request, error) { return nil, err } + req.Header.Set("User-Agent", userAgent) + if cfg.Username != "" || cfg.Password != "" { req.SetBasicAuth(cfg.Username, cfg.Password) } |