Skip to content

Commit

Permalink
fix(spanner): pass userAgent to cloud spanner requests (#6598)
Browse files Browse the repository at this point in the history
* fix(spanner): pass userAgent to cloud spanner requests

* incorporating requested changes
  • Loading branch information
rahul2393 committed Sep 2, 2022
1 parent eedc632 commit 59d162b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
11 changes: 10 additions & 1 deletion spanner/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,15 @@ type ClientConfig struct {
// override the default values.
CallOptions *vkit.CallOptions

// UserAgent is the prefix to the user agent header. This is used to supply information
// such as application name or partner tool.
//
// Internal Use Only: This field is for internal tracking purpose only,
// setting the value for this config is not required.
//
// Recommended format: ``application-or-tool-ID/major.minor.version``.
UserAgent string

// logger is the logger to use for this client. If it is nil, all logging
// will be directed to the standard logger.
logger *log.Logger
Expand Down Expand Up @@ -211,7 +220,7 @@ func NewClientWithConfig(ctx context.Context, database string, config ClientConf
config.incStep = DefaultSessionPoolConfig.incStep
}
// Create a session client.
sc := newSessionClient(pool, database, sessionLabels, metadata.Pairs(resourcePrefixHeader, database), config.logger, config.CallOptions)
sc := newSessionClient(pool, database, config.UserAgent, sessionLabels, metadata.Pairs(resourcePrefixHeader, database), config.logger, config.CallOptions)
// Create a session pool.
config.SessionPoolConfig.sessionLabels = sessionLabels
sp, err := newSessionPool(sc, config.SessionPoolConfig)
Expand Down
14 changes: 12 additions & 2 deletions spanner/sessionclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"log"
"reflect"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -90,6 +91,7 @@ type sessionClient struct {
connPool gtransport.ConnPool
database string
id string
userAgent string
sessionLabels map[string]string
md metadata.MD
batchTimeout time.Duration
Expand All @@ -98,10 +100,11 @@ type sessionClient struct {
}

// newSessionClient creates a session client to use for a database.
func newSessionClient(connPool gtransport.ConnPool, database string, sessionLabels map[string]string, md metadata.MD, logger *log.Logger, callOptions *vkit.CallOptions) *sessionClient {
func newSessionClient(connPool gtransport.ConnPool, database, userAgent string, sessionLabels map[string]string, md metadata.MD, logger *log.Logger, callOptions *vkit.CallOptions) *sessionClient {
return &sessionClient{
connPool: connPool,
database: database,
userAgent: userAgent,
id: cidGen.nextID(database),
sessionLabels: sessionLabels,
md: md,
Expand Down Expand Up @@ -322,7 +325,14 @@ func (sc *sessionClient) nextClient() (*vkit.Client, error) {
if err != nil {
return nil, err
}
client.SetGoogleClientInfo("gccl", internal.Version)
clientInfo := []string{"gccl", internal.Version}
if sc.userAgent != "" {
agentWithVersion := strings.SplitN(sc.userAgent, "/", 2)
if len(agentWithVersion) == 2 {
clientInfo = append(clientInfo, agentWithVersion[0], agentWithVersion[1])
}
}
client.SetGoogleClientInfo(clientInfo...)
if sc.callOptions != nil {
client.CallOptions = mergeCallOptions(client.CallOptions, sc.callOptions)
}
Expand Down

0 comments on commit 59d162b

Please sign in to comment.