Skip to content

Commit 968b691

Browse files
committed
Add ability to attach arbitrary headers
1 parent 02fbeec commit 968b691

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

cmd/parca-push/main.go

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"google.golang.org/grpc"
3333
"google.golang.org/grpc/credentials"
3434
"google.golang.org/grpc/credentials/insecure"
35+
"google.golang.org/grpc/metadata"
3536
)
3637

3738
type flags struct {
@@ -45,11 +46,12 @@ type flags struct {
4546

4647
// FlagsRemoteStore provides remote store configuration flags.
4748
type FlagsRemoteStore struct {
48-
Address string `kong:"help='gRPC address to send profiles and symbols to.'"`
49-
BearerToken string `kong:"help='Bearer token to authenticate with store.'"`
50-
BearerTokenFile string `kong:"help='File to read bearer token from to authenticate with store.'"`
51-
Insecure bool `kong:"help='Send gRPC requests via plaintext instead of TLS.'"`
52-
InsecureSkipVerify bool `kong:"help='Skip TLS certificate verification.'"`
49+
Address string `kong:"help='gRPC address to send profiles and symbols to.'"`
50+
BearerToken string `kong:"help='Bearer token to authenticate with store.'"`
51+
BearerTokenFile string `kong:"help='File to read bearer token from to authenticate with store.'"`
52+
Insecure bool `kong:"help='Send gRPC requests via plaintext instead of TLS.'"`
53+
InsecureSkipVerify bool `kong:"help='Skip TLS certificate verification.'"`
54+
GRPCHeaders map[string]string `kong:"help='Additional gRPC headers to send with each request (key=value pairs).'"`
5355
}
5456

5557
func main() {
@@ -132,16 +134,33 @@ func run(flags flags) error {
132134
return g.Run()
133135
}
134136

137+
func customHeadersUnaryInterceptor(headers map[string]string) grpc.UnaryClientInterceptor {
138+
return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
139+
for key, value := range headers {
140+
ctx = metadata.AppendToOutgoingContext(ctx, key, value)
141+
}
142+
return invoker(ctx, method, req, reply, cc, opts...)
143+
}
144+
}
145+
135146
func grpcConn(reg prometheus.Registerer, flags FlagsRemoteStore) (*grpc.ClientConn, error) {
136147
met := grpc_prometheus.NewClientMetrics()
137148
met.EnableClientHandlingTimeHistogram()
138149
reg.MustRegister(met)
139150

140-
opts := []grpc.DialOption{
141-
grpc.WithUnaryInterceptor(
142-
met.UnaryClientInterceptor(),
143-
),
151+
opts := []grpc.DialOption{}
152+
153+
// Add custom headers interceptor first if headers are provided
154+
if len(flags.GRPCHeaders) > 0 {
155+
opts = append(opts, grpc.WithUnaryInterceptor(
156+
customHeadersUnaryInterceptor(flags.GRPCHeaders),
157+
))
144158
}
159+
160+
// Add metrics interceptor
161+
opts = append(opts, grpc.WithUnaryInterceptor(
162+
met.UnaryClientInterceptor(),
163+
))
145164
if flags.Insecure {
146165
opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
147166
} else {

0 commit comments

Comments
 (0)