Skip to content
Merged
1 change: 1 addition & 0 deletions .swiftformat
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
--exclude Sources/MongoSwift/MongoSwiftVersion.swift,Sources/MongoSwiftSync/*
--exclude .build/*
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure why this wasn't a problem for me before, maybe a linux only thing?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔weird, I don't think I've run into this either


# unnecessary
--disable andOperator
Expand Down
92 changes: 58 additions & 34 deletions Sources/MongoSwift/APM.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public protocol MongoCommandEvent: MongoEvent {
/// as bulk write operations.
var operationId: Int64 { get }

/// The connection id for the command.
var connectionId: ConnectionId { get }
/// The address of the server the command was run against.
var serverAddress: Address { get }
}

/// A protocol for monitoring events to implement, indicating that they can be initialized from an OpaquePointer
Expand Down Expand Up @@ -52,8 +52,8 @@ public struct CommandStartedEvent: MongoCommandEvent, InitializableFromOpaquePoi
/// as bulk write operations.
public let operationId: Int64

/// The connection id for the command.
public let connectionId: ConnectionId
/// The address of the server the command was run against.
public let serverAddress: Address

/// Initializes a CommandStartedEvent from an OpaquePointer to a mongoc_apm_command_started_t
fileprivate init(_ event: OpaquePointer) {
Expand All @@ -63,7 +63,7 @@ public struct CommandStartedEvent: MongoCommandEvent, InitializableFromOpaquePoi
self.commandName = String(cString: mongoc_apm_command_started_get_command_name(event))
self.requestId = mongoc_apm_command_started_get_request_id(event)
self.operationId = mongoc_apm_command_started_get_operation_id(event)
self.connectionId = ConnectionId(mongoc_apm_command_started_get_host(event))
self.serverAddress = Address(mongoc_apm_command_started_get_host(event))
}
}

Expand All @@ -89,8 +89,8 @@ public struct CommandSucceededEvent: MongoCommandEvent, InitializableFromOpaqueP
/// as bulk write operations.
public let operationId: Int64

/// The connection id for the command.
public let connectionId: ConnectionId
/// The address of the server the command was run against.
public let serverAddress: Address

/// Initializes a CommandSucceededEvent from an OpaquePointer to a mongoc_apm_command_succeeded_t
fileprivate init(_ event: OpaquePointer) {
Expand All @@ -100,7 +100,7 @@ public struct CommandSucceededEvent: MongoCommandEvent, InitializableFromOpaqueP
self.commandName = String(cString: mongoc_apm_command_succeeded_get_command_name(event))
self.requestId = mongoc_apm_command_succeeded_get_request_id(event)
self.operationId = mongoc_apm_command_succeeded_get_operation_id(event)
self.connectionId = ConnectionId(mongoc_apm_command_succeeded_get_host(event))
self.serverAddress = Address(mongoc_apm_command_succeeded_get_host(event))
}
}

Expand All @@ -127,7 +127,7 @@ public struct CommandFailedEvent: MongoCommandEvent, InitializableFromOpaquePoin
public let operationId: Int64

/// The connection id for the command.
public let connectionId: ConnectionId
public let serverAddress: Address

/// Initializes a CommandFailedEvent from an OpaquePointer to a mongoc_apm_command_failed_t
fileprivate init(_ event: OpaquePointer) {
Expand All @@ -139,17 +139,34 @@ public struct CommandFailedEvent: MongoCommandEvent, InitializableFromOpaquePoin
self.failure = extractMongoError(error: error, reply: reply) // should always return a CommandError
self.requestId = mongoc_apm_command_failed_get_request_id(event)
self.operationId = mongoc_apm_command_failed_get_operation_id(event)
self.connectionId = ConnectionId(mongoc_apm_command_failed_get_host(event))
self.serverAddress = Address(mongoc_apm_command_failed_get_host(event))
}
}

/// A protocol for SDAM monitoring events to implement.
public protocol MongoSDAMEvent: MongoEvent {}

/// A protocol for SDAM monitoring events that relate to topology updates to implement, specifying the id of the
/// topology being updated.
public protocol MongoTopologyUpdateEvent: MongoEvent {
/// The id of the topology being updated.
var topologyId: ObjectId { get }
}

/// A protocol for topology update events that relate to specific servers to implement, specifying the address of the
/// server being updated.
public protocol MongoServerUpdateEvent: MongoTopologyUpdateEvent {
/// The address of the server being updated.
var serverAddress: Address { get }
}

/// Published when a server description changes. This does NOT include changes to the server's roundTripTime property.
public struct ServerDescriptionChangedEvent: MongoEvent, InitializableFromOpaquePointer {
public struct ServerDescriptionChangedEvent: MongoServerUpdateEvent, InitializableFromOpaquePointer {
/// The name this event will be posted under.
public static var eventName: Notification.Name { return .serverDescriptionChanged }

/// The connection ID (host/port pair) of the server.
public let connectionId: ConnectionId
public let serverAddress: Address

/// A unique identifier for the topology.
public let topologyId: ObjectId
Expand All @@ -162,7 +179,7 @@ public struct ServerDescriptionChangedEvent: MongoEvent, InitializableFromOpaque

/// Initializes a ServerDescriptionChangedEvent from an OpaquePointer to a mongoc_server_changed_t
fileprivate init(_ event: OpaquePointer) {
self.connectionId = ConnectionId(mongoc_apm_server_changed_get_host(event))
self.serverAddress = Address(mongoc_apm_server_changed_get_host(event))
var oid = bson_oid_t()
withUnsafeMutablePointer(to: &oid) { oidPtr in
mongoc_apm_server_changed_get_topology_id(event, oidPtr)
Expand All @@ -174,19 +191,19 @@ public struct ServerDescriptionChangedEvent: MongoEvent, InitializableFromOpaque
}

/// Published when a server is initialized.
public struct ServerOpeningEvent: MongoEvent, InitializableFromOpaquePointer {
public struct ServerOpeningEvent: MongoServerUpdateEvent, InitializableFromOpaquePointer {
/// The name this event will be posted under.
public static var eventName: Notification.Name { return .serverOpening }

/// The connection ID (host/port pair) of the server.
public let connectionId: ConnectionId
public let serverAddress: Address

/// A unique identifier for the topology.
public let topologyId: ObjectId

/// Initializes a ServerOpeningEvent from an OpaquePointer to a mongoc_apm_server_opening_t
fileprivate init(_ event: OpaquePointer) {
self.connectionId = ConnectionId(mongoc_apm_server_opening_get_host(event))
self.serverAddress = Address(mongoc_apm_server_opening_get_host(event))
var oid = bson_oid_t()
withUnsafeMutablePointer(to: &oid) { oidPtr in
mongoc_apm_server_opening_get_topology_id(event, oidPtr)
Expand All @@ -196,19 +213,19 @@ public struct ServerOpeningEvent: MongoEvent, InitializableFromOpaquePointer {
}

/// Published when a server is closed.
public struct ServerClosedEvent: MongoEvent, InitializableFromOpaquePointer {
public struct ServerClosedEvent: MongoServerUpdateEvent, InitializableFromOpaquePointer {
/// The name this event will be posted under.
public static var eventName: Notification.Name { return .serverClosed }

/// The connection ID (host/port pair) of the server.
public let connectionId: ConnectionId
public let serverAddress: Address

/// A unique identifier for the topology.
public let topologyId: ObjectId

/// Initializes a TopologyClosedEvent from an OpaquePointer to a mongoc_apm_topology_closed_t
fileprivate init(_ event: OpaquePointer) {
self.connectionId = ConnectionId(mongoc_apm_server_closed_get_host(event))
self.serverAddress = Address(mongoc_apm_server_closed_get_host(event))
var oid = bson_oid_t()
withUnsafeMutablePointer(to: &oid) { oidPtr in
mongoc_apm_server_closed_get_topology_id(event, oidPtr)
Expand All @@ -218,7 +235,7 @@ public struct ServerClosedEvent: MongoEvent, InitializableFromOpaquePointer {
}

/// Published when a topology description changes.
public struct TopologyDescriptionChangedEvent: MongoEvent, InitializableFromOpaquePointer {
public struct TopologyDescriptionChangedEvent: MongoTopologyUpdateEvent, InitializableFromOpaquePointer {
/// The name this event will be posted under.
public static var eventName: Notification.Name { return .topologyDescriptionChanged }

Expand All @@ -244,7 +261,7 @@ public struct TopologyDescriptionChangedEvent: MongoEvent, InitializableFromOpaq
}

/// Published when a topology is initialized.
public struct TopologyOpeningEvent: MongoEvent, InitializableFromOpaquePointer {
public struct TopologyOpeningEvent: MongoTopologyUpdateEvent, InitializableFromOpaquePointer {
/// The name this event will be posted under.
public static var eventName: Notification.Name { return .topologyOpening }

Expand All @@ -262,7 +279,7 @@ public struct TopologyOpeningEvent: MongoEvent, InitializableFromOpaquePointer {
}

/// Published when a topology is closed.
public struct TopologyClosedEvent: MongoEvent, InitializableFromOpaquePointer {
public struct TopologyClosedEvent: MongoTopologyUpdateEvent, InitializableFromOpaquePointer {
/// The name this event will be posted under.
public static var eventName: Notification.Name { return .topologyClosed }

Expand All @@ -279,23 +296,30 @@ public struct TopologyClosedEvent: MongoEvent, InitializableFromOpaquePointer {
}
}

/// A protocol for SDAM monitoring events that are related to server heartbeats to implement, specifying the address of
/// the server being checked.
public protocol MongoServerHeartbeatEvent: MongoSDAMEvent {
/// The address of the server being checked.
var serverAddress: Address { get }
}

/// Published when the server monitor’s ismaster command is started - immediately before
/// the ismaster command is serialized into raw BSON and written to the socket.
public struct ServerHeartbeatStartedEvent: MongoEvent, InitializableFromOpaquePointer {
public struct ServerHeartbeatStartedEvent: MongoServerHeartbeatEvent, InitializableFromOpaquePointer {
/// The name this event will be posted under.
public static var eventName: Notification.Name { return .serverHeartbeatStarted }

/// The connection ID (host/port pair) of the server.
public let connectionId: ConnectionId
/// The address of the server.
public let serverAddress: Address

/// Initializes a ServerHeartbeatStartedEvent from an OpaquePointer to a mongoc_apm_server_heartbeat_started_t
fileprivate init(_ event: OpaquePointer) {
self.connectionId = ConnectionId(mongoc_apm_server_heartbeat_started_get_host(event))
self.serverAddress = Address(mongoc_apm_server_heartbeat_started_get_host(event))
}
}

/// Published when the server monitor’s ismaster succeeds.
public struct ServerHeartbeatSucceededEvent: MongoEvent, InitializableFromOpaquePointer {
public struct ServerHeartbeatSucceededEvent: MongoServerHeartbeatEvent, InitializableFromOpaquePointer {
/// The name this event will be posted under.
public static var eventName: Notification.Name { return .serverHeartbeatSucceeded }

Expand All @@ -305,20 +329,20 @@ public struct ServerHeartbeatSucceededEvent: MongoEvent, InitializableFromOpaque
/// The command reply.
public let reply: Document

/// The connection ID (host/port pair) of the server.
public let connectionId: ConnectionId
/// The address of the server.
public let serverAddress: Address

/// Initializes a ServerHeartbeatSucceededEvent from an OpaquePointer to a mongoc_apm_server_heartbeat_succeeded_t
fileprivate init(_ event: OpaquePointer) {
self.duration = mongoc_apm_server_heartbeat_succeeded_get_duration(event)
// we have to copy because libmongoc owns the pointer.
self.reply = Document(copying: mongoc_apm_server_heartbeat_succeeded_get_reply(event))
self.connectionId = ConnectionId(mongoc_apm_server_heartbeat_succeeded_get_host(event))
self.serverAddress = Address(mongoc_apm_server_heartbeat_succeeded_get_host(event))
}
}

/// Published when the server monitor’s ismaster fails, either with an “ok: 0” or a socket exception.
public struct ServerHeartbeatFailedEvent: MongoEvent, InitializableFromOpaquePointer {
public struct ServerHeartbeatFailedEvent: MongoServerHeartbeatEvent, InitializableFromOpaquePointer {
/// The name this event will be posted under.
public static var eventName: Notification.Name { return .serverHeartbeatFailed }

Expand All @@ -328,16 +352,16 @@ public struct ServerHeartbeatFailedEvent: MongoEvent, InitializableFromOpaquePoi
/// The failure.
public let failure: MongoError

/// The connection ID (host/port pair) of the server.
public let connectionId: ConnectionId
/// The address of the server.
public let serverAddress: Address

/// Initializes a ServerHeartbeatFailedEvent from an OpaquePointer to a mongoc_apm_server_heartbeat_failed_t
fileprivate init(_ event: OpaquePointer) {
self.duration = mongoc_apm_server_heartbeat_failed_get_duration(event)
var error = bson_error_t()
mongoc_apm_server_heartbeat_failed_get_error(event, &error)
self.failure = extractMongoError(error: error)
self.connectionId = ConnectionId(mongoc_apm_server_heartbeat_failed_get_host(event))
self.serverAddress = Address(mongoc_apm_server_heartbeat_failed_get_host(event))
}
}

Expand Down
3 changes: 3 additions & 0 deletions Sources/MongoSwift/MongoSwift.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import CLibMongoC

/// MongoSwift only supports MongoDB 3.6+.
internal let MIN_SUPPORTED_WIRE_VERSION = 6

private final class MongocInitializer {
internal static let shared = MongocInitializer()

Expand Down
Loading