...
1
2
3
4
5
6
7 package unified
8
9 import (
10 "strings"
11
12 "go.mongodb.org/mongo-driver/bson"
13 "go.mongodb.org/mongo-driver/event"
14 "go.mongodb.org/mongo-driver/mongo/description"
15 )
16
17 type monitoringEventType string
18
19 const (
20 commandStartedEvent monitoringEventType = "CommandStartedEvent"
21 commandSucceededEvent monitoringEventType = "CommandSucceededEvent"
22 commandFailedEvent monitoringEventType = "CommandFailedEvent"
23 poolCreatedEvent monitoringEventType = "PoolCreatedEvent"
24 poolReadyEvent monitoringEventType = "PoolReadyEvent"
25 poolClearedEvent monitoringEventType = "PoolClearedEvent"
26 poolClosedEvent monitoringEventType = "PoolClosedEvent"
27 connectionCreatedEvent monitoringEventType = "ConnectionCreatedEvent"
28 connectionReadyEvent monitoringEventType = "ConnectionReadyEvent"
29 connectionClosedEvent monitoringEventType = "ConnectionClosedEvent"
30 connectionCheckOutStartedEvent monitoringEventType = "ConnectionCheckOutStartedEvent"
31 connectionCheckOutFailedEvent monitoringEventType = "ConnectionCheckOutFailedEvent"
32 connectionCheckedOutEvent monitoringEventType = "ConnectionCheckedOutEvent"
33 connectionCheckedInEvent monitoringEventType = "ConnectionCheckedInEvent"
34 serverDescriptionChangedEvent monitoringEventType = "ServerDescriptionChangedEvent"
35 serverHeartbeatFailedEvent monitoringEventType = "ServerHeartbeatFailedEvent"
36 serverHeartbeatStartedEvent monitoringEventType = "ServerHeartbeatStartedEvent"
37 serverHeartbeatSucceededEvent monitoringEventType = "ServerHeartbeatSucceededEvent"
38 topologyDescriptionChangedEvent monitoringEventType = "TopologyDescriptionChangedEvent"
39 )
40
41 func monitoringEventTypeFromString(eventStr string) (monitoringEventType, bool) {
42 switch strings.ToLower(eventStr) {
43 case "commandstartedevent":
44 return commandStartedEvent, true
45 case "commandsucceededevent":
46 return commandSucceededEvent, true
47 case "commandfailedevent":
48 return commandFailedEvent, true
49 case "poolcreatedevent":
50 return poolCreatedEvent, true
51 case "poolreadyevent":
52 return poolReadyEvent, true
53 case "poolclearedevent":
54 return poolClearedEvent, true
55 case "poolclosedevent":
56 return poolClosedEvent, true
57 case "connectioncreatedevent":
58 return connectionCreatedEvent, true
59 case "connectionreadyevent":
60 return connectionReadyEvent, true
61 case "connectionclosedevent":
62 return connectionClosedEvent, true
63 case "connectioncheckoutstartedevent":
64 return connectionCheckOutStartedEvent, true
65 case "connectioncheckoutfailedevent":
66 return connectionCheckOutFailedEvent, true
67 case "connectioncheckedoutevent":
68 return connectionCheckedOutEvent, true
69 case "connectioncheckedinevent":
70 return connectionCheckedInEvent, true
71 case "serverdescriptionchangedevent":
72 return serverDescriptionChangedEvent, true
73 case "serverheartbeatfailedevent":
74 return serverHeartbeatFailedEvent, true
75 case "serverheartbeatstartedevent":
76 return serverHeartbeatStartedEvent, true
77 case "serverheartbeatsucceededevent":
78 return serverHeartbeatSucceededEvent, true
79 case "topologydescriptionchangedevent":
80 return topologyDescriptionChangedEvent, true
81 default:
82 return "", false
83 }
84 }
85
86 func monitoringEventTypeFromPoolEvent(evt *event.PoolEvent) monitoringEventType {
87 switch evt.Type {
88 case event.PoolCreated:
89 return poolCreatedEvent
90 case event.PoolReady:
91 return poolReadyEvent
92 case event.PoolCleared:
93 return poolClearedEvent
94 case event.PoolClosedEvent:
95 return poolClosedEvent
96 case event.ConnectionCreated:
97 return connectionCreatedEvent
98 case event.ConnectionReady:
99 return connectionReadyEvent
100 case event.ConnectionClosed:
101 return connectionClosedEvent
102 case event.GetStarted:
103 return connectionCheckOutStartedEvent
104 case event.GetFailed:
105 return connectionCheckOutFailedEvent
106 case event.GetSucceeded:
107 return connectionCheckedOutEvent
108 case event.ConnectionReturned:
109 return connectionCheckedInEvent
110 default:
111 return ""
112 }
113 }
114
115
116 type serverDescription struct {
117
118
119 Type string
120 }
121
122
123
124 type serverDescriptionChangedEventInfo struct {
125
126
127 NewDescription serverDescription
128
129
130
131 PreviousDescription serverDescription
132 }
133
134
135
136 func newServerDescriptionChangedEventInfo(evt *event.ServerDescriptionChangedEvent) *serverDescriptionChangedEventInfo {
137 return &serverDescriptionChangedEventInfo{
138 NewDescription: serverDescription{
139 Type: evt.NewDescription.Kind.String(),
140 },
141 PreviousDescription: serverDescription{
142 Type: evt.PreviousDescription.Kind.String(),
143 },
144 }
145 }
146
147
148
149 func (evt *serverDescriptionChangedEventInfo) UnmarshalBSON(data []byte) error {
150 if len(data) == 0 {
151 return nil
152 }
153
154 var raw bson.Raw
155 if err := bson.Unmarshal(data, &raw); err != nil {
156 return err
157 }
158
159
160 evt.NewDescription.Type = description.TopologyKind(description.Unknown).String()
161
162
163 if newDescription, err := raw.LookupErr("newDescription"); err == nil {
164 evt.NewDescription.Type = newDescription.Document().Lookup("type").StringValue()
165 }
166
167
168
169 evt.PreviousDescription.Type = description.TopologyKind(description.Unknown).String()
170
171
172 if previousDescription, err := raw.LookupErr("previousDescription"); err == nil {
173 evt.PreviousDescription.Type = previousDescription.Document().Lookup("type").StringValue()
174 }
175
176 return nil
177 }
178
View as plain text