@@ -24,11 +24,18 @@ private protocol MongoSwiftEvent {
2424 associatedtype MongocEventType : MongocEvent
2525 associatedtype PublishableEventType : Publishable
2626
27+ static var monitoringComponent : MonitoringComponent { get }
28+
2729 init ( mongocEvent: MongocEventType )
2830
2931 func toPublishable( ) -> PublishableEventType
3032}
3133
34+ /// Indicates which type of monitoring an event is associated with.
35+ private enum MonitoringComponent {
36+ case command, sdam
37+ }
38+
3239/// A protocol for libmongoc event wrappers to implement.
3340private protocol MongocEvent {
3441 init ( _ eventPtr: OpaquePointer )
@@ -117,6 +124,8 @@ public struct CommandStartedEvent: MongoSwiftEvent, CommandEventProtocol {
117124 }
118125 }
119126
127+ fileprivate static var monitoringComponent : MonitoringComponent { . command }
128+
120129 /// The command.
121130 public let command : BSONDocument
122131
@@ -168,6 +177,8 @@ public struct CommandSucceededEvent: MongoSwiftEvent, CommandEventProtocol {
168177 }
169178 }
170179
180+ fileprivate static var monitoringComponent : MonitoringComponent { . command }
181+
171182 /// The execution time of the event, in microseconds.
172183 public let duration : Int
173184
@@ -220,6 +231,8 @@ public struct CommandFailedEvent: MongoSwiftEvent, CommandEventProtocol {
220231 }
221232 }
222233
234+ fileprivate static var monitoringComponent : MonitoringComponent { . command }
235+
223236 /// The execution time of the event, in microseconds.
224237 public let duration : Int
225238
@@ -310,6 +323,8 @@ public struct ServerDescriptionChangedEvent: MongoSwiftEvent {
310323 }
311324 }
312325
326+ fileprivate static var monitoringComponent : MonitoringComponent { . sdam }
327+
313328 /// The connection ID (host/port pair) of the server.
314329 public let serverAddress : ServerAddress
315330
@@ -345,6 +360,8 @@ public struct ServerOpeningEvent: MongoSwiftEvent {
345360 fileprivate struct MongocServerOpeningEvent : MongocEvent {
346361 fileprivate let ptr : OpaquePointer
347362
363+ fileprivate static var monitoringComponent : MonitoringComponent { . sdam }
364+
348365 fileprivate init ( _ eventPtr: OpaquePointer ) {
349366 self . ptr = eventPtr
350367 }
@@ -354,6 +371,8 @@ public struct ServerOpeningEvent: MongoSwiftEvent {
354371 }
355372 }
356373
374+ fileprivate static var monitoringComponent : MonitoringComponent { . sdam }
375+
357376 /// The connection ID (host/port pair) of the server.
358377 public let serverAddress : ServerAddress
359378
@@ -380,6 +399,8 @@ public struct ServerClosedEvent: MongoSwiftEvent {
380399 fileprivate struct MongocServerClosedEvent : MongocEvent {
381400 fileprivate let ptr : OpaquePointer
382401
402+ fileprivate static var monitoringComponent : MonitoringComponent { . sdam }
403+
383404 fileprivate init ( _ eventPtr: OpaquePointer ) {
384405 self . ptr = eventPtr
385406 }
@@ -389,6 +410,8 @@ public struct ServerClosedEvent: MongoSwiftEvent {
389410 }
390411 }
391412
413+ fileprivate static var monitoringComponent : MonitoringComponent { . sdam }
414+
392415 /// The connection ID (host/port pair) of the server.
393416 public let serverAddress : ServerAddress
394417
@@ -424,6 +447,8 @@ public struct TopologyDescriptionChangedEvent: MongoSwiftEvent {
424447 }
425448 }
426449
450+ fileprivate static var monitoringComponent : MonitoringComponent { . sdam }
451+
427452 /// A unique identifier for the topology.
428453 public let topologyID : BSONObjectID
429454
@@ -464,6 +489,8 @@ public struct TopologyOpeningEvent: MongoSwiftEvent {
464489 }
465490 }
466491
492+ fileprivate static var monitoringComponent : MonitoringComponent { . sdam }
493+
467494 /// A unique identifier for the topology.
468495 public let topologyID : BSONObjectID
469496
@@ -495,6 +522,8 @@ public struct TopologyClosedEvent: MongoSwiftEvent {
495522 }
496523 }
497524
525+ fileprivate static var monitoringComponent : MonitoringComponent { . sdam }
526+
498527 /// A unique identifier for the topology.
499528 public let topologyID : BSONObjectID
500529
@@ -527,6 +556,8 @@ public struct ServerHeartbeatStartedEvent: MongoSwiftEvent {
527556 }
528557 }
529558
559+ fileprivate static var monitoringComponent : MonitoringComponent { . sdam }
560+
530561 /// The address of the server.
531562 public let serverAddress : ServerAddress
532563
@@ -554,6 +585,8 @@ public struct ServerHeartbeatSucceededEvent: MongoSwiftEvent {
554585 }
555586 }
556587
588+ fileprivate static var monitoringComponent : MonitoringComponent { . sdam }
589+
557590 /// The execution time of the event, in microseconds.
558591 public let duration : Int
559592
@@ -592,6 +625,8 @@ public struct ServerHeartbeatFailedEvent: MongoSwiftEvent {
592625 }
593626 }
594627
628+ fileprivate static var monitoringComponent : MonitoringComponent { . sdam }
629+
595630 /// The execution time of the event, in microseconds.
596631 public let duration : Int
597632
@@ -691,19 +726,19 @@ private func publishEvent<T: MongoSwiftEvent>(type: T.Type, eventPtr: OpaquePoin
691726 }
692727 let client = Unmanaged < MongoClient > . fromOpaque ( context) . takeUnretainedValue ( )
693728
694- let event = type. init ( mongocEvent: mongocEvent)
695-
696- // TODO: SWIFT-524: remove workaround for CDRIVER-3256
697- if let tdChanged = event as? TopologyDescriptionChangedEvent ,
698- tdChanged. previousDescription == tdChanged. newDescription {
699- return
700- }
701-
702- if let sdChanged = event as? ServerDescriptionChangedEvent ,
703- sdChanged. previousDescription == sdChanged. newDescription {
704- return
729+ // only create Swift events if handlers are actually registered for this type of event.
730+ switch type. monitoringComponent {
731+ case . sdam:
732+ guard !client. sdamEventHandlers. isEmpty else {
733+ return
734+ }
735+ case . command:
736+ guard !client. commandEventHandlers. isEmpty else {
737+ return
738+ }
705739 }
706740
741+ let event = type. init ( mongocEvent: mongocEvent)
707742 event. toPublishable ( ) . publish ( to: client)
708743}
709744
0 commit comments