-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Turn frontend Tripperware into a Middleware. (#10688)
**What this PR does / why we need it**: Currently, a request to Loki's frontend API goes through these conversions: ``` http.Request ↓ limitedRoundTripper queryrangebase.Request ↓ queryrangebase.Middlware … ↓ queryrangebase.Request ↓ limitedRoundTripper http.Request ↓ grpcRoundTripperAdapter httpgrpc ↓ grpcRoundTripperAdapter http.Response ↓ limitedRoundTripper queryrangebase.Response ↓ limitedRoundtripper http.Response ``` Since `httgrpc` and `queryrangebase.Request` are Protobufs there's no good reason to encode and decode them to HTTP responses/requests. Furthermore, the encoding to HTTP makes it harder for us to encode query plans. Thus the conversions are changed to the following: ``` http.Request ↓ queryrangebase.Request ↓ queryrangebase.Middlware … ↓ queryrangebase.Request ↓ httpgrpc ↓ queryrangebase.Response ↓ http.Response ``` In order to achieve this the `http.RoundTripper` is pushed to the outside. Only the serialization layer from `http.Request` to `queryrangebase.Request` and `http.Response` to `queryrangebase.Response` will be an `http.RoundTripper`. Everything else is either a `queryrangebase.Handler` or `queryrangebase.Middleware`. **Checklist** - [ ] Reviewed the [`CONTRIBUTING.md`](https://github.com/grafana/loki/blob/main/CONTRIBUTING.md) guide (**required**) - [ ] Documentation added - [x] Tests updated - [ ] `CHANGELOG.md` updated - [ ] If the change is worth mentioning in the release notes, add `add-to-release-notes` label - [ ] Changes that require user attention or interaction to upgrade are documented in `docs/sources/setup/upgrade/_index.md` - [ ] For Helm chart changes bump the Helm chart version in `production/helm/loki/Chart.yaml` and update `production/helm/loki/CHANGELOG.md` and `production/helm/loki/README.md`. [Example PR](d10549e)
- Loading branch information
Showing
28 changed files
with
844 additions
and
1,229 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,19 @@ | ||
package transport | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"io" | ||
"net/http" | ||
|
||
"github.com/grafana/dskit/httpgrpc" | ||
"github.com/grafana/dskit/httpgrpc/server" | ||
|
||
"github.com/grafana/loki/pkg/querier/queryrange/queryrangebase" | ||
) | ||
|
||
// GrpcRoundTripper is similar to http.RoundTripper, but works with HTTP requests converted to protobuf messages. | ||
type GrpcRoundTripper interface { | ||
RoundTripGRPC(context.Context, *httpgrpc.HTTPRequest) (*httpgrpc.HTTPResponse, error) | ||
} | ||
|
||
func AdaptGrpcRoundTripperToHTTPRoundTripper(r GrpcRoundTripper) http.RoundTripper { | ||
return &grpcRoundTripperAdapter{roundTripper: r} | ||
} | ||
|
||
// This adapter wraps GrpcRoundTripper and converted it into http.RoundTripper | ||
type grpcRoundTripperAdapter struct { | ||
roundTripper GrpcRoundTripper | ||
} | ||
|
||
type buffer struct { | ||
buff []byte | ||
io.ReadCloser | ||
} | ||
|
||
func (b *buffer) Bytes() []byte { | ||
return b.buff | ||
} | ||
|
||
func (a *grpcRoundTripperAdapter) RoundTrip(r *http.Request) (*http.Response, error) { | ||
req, err := server.HTTPRequest(r) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resp, err := a.roundTripper.RoundTripGRPC(r.Context(), req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
httpResp := &http.Response{ | ||
StatusCode: int(resp.Code), | ||
Body: &buffer{buff: resp.Body, ReadCloser: io.NopCloser(bytes.NewReader(resp.Body))}, | ||
Header: http.Header{}, | ||
ContentLength: int64(len(resp.Body)), | ||
} | ||
for _, h := range resp.Headers { | ||
httpResp.Header[h.Key] = h.Values | ||
} | ||
return httpResp, nil | ||
type Codec interface { | ||
queryrangebase.Codec | ||
DecodeHTTPGrpcResponse(r *httpgrpc.HTTPResponse, req queryrangebase.Request) (queryrangebase.Response, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.