Skip to content

Commit

Permalink
Add Content-Type header on query-frontend paths
Browse files Browse the repository at this point in the history
  • Loading branch information
wperron committed Feb 23, 2022
1 parent 12fff13 commit fb474be
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 5 deletions.
2 changes: 2 additions & 0 deletions modules/frontend/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ func newTraceByIDMiddleware(cfg Config, logger log.Logger) Middleware {
span.SetTag("contentType", marshallingFormat)
}

resp.Header.Set(api.HeaderContentType, marshallingFormat)

return resp, err
})
})
Expand Down
11 changes: 10 additions & 1 deletion modules/frontend/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ func (f *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}

// write header and body
// write headers, status code and body
copyHeader(w.Header(), resp.Header)
w.WriteHeader(resp.StatusCode)
if resp.Body != nil {
_, _ = io.Copy(w, resp.Body)
Expand Down Expand Up @@ -126,6 +127,14 @@ func (f *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
)
}

func copyHeader(dst, src http.Header) {
for k, vv := range src {
for _, v := range vv {
dst.Add(k, v)
}
}
}

// writeError handles writing errors to the http.ResponseWriter. It uses weavework common
// server.WriteError() to handle httpgrc errors. The handler handles all incoming HTTP requests
// to the query frontend which then distributes them via httpgrpc to the queriers. As a result
Expand Down
6 changes: 4 additions & 2 deletions modules/frontend/searchsharding.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,10 @@ func (s searchSharder) RoundTrip(r *http.Request) (*http.Response, error) {
}

return &http.Response{
StatusCode: http.StatusOK,
Header: http.Header{},
StatusCode: http.StatusOK,
Header: http.Header{
api.HeaderContentType: {api.HeaderAcceptJSON},
},
Body: io.NopCloser(strings.NewReader(bodyString)),
ContentLength: int64(len([]byte(bodyString))),
}, nil
Expand Down
3 changes: 3 additions & 0 deletions modules/frontend/searchsharding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,9 @@ func TestSearchSharderRoundTrip(t *testing.T) {
}
require.NoError(t, err)
assert.Equal(t, tc.expectedStatus, resp.StatusCode)
if tc.expectedStatus == http.StatusOK {
assert.Equal(t, "application/json", resp.Header.Get("Content-Type"))
}
if tc.expectedResponse != nil {
actualResp := &tempopb.SearchResponse{}
bytesResp, err := io.ReadAll(resp.Body)
Expand Down
7 changes: 5 additions & 2 deletions modules/frontend/tracebyidsharding.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/go-kit/log/level"
"github.com/golang/protobuf/proto"
"github.com/grafana/tempo/modules/querier"
"github.com/grafana/tempo/pkg/api"
"github.com/grafana/tempo/pkg/model/trace"
"github.com/grafana/tempo/pkg/tempopb"
"github.com/opentracing/opentracing-go"
Expand Down Expand Up @@ -175,8 +176,10 @@ func (s shardQuery) RoundTrip(r *http.Request) (*http.Response, error) {
}

return &http.Response{
StatusCode: http.StatusOK,
Header: http.Header{},
StatusCode: http.StatusOK,
Header: http.Header{
api.HeaderContentType: {api.HeaderAcceptProtobuf},
},
Body: io.NopCloser(bytes.NewReader(buff)),
ContentLength: int64(len(buff)),
}, nil
Expand Down
3 changes: 3 additions & 0 deletions modules/frontend/tracebyidsharding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,9 @@ func TestShardingWareDoRequest(t *testing.T) {
}
require.NoError(t, err)
assert.Equal(t, tc.expectedStatus, resp.StatusCode)
if tc.expectedStatus == http.StatusOK {
assert.Equal(t, "application/protobuf", resp.Header.Get("Content-Type"))
}
if tc.expectedTrace != nil {
actualResp := &tempopb.TraceByIDResponse{}
bytesTrace, err := io.ReadAll(resp.Body)
Expand Down
3 changes: 3 additions & 0 deletions modules/querier/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ func (q *Querier) SearchHandler(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set(api.HeaderContentType, api.HeaderAcceptJSON)
}

func (q *Querier) SearchTagsHandler(w http.ResponseWriter, r *http.Request) {
Expand All @@ -217,6 +218,7 @@ func (q *Querier) SearchTagsHandler(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set(api.HeaderContentType, api.HeaderAcceptJSON)
}

func (q *Querier) SearchTagValuesHandler(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -249,4 +251,5 @@ func (q *Querier) SearchTagValuesHandler(w http.ResponseWriter, r *http.Request)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set(api.HeaderContentType, api.HeaderAcceptJSON)
}
1 change: 1 addition & 0 deletions pkg/api/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const (
urlParamVersion = "version"

HeaderAccept = "Accept"
HeaderContentType = "Content-Type"
HeaderAcceptProtobuf = "application/protobuf"
HeaderAcceptJSON = "application/json"

Expand Down

0 comments on commit fb474be

Please sign in to comment.