...

Source file src/go.mongodb.org/mongo-driver/event/doc.go

Documentation: go.mongodb.org/mongo-driver/event

     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
    57  

View as plain text