Skip to content

Commit c2d06af

Browse files
author
iwysiu
authored
GODRIVER-1405 set appname on ServerConfig (#226)
1 parent f4f4ef0 commit c2d06af

File tree

6 files changed

+27
-4
lines changed

6 files changed

+27
-4
lines changed

x/mongo/driver/examples/server_monitoring/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func main() {
2222
topology.WithHeartbeatInterval(func(time.Duration) time.Duration { return 2 * time.Second }),
2323
topology.WithConnectionOptions(
2424
func(opts ...topology.ConnectionOption) []topology.ConnectionOption {
25-
return append(opts, topology.WithAppName(func(string) string { return "server monitoring test" }))
25+
return append(opts, topology.WithConnectionAppName(func(string) string { return "server monitoring test" }))
2626
},
2727
),
2828
)

x/mongo/driver/topology/connection_options.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ func withServerDescriptionCallback(callback func(description.Server), opts ...Co
8484
// ConnectionOption is used to configure a connection.
8585
type ConnectionOption func(*connectionConfig) error
8686

87-
// WithAppName sets the application name which gets sent to MongoDB when it
87+
// WithConnectionAppName sets the application name which gets sent to MongoDB when it
8888
// first connects.
89-
func WithAppName(fn func(string) string) ConnectionOption {
89+
func WithConnectionAppName(fn func(string) string) ConnectionOption {
9090
return func(c *connectionConfig) error {
9191
c.appName = fn(c.appName)
9292
return nil

x/mongo/driver/topology/server_options.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ func WithCompressionOptions(fn func(...string) []string) ServerOption {
6868
}
6969
}
7070

71+
// WithServerAppName configures the server's application name.
72+
func WithServerAppName(fn func(string) string) ServerOption {
73+
return func(cfg *serverConfig) error {
74+
cfg.appname = fn(cfg.appname)
75+
return nil
76+
}
77+
}
78+
7179
// WithHeartbeatInterval configures a server's heartbeat interval.
7280
func WithHeartbeatInterval(fn func(time.Duration) time.Duration) ServerOption {
7381
return func(cfg *serverConfig) error {

x/mongo/driver/topology/server_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,14 @@ func TestServer(t *testing.T) {
290290
t.Fatal("client metadata not expected in heartbeat but found")
291291
}
292292
})
293+
t.Run("WithServerAppName", func(t *testing.T) {
294+
name := "test"
295+
296+
s, err := NewServer(address.Address("localhost"),
297+
WithServerAppName(func(string) string { return name }))
298+
require.Nil(t, err, "error from NewServer: %v", err)
299+
require.Equal(t, name, s.cfg.appname, "expected appname to be: %v, got: %v", name, s.cfg.appname)
300+
})
293301
}
294302

295303
func includesMetadata(t *testing.T, wm []byte) bool {

x/mongo/driver/topology/topology_options.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ func WithConnString(fn func(connstring.ConnString) connstring.ConnString) Option
6464
var connOpts []ConnectionOption
6565

6666
if cs.AppName != "" {
67-
connOpts = append(connOpts, WithAppName(func(string) string { return cs.AppName }))
67+
connOpts = append(connOpts, WithConnectionAppName(func(string) string { return cs.AppName }))
68+
c.serverOpts = append(c.serverOpts, WithServerAppName(func(string) string { return cs.AppName }))
6869
}
6970

7071
switch cs.Connect {

x/mongo/driver/topology/topology_options_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,23 @@ import (
1717
func TestOptionsSetting(t *testing.T) {
1818
conf := &config{}
1919
ssts := time.Minute
20+
name := "test"
2021
assert.Zero(t, conf.cs)
2122

2223
opt := WithConnString(func(connstring.ConnString) connstring.ConnString {
2324
return connstring.ConnString{
2425
ServerSelectionTimeout: ssts,
2526
ServerSelectionTimeoutSet: true,
27+
AppName: name,
2628
}
2729

2830
})
2931

3032
assert.NoError(t, opt(conf))
3133

3234
assert.Equal(t, ssts, conf.serverSelectionTimeout)
35+
36+
serverConf, err := newServerConfig(conf.serverOpts...)
37+
assert.Nil(t, err, "error from newServerConfig: %v", err)
38+
assert.Equal(t, name, serverConf.appname, "expected appname to be: %v, got: %v", name, serverConf.appname)
3339
}

0 commit comments

Comments
 (0)