Skip to content

Commit

Permalink
feat(query): add source to query request via user agent header
Browse files Browse the repository at this point in the history
  • Loading branch information
gavincabbage committed Jan 7, 2020
1 parent ec35815 commit f036307
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
1. [16014](https://github.com/influxdata/influxdb/pull/16014): While creating check, also display notification rules that would match check based on tag rules
1. [16389](https://github.com/influxdata/influxdb/pull/16389): Increase default bucket retention period to 30 days
1. [16418](https://github.com/influxdata/influxdb/pull/16418): Add Developer Documentation
1. [16260](https://github.com/influxdata/influxdb/pull/16260): Capture User-Agent header as query source for logging purposes

### Bug Fixes

Expand Down
13 changes: 13 additions & 0 deletions http/query_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ func (h *FluxHandler) handleQuery(w http.ResponseWriter, r *http.Request) {
h.HandleHTTPError(ctx, err, w)
return
}
req.Request.Source = r.Header.Get("User-Agent")
orgID = req.Request.OrganizationID
requestBytes = n

Expand Down Expand Up @@ -349,6 +350,7 @@ var _ query.ProxyQueryService = (*FluxService)(nil)
type FluxService struct {
Addr string
Token string
Name string
InsecureSkipVerify bool
}

Expand Down Expand Up @@ -383,6 +385,11 @@ func (s *FluxService) Query(ctx context.Context, w io.Writer, r *query.ProxyRequ

hreq.Header.Set("Content-Type", "application/json")
hreq.Header.Set("Accept", "text/csv")
if r.Request.Source != "" {
hreq.Header.Add("User-Agent", r.Request.Source)
} else if s.Name != "" {
hreq.Header.Add("User-Agent", s.Name)
}
hreq = hreq.WithContext(ctx)

hc := NewClient(u.Scheme, s.InsecureSkipVerify)
Expand Down Expand Up @@ -412,6 +419,7 @@ var _ query.QueryService = (*FluxQueryService)(nil)
type FluxQueryService struct {
Addr string
Token string
Name string
InsecureSkipVerify bool
}

Expand Down Expand Up @@ -450,6 +458,11 @@ func (s *FluxQueryService) Query(ctx context.Context, r *query.Request) (flux.Re

hreq.Header.Set("Content-Type", "application/json")
hreq.Header.Set("Accept", "text/csv")
if r.Source != "" {
hreq.Header.Add("User-Agent", r.Source)
} else if s.Name != "" {
hreq.Header.Add("User-Agent", s.Name)
}
hreq = hreq.WithContext(ctx)

hc := NewClient(u.Scheme, s.InsecureSkipVerify)
Expand Down
3 changes: 3 additions & 0 deletions query/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ type Request struct {
// Compiler converts the query to a specification to run against the data.
Compiler flux.Compiler `json:"compiler"`

// Source represents the ultimate source of the request.
Source string `json:"source"`

// compilerMappings maps compiler types to creation methods
compilerMappings flux.CompilerMappings
}
Expand Down
6 changes: 4 additions & 2 deletions query/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,13 @@ func TestRequest_JSON(t *testing.T) {
}{
{
name: "simple",
data: `{"organization_id":"aaaaaaaaaaaaaaaa","compiler":{"a":"my custom compiler"},"compiler_type":"compilerA"}`,
data: `{"organization_id":"aaaaaaaaaaaaaaaa","compiler":{"a":"my custom compiler"},"source":"source","compiler_type":"compilerA"}`,
want: query.Request{
OrganizationID: platformtesting.MustIDBase16("aaaaaaaaaaaaaaaa"),
Compiler: &compilerA{
A: "my custom compiler",
},
Source: "source",
},
},
}
Expand Down Expand Up @@ -94,13 +95,14 @@ func TestProxyRequest_JSON(t *testing.T) {
}{
{
name: "simple",
data: `{"request":{"organization_id":"aaaaaaaaaaaaaaaa","compiler":{"a":"my custom compiler"},"compiler_type":"compilerA"},"dialect":{"b":42},"dialect_type":"dialectB"}`,
data: `{"request":{"organization_id":"aaaaaaaaaaaaaaaa","compiler":{"a":"my custom compiler"},"source":"source","compiler_type":"compilerA"},"dialect":{"b":42},"dialect_type":"dialectB"}`,
want: query.ProxyRequest{
Request: query.Request{
OrganizationID: platformtesting.MustIDBase16("aaaaaaaaaaaaaaaa"),
Compiler: &compilerA{
A: "my custom compiler",
},
Source: "source",
},
Dialect: &dialectB{
B: 42,
Expand Down

0 comments on commit f036307

Please sign in to comment.