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

ruler: build user-agent header at runtime #7264

Merged
merged 1 commit into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions pkg/mimirtool/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,16 @@ const (
)

var (
UserAgent = fmt.Sprintf("mimirtool/%s %s", version.Version, version.Info())
ErrResourceNotFound = errors.New("requested resource not found")
errConflict = errors.New("conflict with current state of target resource")
errTooManyRequests = errors.New("too many requests")
)

// UserAgent returns build information in format suitable to be used in HTTP User-Agent header.
func UserAgent() string {
return fmt.Sprintf("mimirtool/%s %s", version.Version, version.Info())
}

// Config is used to configure a MimirClient.
type Config struct {
User string `yaml:"user"`
Expand Down Expand Up @@ -253,6 +257,6 @@ func buildRequest(ctx context.Context, p, m string, endpoint url.URL, payload io
if contentLength >= 0 {
r.ContentLength = contentLength
}
r.Header.Add("User-Agent", UserAgent)
r.Header.Add("User-Agent", UserAgent())
return r, nil
}
2 changes: 1 addition & 1 deletion pkg/mimirtool/commands/analyse_prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (cmd *PrometheusAnalyzeCommand) parseUsedMetrics() (model.LabelValues, erro

func (cmd *PrometheusAnalyzeCommand) newAPI() (v1.API, error) {
rt := api.DefaultRoundTripper
rt = config.NewUserAgentRoundTripper(client.UserAgent, rt)
rt = config.NewUserAgentRoundTripper(client.UserAgent(), rt)
if cmd.username != "" {
rt = &setTenantIDTransport{
RoundTripper: rt,
Expand Down
2 changes: 1 addition & 1 deletion pkg/mimirtool/commands/loadgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (c *LoadgenCommand) run(_ *kingpin.ParseContext) error {
URL: &config.URL{URL: writeURL},
Timeout: model.Duration(c.writeTimeout),
Headers: map[string]string{
"User-Agent": client.UserAgent,
"User-Agent": client.UserAgent(),
},
})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/mimirtool/commands/remote_read.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func (c *RemoteReadCommand) readClient() (remote.ReadClient, error) {
},
},
Headers: map[string]string{
"User-Agent": client.UserAgent,
"User-Agent": client.UserAgent(),
},
})
if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions pkg/ruler/remotequerier.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ const (
formatProtobuf = "protobuf"
)

var userAgent = fmt.Sprintf("mimir/%s", version.Version)
var allFormats = []string{formatJSON, formatProtobuf}

// QueryFrontendConfig defines query-frontend transport configuration.
Expand Down Expand Up @@ -168,7 +167,7 @@ func (q *RemoteQuerier) Read(ctx context.Context, query *prompb.Query) (*prompb.
{Key: textproto.CanonicalMIMEHeaderKey("Content-Encoding"), Values: []string{"snappy"}},
{Key: textproto.CanonicalMIMEHeaderKey("Accept-Encoding"), Values: []string{"snappy"}},
{Key: textproto.CanonicalMIMEHeaderKey("Content-Type"), Values: []string{"application/x-protobuf"}},
{Key: textproto.CanonicalMIMEHeaderKey("User-Agent"), Values: []string{userAgent}},
{Key: textproto.CanonicalMIMEHeaderKey("User-Agent"), Values: []string{version.UserAgent()}},
{Key: textproto.CanonicalMIMEHeaderKey("X-Prometheus-Remote-Read-Version"), Values: []string{"0.1.0"}},
}),
}
Expand Down Expand Up @@ -272,7 +271,7 @@ func (q *RemoteQuerier) createRequest(ctx context.Context, query string, ts time
Url: q.promHTTPPrefix + queryEndpointPath,
Body: body,
Headers: injectHTTPGrpcReadConsistencyHeader(ctx, []*httpgrpc.Header{
{Key: textproto.CanonicalMIMEHeaderKey("User-Agent"), Values: []string{userAgent}},
{Key: textproto.CanonicalMIMEHeaderKey("User-Agent"), Values: []string{version.UserAgent()}},
{Key: textproto.CanonicalMIMEHeaderKey("Content-Type"), Values: []string{mimeTypeFormPost}},
{Key: textproto.CanonicalMIMEHeaderKey("Content-Length"), Values: []string{strconv.Itoa(len(body))}},
{Key: textproto.CanonicalMIMEHeaderKey("Accept"), Values: []string{acceptHeader}},
Expand Down
5 changes: 5 additions & 0 deletions pkg/util/version/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,8 @@ func Print(program string) string {
func Info() string {
return fmt.Sprintf("(version=%s, branch=%s, revision=%s)", Version, Branch, Revision)
}

// UserAgent returns build information in format suitable to be used in HTTP User-Agent header.
func UserAgent() string {
return fmt.Sprintf("mimir/%s", Version)
}