@@ -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
@@ -166,6 +175,8 @@ public struct CommandSucceededEvent: MongoSwiftEvent, CommandEventProtocol {
166175 }
167176 }
168177
178+ fileprivate static var monitoringComponent : MonitoringComponent { . command }
179+
169180 /// The execution time of the event, in microseconds.
170181 public let duration : Int
171182
@@ -216,6 +227,8 @@ public struct CommandFailedEvent: MongoSwiftEvent, CommandEventProtocol {
216227 }
217228 }
218229
230+ fileprivate static var monitoringComponent : MonitoringComponent { . command }
231+
219232 /// The execution time of the event, in microseconds.
220233 public let duration : Int
221234
@@ -304,6 +317,8 @@ public struct ServerDescriptionChangedEvent: MongoSwiftEvent {
304317 }
305318 }
306319
320+ fileprivate static var monitoringComponent : MonitoringComponent { . sdam }
321+
307322 /// The connection ID (host/port pair) of the server.
308323 public let serverAddress : ServerAddress
309324
@@ -339,6 +354,8 @@ public struct ServerOpeningEvent: MongoSwiftEvent {
339354 fileprivate struct MongocServerOpeningEvent : MongocEvent {
340355 fileprivate let ptr : OpaquePointer
341356
357+ fileprivate static var monitoringComponent : MonitoringComponent { . sdam }
358+
342359 fileprivate init ( _ eventPtr: OpaquePointer ) {
343360 self . ptr = eventPtr
344361 }
@@ -348,6 +365,8 @@ public struct ServerOpeningEvent: MongoSwiftEvent {
348365 }
349366 }
350367
368+ fileprivate static var monitoringComponent : MonitoringComponent { . sdam }
369+
351370 /// The connection ID (host/port pair) of the server.
352371 public let serverAddress : ServerAddress
353372
@@ -374,6 +393,8 @@ public struct ServerClosedEvent: MongoSwiftEvent {
374393 fileprivate struct MongocServerClosedEvent : MongocEvent {
375394 fileprivate let ptr : OpaquePointer
376395
396+ fileprivate static var monitoringComponent : MonitoringComponent { . sdam }
397+
377398 fileprivate init ( _ eventPtr: OpaquePointer ) {
378399 self . ptr = eventPtr
379400 }
@@ -383,6 +404,8 @@ public struct ServerClosedEvent: MongoSwiftEvent {
383404 }
384405 }
385406
407+ fileprivate static var monitoringComponent : MonitoringComponent { . sdam }
408+
386409 /// The connection ID (host/port pair) of the server.
387410 public let serverAddress : ServerAddress
388411
@@ -418,6 +441,8 @@ public struct TopologyDescriptionChangedEvent: MongoSwiftEvent {
418441 }
419442 }
420443
444+ fileprivate static var monitoringComponent : MonitoringComponent { . sdam }
445+
421446 /// A unique identifier for the topology.
422447 public let topologyID : BSONObjectID
423448
@@ -458,6 +483,8 @@ public struct TopologyOpeningEvent: MongoSwiftEvent {
458483 }
459484 }
460485
486+ fileprivate static var monitoringComponent : MonitoringComponent { . sdam }
487+
461488 /// A unique identifier for the topology.
462489 public let topologyID : BSONObjectID
463490
@@ -489,6 +516,8 @@ public struct TopologyClosedEvent: MongoSwiftEvent {
489516 }
490517 }
491518
519+ fileprivate static var monitoringComponent : MonitoringComponent { . sdam }
520+
492521 /// A unique identifier for the topology.
493522 public let topologyID : BSONObjectID
494523
@@ -521,6 +550,8 @@ public struct ServerHeartbeatStartedEvent: MongoSwiftEvent {
521550 }
522551 }
523552
553+ fileprivate static var monitoringComponent : MonitoringComponent { . sdam }
554+
524555 /// The address of the server.
525556 public let serverAddress : ServerAddress
526557
@@ -548,6 +579,8 @@ public struct ServerHeartbeatSucceededEvent: MongoSwiftEvent {
548579 }
549580 }
550581
582+ fileprivate static var monitoringComponent : MonitoringComponent { . sdam }
583+
551584 /// The execution time of the event, in microseconds.
552585 public let duration : Int
553586
@@ -584,6 +617,8 @@ public struct ServerHeartbeatFailedEvent: MongoSwiftEvent {
584617 }
585618 }
586619
620+ fileprivate static var monitoringComponent : MonitoringComponent { . sdam }
621+
587622 /// The execution time of the event, in microseconds.
588623 public let duration : Int
589624
@@ -683,19 +718,19 @@ private func publishEvent<T: MongoSwiftEvent>(type: T.Type, eventPtr: OpaquePoin
683718 }
684719 let client = Unmanaged < MongoClient > . fromOpaque ( context) . takeUnretainedValue ( )
685720
686- let event = type. init ( mongocEvent: mongocEvent)
687-
688- // TODO: SWIFT-524: remove workaround for CDRIVER-3256
689- if let tdChanged = event as? TopologyDescriptionChangedEvent ,
690- tdChanged. previousDescription == tdChanged. newDescription {
691- return
692- }
693-
694- if let sdChanged = event as? ServerDescriptionChangedEvent ,
695- sdChanged. previousDescription == sdChanged. newDescription {
696- return
721+ // only create Swift events if handlers are actually registered for this type of event.
722+ switch type. monitoringComponent {
723+ case . sdam:
724+ guard !client. sdamEventHandlers. isEmpty else {
725+ return
726+ }
727+ case . command:
728+ guard !client. commandEventHandlers. isEmpty else {
729+ return
730+ }
697731 }
698732
733+ let event = type. init ( mongocEvent: mongocEvent)
699734 event. toPublishable ( ) . publish ( to: client)
700735}
701736
0 commit comments