Skip to content

Commit

Permalink
add goos and operator flag
Browse files Browse the repository at this point in the history
  • Loading branch information
captncraig committed Nov 14, 2023
1 parent d82354a commit 43494f2
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 11 deletions.
35 changes: 30 additions & 5 deletions pkg/useragent/useragent.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,39 @@ package useragent
import (
"fmt"
"os"
"runtime"
"strings"

"github.com/grafana/agent/pkg/build"
)

const (
operatorEnv = "AGENT_OPERATOR"
modeEnv = "AGENT_MODE"
)

// settable by tests
var goos = runtime.GOOS

func UserAgent() string {
parenthesis := ""
metadata := []string{}
if mode := getRunMode(); mode != "" {
parenthesis = fmt.Sprintf(" (%s)", mode)
metadata = append(metadata, mode)
}
metadata = append(metadata, goos)
if op := getOperator(); 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.
// if an unknown value is found we will simply omit it.
// getRunMode attempts to get agent mode, using `unknown` for invalid values.
func getRunMode() string {
key, found := os.LookupEnv("AGENT_MODE")
key, found := os.LookupEnv(modeEnv)
if !found {
return "static"
}
Expand All @@ -31,6 +48,14 @@ func getRunMode() string {
case "static", "":
return "static"
default:
return ""
return "unknown"
}
}

func getOperator() string {
op := os.Getenv(operatorEnv)
if op == "1" {
return "operator"
}
return ""
}
31 changes: 25 additions & 6 deletions pkg/useragent/useragent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,51 @@ func TestUserAgent(t *testing.T) {
Name string
Mode string
Expected string
Operator bool
GOOS string
}{
{
Name: "basic",
Mode: "",
Expected: "GrafanaAgent/v1.2.3 (static)",
Expected: "GrafanaAgent/v1.2.3 (static;linux)",
GOOS: "linux",
},
{
Name: "flow",
Mode: "flow",
Expected: "GrafanaAgent/v1.2.3 (flow)",
Expected: "GrafanaAgent/v1.2.3 (flow;windows)",
GOOS: "windows",
},
{
Name: "static",
Mode: "static",
Expected: "GrafanaAgent/v1.2.3 (static)",
Expected: "GrafanaAgent/v1.2.3 (static;darwin)",
GOOS: "darwin",
},
{
Name: "unknown",
Mode: "blahlahblah",
// unknown mode, just leave it out of user-agent. Don't want arbitrary values to get sent here.
Expected: "GrafanaAgent/v1.2.3",
// unknown mode, should not happen. But we will substitute 'unknown' to avoid allowing arbitrary cardinality.
Expected: "GrafanaAgent/v1.2.3 (unknown;freebsd)",
GOOS: "freebsd",
},
{
Name: "operator",
Mode: "static",
Operator: true,
Expected: "GrafanaAgent/v1.2.3 (static;linux;operator)",
GOOS: "linux",
},
}
for _, tst := range tests {
t.Run(tst.Name, func(t *testing.T) {
t.Setenv("AGENT_MODE", tst.Mode)
goos = tst.GOOS
if tst.Operator {
t.Setenv(operatorEnv, "1")
} else {
t.Setenv(operatorEnv, "")
}
t.Setenv(modeEnv, tst.Mode)
actual := UserAgent()
require.Equal(t, tst.Expected, actual)
})
Expand Down

0 comments on commit 43494f2

Please sign in to comment.