Skip to content

Commit

Permalink
Merge pull request grafana#12909 from grafana/ds-user-agent
Browse files Browse the repository at this point in the history
Set User-Agent header in datasource proxied requests
  • Loading branch information
marefr authored Aug 15, 2018
2 parents 713fac8 + 1392190 commit 8af80b3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
1 change: 1 addition & 0 deletions pkg/api/pluginproxy/ds_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ func (proxy *DataSourceProxy) getDirector() func(req *http.Request) {
req.Header.Del("X-Forwarded-Host")
req.Header.Del("X-Forwarded-Port")
req.Header.Del("X-Forwarded-Proto")
req.Header.Set("User-Agent", fmt.Sprintf("Grafana/%s", setting.BuildVersion))

// set X-Forwarded-For header
if req.RemoteAddr != "" {
Expand Down
15 changes: 8 additions & 7 deletions pkg/api/pluginproxy/ds_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,20 +212,21 @@ func TestDSRouteRule(t *testing.T) {
})

Convey("When proxying graphite", func() {
setting.BuildVersion = "5.3.0"
plugin := &plugins.DataSourcePlugin{}
ds := &m.DataSource{Url: "htttp://graphite:8080", Type: m.DS_GRAPHITE}
ctx := &m.ReqContext{}

proxy := NewDataSourceProxy(ds, plugin, ctx, "/render")
req, err := http.NewRequest(http.MethodGet, "http://grafana.com/sub", nil)
So(err, ShouldBeNil)

requestURL, _ := url.Parse("http://grafana.com/sub")
req := http.Request{URL: requestURL}

proxy.getDirector()(&req)
proxy.getDirector()(req)

Convey("Can translate request url and path", func() {
So(req.URL.Host, ShouldEqual, "graphite:8080")
So(req.URL.Path, ShouldEqual, "/render")
So(req.Header.Get("User-Agent"), ShouldEqual, "Grafana/5.3.0")
})
})

Expand All @@ -243,10 +244,10 @@ func TestDSRouteRule(t *testing.T) {
ctx := &m.ReqContext{}
proxy := NewDataSourceProxy(ds, plugin, ctx, "")

requestURL, _ := url.Parse("http://grafana.com/sub")
req := http.Request{URL: requestURL}
req, err := http.NewRequest(http.MethodGet, "http://grafana.com/sub", nil)
So(err, ShouldBeNil)

proxy.getDirector()(&req)
proxy.getDirector()(req)

Convey("Should add db to url", func() {
So(req.URL.Path, ShouldEqual, "/db/site/")
Expand Down

0 comments on commit 8af80b3

Please sign in to comment.