@@ -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
3738type flags struct {
@@ -45,11 +46,12 @@ type flags struct {
4546
4647// FlagsRemoteStore provides remote store configuration flags.
4748type 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
5557func 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+
135146func 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