Skip to content

Commit 94705d6

Browse files
committed
ref: use user agent roundtripper from prom common
Signed-off-by: TJ Hoplock <[email protected]>
1 parent 4516458 commit 94705d6

File tree

1 file changed

+2
-27
lines changed

1 file changed

+2
-27
lines changed

pkg/prometheus/api.go

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/prometheus/client_golang/api"
88
promv1 "github.com/prometheus/client_golang/api/prometheus/v1"
9+
"github.com/prometheus/common/config"
910

1011
"github.com/tjhop/prometheus-mcp-server/internal/version"
1112
)
@@ -14,38 +15,12 @@ var (
1415
userAgent = fmt.Sprintf("prometheus-mcp-server/%s (https://github.com/tjhop/prometheus-mcp-server)", version.Version)
1516
)
1617

17-
type userAgentRoundTripper struct {
18-
name string
19-
rt http.RoundTripper
20-
}
21-
22-
// RoundTrip implements the http.RoundTripper interface.
23-
func (u userAgentRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
24-
if r.UserAgent() == "" {
25-
// The specification of http.RoundTripper says that it shouldn't mutate
26-
// the request so make a copy of req.Header since this is all that is
27-
// modified.
28-
r2 := new(http.Request)
29-
*r2 = *r
30-
r2.Header = make(http.Header)
31-
for k, s := range r.Header {
32-
r2.Header[k] = s
33-
}
34-
r2.Header.Set("User-Agent", u.name)
35-
r = r2
36-
}
37-
return u.rt.RoundTrip(r)
38-
}
39-
4018
func NewAPIClient(prometheusUrl string, rt http.RoundTripper) (promv1.API, error) {
4119
if rt == nil {
4220
rt = http.DefaultTransport
4321
}
4422

45-
uart := userAgentRoundTripper{
46-
name: userAgent,
47-
rt: rt,
48-
}
23+
uart := config.NewUserAgentRoundTripper(userAgent, rt)
4924

5025
client, err := api.NewClient(api.Config{
5126
Address: prometheusUrl,

0 commit comments

Comments
 (0)