Skip to content

Commit ac618fa

Browse files
Prince Rachit Sinharoboquat
Prince Rachit Sinha
authored andcommitted
Introduce client metadata headers
1 parent ad08ada commit ac618fa

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

dev/gpctl/cmd/clusters.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func getClustersClient(ctx context.Context) (*grpc.ClientConn, api.ClusterServic
8888
secopt = grpc.WithTransportCredentials(creds)
8989
}
9090

91-
conn, err := grpc.Dial(fmt.Sprintf("localhost:%s", localPort), secopt)
91+
conn, err := grpc.Dial(fmt.Sprintf("localhost:%s", localPort), secopt, util.WithClientUnaryInterceptor())
9292
if err != nil {
9393
return nil, nil, err
9494
}

dev/gpctl/cmd/imagebuilds.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func getImagebuildsClient(ctx context.Context) (*grpc.ClientConn, api.ImageBuild
7878
secopt = grpc.WithTransportCredentials(creds)
7979
}
8080

81-
conn, err := grpc.Dial(fmt.Sprintf("localhost:%d", freePort), secopt)
81+
conn, err := grpc.Dial(fmt.Sprintf("localhost:%d", freePort), secopt, util.WithClientUnaryInterceptor())
8282
if err != nil {
8383
return nil, nil, err
8484
}

dev/gpctl/cmd/workspaces.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func getWorkspacesClient(ctx context.Context) (*grpc.ClientConn, api.WorkspaceMa
133133

134134
secopt = grpc.WithTransportCredentials(creds)
135135
}
136-
conn, err := grpc.Dial(addr, secopt)
136+
conn, err := grpc.Dial(addr, secopt, util.WithClientUnaryInterceptor())
137137
if err != nil {
138138
return nil, nil, err
139139
}

dev/gpctl/pkg/util/grpc.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright (c) 2020 Gitpod GmbH. All rights reserved.
2+
// Licensed under the GNU Affero General Public License (AGPL).
3+
// See License-AGPL.txt in the project root for license information.
4+
5+
package util
6+
7+
import (
8+
"context"
9+
"os"
10+
11+
"google.golang.org/grpc"
12+
"google.golang.org/grpc/metadata"
13+
)
14+
15+
const clientNameKey = "client-name"
16+
17+
// create unique client names. Concatenate both HOST and HOSTNAME so that we can track the executions
18+
// from both linux and macos
19+
var clientName = "gpctl:" + os.Getenv("USER") + "@" + os.Getenv("HOSTNAME") + os.Getenv("HOST")
20+
21+
func clientInterceptor(
22+
ctx context.Context,
23+
method string,
24+
req interface{},
25+
reply interface{},
26+
cc *grpc.ClientConn,
27+
invoker grpc.UnaryInvoker,
28+
opts ...grpc.CallOption,
29+
) error {
30+
ctx = metadata.AppendToOutgoingContext(ctx, clientNameKey, clientName)
31+
err := invoker(ctx, method, req, reply, cc, opts...)
32+
return err
33+
}
34+
35+
func WithClientUnaryInterceptor() grpc.DialOption {
36+
return grpc.WithUnaryInterceptor(clientInterceptor)
37+
}

0 commit comments

Comments
 (0)