Skip to content

Commit 84f473e

Browse files
committed
feat: add user agent to requests made by operator
This adds a user agent which includes the operator version to all requests made by the operator. By doing this, Grafana administrators can better differentiate between requests that originated from humans and those performed by the operator
1 parent ca143c5 commit 84f473e

File tree

5 files changed

+20
-2
lines changed

5 files changed

+20
-2
lines changed

.ko.yaml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
builds:
2+
- id: grafana-operator
3+
dir: .
4+
main: .
5+
flags:
6+
- '-trimpath'
7+
ldflags:
8+
- -X github.com/grafana/grafana-operator/v5/embeds.Version={{.Git.Tag}}

controllers/client/round_tripper.go

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"net/http"
66
"strconv"
77

8+
"github.com/grafana/grafana-operator/v5/embeds"
89
"github.com/prometheus/client_golang/prometheus"
910
)
1011

@@ -36,6 +37,7 @@ func NewInstrumentedRoundTripper(relatedResource string, metric *prometheus.Coun
3637
}
3738

3839
func (in *instrumentedRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
40+
r.Header.Add("user-agent", "grafana-operator/"+embeds.Version)
3941
resp, err := in.wrapped.RoundTrip(r)
4042
if resp != nil {
4143
in.metric.WithLabelValues(

embeds/README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
# Embeds
22

3-
This directory is used for embedding FS structs into go code.
3+
This package contains static information compiled into the binary at build time.
44

55
# Grafonnet
66

77
The Grafonnet Jsonnet is used to allow users to define their dashboards using the jsonnet framework.
8+
9+
# Version
10+
11+
The `Version` variable is set during production builds by `ko`. Configuration for this can be found in `.ko.yaml`

embeds/grafana_embeds.go

+3
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@ var TestDashboardEmbedWithEnvExpectedJSON []byte
1919

2020
//go:embed testing/jsonnetProjectWithRuntimeRaw.tar.gz
2121
var TestJsonnetProjectBuildFolderGzip []byte
22+
23+
// this variable is replaced during production builds
24+
var Version = "dev"

main.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import (
4444
grafanav1beta1 "github.com/grafana/grafana-operator/v5/api/v1beta1"
4545
"github.com/grafana/grafana-operator/v5/controllers"
4646
"github.com/grafana/grafana-operator/v5/controllers/autodetect"
47+
"github.com/grafana/grafana-operator/v5/embeds"
4748
"k8s.io/apimachinery/pkg/runtime"
4849
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
4950
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
@@ -66,7 +67,7 @@ const (
6667

6768
var (
6869
scheme = runtime.NewScheme()
69-
setupLog = ctrl.Log.WithName("setup")
70+
setupLog = ctrl.Log.WithName("setup").WithValues("version", embeds.Version)
7071
)
7172

7273
func init() {

0 commit comments

Comments
 (0)