Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add metadata to User-Agent header #5774

Merged
merged 18 commits into from
Nov 15, 2023
Merged

Add metadata to User-Agent header #5774

merged 18 commits into from
Nov 15, 2023

Conversation

captncraig
Copy link
Contributor

@captncraig captncraig commented Nov 14, 2023

  • Refactors User-Agent code into a shared package, instead of the same copy/paste snippet everywhere.
  • Add additional metadata in parenthesis at end of user agent, semicolon delimeted.
  • Current metadata added:
    • agent mode (flow or static)
    • GOOS
    • operator if running in operator generated pod.
  • Operator adds AGENT_OPERATOR=1 to agent pods it creates to facilitate the above.

Example User Agents:

  • GrafanaAgent/v1.2.3 (static; linux; operator)
  • GrafanaAgent/v1.2.3 (flow; windows)
  • GrafanaAgent/v1.2.3 (static; linux)

Why?

This will allow database operators (mimir,loki,pyroscope, etc.) to better understand agent traffic patterns sending them data.

@captncraig captncraig requested a review from a team as a code owner November 14, 2023 21:55
Copy link
Member

@rfratto rfratto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Just a few small comments about style

// 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we put this in internal/useragent instead of pkg/useragent? Since we're heading towards 1.0 where we're unlikely to have a large public API (or any public API at all), I'd like to get in the habit of only exposing what absolutely needs to be made public.

metadata = append(metadata, op)
}
if len(metadata) > 0 {
parenthesis = fmt.Sprintf(" (%s)", strings.Join(metadata, ";"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about using strings.Join(metadata, "; ") here (with the space after the semicolon)? That would help make the metadata slightly more human-readable:

GrafanaAgent/v1.2.3 (static; linux; operator)

vs

GrafanaAgent/v1.2.3 (static;linux;operator)

// settable by tests
var goos = runtime.GOOS

func UserAgent() string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling this Get() might be less repetitive so callers use useragent.Get() instead of useragent.UserAgent()

@captncraig captncraig enabled auto-merge (squash) November 15, 2023 20:55
@captncraig captncraig merged commit ea7b005 into main Nov 15, 2023
8 checks passed
@captncraig captncraig deleted the cmp_useragent branch November 15, 2023 20:58
@github-actions github-actions bot added the frozen-due-to-age Locked due to a period of inactivity. Please open new issues or PRs if more discussion is needed. label Feb 21, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 21, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
frozen-due-to-age Locked due to a period of inactivity. Please open new issues or PRs if more discussion is needed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants