Skip to content

Commit 927c445

Browse files
authored
CSHARP-4722: Add durations to connection pool events (#1177)
1 parent 00a0c72 commit 927c445

10 files changed

+203
-74
lines changed

specifications/connection-monitoring-and-pooling/connection-monitoring-and-pooling.rst

+132-59
Large diffs are not rendered by default.

specifications/connection-monitoring-and-pooling/tests/logging/connection-logging.json

+21
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,13 @@
140140
"int",
141141
"long"
142142
]
143+
},
144+
"durationMS": {
145+
"$$type": [
146+
"double",
147+
"int",
148+
"long"
149+
]
143150
}
144151
}
145152
},
@@ -162,6 +169,13 @@
162169
"int",
163170
"long"
164171
]
172+
},
173+
"durationMS": {
174+
"$$type": [
175+
"double",
176+
"int",
177+
"long"
178+
]
165179
}
166180
}
167181
},
@@ -421,6 +435,13 @@
421435
"reason": "An error occurred while trying to establish a new connection",
422436
"error": {
423437
"$$exists": true
438+
},
439+
"durationMS": {
440+
"$$type": [
441+
"double",
442+
"int",
443+
"long"
444+
]
424445
}
425446
}
426447
}

specifications/connection-monitoring-and-pooling/tests/logging/connection-logging.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,16 @@ tests:
6666
driverConnectionId: { $$type: [int, long] }
6767
serverHost: { $$type: string }
6868
serverPort: { $$type: [int, long] }
69-
69+
durationMS: { $$type: [double, int, long] }
70+
7071
- level: debug
7172
component: connection
7273
data:
7374
message: "Connection checked out"
7475
driverConnectionId: { $$type: [int, long] }
7576
serverHost: { $$type: string }
7677
serverPort: { $$type: [int, long] }
78+
durationMS: { $$type: [double, int, long] }
7779

7880
- level: debug
7981
component: connection
@@ -194,3 +196,4 @@ tests:
194196
serverPort: { $$type: [int, long] }
195197
reason: "An error occurred while trying to establish a new connection"
196198
error: { $$exists: true }
199+
durationMS: { $$type: [double, int, long] }

specifications/connection-monitoring-and-pooling/tests/logging/connection-pool-options.json

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"description": "connection-logging",
2+
"description": "connection-pool-options",
33
"schemaVersion": "1.13",
44
"runOnRequirements": [
55
{
@@ -128,6 +128,13 @@
128128
"int",
129129
"long"
130130
]
131+
},
132+
"durationMS": {
133+
"$$type": [
134+
"double",
135+
"int",
136+
"long"
137+
]
131138
}
132139
}
133140
}

specifications/connection-monitoring-and-pooling/tests/logging/connection-pool-options.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
description: "connection-logging"
1+
description: "connection-pool-options"
22

33
schemaVersion: "1.13"
44

@@ -71,6 +71,7 @@ tests:
7171
driverConnectionId: { $$type: [int, long] }
7272
serverHost: { $$type: string }
7373
serverPort: { $$type: [int, long] }
74+
durationMS: { $$type: [double, int, long] }
7475

7576
# Drivers who have not done DRIVERS-1943 will need to skip this test.
7677
- description: "maxConnecting should be included in connection pool created message when specified"

src/MongoDB.Driver.Core/Core/ConnectionPools/ExclusiveConnectionPool.Helpers.cs

+10-6
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,12 @@ public AcquireConnectionHelper(ExclusiveConnectionPool pool)
180180

181181
public IConnectionHandle AcquireConnection(CancellationToken cancellationToken)
182182
{
183+
Stopwatch stopwatch = new();
183184
try
184185
{
185186
StartCheckingOut();
186187

187-
var stopwatch = Stopwatch.StartNew();
188+
stopwatch.Start();
188189
_poolQueueWaitResult = _pool._maxConnectionsQueue.WaitSignaled(_timeout, cancellationToken);
189190

190191
if (_poolQueueWaitResult == SemaphoreSlimSignalable.SemaphoreWaitResult.Entered)
@@ -205,18 +206,20 @@ public IConnectionHandle AcquireConnection(CancellationToken cancellationToken)
205206
}
206207
catch (Exception ex)
207208
{
208-
HandleException(ex);
209+
stopwatch.Stop();
210+
HandleException(ex, stopwatch.Elapsed);
209211
throw;
210212
}
211213
}
212214

213215
public async Task<IConnectionHandle> AcquireConnectionAsync(CancellationToken cancellationToken)
214216
{
217+
Stopwatch stopwatch = new();
215218
try
216219
{
217220
StartCheckingOut();
218221

219-
var stopwatch = Stopwatch.StartNew();
222+
stopwatch.Start();
220223
_poolQueueWaitResult = await _pool._maxConnectionsQueue.WaitSignaledAsync(_timeout, cancellationToken).ConfigureAwait(false);
221224

222225
if (_poolQueueWaitResult == SemaphoreSlimSignalable.SemaphoreWaitResult.Entered)
@@ -237,7 +240,8 @@ public async Task<IConnectionHandle> AcquireConnectionAsync(CancellationToken ca
237240
}
238241
catch (Exception ex)
239242
{
240-
HandleException(ex);
243+
stopwatch.Stop();
244+
HandleException(ex, stopwatch.Elapsed);
241245
throw;
242246
}
243247
}
@@ -328,7 +332,7 @@ private Exception CreateException(Stopwatch stopwatch) =>
328332
_ => new InvalidOperationException($"Invalid {_poolQueueWaitResult}.")
329333
};
330334

