-
Notifications
You must be signed in to change notification settings - Fork 524
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Query-Frontend: Add middleware to drop headers (#4298)
* header strip ware Signed-off-by: Joe Elliott <[email protected]> * comment Signed-off-by: Joe Elliott <[email protected]> * changelog Signed-off-by: Joe Elliott <[email protected]> * remove header strip wear from metrics summary Signed-off-by: Joe Elliott <[email protected]> --------- Signed-off-by: Joe Elliott <[email protected]>
- Loading branch information
1 parent
0ede155
commit 2bc0b62
Showing
10 changed files
with
122 additions
and
13 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
46 changes: 46 additions & 0 deletions
46
modules/frontend/pipeline/async_strip_headers_middleware.go
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package pipeline | ||
|
||
import ( | ||
"github.com/grafana/tempo/modules/frontend/combiner" | ||
) | ||
|
||
type stripHeadersWare struct { | ||
allowed map[string]struct{} | ||
next AsyncRoundTripper[combiner.PipelineResponse] | ||
} | ||
|
||
// NewStripHeadersWare creates a middleware that strips headers not in the allow list. This exists to reduce allocations further | ||
// down the pipeline. All request headers should be handled at the Combiner/Collector levels. Once the request is in the pipeline | ||
// nothing else needs HTTP headers. Stripping them out reduces allocations for copying, marshalling and unmashalling them to sometimes | ||
// 100s of thousands of subrequests. | ||
func NewStripHeadersWare(allowList []string) AsyncMiddleware[combiner.PipelineResponse] { | ||
// build allowed map | ||
allowed := make(map[string]struct{}, len(allowList)) | ||
for _, header := range allowList { | ||
allowed[header] = struct{}{} | ||
} | ||
|
||
return AsyncMiddlewareFunc[combiner.PipelineResponse](func(next AsyncRoundTripper[combiner.PipelineResponse]) AsyncRoundTripper[combiner.PipelineResponse] { | ||
return &stripHeadersWare{ | ||
next: next, | ||
allowed: allowed, | ||
} | ||
}) | ||
} | ||
|
||
func (c stripHeadersWare) RoundTrip(req Request) (Responses[combiner.PipelineResponse], error) { | ||
httpReq := req.HTTPRequest() | ||
|
||
if len(c.allowed) == 0 { | ||
clear(httpReq.Header) | ||
} else { | ||
// clear out headers not in allow list | ||
for header := range httpReq.Header { | ||
if _, ok := c.allowed[header]; !ok { | ||
delete(httpReq.Header, header) | ||
} | ||
} | ||
} | ||
|
||
return c.next.RoundTrip(req.CloneFromHTTPRequest(httpReq)) | ||
} |
55 changes: 55 additions & 0 deletions
55
modules/frontend/pipeline/async_strip_headers_middleware_test.go
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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package pipeline | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/grafana/tempo/modules/frontend/combiner" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestStripHeaders(t *testing.T) { | ||
tcs := []struct { | ||
name string | ||
allow []string | ||
headers map[string][]string | ||
expected http.Header | ||
}{ | ||
{ | ||
name: "empty allow list", | ||
allow: []string{}, | ||
headers: map[string][]string{"header1": {"value1"}, "header2": {"value2"}}, | ||
expected: map[string][]string{}, | ||
}, | ||
{ | ||
name: "allow list with one header", | ||
allow: []string{"header1"}, | ||
headers: map[string][]string{"header1": {"value1"}, "header2": {"value2"}}, | ||
expected: map[string][]string{"header1": {"value1"}}, | ||
}, | ||
} | ||
|
||
for _, tc := range tcs { | ||
t.Run(tc.name, func(t *testing.T) { | ||
next := AsyncRoundTripperFunc[combiner.PipelineResponse](func(req Request) (Responses[combiner.PipelineResponse], error) { | ||
actualHeaders := req.HTTPRequest().Header | ||
require.Equal(t, tc.expected, actualHeaders) | ||
|
||
return NewHTTPToAsyncResponse(&http.Response{ | ||
StatusCode: 200, | ||
Body: io.NopCloser(bytes.NewReader([]byte{})), | ||
}), nil | ||
}) | ||
|
||
stripHeaders := NewStripHeadersWare(tc.allow).Wrap(next) | ||
|
||
req, _ := http.NewRequest(http.MethodGet, "http://localhost:8080", nil) | ||
req.Header = tc.headers | ||
|
||
_, err := stripHeaders.RoundTrip(NewHTTPRequest(req)) | ||
require.NoError(t, err) | ||
}) | ||
} | ||
} |
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