From ea7b005c1a41ac218bac345b7634419531d8d1cf Mon Sep 17 00:00:00 2001 From: Craig Peterson <192540+captncraig@users.noreply.github.com> Date: Wed, 15 Nov 2023 15:58:41 -0500 Subject: [PATCH] Add metadata to User-Agent header (#5774) * extract widely duplicated user agent code into shared package * add useragent package * space * add goos and operator flag * set AGENT_OPERATOR env var * add space * move to internal * switch to deploy mode env * switch to deploy mode env * add deploy mode to helm chart * add deploy mode to deb and rpm * add deploy mode to docker image * tests * regen helm * use binary as fallback deploy mode * fix test * changelog --- CHANGELOG.md | 2 + cmd/grafana-agent/Dockerfile | 1 + component/common/kubernetes/kubernetes.go | 4 +- component/common/loki/client/client.go | 6 +- component/common/loki/client/queue_client.go | 2 +- component/loki/source/docker/docker.go | 4 +- component/loki/write/write.go | 3 - .../prometheus/remotewrite/remote_write.go | 4 +- component/prometheus/scrape/scrape.go | 4 +- component/pyroscope/scrape/scrape_loop.go | 5 +- component/pyroscope/write/write.go | 5 +- component/remote/http/http.go | 4 +- internal/useragent/useragent.go | 60 +++++++++++++ internal/useragent/useragent_test.go | 89 +++++++++++++++++++ .../templates/containers/_agent.yaml | 2 + .../templates/controllers/statefulset.yaml | 2 + .../templates/controllers/daemonset.yaml | 2 + .../templates/controllers/daemonset.yaml | 2 + .../templates/controllers/daemonset.yaml | 2 + .../templates/controllers/deployment.yaml | 2 + .../templates/controllers/deployment.yaml | 2 + .../templates/controllers/statefulset.yaml | 2 + .../templates/controllers/statefulset.yaml | 2 + .../templates/controllers/daemonset.yaml | 2 + .../templates/controllers/daemonset.yaml | 2 + .../templates/controllers/daemonset.yaml | 2 + .../templates/controllers/daemonset.yaml | 2 + .../templates/controllers/daemonset.yaml | 2 + .../templates/controllers/daemonset.yaml | 2 + .../templates/controllers/daemonset.yaml | 2 + .../templates/controllers/daemonset.yaml | 2 + .../templates/controllers/daemonset.yaml | 2 + .../templates/controllers/daemonset.yaml | 2 + .../templates/controllers/daemonset.yaml | 2 + .../templates/controllers/daemonset.yaml | 2 + .../templates/controllers/daemonset.yaml | 2 + .../templates/controllers/daemonset.yaml | 2 + .../templates/controllers/daemonset.yaml | 2 + .../deb/grafana-agent-flow.service | 1 + .../rpm/grafana-agent-flow.service | 1 + .../grafana-agent/deb/grafana-agent.service | 1 + .../grafana-agent/rpm/grafana-agent.service | 1 + pkg/logs/logs.go | 4 +- pkg/metrics/instance/instance.go | 6 +- pkg/operator/resources_pod_template.go | 17 ++-- pkg/operator/resources_pod_template_test.go | 13 +++ 46 files changed, 252 insertions(+), 33 deletions(-) create mode 100644 internal/useragent/useragent.go create mode 100644 internal/useragent/useragent_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index e3e1a9925073..e2f10620bf6c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -152,6 +152,8 @@ Main (unreleased) - Use Go 1.21.4 for builds. (@rfratto) +- Change User-Agent header for outbound requests to include agent-mode, goos, and deployment mode. Example `GrafanaAgent/v0.38.0 (flow; linux; docker)` (@captncraig) + v0.37.4 (2023-11-06) ----------------- diff --git a/cmd/grafana-agent/Dockerfile b/cmd/grafana-agent/Dockerfile index 6a514a9d501e..7bd934790fe9 100644 --- a/cmd/grafana-agent/Dockerfile +++ b/cmd/grafana-agent/Dockerfile @@ -47,4 +47,5 @@ COPY cmd/grafana-agent/agent-local-config.yaml /etc/agent/agent.yaml ENTRYPOINT ["/bin/grafana-agent"] +ENV AGENT_DEPLOY_MODE=docker CMD ["--config.file=/etc/agent/agent.yaml", "--metrics.wal-directory=/etc/agent/data"] diff --git a/component/common/kubernetes/kubernetes.go b/component/common/kubernetes/kubernetes.go index a317fb58f11e..d46c3c0238c0 100644 --- a/component/common/kubernetes/kubernetes.go +++ b/component/common/kubernetes/kubernetes.go @@ -6,7 +6,7 @@ import ( "github.com/go-kit/log" commoncfg "github.com/grafana/agent/component/common/config" - "github.com/grafana/agent/pkg/build" + "github.com/grafana/agent/internal/useragent" "github.com/grafana/agent/pkg/flow/logging/level" promconfig "github.com/prometheus/common/config" "k8s.io/client-go/rest" @@ -79,7 +79,7 @@ func (args *ClientArguments) BuildRESTConfig(l log.Logger) (*rest.Config, error) } } - cfg.UserAgent = fmt.Sprintf("GrafanaAgent/%s", build.Version) + cfg.UserAgent = useragent.Get() cfg.ContentType = "application/vnd.kubernetes.protobuf" return cfg, nil diff --git a/component/common/loki/client/client.go b/component/common/loki/client/client.go index 964b6c98513b..5b426b4a1873 100644 --- a/component/common/loki/client/client.go +++ b/component/common/loki/client/client.go @@ -13,6 +13,7 @@ import ( "time" "github.com/go-kit/log" + "github.com/grafana/agent/internal/useragent" "github.com/grafana/agent/pkg/flow/logging/level" "github.com/grafana/dskit/backoff" "github.com/prometheus/client_golang/prometheus" @@ -20,7 +21,6 @@ import ( "github.com/prometheus/common/model" "github.com/grafana/agent/component/common/loki" - "github.com/grafana/agent/pkg/build" "github.com/grafana/agent/pkg/util" lokiutil "github.com/grafana/loki/pkg/util" ) @@ -47,7 +47,7 @@ const ( var Reasons = []string{ReasonGeneric, ReasonRateLimited, ReasonStreamLimited, ReasonLineTooLong} -var UserAgent = fmt.Sprintf("GrafanaAgent/%s", build.Version) +var userAgent = useragent.Get() type Metrics struct { encodedBytes *prometheus.CounterVec @@ -419,7 +419,7 @@ func (c *client) send(ctx context.Context, tenantID string, buf []byte) (int, er } req = req.WithContext(ctx) req.Header.Set("Content-Type", contentType) - req.Header.Set("User-Agent", UserAgent) + req.Header.Set("User-Agent", userAgent) // If the tenant ID is not empty promtail is running in multi-tenant mode, so // we should send it to Loki diff --git a/component/common/loki/client/queue_client.go b/component/common/loki/client/queue_client.go index dc8ed469fba3..871880cad17b 100644 --- a/component/common/loki/client/queue_client.go +++ b/component/common/loki/client/queue_client.go @@ -520,7 +520,7 @@ func (c *queueClient) send(ctx context.Context, tenantID string, buf []byte) (in } req = req.WithContext(ctx) req.Header.Set("Content-Type", contentType) - req.Header.Set("User-Agent", UserAgent) + req.Header.Set("User-Agent", userAgent) // If the tenant ID is not empty promtail is running in multi-tenant mode, so // we should send it to Loki diff --git a/component/loki/source/docker/docker.go b/component/loki/source/docker/docker.go index 584838f244fc..53a25b85da47 100644 --- a/component/loki/source/docker/docker.go +++ b/component/loki/source/docker/docker.go @@ -22,7 +22,7 @@ import ( flow_relabel "github.com/grafana/agent/component/common/relabel" "github.com/grafana/agent/component/discovery" dt "github.com/grafana/agent/component/loki/source/docker/internal/dockertarget" - "github.com/grafana/agent/pkg/build" + "github.com/grafana/agent/internal/useragent" "github.com/grafana/agent/pkg/flow/logging/level" "github.com/prometheus/common/config" "github.com/prometheus/common/model" @@ -40,7 +40,7 @@ func init() { }) } -var userAgent = fmt.Sprintf("GrafanaAgent/%s", build.Version) +var userAgent = useragent.Get() const ( dockerLabel = model.MetaLabelPrefix + "docker_" diff --git a/component/loki/write/write.go b/component/loki/write/write.go index a9d9c2730936..efda7b597c6f 100644 --- a/component/loki/write/write.go +++ b/component/loki/write/write.go @@ -12,7 +12,6 @@ import ( "github.com/grafana/agent/component/common/loki/client" "github.com/grafana/agent/component/common/loki/limit" "github.com/grafana/agent/component/common/loki/wal" - "github.com/grafana/agent/pkg/build" ) func init() { @@ -25,8 +24,6 @@ func init() { return New(opts, args.(Arguments)) }, }) - - client.UserAgent = fmt.Sprintf("GrafanaAgent/%s", build.Version) } // Arguments holds values which are used to configure the loki.write component. diff --git a/component/prometheus/remotewrite/remote_write.go b/component/prometheus/remotewrite/remote_write.go index d36d5cde8cfe..ed6ec0089b39 100644 --- a/component/prometheus/remotewrite/remote_write.go +++ b/component/prometheus/remotewrite/remote_write.go @@ -21,7 +21,7 @@ import ( "github.com/go-kit/log" "github.com/grafana/agent/component" - "github.com/grafana/agent/pkg/build" + "github.com/grafana/agent/internal/useragent" "github.com/grafana/agent/pkg/flow/logging/level" "github.com/grafana/agent/pkg/metrics/wal" "github.com/prometheus/prometheus/model/timestamp" @@ -35,7 +35,7 @@ import ( var remoteFlushDeadline = 1 * time.Minute func init() { - remote.UserAgent = fmt.Sprintf("GrafanaAgent/%s", build.Version) + remote.UserAgent = useragent.Get() component.Register(component.Registration{ Name: "prometheus.remote_write", diff --git a/component/prometheus/scrape/scrape.go b/component/prometheus/scrape/scrape.go index 750f71bd9aa0..d700221d1a15 100644 --- a/component/prometheus/scrape/scrape.go +++ b/component/prometheus/scrape/scrape.go @@ -12,7 +12,7 @@ import ( component_config "github.com/grafana/agent/component/common/config" "github.com/grafana/agent/component/discovery" "github.com/grafana/agent/component/prometheus" - "github.com/grafana/agent/pkg/build" + "github.com/grafana/agent/internal/useragent" "github.com/grafana/agent/pkg/flow/logging/level" "github.com/grafana/agent/service/cluster" "github.com/grafana/agent/service/http" @@ -27,7 +27,7 @@ import ( ) func init() { - scrape.UserAgent = fmt.Sprintf("GrafanaAgent/%s", build.Version) + scrape.UserAgent = useragent.Get() component.Register(component.Registration{ Name: "prometheus.scrape", diff --git a/component/pyroscope/scrape/scrape_loop.go b/component/pyroscope/scrape/scrape_loop.go index 5abb3fd801f3..2b74930ed191 100644 --- a/component/pyroscope/scrape/scrape_loop.go +++ b/component/pyroscope/scrape/scrape_loop.go @@ -12,18 +12,17 @@ import ( "github.com/go-kit/log" "github.com/grafana/agent/component/pyroscope" + "github.com/grafana/agent/internal/useragent" "github.com/grafana/agent/pkg/flow/logging/level" commonconfig "github.com/prometheus/common/config" "github.com/prometheus/prometheus/discovery/targetgroup" "github.com/prometheus/prometheus/util/pool" "golang.org/x/net/context/ctxhttp" - - "github.com/grafana/agent/pkg/build" ) var ( payloadBuffers = pool.New(1e3, 1e6, 3, func(sz int) interface{} { return make([]byte, 0, sz) }) - userAgentHeader = fmt.Sprintf("GrafanaAgent/%s", build.Version) + userAgentHeader = useragent.Get() ) type scrapePool struct { diff --git a/component/pyroscope/write/write.go b/component/pyroscope/write/write.go index e92c7dfb1706..165a7f20a812 100644 --- a/component/pyroscope/write/write.go +++ b/component/pyroscope/write/write.go @@ -3,12 +3,12 @@ package write import ( "context" "errors" - "fmt" "strings" "time" "github.com/bufbuild/connect-go" "github.com/grafana/agent/component/pyroscope" + "github.com/grafana/agent/internal/useragent" "github.com/grafana/agent/pkg/flow/logging/level" "github.com/oklog/run" commonconfig "github.com/prometheus/common/config" @@ -18,7 +18,6 @@ import ( "github.com/grafana/agent/component" "github.com/grafana/agent/component/common/config" - "github.com/grafana/agent/pkg/build" "github.com/grafana/dskit/backoff" pushv1 "github.com/grafana/pyroscope/api/gen/proto/go/push/v1" "github.com/grafana/pyroscope/api/gen/proto/go/push/v1/pushv1connect" @@ -26,7 +25,7 @@ import ( ) var ( - userAgent = fmt.Sprintf("GrafanaAgent/%s", build.Version) + userAgent = useragent.Get() DefaultArguments = func() Arguments { return Arguments{} } diff --git a/component/remote/http/http.go b/component/remote/http/http.go index 3966a63c6522..c45833ba81df 100644 --- a/component/remote/http/http.go +++ b/component/remote/http/http.go @@ -13,13 +13,13 @@ import ( "github.com/go-kit/log" "github.com/grafana/agent/component" common_config "github.com/grafana/agent/component/common/config" - "github.com/grafana/agent/pkg/build" + "github.com/grafana/agent/internal/useragent" "github.com/grafana/agent/pkg/flow/logging/level" "github.com/grafana/river/rivertypes" prom_config "github.com/prometheus/common/config" ) -var userAgent = fmt.Sprintf("GrafanaAgent/%s", build.Version) +var userAgent = useragent.Get() func init() { component.Register(component.Registration{ diff --git a/internal/useragent/useragent.go b/internal/useragent/useragent.go new file mode 100644 index 000000000000..bb6043f97aa3 --- /dev/null +++ b/internal/useragent/useragent.go @@ -0,0 +1,60 @@ +// package useragent provides a consistent way to get a user agent for outbound http requests from Grafana Agent. +// The default User-Agent is `GrafanaAgent/$VERSION($MODE)` +// Where version is the build version of the agent and MODE is one of "static" or "flow". +package useragent + +import ( + "fmt" + "os" + "runtime" + "strings" + + "github.com/grafana/agent/pkg/build" +) + +const ( + deployModeEnv = "AGENT_DEPLOY_MODE" + modeEnv = "AGENT_MODE" +) + +// settable by tests +var goos = runtime.GOOS + +func Get() string { + parenthesis := "" + metadata := []string{} + if mode := getRunMode(); mode != "" { + metadata = append(metadata, mode) + } + metadata = append(metadata, goos) + if op := getDeployMode(); op != "" { + metadata = append(metadata, op) + } + if len(metadata) > 0 { + parenthesis = fmt.Sprintf(" (%s)", strings.Join(metadata, "; ")) + } + return fmt.Sprintf("GrafanaAgent/%s%s", build.Version, parenthesis) +} + +// getRunMode attempts to get agent mode, using `unknown` for invalid values. +func getRunMode() string { + key := os.Getenv(modeEnv) + switch key { + case "flow": + return "flow" + case "static", "": + return "static" + default: + return "unknown" + } +} + +func getDeployMode() string { + op := os.Getenv(deployModeEnv) + // only return known modes. Use "binary" as a default catch-all. + switch op { + case "operator", "helm", "docker", "deb", "rpm", "brew": + return op + } + return "binary" +} diff --git a/internal/useragent/useragent_test.go b/internal/useragent/useragent_test.go new file mode 100644 index 000000000000..abaf0b80d192 --- /dev/null +++ b/internal/useragent/useragent_test.go @@ -0,0 +1,89 @@ +package useragent + +import ( + "testing" + + "github.com/grafana/agent/pkg/build" + "github.com/stretchr/testify/require" +) + +func TestUserAgent(t *testing.T) { + build.Version = "v1.2.3" + tests := []struct { + Name string + Mode string + Expected string + DeployMode string + GOOS string + }{ + { + Name: "basic", + Mode: "", + Expected: "GrafanaAgent/v1.2.3 (static; linux; binary)", + GOOS: "linux", + }, + { + Name: "flow", + Mode: "flow", + Expected: "GrafanaAgent/v1.2.3 (flow; windows; binary)", + GOOS: "windows", + }, + { + Name: "static", + Mode: "static", + Expected: "GrafanaAgent/v1.2.3 (static; darwin; binary)", + GOOS: "darwin", + }, + { + Name: "unknown", + Mode: "blahlahblah", + // unknown mode, should not happen. But we will substitute 'unknown' to avoid allowing arbitrary cardinality. + Expected: "GrafanaAgent/v1.2.3 (unknown; freebsd; binary)", + GOOS: "freebsd", + }, + { + Name: "operator", + Mode: "static", + DeployMode: "operator", + Expected: "GrafanaAgent/v1.2.3 (static; linux; operator)", + GOOS: "linux", + }, + { + Name: "deb", + Mode: "flow", + DeployMode: "deb", + Expected: "GrafanaAgent/v1.2.3 (flow; linux; deb)", + GOOS: "linux", + }, + { + Name: "rpm", + Mode: "static", + DeployMode: "rpm", + Expected: "GrafanaAgent/v1.2.3 (static; linux; rpm)", + GOOS: "linux", + }, + { + Name: "docker", + Mode: "flow", + DeployMode: "docker", + Expected: "GrafanaAgent/v1.2.3 (flow; linux; docker)", + GOOS: "linux", + }, + { + Name: "helm", + Mode: "flow", + DeployMode: "helm", + Expected: "GrafanaAgent/v1.2.3 (flow; linux; helm)", + GOOS: "linux", + }, + } + for _, tst := range tests { + t.Run(tst.Name, func(t *testing.T) { + goos = tst.GOOS + t.Setenv(deployModeEnv, tst.DeployMode) + t.Setenv(modeEnv, tst.Mode) + actual := Get() + require.Equal(t, tst.Expected, actual) + }) + } +} diff --git a/operations/helm/charts/grafana-agent/templates/containers/_agent.yaml b/operations/helm/charts/grafana-agent/templates/containers/_agent.yaml index c486c8ec30d8..32d081b58dc4 100644 --- a/operations/helm/charts/grafana-agent/templates/containers/_agent.yaml +++ b/operations/helm/charts/grafana-agent/templates/containers/_agent.yaml @@ -27,6 +27,8 @@ env: - name: AGENT_MODE value: {{ .Values.agent.mode }} + - name: AGENT_DEPLOY_MODE + value: "helm" - name: HOSTNAME valueFrom: fieldRef: diff --git a/operations/helm/tests/clustering/grafana-agent/templates/controllers/statefulset.yaml b/operations/helm/tests/clustering/grafana-agent/templates/controllers/statefulset.yaml index 1aca2fb93859..69354494639f 100644 --- a/operations/helm/tests/clustering/grafana-agent/templates/controllers/statefulset.yaml +++ b/operations/helm/tests/clustering/grafana-agent/templates/controllers/statefulset.yaml @@ -41,6 +41,8 @@ spec: env: - name: AGENT_MODE value: flow + - name: AGENT_DEPLOY_MODE + value: "helm" - name: HOSTNAME valueFrom: fieldRef: diff --git a/operations/helm/tests/controller-volumes-extra/grafana-agent/templates/controllers/daemonset.yaml b/operations/helm/tests/controller-volumes-extra/grafana-agent/templates/controllers/daemonset.yaml index f19ed25ad63b..de0c1e307bc6 100644 --- a/operations/helm/tests/controller-volumes-extra/grafana-agent/templates/controllers/daemonset.yaml +++ b/operations/helm/tests/controller-volumes-extra/grafana-agent/templates/controllers/daemonset.yaml @@ -36,6 +36,8 @@ spec: env: - name: AGENT_MODE value: flow + - name: AGENT_DEPLOY_MODE + value: "helm" - name: HOSTNAME valueFrom: fieldRef: diff --git a/operations/helm/tests/create-daemonset-hostnetwork/grafana-agent/templates/controllers/daemonset.yaml b/operations/helm/tests/create-daemonset-hostnetwork/grafana-agent/templates/controllers/daemonset.yaml index f7d69461630e..0921197e38ef 100644 --- a/operations/helm/tests/create-daemonset-hostnetwork/grafana-agent/templates/controllers/daemonset.yaml +++ b/operations/helm/tests/create-daemonset-hostnetwork/grafana-agent/templates/controllers/daemonset.yaml @@ -36,6 +36,8 @@ spec: env: - name: AGENT_MODE value: flow + - name: AGENT_DEPLOY_MODE + value: "helm" - name: HOSTNAME valueFrom: fieldRef: diff --git a/operations/helm/tests/create-daemonset/grafana-agent/templates/controllers/daemonset.yaml b/operations/helm/tests/create-daemonset/grafana-agent/templates/controllers/daemonset.yaml index 0038338c8e15..640801c35de4 100644 --- a/operations/helm/tests/create-daemonset/grafana-agent/templates/controllers/daemonset.yaml +++ b/operations/helm/tests/create-daemonset/grafana-agent/templates/controllers/daemonset.yaml @@ -36,6 +36,8 @@ spec: env: - name: AGENT_MODE value: flow + - name: AGENT_DEPLOY_MODE + value: "helm" - name: HOSTNAME valueFrom: fieldRef: diff --git a/operations/helm/tests/create-deployment-autoscaling/grafana-agent/templates/controllers/deployment.yaml b/operations/helm/tests/create-deployment-autoscaling/grafana-agent/templates/controllers/deployment.yaml index 33115f481940..c5383a670125 100644 --- a/operations/helm/tests/create-deployment-autoscaling/grafana-agent/templates/controllers/deployment.yaml +++ b/operations/helm/tests/create-deployment-autoscaling/grafana-agent/templates/controllers/deployment.yaml @@ -36,6 +36,8 @@ spec: env: - name: AGENT_MODE value: flow + - name: AGENT_DEPLOY_MODE + value: "helm" - name: HOSTNAME valueFrom: fieldRef: diff --git a/operations/helm/tests/create-deployment/grafana-agent/templates/controllers/deployment.yaml b/operations/helm/tests/create-deployment/grafana-agent/templates/controllers/deployment.yaml index 17238227bbe4..37fcf8fc0dc6 100644 --- a/operations/helm/tests/create-deployment/grafana-agent/templates/controllers/deployment.yaml +++ b/operations/helm/tests/create-deployment/grafana-agent/templates/controllers/deployment.yaml @@ -37,6 +37,8 @@ spec: env: - name: AGENT_MODE value: flow + - name: AGENT_DEPLOY_MODE + value: "helm" - name: HOSTNAME valueFrom: fieldRef: diff --git a/operations/helm/tests/create-statefulset-autoscaling/grafana-agent/templates/controllers/statefulset.yaml b/operations/helm/tests/create-statefulset-autoscaling/grafana-agent/templates/controllers/statefulset.yaml index 56f4e29353a8..bc9d31d9cf75 100644 --- a/operations/helm/tests/create-statefulset-autoscaling/grafana-agent/templates/controllers/statefulset.yaml +++ b/operations/helm/tests/create-statefulset-autoscaling/grafana-agent/templates/controllers/statefulset.yaml @@ -38,6 +38,8 @@ spec: env: - name: AGENT_MODE value: flow + - name: AGENT_DEPLOY_MODE + value: "helm" - name: HOSTNAME valueFrom: fieldRef: diff --git a/operations/helm/tests/create-statefulset/grafana-agent/templates/controllers/statefulset.yaml b/operations/helm/tests/create-statefulset/grafana-agent/templates/controllers/statefulset.yaml index 57ffe8a5b38d..7a641636bc13 100644 --- a/operations/helm/tests/create-statefulset/grafana-agent/templates/controllers/statefulset.yaml +++ b/operations/helm/tests/create-statefulset/grafana-agent/templates/controllers/statefulset.yaml @@ -39,6 +39,8 @@ spec: env: - name: AGENT_MODE value: flow + - name: AGENT_DEPLOY_MODE + value: "helm" - name: HOSTNAME valueFrom: fieldRef: diff --git a/operations/helm/tests/custom-config/grafana-agent/templates/controllers/daemonset.yaml b/operations/helm/tests/custom-config/grafana-agent/templates/controllers/daemonset.yaml index 0038338c8e15..640801c35de4 100644 --- a/operations/helm/tests/custom-config/grafana-agent/templates/controllers/daemonset.yaml +++ b/operations/helm/tests/custom-config/grafana-agent/templates/controllers/daemonset.yaml @@ -36,6 +36,8 @@ spec: env: - name: AGENT_MODE value: flow + - name: AGENT_DEPLOY_MODE + value: "helm" - name: HOSTNAME valueFrom: fieldRef: diff --git a/operations/helm/tests/default-values/grafana-agent/templates/controllers/daemonset.yaml b/operations/helm/tests/default-values/grafana-agent/templates/controllers/daemonset.yaml index 0038338c8e15..640801c35de4 100644 --- a/operations/helm/tests/default-values/grafana-agent/templates/controllers/daemonset.yaml +++ b/operations/helm/tests/default-values/grafana-agent/templates/controllers/daemonset.yaml @@ -36,6 +36,8 @@ spec: env: - name: AGENT_MODE value: flow + - name: AGENT_DEPLOY_MODE + value: "helm" - name: HOSTNAME valueFrom: fieldRef: diff --git a/operations/helm/tests/enable-servicemonitor/grafana-agent/templates/controllers/daemonset.yaml b/operations/helm/tests/enable-servicemonitor/grafana-agent/templates/controllers/daemonset.yaml index 0038338c8e15..640801c35de4 100644 --- a/operations/helm/tests/enable-servicemonitor/grafana-agent/templates/controllers/daemonset.yaml +++ b/operations/helm/tests/enable-servicemonitor/grafana-agent/templates/controllers/daemonset.yaml @@ -36,6 +36,8 @@ spec: env: - name: AGENT_MODE value: flow + - name: AGENT_DEPLOY_MODE + value: "helm" - name: HOSTNAME valueFrom: fieldRef: diff --git a/operations/helm/tests/envFrom/grafana-agent/templates/controllers/daemonset.yaml b/operations/helm/tests/envFrom/grafana-agent/templates/controllers/daemonset.yaml index af9f8733bfa3..ab153416d599 100644 --- a/operations/helm/tests/envFrom/grafana-agent/templates/controllers/daemonset.yaml +++ b/operations/helm/tests/envFrom/grafana-agent/templates/controllers/daemonset.yaml @@ -36,6 +36,8 @@ spec: env: - name: AGENT_MODE value: flow + - name: AGENT_DEPLOY_MODE + value: "helm" - name: HOSTNAME valueFrom: fieldRef: diff --git a/operations/helm/tests/existing-config/grafana-agent/templates/controllers/daemonset.yaml b/operations/helm/tests/existing-config/grafana-agent/templates/controllers/daemonset.yaml index b3acab2c314f..39cf2262d661 100644 --- a/operations/helm/tests/existing-config/grafana-agent/templates/controllers/daemonset.yaml +++ b/operations/helm/tests/existing-config/grafana-agent/templates/controllers/daemonset.yaml @@ -36,6 +36,8 @@ spec: env: - name: AGENT_MODE value: flow + - name: AGENT_DEPLOY_MODE + value: "helm" - name: HOSTNAME valueFrom: fieldRef: diff --git a/operations/helm/tests/extra-env/grafana-agent/templates/controllers/daemonset.yaml b/operations/helm/tests/extra-env/grafana-agent/templates/controllers/daemonset.yaml index b375f5c2d740..2b4663c47d86 100644 --- a/operations/helm/tests/extra-env/grafana-agent/templates/controllers/daemonset.yaml +++ b/operations/helm/tests/extra-env/grafana-agent/templates/controllers/daemonset.yaml @@ -36,6 +36,8 @@ spec: env: - name: AGENT_MODE value: flow + - name: AGENT_DEPLOY_MODE + value: "helm" - name: HOSTNAME valueFrom: fieldRef: diff --git a/operations/helm/tests/extra-ports/grafana-agent/templates/controllers/daemonset.yaml b/operations/helm/tests/extra-ports/grafana-agent/templates/controllers/daemonset.yaml index b949dab97850..388eb68a16eb 100644 --- a/operations/helm/tests/extra-ports/grafana-agent/templates/controllers/daemonset.yaml +++ b/operations/helm/tests/extra-ports/grafana-agent/templates/controllers/daemonset.yaml @@ -36,6 +36,8 @@ spec: env: - name: AGENT_MODE value: flow + - name: AGENT_DEPLOY_MODE + value: "helm" - name: HOSTNAME valueFrom: fieldRef: diff --git a/operations/helm/tests/faro-ingress/grafana-agent/templates/controllers/daemonset.yaml b/operations/helm/tests/faro-ingress/grafana-agent/templates/controllers/daemonset.yaml index 1a4310f118dc..840b64e244cd 100644 --- a/operations/helm/tests/faro-ingress/grafana-agent/templates/controllers/daemonset.yaml +++ b/operations/helm/tests/faro-ingress/grafana-agent/templates/controllers/daemonset.yaml @@ -36,6 +36,8 @@ spec: env: - name: AGENT_MODE value: flow + - name: AGENT_DEPLOY_MODE + value: "helm" - name: HOSTNAME valueFrom: fieldRef: diff --git a/operations/helm/tests/global-image-pullsecrets/grafana-agent/templates/controllers/daemonset.yaml b/operations/helm/tests/global-image-pullsecrets/grafana-agent/templates/controllers/daemonset.yaml index 23c984e2bd59..93e08d06f66b 100644 --- a/operations/helm/tests/global-image-pullsecrets/grafana-agent/templates/controllers/daemonset.yaml +++ b/operations/helm/tests/global-image-pullsecrets/grafana-agent/templates/controllers/daemonset.yaml @@ -41,6 +41,8 @@ spec: env: - name: AGENT_MODE value: flow + - name: AGENT_DEPLOY_MODE + value: "helm" - name: HOSTNAME valueFrom: fieldRef: diff --git a/operations/helm/tests/global-image-registry/grafana-agent/templates/controllers/daemonset.yaml b/operations/helm/tests/global-image-registry/grafana-agent/templates/controllers/daemonset.yaml index fb0c5bccfba3..83d83473d371 100644 --- a/operations/helm/tests/global-image-registry/grafana-agent/templates/controllers/daemonset.yaml +++ b/operations/helm/tests/global-image-registry/grafana-agent/templates/controllers/daemonset.yaml @@ -36,6 +36,8 @@ spec: env: - name: AGENT_MODE value: flow + - name: AGENT_DEPLOY_MODE + value: "helm" - name: HOSTNAME valueFrom: fieldRef: diff --git a/operations/helm/tests/initcontainers/grafana-agent/templates/controllers/daemonset.yaml b/operations/helm/tests/initcontainers/grafana-agent/templates/controllers/daemonset.yaml index 00b72a087f56..dea955f56900 100644 --- a/operations/helm/tests/initcontainers/grafana-agent/templates/controllers/daemonset.yaml +++ b/operations/helm/tests/initcontainers/grafana-agent/templates/controllers/daemonset.yaml @@ -54,6 +54,8 @@ spec: env: - name: AGENT_MODE value: flow + - name: AGENT_DEPLOY_MODE + value: "helm" - name: HOSTNAME valueFrom: fieldRef: diff --git a/operations/helm/tests/local-image-pullsecrets/grafana-agent/templates/controllers/daemonset.yaml b/operations/helm/tests/local-image-pullsecrets/grafana-agent/templates/controllers/daemonset.yaml index c9bbe3a6d165..8be0679577ea 100644 --- a/operations/helm/tests/local-image-pullsecrets/grafana-agent/templates/controllers/daemonset.yaml +++ b/operations/helm/tests/local-image-pullsecrets/grafana-agent/templates/controllers/daemonset.yaml @@ -38,6 +38,8 @@ spec: env: - name: AGENT_MODE value: flow + - name: AGENT_DEPLOY_MODE + value: "helm" - name: HOSTNAME valueFrom: fieldRef: diff --git a/operations/helm/tests/local-image-registry/grafana-agent/templates/controllers/daemonset.yaml b/operations/helm/tests/local-image-registry/grafana-agent/templates/controllers/daemonset.yaml index fb0c5bccfba3..83d83473d371 100644 --- a/operations/helm/tests/local-image-registry/grafana-agent/templates/controllers/daemonset.yaml +++ b/operations/helm/tests/local-image-registry/grafana-agent/templates/controllers/daemonset.yaml @@ -36,6 +36,8 @@ spec: env: - name: AGENT_MODE value: flow + - name: AGENT_DEPLOY_MODE + value: "helm" - name: HOSTNAME valueFrom: fieldRef: diff --git a/operations/helm/tests/nodeselectors-and-tolerations/grafana-agent/templates/controllers/daemonset.yaml b/operations/helm/tests/nodeselectors-and-tolerations/grafana-agent/templates/controllers/daemonset.yaml index d38ffc899ae0..a9d14c8e2d29 100644 --- a/operations/helm/tests/nodeselectors-and-tolerations/grafana-agent/templates/controllers/daemonset.yaml +++ b/operations/helm/tests/nodeselectors-and-tolerations/grafana-agent/templates/controllers/daemonset.yaml @@ -36,6 +36,8 @@ spec: env: - name: AGENT_MODE value: flow + - name: AGENT_DEPLOY_MODE + value: "helm" - name: HOSTNAME valueFrom: fieldRef: diff --git a/operations/helm/tests/static-mode/grafana-agent/templates/controllers/daemonset.yaml b/operations/helm/tests/static-mode/grafana-agent/templates/controllers/daemonset.yaml index afd7d674720b..5fc4e67fd107 100644 --- a/operations/helm/tests/static-mode/grafana-agent/templates/controllers/daemonset.yaml +++ b/operations/helm/tests/static-mode/grafana-agent/templates/controllers/daemonset.yaml @@ -33,6 +33,8 @@ spec: env: - name: AGENT_MODE value: static + - name: AGENT_DEPLOY_MODE + value: "helm" - name: HOSTNAME valueFrom: fieldRef: diff --git a/packaging/grafana-agent-flow/deb/grafana-agent-flow.service b/packaging/grafana-agent-flow/deb/grafana-agent-flow.service index aaf037d9f162..afca01973135 100644 --- a/packaging/grafana-agent-flow/deb/grafana-agent-flow.service +++ b/packaging/grafana-agent-flow/deb/grafana-agent-flow.service @@ -8,6 +8,7 @@ After=network-online.target Restart=always User=grafana-agent-flow Environment=HOSTNAME=%H +Environment=AGENT_DEPLOY_MODE=deb EnvironmentFile=/etc/default/grafana-agent-flow WorkingDirectory=/var/lib/grafana-agent-flow ExecStart=/usr/bin/grafana-agent-flow run $CUSTOM_ARGS --storage.path=/var/lib/grafana-agent-flow $CONFIG_FILE diff --git a/packaging/grafana-agent-flow/rpm/grafana-agent-flow.service b/packaging/grafana-agent-flow/rpm/grafana-agent-flow.service index 98638ed8e47a..330960790500 100644 --- a/packaging/grafana-agent-flow/rpm/grafana-agent-flow.service +++ b/packaging/grafana-agent-flow/rpm/grafana-agent-flow.service @@ -8,6 +8,7 @@ After=network-online.target Restart=always User=grafana-agent-flow Environment=HOSTNAME=%H +Environment=AGENT_DEPLOY_MODE=rpm EnvironmentFile=/etc/sysconfig/grafana-agent-flow WorkingDirectory=/var/lib/grafana-agent-flow ExecStart=/usr/bin/grafana-agent-flow run $CUSTOM_ARGS --storage.path=/var/lib/grafana-agent-flow $CONFIG_FILE diff --git a/packaging/grafana-agent/deb/grafana-agent.service b/packaging/grafana-agent/deb/grafana-agent.service index 1f2d95315f03..eea8677f3f6e 100644 --- a/packaging/grafana-agent/deb/grafana-agent.service +++ b/packaging/grafana-agent/deb/grafana-agent.service @@ -8,6 +8,7 @@ After=network-online.target Restart=always User=grafana-agent Environment=HOSTNAME=%H +Environment=AGENT_DEPLOY_MODE=deb EnvironmentFile=/etc/default/grafana-agent WorkingDirectory=/var/lib/grafana-agent ExecStart=/usr/bin/grafana-agent --config.file $CONFIG_FILE $CUSTOM_ARGS diff --git a/packaging/grafana-agent/rpm/grafana-agent.service b/packaging/grafana-agent/rpm/grafana-agent.service index fb5e24607a98..0089827dcd85 100644 --- a/packaging/grafana-agent/rpm/grafana-agent.service +++ b/packaging/grafana-agent/rpm/grafana-agent.service @@ -8,6 +8,7 @@ After=network-online.target Restart=always User=grafana-agent Environment=HOSTNAME=%H +Environment=AGENT_DEPLOY_MODE=rpm EnvironmentFile=/etc/sysconfig/grafana-agent WorkingDirectory=/var/lib/grafana-agent ExecStart=/usr/bin/grafana-agent --config.file $CONFIG_FILE $CUSTOM_ARGS diff --git a/pkg/logs/logs.go b/pkg/logs/logs.go index 7b0d9b2c27c0..a821298be9fd 100644 --- a/pkg/logs/logs.go +++ b/pkg/logs/logs.go @@ -11,6 +11,7 @@ import ( "github.com/go-kit/log" "github.com/go-kit/log/level" + "github.com/grafana/agent/internal/useragent" "github.com/grafana/agent/pkg/util" "github.com/grafana/loki/clients/pkg/promtail" "github.com/grafana/loki/clients/pkg/promtail/api" @@ -21,11 +22,10 @@ import ( "github.com/grafana/loki/clients/pkg/promtail/wal" "github.com/grafana/loki/pkg/tracing" "github.com/prometheus/client_golang/prometheus" - "github.com/prometheus/common/version" ) func init() { - client.UserAgent = fmt.Sprintf("GrafanaAgent/%s", version.Version) + client.UserAgent = useragent.Get() } // Logs is a Logs log collection. It uses multiple distinct sets of Logs diff --git a/pkg/metrics/instance/instance.go b/pkg/metrics/instance/instance.go index 9a9e36c788b1..3d0e9fd47ca7 100644 --- a/pkg/metrics/instance/instance.go +++ b/pkg/metrics/instance/instance.go @@ -17,7 +17,7 @@ import ( "github.com/go-kit/log" "github.com/go-kit/log/level" - "github.com/grafana/agent/pkg/build" + "github.com/grafana/agent/internal/useragent" "github.com/grafana/agent/pkg/metrics/wal" "github.com/grafana/agent/pkg/util" "github.com/oklog/run" @@ -35,8 +35,8 @@ import ( ) func init() { - remote.UserAgent = fmt.Sprintf("GrafanaAgent/%s", build.Version) - scrape.UserAgent = fmt.Sprintf("GrafanaAgent/%s", build.Version) + remote.UserAgent = useragent.Get() + scrape.UserAgent = useragent.Get() // default remote_write send_exemplars to true config.DefaultRemoteWriteConfig.SendExemplars = true diff --git a/pkg/operator/resources_pod_template.go b/pkg/operator/resources_pod_template.go index c826ea6ea980..5e97851cd8ce 100644 --- a/pkg/operator/resources_pod_template.go +++ b/pkg/operator/resources_pod_template.go @@ -190,12 +190,19 @@ func generatePodTemplate( finalLabels = cfg.Labels.Merge(podLabels) ) - envVars := []core_v1.EnvVar{{ - Name: "POD_NAME", - ValueFrom: &core_v1.EnvVarSource{ - FieldRef: &core_v1.ObjectFieldSelector{FieldPath: "metadata.name"}, + envVars := []core_v1.EnvVar{ + { + Name: "POD_NAME", + ValueFrom: &core_v1.EnvVarSource{ + FieldRef: &core_v1.ObjectFieldSelector{FieldPath: "metadata.name"}, + }, }, - }} + // Allows the agent to identify this is an operator-created pod. + { + Name: "AGENT_DEPLOY_MODE", + Value: "operator", + }, + } envVars = append(envVars, opts.ExtraEnvVars...) useConfigReloaderVersion := d.Agent.Spec.ConfigReloaderVersion diff --git a/pkg/operator/resources_pod_template_test.go b/pkg/operator/resources_pod_template_test.go index eb855820cc9d..fa189a203385 100644 --- a/pkg/operator/resources_pod_template_test.go +++ b/pkg/operator/resources_pod_template_test.go @@ -155,4 +155,17 @@ func Test_generatePodTemplate(t *testing.T) { require.NoError(t, err) assert.Nil(t, tmpl.Spec.RuntimeClassName) }) + + t.Run("AGENT_DEPLOY_MODE env ser", func(t *testing.T) { + deploy := gragent.Deployment{ + Agent: &gragent.GrafanaAgent{ + ObjectMeta: v1.ObjectMeta{Name: name, Namespace: name}, + }, + } + + tmpl, _, err := generatePodTemplate(cfg, "agent", deploy, podTemplateOptions{}) + require.NoError(t, err) + require.Equal(t, "operator", tmpl.Spec.Containers[1].Env[1].Value) + require.Equal(t, "AGENT_DEPLOY_MODE", tmpl.Spec.Containers[1].Env[1].Name) + }) }