Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/dev/gpctl/pkg/util/grpc.go
2500 views
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
}
38
39