Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set correct Content-Type header in query response #4828

Merged
merged 3 commits into from
Nov 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Main

* [4828](https://github.com/grafana/loki/pull/4282) **chaudum**: Set correct `Content-Type` header in query response
* [4794](https://github.com/grafana/loki/pull/4794) **taisho6339**: Aggregate inotify watcher to file target manager
* [4663](https://github.com/grafana/loki/pull/4663) **taisho6339**: Add SASL&mTLS authentication support for Kafka in Promtail
* [4745](https://github.com/grafana/loki/pull/4745) **taisho6339**: Expose Kafka message key in labels
Expand Down
14 changes: 5 additions & 9 deletions pkg/querier/worker_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,11 @@ func InitWorkerService(
authMiddleware middleware.Interface,
) (serve services.Service, err error) {

// Create a couple Middlewares used to handle panics, perform auth, and parse Form's in http request
internalMiddleware := middleware.Merge(
// Create a couple Middlewares used to handle panics, perform auth, parse forms in http request, and set content type in response
handlerMiddleware := middleware.Merge(
serverutil.RecoveryHTTPMiddleware,
authMiddleware,
serverutil.NewPrepopulateMiddleware(),
)
// External middleware also needs to set JSON content type headers
externalMiddleware := middleware.Merge(
internalMiddleware,
serverutil.ResponseJSONMiddleware(),
)

Expand All @@ -72,7 +68,7 @@ func InitWorkerService(
// There are some routes which are always registered on the external router, add them now and
// wrap them with the externalMiddleware
for route, handler := range alwaysExternalRoutesToHandlers {
externalRouter.Path(route).Methods("GET", "POST").Handler(externalMiddleware.Wrap(handler))
externalRouter.Path(route).Methods("GET", "POST").Handler(handlerMiddleware.Wrap(handler))
}

// If the querier is running standalone without the query-frontend or query-scheduler, we must register the internal
Expand All @@ -91,7 +87,7 @@ func InitWorkerService(

// Register routes externally
for _, route := range routes {
externalRouter.Path(route).Methods("GET", "POST").Handler(externalMiddleware.Wrap(internalRouter))
externalRouter.Path(route).Methods("GET", "POST").Handler(handlerMiddleware.Wrap(internalRouter))
}

//If no frontend or scheduler address has been configured, then there is no place for the
Expand Down Expand Up @@ -129,7 +125,7 @@ func InitWorkerService(
return "internalQuerier"
}))

internalHandler = internalMiddleware.Wrap(internalHandler)
internalHandler = handlerMiddleware.Wrap(internalHandler)

//Return a querier worker pointed to the internal querier HTTP handler so there is not a conflict in routes between the querier
//and the query frontend
Expand Down
4 changes: 4 additions & 0 deletions pkg/querier/worker_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ func Test_InitQuerierService(t *testing.T) {
})

t.Run("wrap external handler with response json middleware", func(t *testing.T) {
// note: this test only assures that the content type of the response is
// set if the handler function does not override it, which happens in the
// actual implementation, see
// https://github.com/grafana/loki/blob/34a012adcfade43291de3a7670f53679ea06aefe/pkg/lokifrontend/frontend/transport/handler.go#L136-L139
config := WorkerServiceConfig{
QueryFrontendEnabled: false,
QuerySchedulerEnabled: false,
Expand Down