Skip to content

Commit

Permalink
feat: set zbctl user agent
Browse files Browse the repository at this point in the history
In order to distinguish between go-client and zbctl add a new possibility to set the UserAgent as client config.
Zbctl use that config and set their own user agent header.

This is useful to understand which client types and versions are mostly used.

(cherry picked from commit 329884a)
  • Loading branch information
Zelldon authored and github-actions[bot] committed May 31, 2022
1 parent b1cf5c2 commit 823bc5e
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
1 change: 1 addition & 0 deletions clients/go/cmd/zbctl/internal/commands/root.go
Expand Up @@ -131,6 +131,7 @@ var initClient = func(cmd *cobra.Command, args []string) error {
client, err = zbc.NewClient(&zbc.ClientConfig{
GatewayAddress: fmt.Sprintf("%s:%s", host, port),
CredentialsProvider: credsProvider,
UserAgent: "zeebe-client-zbctl/" + Version,
})
return err
}
Expand Down
11 changes: 9 additions & 2 deletions clients/go/pkg/zbc/client.go
Expand Up @@ -65,7 +65,10 @@ type ClientConfig struct {
// of 45 seconds being used
KeepAlive time.Duration

DialOpts []grpc.DialOption
// UserAgent is an optional field, to specify the user-agent header which should be sent by each
// gRPC request. Defaults to zeebe-client-go/%version.
UserAgent string
DialOpts []grpc.DialOption
}

// ErrFileNotFound is returned whenever a file can't be found at the provided path. Use this value to do error comparison.
Expand Down Expand Up @@ -158,7 +161,11 @@ func NewClient(config *ClientConfig) (Client, error) {
return nil, err
}

config.DialOpts = append(config.DialOpts, grpc.WithUserAgent("zeebe-client-go/"+getVersion()))
if config.UserAgent == "" {
config.UserAgent = "zeebe-client-go/" + getVersion()
}

config.DialOpts = append(config.DialOpts, grpc.WithUserAgent(config.UserAgent))

conn, err := grpc.Dial(config.GatewayAddress, config.DialOpts...)
if err != nil {
Expand Down
57 changes: 57 additions & 0 deletions clients/go/pkg/zbc/client_test.go
Expand Up @@ -18,6 +18,8 @@ import (
"context"
"errors"
"fmt"
"github.com/camunda/zeebe/clients/go/v8/internal/utils"
"google.golang.org/grpc/metadata"
"net"
"strconv"
"strings"
Expand Down Expand Up @@ -164,6 +166,61 @@ func (s *clientTestSuite) TestGatewayAddressEnvVar() {
s.EqualValues(fmt.Sprintf("0.0.0.0:%s", parts[len(parts)-1]), config.GatewayAddress)
}

func (s *clientTestSuite) TestDefaultUserAgent() {
// given
var incomingContext = make(map[string][]string)
lis, server := createServerWithUnaryInterceptor(func(ctx context.Context, _ interface{}, _ *grpc.UnaryServerInfo, _ grpc.UnaryHandler) (interface{}, error) {
incomingContext, _ = metadata.FromIncomingContext(ctx)
return nil, nil
})
go server.Serve(lis)
defer server.Stop()

// when
client, err := NewClient(&ClientConfig{
GatewayAddress: lis.Addr().String(),
UsePlaintextConnection: true,
})
s.Require().NoError(err)
ctx, cancel := context.WithTimeout(context.Background(), utils.DefaultTestTimeout)
defer cancel()

_, err = client.NewTopologyCommand().Send(ctx)
userAgent := incomingContext["user-agent"]

// then
s.Require().Len(userAgent, 1)
s.Require().Contains(userAgent[0], "zeebe-client-go/"+getVersion())
}

func (s *clientTestSuite) TestSpecificUserAgent() {
// given
var incomingContext = make(map[string][]string)
lis, server := createServerWithUnaryInterceptor(func(ctx context.Context, _ interface{}, _ *grpc.UnaryServerInfo, _ grpc.UnaryHandler) (interface{}, error) {
incomingContext, _ = metadata.FromIncomingContext(ctx)
return nil, nil
})
go server.Serve(lis)
defer server.Stop()

// when
client, err := NewClient(&ClientConfig{
GatewayAddress: lis.Addr().String(),
UsePlaintextConnection: true,
UserAgent: "anotherUserAgentLikeZbctl",
})
s.Require().NoError(err)
ctx, cancel := context.WithTimeout(context.Background(), utils.DefaultTestTimeout)
defer cancel()

_, err = client.NewTopologyCommand().Send(ctx)
userAgent := incomingContext["user-agent"]

// then
s.Require().Len(userAgent, 1)
s.Require().Contains(userAgent[0], "anotherUserAgentLikeZbctl")
}

func (s *clientTestSuite) TestCaCertificateEnvVar() {
// given
lis, grpcServer := createSecureServer(false)
Expand Down

0 comments on commit 823bc5e

Please sign in to comment.