331-
private void HandleException(Exception ex)
335+
private void HandleException(Exception ex, TimeSpan elapsedTime)
332336
{
333337
var reason = ex switch
334338
{
@@ -337,7 +341,7 @@ private void HandleException(Exception ex)
337341
_ => ConnectionCheckOutFailedReason.ConnectionError
338342
};
339343

340-
_pool._eventLogger.LogAndPublish(new ConnectionPoolCheckingOutConnectionFailedEvent(_pool._serverId, ex, EventContext.OperationId, reason));
344+
_pool._eventLogger.LogAndPublish(new ConnectionPoolCheckingOutConnectionFailedEvent(_pool._serverId, ex, EventContext.OperationId, elapsedTime, reason));
341345
}
342346
}
343347

src/MongoDB.Driver.Core/Core/Events/ConnectionPoolCheckingOutConnectionFailedEvent.cs

+12
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,28 @@ public struct ConnectionPoolCheckingOutConnectionFailedEvent : IEvent
2929
private readonly Exception _exception;
3030
private readonly long? _operationId;
3131
private readonly DateTime _timestamp;
32+
private readonly TimeSpan _duration;
3233

3334
/// <summary>
3435
/// Initializes a new instance of the <see cref="ConnectionPoolCheckingOutConnectionFailedEvent" /> struct.
3536
/// </summary>
3637
/// <param name="serverId">The server identifier.</param>
3738
/// <param name="exception">The exception.</param>
3839
/// <param name="operationId">The operation identifier.</param>
40+
/// <param name="duration">The duration of time it took trying to check out the connection.</param>
3941
/// <param name="reason">The reason the checkout failed.</param>
4042
public ConnectionPoolCheckingOutConnectionFailedEvent(
4143
ServerId serverId,
4244
Exception exception,
4345
long? operationId,
46+
TimeSpan duration,
4447
ConnectionCheckOutFailedReason reason)
4548
{
4649
_serverId = serverId;
4750
_exception = exception;
4851
_operationId = operationId;
4952
_reason = reason;
53+
_duration = duration;
5054
_timestamp = DateTime.UtcNow;
5155
}
5256

@@ -90,6 +94,14 @@ public ServerId ServerId
9094
get { return _serverId; }
9195
}
9296

97+
/// <summary>
98+
/// Gets the duration of time it took trying to check out the connection.
99+
/// </summary>
100+
public TimeSpan Duration
101+
{
102+
get { return _duration; }
103+
}
104+
93105
/// <summary>
94106
/// Gets the timestamp.
95107
/// </summary>

src/MongoDB.Driver.Core/Core/Logging/StructuredLogTemplateProviders.cs

+7
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,13 @@ public static object[] GetParams(ServerId serverId, object arg1, object arg2, ob
141141
return new[] { serverId.ClusterId.Value, host, port, arg1, arg2, arg3 };
142142
}
143143

144+
public static object[] GetParams(ServerId serverId, object arg1, object arg2, object arg3, object arg4)
145+
{
146+
var (host, port) = serverId.EndPoint.GetHostAndPort();
147+
148+
return new[] { serverId.ClusterId.Value, host, port, arg1, arg2, arg3, arg4 };
149+
}
150+
144151
public static object[] GetParams(ServerId serverId, object arg1, object arg2, object arg3, object arg4, object arg5, object arg6)
145152
{
146153
var (host, port) = serverId.EndPoint.GetHostAndPort();

src/MongoDB.Driver.Core/Core/Logging/StructuredLogTemplateProvidersCmap.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,18 @@ private static void AddCmapTemplates()
5555

5656
AddTemplateProvider<ConnectionPoolCheckedOutConnectionEvent>(
5757
LogLevel.Debug,
58-
ConnectionCommonParams(),
59-
(e, _) => GetParams(e.ConnectionId, "Connection checked out"));
58+
ConnectionCommonParams(DurationMS),
59+
(e, _) => GetParams(e.ConnectionId, "Connection checked out", e.Duration.TotalMilliseconds));
6060

6161
AddTemplateProvider<ConnectionPoolCheckingOutConnectionFailedEvent>(
6262
LogLevel.Debug,
63-
CmapCommonParams(Reason, Error),
63+
CmapCommonParams(Reason, Error, DurationMS),
6464
(e, o) => GetParams(
6565
e.ServerId,
6666
"Connection checkout failed",
6767
GetCheckoutFailedReason(e.Reason),
68-
FormatException(e.Exception, o)));
68+
FormatException(e.Exception, o),
69+
e.Duration.TotalMilliseconds));
6970

7071
AddTemplateProvider<ConnectionPoolRemovingConnectionEvent>(
7172
LogLevel.Debug,

src/MongoDB.Driver.Core/Core/Logging/StructuredLogTemplateProvidersConnection.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ private static void AddConnectionTemplates()
4141

4242
AddTemplateProvider<ConnectionOpenedEvent>(
4343
LogLevel.Debug,
44-
ConnectionCommonParams(),
45-
(e, _) => GetParams(e.ConnectionId, "Connection ready"));
44+
ConnectionCommonParams(DurationMS),
45+
(e, _) => GetParams(e.ConnectionId, "Connection ready", e.Duration.TotalMilliseconds));
4646

4747
AddTemplateProvider<ConnectionOpeningEvent>(
4848
LogLevel.Debug,

0 commit comments

Comments
 (0)