Skip to content

Commit

Permalink
go.d: set User-Agent automatically when creating HTTP req (netdata#17286
Browse files Browse the repository at this point in the history
)
  • Loading branch information
ilyam8 authored Mar 29, 2024
1 parent 416b95e commit ec01c76
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 10 deletions.
4 changes: 2 additions & 2 deletions packaging/cmake/Modules/NetdataGoTools.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 3 additions & 4 deletions src/go/collectors/go.d.plugin/cmd/godplugin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}

Expand All @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion src/go/collectors/go.d.plugin/hack/go-build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
6 changes: 6 additions & 0 deletions src/go/collectors/go.d.plugin/pkg/buildinfo/version.go
Original file line number Diff line number Diff line change
@@ -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"
4 changes: 1 addition & 3 deletions src/go/collectors/go.d.plugin/pkg/prometheus/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions src/go/collectors/go.d.plugin/pkg/web/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
Expand Down

0 comments on commit ec01c76

Please sign in to comment.