@cryptotaxi247 / netdata-1 / commits / ec01c7665

go.d: set User-Agent automatically when creating HTTP req (#17286)

Ilya Mashchenko committed Mar 29, 2024 at 09:27 UTC ec01c7665c212e8fa3e6e73bffc96448be62acc7
6 files changed +21 -10
packaging/cmake/Modules/NetdataGoTools.cmake
+2 -2
@@ -5,9 +5,9 @@
5 # SPDX-License-Identifier: GPL
6
7 if(CMAKE_BUILD_TYPE STREQUAL Debug OR CMAKE_BUILD_TYPE STREQUAL RelWithDebInfo)
8 - set(GO_LDFLAGS "-X main.version=${NETDATA_VERSION}")
8 + set(GO_LDFLAGS "-X github.com/netdata/netdata/go/go.d.plugin/pkg/buildinfo.Version=${NETDATA_VERSION}")
9 else()
10 - set(GO_LDFLAGS "-w -s -X main.version=${NETDATA_VERSION}")
10 + set(GO_LDFLAGS "-w -s -X github.com/netdata/netdata/go/go.d.plugin/pkg/buildinfo.Version=${NETDATA_VERSION}")
11 endif()
12
13 # add_go_target: Add a new target that needs to be built using the Go toolchain.
src/go/collectors/go.d.plugin/cmd/godplugin/main.go
+3 -4
@@ -15,6 +15,7 @@ import (
15 "github.com/netdata/netdata/go/go.d.plugin/agent/executable"
16 "github.com/netdata/netdata/go/go.d.plugin/cli"
17 "github.com/netdata/netdata/go/go.d.plugin/logger"
18 + "github.com/netdata/netdata/go/go.d.plugin/pkg/buildinfo"
19 "github.com/netdata/netdata/go/go.d.plugin/pkg/multipath"
20
21 "github.com/jessevdk/go-flags"
@@ -32,8 +33,6 @@ var (
33 lockDir = os.Getenv("NETDATA_LOCK_DIR")
34 watchPath = os.Getenv("NETDATA_PLUGINS_GOD_WATCH_PATH")
35 envLogLevel = os.Getenv("NETDATA_LOG_LEVEL")
35 -
36 - version = "unknown"
36 )
37
38 func confDir(opts *cli.Option) multipath.MultiPath {
@@ -115,7 +114,7 @@ func main() {
114 opts := parseCLI()
115
116 if opts.Version {
118 - fmt.Printf("go.d.plugin, version: %s\n", version)
117 + fmt.Printf("go.d.plugin, version: %s\n", buildinfo.Version)
118 return
119 }
120
@@ -142,7 +141,7 @@ func main() {
141 MinUpdateEvery: opts.UpdateEvery,
142 })
143
145 - a.Debugf("plugin: name=%s, version=%s", a.Name, version)
144 + a.Debugf("plugin: name=%s, version=%s", a.Name, buildinfo.Version)
145 if u, err := user.Current(); err == nil {
146 a.Debugf("current user: name=%s, uid=%s", u.Username, u.Uid)
147 }
src/go/collectors/go.d.plugin/hack/go-build.sh
+1 -1
@@ -36,7 +36,7 @@ WHICH="$1"
36 VERSION="${TRAVIS_TAG:-$(git describe --tags --always --dirty)}"
37
38 GOLDFLAGS=${GLDFLAGS:-}
39 -GOLDFLAGS="$GOLDFLAGS -w -s -X main.version=$VERSION"
39 +GOLDFLAGS="$GOLDFLAGS -w -s -X github.com/netdata/netdata/go/go.d.plugin/pkg/buildinfo.Version=$VERSION"
40
41 build() {
42 echo "Building ${GOOS}/${GOARCH}"
src/go/collectors/go.d.plugin/pkg/buildinfo/version.go new
+6
@@ -0,0 +1,6 @@
1 +// SPDX-License-Identifier: GPL-3.0-or-later
2 +
3 +package buildinfo
4 +
5 +// Version stores the agent's version number. It's set during the build process using build flags.
6 +var Version = "v0.0.0"
src/go/collectors/go.d.plugin/pkg/prometheus/client.go
+1 -3
@@ -42,8 +42,7 @@ type (
42 )
43
44 const (
45 - acceptHeader = `text/plain;version=0.0.4;q=1,*/*;q=0.1`
46 - userAgentHeader = `netdata/go.d.plugin`
45 + acceptHeader = `text/plain;version=0.0.4;q=1,*/*;q=0.1`
46 )
47
48 // New creates a Prometheus instance.
@@ -118,7 +117,6 @@ func (p *prometheus) fetch(w io.Writer) error {
117
118 req.Header.Add("Accept", acceptHeader)
119 req.Header.Add("Accept-Encoding", "gzip")
121 - req.Header.Set("User-Agent", userAgentHeader)
120
121 resp, err := p.client.Do(req)
122 if err != nil {
src/go/collectors/go.d.plugin/pkg/web/request.go
+8
@@ -4,9 +4,13 @@ package web
4
5 import (
6 "encoding/base64"
7 + "fmt"
8 "io"
9 "net/http"
10 "strings"
11 +
12 + "github.com/netdata/netdata/go/go.d.plugin/agent/executable"
13 + "github.com/netdata/netdata/go/go.d.plugin/pkg/buildinfo"
14 )
15
16 // Request is the configuration of the HTTP request.
@@ -50,6 +54,8 @@ func (r Request) Copy() Request {
54 return r
55 }
56
57 +var userAgent = fmt.Sprintf("Netdata %s.plugin/%s", executable.Name, buildinfo.Version)
58 +
59 // NewHTTPRequest returns a new *http.Requests given a Request configuration and an error if any.
60 func NewHTTPRequest(cfg Request) (*http.Request, error) {
61 var body io.Reader
@@ -62,6 +68,8 @@ func NewHTTPRequest(cfg Request) (*http.Request, error) {
68 return nil, err
69 }
70
71 + req.Header.Set("User-Agent", userAgent)
72 +
73 if cfg.Username != "" || cfg.Password != "" {
74 req.SetBasicAuth(cfg.Username, cfg.Password)
75 }