-
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.
fix(querytags): propagate query tags to ingesters (#12267)
Co-authored-by: Travis Patterson <[email protected]>
- Loading branch information
1 parent
74b2d04
commit ef742c4
Showing
5 changed files
with
222 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package server | ||
|
||
import ( | ||
"context" | ||
|
||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/metadata" | ||
|
||
"github.com/grafana/loki/pkg/util/httpreq" | ||
) | ||
|
||
func getQueryTags(ctx context.Context) string { | ||
v, _ := ctx.Value(httpreq.QueryTagsHTTPHeader).(string) | ||
return v | ||
} | ||
|
||
func injectIntoGRPCRequest(ctx context.Context) context.Context { | ||
queryTags := getQueryTags(ctx) | ||
if queryTags == "" { | ||
return ctx | ||
} | ||
|
||
// inject into GRPC metadata | ||
md, ok := metadata.FromOutgoingContext(ctx) | ||
if !ok { | ||
md = metadata.New(map[string]string{}) | ||
} | ||
md = md.Copy() | ||
md.Set(string(httpreq.QueryTagsHTTPHeader), queryTags) | ||
|
||
return metadata.NewOutgoingContext(ctx, md) | ||
} | ||
|
||
func extractFromGRPCRequest(ctx context.Context) context.Context { | ||
md, ok := metadata.FromIncomingContext(ctx) | ||
if !ok { | ||
// No metadata, just return as is | ||
return ctx | ||
} | ||
|
||
headerValues := md.Get(string(httpreq.QueryTagsHTTPHeader)) | ||
if len(headerValues) == 0 { | ||
return ctx | ||
} | ||
|
||
return context.WithValue(ctx, httpreq.QueryTagsHTTPHeader, headerValues[0]) | ||
} | ||
|
||
// UnaryClientQueryTagsInterceptor propagates the query tags from the context to gRPC metadata, which eventually ends up as a HTTP2 header. | ||
// For unary gRPC requests. | ||
func UnaryClientQueryTagsInterceptor(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { | ||
return invoker(injectIntoGRPCRequest(ctx), method, req, reply, cc, opts...) | ||
} | ||
|
||
// StreamClientQueryTagsInterceptor propagates the query tags from the context to gRPC metadata, which eventually ends up as a HTTP2 header. | ||
// For streaming gRPC requests. | ||
func StreamClientQueryTagsInterceptor(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) { | ||
return streamer(injectIntoGRPCRequest(ctx), desc, cc, method, opts...) | ||
} | ||
|
||
// UnaryServerQueryTagsInterceptor propagates the query tags from the gRPC metadata back to our context for unary gRPC requests. | ||
func UnaryServerQueryTagsInterceptor(ctx context.Context, req interface{}, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { | ||
return handler(extractFromGRPCRequest(ctx), req) | ||
} | ||
|
||
// StreamServerQueryTagsInterceptor propagates the query tags from the gRPC metadata back to our context for streaming gRPC requests. | ||
func StreamServerQueryTagsInterceptor(srv interface{}, ss grpc.ServerStream, _ *grpc.StreamServerInfo, handler grpc.StreamHandler) error { | ||
return handler(srv, serverStream{ | ||
ctx: extractFromGRPCRequest(ss.Context()), | ||
ServerStream: ss, | ||
}) | ||
} | ||
|
||
type serverStream struct { | ||
ctx context.Context | ||
grpc.ServerStream | ||
} | ||
|
||
func (ss serverStream) Context() context.Context { | ||
return ss.ctx | ||
} |
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,81 @@ | ||
package server | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"google.golang.org/grpc/metadata" | ||
|
||
"github.com/grafana/loki/pkg/util/httpreq" | ||
) | ||
|
||
func TestInjectQueryTagsIntoGRPCRequest(t *testing.T) { | ||
for _, tt := range []struct { | ||
name, tags string | ||
md, expectMetadata metadata.MD | ||
}{ | ||
{ | ||
name: "creates new metadata and sets query tags", | ||
tags: "Source=logvolhist", | ||
expectMetadata: metadata.New(map[string]string{"x-query-tags": "Source=logvolhist"}), | ||
}, | ||
{ | ||
name: "sets query tags on existing metadata", | ||
tags: "Source=logvolhist", | ||
md: metadata.New(map[string]string{"x-foo": "bar"}), | ||
expectMetadata: metadata.New(map[string]string{"x-foo": "bar", "x-query-tags": "Source=logvolhist"}), | ||
}, | ||
{ | ||
name: "no query tags, leave metadata untouched", | ||
md: metadata.New(map[string]string{"x-foo": "bar"}), | ||
expectMetadata: metadata.New(map[string]string{"x-foo": "bar"}), | ||
}, | ||
{ | ||
name: "no query tags", | ||
expectMetadata: nil, | ||
}, | ||
} { | ||
t.Run(tt.name, func(t *testing.T) { | ||
ctx := context.Background() | ||
if tt.tags != "" { | ||
ctx = httpreq.InjectQueryTags(context.Background(), tt.tags) | ||
} | ||
|
||
if tt.md != nil { | ||
ctx = metadata.NewOutgoingContext(ctx, tt.md) | ||
} | ||
|
||
ctx = injectIntoGRPCRequest(ctx) | ||
md, _ := metadata.FromOutgoingContext(ctx) | ||
require.EqualValues(t, tt.expectMetadata, md) | ||
}) | ||
} | ||
} | ||
|
||
func TestExtractQueryTagsFromGRPCRequest(t *testing.T) { | ||
for _, tt := range []struct { | ||
name string | ||
md metadata.MD | ||
expectedResp string | ||
}{ | ||
{ | ||
name: "extracts query tags from metadata", | ||
md: metadata.New(map[string]string{"x-query-tags": "Source=logvolhist"}), | ||
expectedResp: "Source=logvolhist", | ||
}, | ||
{ | ||
name: "non-nil metadata without query tags", | ||
md: metadata.New(map[string]string{"x-foo": "bar"}), | ||
}, | ||
{ | ||
name: "nil metadata", | ||
}, | ||
} { | ||
t.Run(tt.name, func(t *testing.T) { | ||
ctx := metadata.NewIncomingContext(context.Background(), tt.md) | ||
require.Equal(t, tt.expectedResp, getQueryTags(extractFromGRPCRequest(ctx))) | ||
}) | ||
} | ||
|
||
} |