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 Dec 20, 2019
1 parent 61dceaa commit 45799cf
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
13 changes: 13 additions & 0 deletions http/query_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ func (h *FluxHandler) handleQuery(w http.ResponseWriter, r *http.Request) {
}

req, n, err := decodeProxyQueryRequest(ctx, r, a, h.OrganizationService)
req.Request.Source = r.Header.Get("User-Agent")
if err != nil && err != influxdb.ErrAuthorizerNotSupported {
err := &influxdb.Error{
Code: influxdb.EInvalid,
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 45799cf

Please sign in to comment.