0
0
Fork 0
mirror of https://github.com/netdata/netdata.git synced 2025-04-14 01:29:11 +00:00

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

This commit is contained in:
Ilya Mashchenko 2024-03-29 09:27:51 +02:00 committed by GitHub
parent 416b95ee6f
commit ec01c7665c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 21 additions and 10 deletions
packaging/cmake/Modules
src/go/collectors/go.d.plugin
cmd/godplugin
hack
pkg
buildinfo
prometheus
web

View file

@ -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.

View file

@ -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)
}

View file

@ -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}"

View 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"

View file

@ -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 {

View file

@ -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)
}