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
7package event // import "go.mongodb.org/mongo-driver/event"
8
9import (
10	"context"
11
12	"go.mongodb.org/mongo-driver/bson"
13)
14
15// CommandStartedEvent represents an event generated when a command is sent to a server.
16type CommandStartedEvent struct {
17	Command      bson.Raw
18	DatabaseName string
19	CommandName  string
20	RequestID    int64
21	ConnectionID string
22}
23
24// CommandFinishedEvent represents a generic command finishing.
25type CommandFinishedEvent struct {
26	DurationNanos int64
27	CommandName   string
28	RequestID     int64
29	ConnectionID  string
30}
31
32// CommandSucceededEvent represents an event generated when a command's execution succeeds.
33type CommandSucceededEvent struct {
34	CommandFinishedEvent
35	Reply bson.Raw
36}
37
38// CommandFailedEvent represents an event generated when a command's execution fails.
39type CommandFailedEvent struct {
40	CommandFinishedEvent
41	Failure string
42}
43
44// CommandMonitor represents a monitor that is triggered for different events.
45type CommandMonitor struct {
46	Started   func(context.Context, *CommandStartedEvent)
47	Succeeded func(context.Context, *CommandSucceededEvent)
48	Failed    func(context.Context, *CommandFailedEvent)
49}
50
51// strings for pool command monitoring reasons
52const (
53	ReasonIdle              = "idle"
54	ReasonPoolClosed        = "poolClosed"
55	ReasonStale             = "stale"
56	ReasonConnectionErrored = "connectionError"
57	ReasonTimedOut          = "timeout"
58)
59
60// strings for pool command monitoring types
61const (
62	ConnectionClosed   = "ConnectionClosed"
63	PoolCreated        = "ConnectionPoolCreated"
64	ConnectionCreated  = "ConnectionCreated"
65	GetFailed          = "ConnectionCheckOutFailed"
66	GetSucceeded       = "ConnectionCheckedOut"
67	ConnectionReturned = "ConnectionCheckedIn"
68	PoolCleared        = "ConnectionPoolCleared"
69	PoolClosedEvent    = "ConnectionPoolClosed"
70)
71
72// MonitorPoolOptions contains pool options as formatted in pool events
73type MonitorPoolOptions struct {
74	MaxPoolSize        uint64 `json:"maxPoolSize"`
75	MinPoolSize        uint64 `json:"minPoolSize"`
76	WaitQueueTimeoutMS uint64 `json:"maxIdleTimeMS"`
77}
78
79// PoolEvent contains all information summarizing a pool event
80type PoolEvent struct {
81	Type         string              `json:"type"`
82	Address      string              `json:"address"`
83	ConnectionID uint64              `json:"connectionId"`
84	PoolOptions  *MonitorPoolOptions `json:"options"`
85	Reason       string              `json:"reason"`
86}
87
88// PoolMonitor is a function that allows the user to gain access to events occurring in the pool
89type PoolMonitor struct {
90	Event func(*PoolEvent)
91}
92