Skip to content

Commit 2524573

Browse files
committed
GODRIVER-1817 add docs.go for event package (#567)
1 parent 3914deb commit 2524573

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

event/doc.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright (C) MongoDB, Inc. 2017-present.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"); you may
4+
// not use this file except in compliance with the License. You may obtain
5+
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6+
7+
// Package event is a library for monitoring events from the MongoDB Go
8+
// driver. Monitors can be set for commands sent to the MongoDB cluster,
9+
// connection pool changes, or changes on the MongoDB cluster.
10+
//
11+
// Monitoring commands requires specifying a CommandMonitor when constructing
12+
// a mongo.Client. A CommandMonitor can be set to monitor started, succeeded,
13+
// and/or failed events. A CommandStartedEvent can be correlated to its matching
14+
// CommandSucceededEvent or CommandFailedEvent through the RequestID field. For
15+
// example, the following code collects the names of started events:
16+
//
17+
// var commandStarted []string
18+
// cmdMonitor := &event.CommandMonitor{
19+
// Started: func(_ context.Context, evt *event.CommandStartedEvent) {
20+
// commandStarted = append(commandStarted, evt.CommandName)
21+
// },
22+
// }
23+
// clientOpts := options.Client().ApplyURI("mongodb://localhost:27017").SetMonitor(cmdMonitor)
24+
// client, err := mongo.Connect(context.Background(), clientOpts)
25+
//
26+
// Monitoring the connection pool requires specifying a PoolMonitor when constructing
27+
// a mongo.Client. The following code tracks the number of checked out connections:
28+
//
29+
// var int connsCheckedOut
30+
// poolMonitor := &event.PoolMonitor{
31+
// Event: func(evt *event.PoolEvent) {
32+
// switch evt.Type {
33+
// case event.GetSucceeded:
34+
// connsCheckedOut++
35+
// case event.ConnectionReturned:
36+
// connsCheckedOut--
37+
// }
38+
// },
39+
// }
40+
// clientOpts := options.Client().ApplyURI("mongodb://localhost:27017").SetPoolMonitor(poolMonitor)
41+
// client, err := mongo.Connect(context.Background(), clientOpts)
42+
//
43+
// Monitoring server changes specifying a ServerMonitor object when constructing
44+
// a mongo.Client. Different functions can be set on the ServerMonitor to
45+
// monitor different kinds of events. See ServerMonitor for more details.
46+
// The following code appends ServerHeartbeatStartedEvents to a slice:
47+
//
48+
// var heartbeatStarted []*event.ServerHeartbeatStartedEvent
49+
// svrMonitor := &event.ServerMonitor{
50+
// ServerHeartbeatStarted: func(e *event.ServerHeartbeatStartedEvent) {
51+
// heartbeatStarted = append(heartbeatStarted, e)
52+
// }
53+
// }
54+
// clientOpts := options.Client().ApplyURI("mongodb://localhost:27017").SetServerMonitor(svrMonitor)
55+
// client, err := mongo.Connect(context.Background(), clientOpts)
56+
package event

event/examples_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright (C) MongoDB, Inc. 2017-present.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"); you may
4+
// not use this file except in compliance with the License. You may obtain
5+
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6+
7+
package event_test
8+
9+
import (
10+
"context"
11+
"log"
12+
13+
"go.mongodb.org/mongo-driver/bson"
14+
"go.mongodb.org/mongo-driver/event"
15+
"go.mongodb.org/mongo-driver/mongo"
16+
"go.mongodb.org/mongo-driver/mongo/options"
17+
)
18+
19+
// Event examples
20+
21+
// CommandMonitor represents a monitor that is triggered for different events.
22+
func ExampleCommandMonitor() {
23+
// If the application makes multiple concurrent requests, it would have to
24+
// use a concurrent map like sync.Map
25+
startedCommands := make(map[int64]bson.Raw)
26+
cmdMonitor := &event.CommandMonitor{
27+
Started: func(_ context.Context, evt *event.CommandStartedEvent) {
28+
startedCommands[evt.RequestID] = evt.Command
29+
},
30+
Succeeded: func(_ context.Context, evt *event.CommandSucceededEvent) {
31+
log.Printf("Command: %v Reply: %v\n",
32+
startedCommands[evt.RequestID],
33+
evt.Reply,
34+
)
35+
},
36+
Failed: func(_ context.Context, evt *event.CommandFailedEvent) {
37+
log.Printf("Command: %v Failure: %v\n",
38+
startedCommands[evt.RequestID],
39+
evt.Failure,
40+
)
41+
},
42+
}
43+
clientOpts := options.Client().ApplyURI("mongodb://localhost:27017").SetMonitor(cmdMonitor)
44+
client, err := mongo.Connect(context.Background(), clientOpts)
45+
if err != nil {
46+
log.Fatal(err)
47+
}
48+
defer func() {
49+
if err = client.Disconnect(context.TODO()); err != nil {
50+
log.Fatal(err)
51+
}
52+
}()
53+
}

0 commit comments

Comments
 (0)