github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/vendor_skip/go.mongodb.org/mongo-driver/event/monitoring.go (about)

     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 // import "go.mongodb.org/mongo-driver/event"
     8  
     9  import (
    10  	"context"
    11  	"time"
    12  
    13  	"go.mongodb.org/mongo-driver/bson"
    14  	"go.mongodb.org/mongo-driver/bson/primitive"
    15  	"go.mongodb.org/mongo-driver/mongo/address"
    16  	"go.mongodb.org/mongo-driver/mongo/description"
    17  )
    18  
    19  // CommandStartedEvent represents an event generated when a command is sent to a server.
    20  type CommandStartedEvent struct {
    21  	Command      bson.Raw
    22  	DatabaseName string
    23  	CommandName  string
    24  	RequestID    int64
    25  	ConnectionID string
    26  	// ServerConnectionID contains the connection ID from the server of the operation. If the server does not return
    27  	// this value (e.g. on MDB < 4.2), it is unset. If the server connection ID would cause an int32 overflow, then
    28  	// then this field will be nil.
    29  	//
    30  	// Deprecated: Use ServerConnectionID64.
    31  	ServerConnectionID *int32
    32  	// ServerConnectionID64 contains the connection ID from the server of the operation. If the server does not
    33  	// return this value (e.g. on MDB < 4.2), it is unset.
    34  	ServerConnectionID64 *int64
    35  	// ServiceID contains the ID of the server to which the command was sent if it is running behind a load balancer.
    36  	// Otherwise, it is unset.
    37  	ServiceID *primitive.ObjectID
    38  }
    39  
    40  // CommandFinishedEvent represents a generic command finishing.
    41  type CommandFinishedEvent struct {
    42  	// Deprecated: Use Duration instead.
    43  	DurationNanos int64
    44  	Duration      time.Duration
    45  	CommandName   string
    46  	RequestID     int64
    47  	ConnectionID  string
    48  	// ServerConnectionID contains the connection ID from the server of the operation. If the server does not return
    49  	// this value (e.g. on MDB < 4.2), it is unset.If the server connection ID would cause an int32 overflow, then
    50  	// this field will be nil.
    51  	//
    52  	// Deprecated: Use ServerConnectionID64.
    53  	ServerConnectionID *int32
    54  	// ServerConnectionID64 contains the connection ID from the server of the operation. If the server does not
    55  	// return this value (e.g. on MDB < 4.2), it is unset.
    56  	ServerConnectionID64 *int64
    57  	// ServiceID contains the ID of the server to which the command was sent if it is running behind a load balancer.
    58  	// Otherwise, it is unset.
    59  	ServiceID *primitive.ObjectID
    60  }
    61  
    62  // CommandSucceededEvent represents an event generated when a command's execution succeeds.
    63  type CommandSucceededEvent struct {
    64  	CommandFinishedEvent
    65  	Reply bson.Raw
    66  }
    67  
    68  // CommandFailedEvent represents an event generated when a command's execution fails.
    69  type CommandFailedEvent struct {
    70  	CommandFinishedEvent
    71  	Failure string
    72  }
    73  
    74  // CommandMonitor represents a monitor that is triggered for different events.
    75  type CommandMonitor struct {
    76  	Started   func(context.Context, *CommandStartedEvent)
    77  	Succeeded func(context.Context, *CommandSucceededEvent)
    78  	Failed    func(context.Context, *CommandFailedEvent)
    79  }
    80  
    81  // strings for pool command monitoring reasons
    82  const (
    83  	ReasonIdle              = "idle"
    84  	ReasonPoolClosed        = "poolClosed"
    85  	ReasonStale             = "stale"
    86  	ReasonConnectionErrored = "connectionError"
    87  	ReasonTimedOut          = "timeout"
    88  	ReasonError             = "error"
    89  )
    90  
    91  // strings for pool command monitoring types
    92  const (
    93  	PoolCreated        = "ConnectionPoolCreated"
    94  	PoolReady          = "ConnectionPoolReady"
    95  	PoolCleared        = "ConnectionPoolCleared"
    96  	PoolClosedEvent    = "ConnectionPoolClosed"
    97  	ConnectionCreated  = "ConnectionCreated"
    98  	ConnectionReady    = "ConnectionReady"
    99  	ConnectionClosed   = "ConnectionClosed"
   100  	GetStarted         = "ConnectionCheckOutStarted"
   101  	GetFailed          = "ConnectionCheckOutFailed"
   102  	GetSucceeded       = "ConnectionCheckedOut"
   103  	ConnectionReturned = "ConnectionCheckedIn"
   104  )
   105  
   106  // MonitorPoolOptions contains pool options as formatted in pool events
   107  type MonitorPoolOptions struct {
   108  	MaxPoolSize        uint64 `json:"maxPoolSize"`
   109  	MinPoolSize        uint64 `json:"minPoolSize"`
   110  	WaitQueueTimeoutMS uint64 `json:"maxIdleTimeMS"`
   111  }
   112  
   113  // PoolEvent contains all information summarizing a pool event
   114  type PoolEvent struct {
   115  	Type         string              `json:"type"`
   116  	Address      string              `json:"address"`
   117  	ConnectionID uint64              `json:"connectionId"`
   118  	PoolOptions  *MonitorPoolOptions `json:"options"`
   119  	Reason       string              `json:"reason"`
   120  	// ServiceID is only set if the Type is PoolCleared and the server is deployed behind a load balancer. This field
   121  	// can be used to distinguish between individual servers in a load balanced deployment.
   122  	ServiceID *primitive.ObjectID `json:"serviceId"`
   123  	Error     error               `json:"error"`
   124  }
   125  
   126  // PoolMonitor is a function that allows the user to gain access to events occurring in the pool
   127  type PoolMonitor struct {
   128  	Event func(*PoolEvent)
   129  }
   130  
   131  // ServerDescriptionChangedEvent represents a server description change.
   132  type ServerDescriptionChangedEvent struct {
   133  	Address             address.Address
   134  	TopologyID          primitive.ObjectID // A unique identifier for the topology this server is a part of
   135  	PreviousDescription description.Server
   136  	NewDescription      description.Server
   137  }
   138  
   139  // ServerOpeningEvent is an event generated when the server is initialized.
   140  type ServerOpeningEvent struct {
   141  	Address    address.Address
   142  	TopologyID primitive.ObjectID // A unique identifier for the topology this server is a part of
   143  }
   144  
   145  // ServerClosedEvent is an event generated when the server is closed.
   146  type ServerClosedEvent struct {
   147  	Address    address.Address
   148  	TopologyID primitive.ObjectID // A unique identifier for the topology this server is a part of
   149  }
   150  
   151  // TopologyDescriptionChangedEvent represents a topology description change.
   152  type TopologyDescriptionChangedEvent struct {
   153  	TopologyID          primitive.ObjectID // A unique identifier for the topology this server is a part of
   154  	PreviousDescription description.Topology
   155  	NewDescription      description.Topology
   156  }
   157  
   158  // TopologyOpeningEvent is an event generated when the topology is initialized.
   159  type TopologyOpeningEvent struct {
   160  	TopologyID primitive.ObjectID // A unique identifier for the topology this server is a part of
   161  }
   162  
   163  // TopologyClosedEvent is an event generated when the topology is closed.
   164  type TopologyClosedEvent struct {
   165  	TopologyID primitive.ObjectID // A unique identifier for the topology this server is a part of
   166  }
   167  
   168  // ServerHeartbeatStartedEvent is an event generated when the heartbeat is started.
   169  type ServerHeartbeatStartedEvent struct {
   170  	ConnectionID string // The address this heartbeat was sent to with a unique identifier
   171  	Awaited      bool   // If this heartbeat was awaitable
   172  }
   173  
   174  // ServerHeartbeatSucceededEvent is an event generated when the heartbeat succeeds.
   175  type ServerHeartbeatSucceededEvent struct {
   176  	// Deprecated: Use Duration instead.
   177  	DurationNanos int64
   178  	Duration      time.Duration
   179  	Reply         description.Server
   180  	ConnectionID  string // The address this heartbeat was sent to with a unique identifier
   181  	Awaited       bool   // If this heartbeat was awaitable
   182  }
   183  
   184  // ServerHeartbeatFailedEvent is an event generated when the heartbeat fails.
   185  type ServerHeartbeatFailedEvent struct {
   186  	// Deprecated: Use Duration instead.
   187  	DurationNanos int64
   188  	Duration      time.Duration
   189  	Failure       error
   190  	ConnectionID  string // The address this heartbeat was sent to with a unique identifier
   191  	Awaited       bool   // If this heartbeat was awaitable
   192  }
   193  
   194  // ServerMonitor represents a monitor that is triggered for different server events. The client
   195  // will monitor changes on the MongoDB deployment it is connected to, and this monitor reports
   196  // the changes in the client's representation of the deployment. The topology represents the
   197  // overall deployment, and heartbeats are sent to individual servers to check their current status.
   198  type ServerMonitor struct {
   199  	ServerDescriptionChanged func(*ServerDescriptionChangedEvent)
   200  	ServerOpening            func(*ServerOpeningEvent)
   201  	ServerClosed             func(*ServerClosedEvent)
   202  	// TopologyDescriptionChanged is called when the topology is locked, so the callback should
   203  	// not attempt any operation that requires server selection on the same client.
   204  	TopologyDescriptionChanged func(*TopologyDescriptionChangedEvent)
   205  	TopologyOpening            func(*TopologyOpeningEvent)
   206  	TopologyClosed             func(*TopologyClosedEvent)
   207  	ServerHeartbeatStarted     func(*ServerHeartbeatStartedEvent)
   208  	ServerHeartbeatSucceeded   func(*ServerHeartbeatSucceededEvent)
   209  	ServerHeartbeatFailed      func(*ServerHeartbeatFailedEvent)
   210  }