Skip to content

Commit

Permalink
Add Deploy Mode to usage stats. (#5880)
Browse files Browse the repository at this point in the history
Co-authored-by: Clayton Cornell <[email protected]>
  • Loading branch information
captncraig and clayton-cornell authored Nov 29, 2023
1 parent 82428ff commit 7bf82ea
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ Main (unreleased)

- Fix issue in `prometheus.operator.*` where targets would be dropped if two crds share a common prefix in their names. (@Paul424, @captncraig)

### Other changes

- Add Agent Deploy Mode to usage report. (@captncraig)

v0.38.0 (2023-11-21)
--------------------

Expand Down
1 change: 1 addition & 0 deletions docs/sources/data-collection.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ The usage information includes the following details:
* List of enabled feature flags ([Static] mode only).
* List of enabled integrations ([Static] mode only).
* List of enabled [components][] ([Flow] mode only).
* Method used to deploy Grafana Agent, for example Docker, Helm, RPM, or Operator.

This list may change over time. All newly reported data is documented in the CHANGELOG.

Expand Down
11 changes: 9 additions & 2 deletions internal/useragent/useragent.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const (

// settable by tests
var goos = runtime.GOOS
var executable = os.Executable

func Get() string {
parenthesis := ""
Expand All @@ -27,7 +28,7 @@ func Get() string {
metadata = append(metadata, mode)
}
metadata = append(metadata, goos)
if op := getDeployMode(); op != "" {
if op := GetDeployMode(); op != "" {
metadata = append(metadata, op)
}
if len(metadata) > 0 {
Expand All @@ -49,12 +50,18 @@ func getRunMode() string {
}
}

func getDeployMode() string {
// GetDeployMode returns our best-effort guess at the way Grafana Agent was deployed.
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
}
// try to detect if executable is in homebrew directory
if path, err := executable(); err == nil && goos == "darwin" && strings.Contains(path, "brew") {
return "brew"
}
// fallback to binary
return "binary"
}
13 changes: 13 additions & 0 deletions internal/useragent/useragent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func TestUserAgent(t *testing.T) {
Expected string
DeployMode string
GOOS string
Exe string
}{
{
Name: "basic",
Expand Down Expand Up @@ -76,9 +77,21 @@ func TestUserAgent(t *testing.T) {
Expected: "GrafanaAgent/v1.2.3 (flow; linux; helm)",
GOOS: "linux",
},
{
Name: "brew",
Mode: "flow",
Expected: "GrafanaAgent/v1.2.3 (flow; darwin; brew)",
GOOS: "darwin",
Exe: "/opt/homebrew/bin/agent",
},
}
for _, tst := range tests {
t.Run(tst.Name, func(t *testing.T) {
if tst.Exe != "" {
executable = func() (string, error) { return tst.Exe, nil }
} else {
executable = func() (string, error) { return "/agent", nil }
}
goos = tst.GOOS
t.Setenv(deployModeEnv, tst.DeployMode)
t.Setenv(modeEnv, tst.Mode)
Expand Down
3 changes: 3 additions & 0 deletions pkg/usagestats/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"runtime"
"time"

"github.com/grafana/agent/internal/useragent"
"github.com/prometheus/common/version"
)

Expand All @@ -27,6 +28,7 @@ type Report struct {
Metrics map[string]interface{} `json:"metrics"`
Os string `json:"os"`
Arch string `json:"arch"`
DeployMode string `json:"deployMode"`
}

func sendReport(ctx context.Context, seed *AgentSeed, interval time.Time, metrics map[string]interface{}) error {
Expand All @@ -38,6 +40,7 @@ func sendReport(ctx context.Context, seed *AgentSeed, interval time.Time, metric
Arch: runtime.GOARCH,
Interval: interval,
Metrics: metrics,
DeployMode: useragent.GetDeployMode(),
}
out, err := json.MarshalIndent(report, "", " ")
if err != nil {
Expand Down

0 comments on commit 7bf82ea

Please sign in to comment.