-
-
Save prestonvasquez/9ef20438f86668c9128ef118ef63aafa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"log" | |
"time" | |
"go.mongodb.org/mongo-driver/bson" | |
"go.mongodb.org/mongo-driver/event" | |
"go.mongodb.org/mongo-driver/mongo" | |
"go.mongodb.org/mongo-driver/mongo/options" | |
) | |
func main() { | |
// Construct a command monitor that logs the raw max time ms from the WM | |
// written to the server. | |
cmdMonitor := &event.CommandMonitor{ | |
Started: func(_ context.Context, cse *event.CommandStartedEvent) { | |
maxTimeMSRaw, err := cse.Command.LookupErr("maxTimeMS") | |
if err != nil { | |
log.Printf("failed to lookup maxTimeMS: %v\n", err) | |
return | |
} | |
log.Println("raw MaxTimeMS: ", maxTimeMSRaw.String()) | |
}, | |
} | |
opts := options.Client().ApplyURI("mongodb://localhost:27017").SetMonitor(cmdMonitor) | |
client, err := mongo.Connect(context.Background(), opts) | |
if err != nil { | |
log.Fatalf("failed to connect to server: %v", err) | |
} | |
defer func() { _ = client.Disconnect(context.Background()) }() | |
coll := client.Database("testdb").Collection("coll") | |
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) | |
defer cancel() | |
_, err = coll.InsertOne(ctx, bson.D{{"x", 1}}) | |
if err != nil { | |
log.Fatalf("failed to insert data: %v", err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment