-
Notifications
You must be signed in to change notification settings - Fork 486
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
36d4dec
commit ea7b005
Showing
46 changed files
with
252 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.