-
-
Notifications
You must be signed in to change notification settings - Fork 203
/
SentrySqlListenerTests.cs
677 lines (602 loc) · 24.8 KB
/
SentrySqlListenerTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
using Sentry.Internal.DiagnosticSource;
using static Sentry.Internal.DiagnosticSource.SentrySqlListener;
namespace Sentry.DiagnosticSource.Tests;
public class SentrySqlListenerTests
{
private static Func<ISpan, bool> GetValidator(string type)
=> type switch
{
SqlDataWriteConnectionOpenBeforeCommand or
SqlMicrosoftWriteConnectionOpenBeforeCommand or
SqlMicrosoftWriteConnectionOpenAfterCommand or
SqlDataWriteConnectionOpenAfterCommand or
SqlMicrosoftWriteConnectionCloseAfterCommand or
SqlDataWriteConnectionCloseAfterCommand =>
span => span.Operation == "db.connection",
SqlDataBeforeExecuteCommand or
SqlMicrosoftBeforeExecuteCommand or
SqlDataAfterExecuteCommand or
SqlMicrosoftAfterExecuteCommand or
SqlDataWriteCommandError or
SqlMicrosoftWriteCommandError =>
span => span.Operation == "db.query",
_ => throw new NotSupportedException()
};
private class ThrowToOperationClass
{
// ReSharper disable UnusedMember.Local
public string OperationId => throw new Exception();
public string ConnectionId { get; set; }
// ReSharper restore UnusedMember.Local
}
private class Fixture
{
internal TransactionTracer Tracer { get; }
public InMemoryDiagnosticLogger Logger { get; }
public SentryOptions Options { get; }
public IReadOnlyCollection<ISpan> Spans => Tracer?.Spans;
public IHub Hub { get; }
public Fixture()
{
Logger = new InMemoryDiagnosticLogger();
Options = new SentryOptions
{
Debug = true,
DiagnosticLogger = Logger,
DiagnosticLevel = SentryLevel.Debug,
TracesSampleRate = 1
};
Hub = Substitute.For<IHub>();
Tracer = new TransactionTracer(Hub, "foo", "bar")
{
IsSampled = true
};
var scope = new Scope
{
Transaction = Tracer
};
Hub.When(hub => hub.ConfigureScope(Arg.Any<Action<Scope>>()))
.Do(callback => callback.Arg<Action<Scope>>().Invoke(scope));
}
}
private readonly Fixture _fixture = new();
[Fact]
public void OnNext_UnknownKey_SpanNotInvoked()
{
// Assert
var hub = _fixture.Hub;
var interceptor = new SentrySqlListener(hub, new SentryOptions());
// Act
interceptor.OnNext(new("Unknown", null));
// Assert
hub.DidNotReceive().GetSpan();
}
[Theory]
[InlineData(SqlMicrosoftBeforeExecuteCommand, true)]
[InlineData(SqlDataBeforeExecuteCommand, true)]
[InlineData(SqlMicrosoftWriteConnectionOpenBeforeCommand, false)]
[InlineData(SqlDataWriteConnectionOpenBeforeCommand, false)]
public void OnNext_KnownKey_GetSpanInvoked(string key, bool addConnectionSpan)
{
// Arrange
var hub = _fixture.Hub;
var interceptor = new SentrySqlListener(hub, new SentryOptions());
if (addConnectionSpan)
{
_fixture.Tracer.StartChild("abc").SetExtra(SqlKeys.DbConnectionId, Guid.Empty);
}
// Act
interceptor.OnNext(
new(key,
new
{
OperationId = Guid.NewGuid(),
ConnectionId = Guid.NewGuid(),
Command = new
{
CommandText = ""
}
}));
// Assert
var spans = _fixture.Spans.Where(s => s.Operation != "abc");
Assert.NotEmpty(spans);
Assert.True(GetValidator(key)(_fixture.Spans.First()));
}
[Theory]
[InlineData(SqlMicrosoftWriteConnectionOpenBeforeCommand)]
[InlineData(SqlDataWriteConnectionOpenBeforeCommand)]
public void OnNext_KnownButNotSampled_SpanNotCreated(string key)
{
// Arrange
var hub = _fixture.Hub;
_fixture.Tracer.IsSampled = false;
var interceptor = new SentrySqlListener(hub, new SentryOptions());
// Act
interceptor.OnNext(
new(key,
new
{
OperationId = Guid.NewGuid(),
ConnectionId = Guid.NewGuid(),
Command = new
{
CommandText = ""
}
}));
Assert.Empty(_fixture.Tracer.Spans);
}
[Theory]
[InlineData(SqlMicrosoftWriteConnectionOpenBeforeCommand, SqlMicrosoftWriteConnectionOpenAfterCommand,
SqlMicrosoftWriteConnectionCloseAfterCommand, SqlMicrosoftBeforeExecuteCommand,
SqlMicrosoftAfterExecuteCommand)]
[InlineData(SqlDataWriteConnectionOpenBeforeCommand, SqlDataWriteConnectionOpenAfterCommand,
SqlDataWriteConnectionCloseAfterCommand, SqlDataBeforeExecuteCommand, SqlDataAfterExecuteCommand)]
public void OnNext_HappyPaths_IsValid(string connectionOpenKey, string connectionUpdateKey,
string connectionCloseKey, string queryStartKey, string queryEndKey)
{
// Arrange
var hub = _fixture.Hub;
var interceptor = new SentrySqlListener(hub, new SentryOptions());
var query = "SELECT * FROM ...";
var connectionId = Guid.NewGuid();
var connectionOperationId = Guid.NewGuid();
var connectionOperationIdClosed = Guid.NewGuid();
var queryOperationId = Guid.NewGuid();
var dbName = "rentals";
var dbSource = "127.0.0.1";
// Act
interceptor.OnNext(
new(connectionOpenKey,
new
{
OperationId = connectionOperationId,
Connection = new
{
Database = dbName,
DataSource = dbSource
}
}));
interceptor.OnNext(
new(connectionUpdateKey,
new
{
OperationId = connectionOperationId,
ConnectionId = connectionId,
Connection = new
{
Database = dbName,
DataSource = dbSource
}
}));
interceptor.OnNext(
new(queryStartKey,
new
{
OperationId = queryOperationId,
ConnectionId = connectionId
}));
interceptor.OnNext(
new(queryEndKey,
new
{
OperationId = queryOperationId,
ConnectionId = connectionId,
Command = new
{
CommandText = query
}
}));
interceptor.OnNext(
new(connectionCloseKey,
new
{
OperationId = connectionOperationIdClosed,
ConnectionId = connectionId
}));
// Assert
_fixture.Spans.Should().HaveCount(2);
var connectionSpan = _fixture.Spans.First(s => GetValidator(connectionOpenKey)(s));
var commandSpan = _fixture.Spans.First(s => GetValidator(queryStartKey)(s));
// Validate if all spans were finished.
// ReSharper disable once ParameterOnlyUsedForPreconditionCheck.Local
Assert.All(_fixture.Spans, span =>
{
Assert.True(span.IsFinished);
Assert.Equal(SpanStatus.Ok, span.Status);
});
// Check connections between spans.
Assert.Equal(_fixture.Tracer.SpanId, connectionSpan.ParentSpanId);
Assert.Equal(_fixture.Tracer.SpanId, commandSpan.ParentSpanId);
// Validate descriptions and extra data is set correctly
Assert.Equal(query, commandSpan.Description);
Assert.Equal(queryOperationId, commandSpan.Extra.TryGetValue<string, Guid>(SqlKeys.DbOperationId));
Assert.Equal(connectionId, commandSpan.Extra.TryGetValue<string, Guid>(SqlKeys.DbConnectionId));
Assert.Equal(dbName, commandSpan.Extra.TryGetValue<string, string>(OTelKeys.DbName));
Assert.Equal("sql", commandSpan.Extra.TryGetValue<string, string>(OTelKeys.DbSystem));
Assert.Equal(dbName, connectionSpan.Description);
Assert.Equal(connectionOperationId, connectionSpan.Extra.TryGetValue<string, Guid>(SqlKeys.DbOperationId));
Assert.Equal(connectionId, connectionSpan.Extra.TryGetValue<string, Guid>(SqlKeys.DbConnectionId));
Assert.Equal(dbName, connectionSpan.Extra.TryGetValue<string, string>(OTelKeys.DbName));
Assert.Equal(dbSource, connectionSpan.Extra.TryGetValue<string, string>(OTelKeys.DbServer));
Assert.Equal("sql", connectionSpan.Extra.TryGetValue<string, string>(OTelKeys.DbSystem));
}
[Theory]
[InlineData(SqlMicrosoftWriteConnectionOpenBeforeCommand, SqlMicrosoftWriteConnectionOpenAfterCommand,
SqlMicrosoftWriteConnectionCloseAfterCommand, SqlMicrosoftBeforeExecuteCommand,
SqlMicrosoftAfterExecuteCommand)]
[InlineData(SqlDataWriteConnectionOpenBeforeCommand, SqlDataWriteConnectionOpenAfterCommand,
SqlDataWriteConnectionCloseAfterCommand, SqlDataBeforeExecuteCommand, SqlDataAfterExecuteCommand)]
public void OnNext_HappyPathsInsideChildSpan_IsValid(string connectionOpenKey, string connectionUpdateKey,
string connectionCloseKey, string queryStartKey, string queryEndKey)
{
// Arrange
var hub = _fixture.Hub;
var interceptor = new SentrySqlListener(hub, new SentryOptions());
var query = "SELECT * FROM ...";
var connectionId = Guid.NewGuid();
var connectionOperationId = Guid.NewGuid();
var connectionOperationIdClosed = Guid.NewGuid();
var queryOperationId = Guid.NewGuid();
// Act
var childSpan = _fixture.Tracer.StartChild("Child Span");
interceptor.OnNext(
new(connectionOpenKey,
new
{
OperationId = connectionOperationId
}));
interceptor.OnNext(
new(connectionUpdateKey,
new
{
OperationId = connectionOperationId,
ConnectionId = connectionId
}));
interceptor.OnNext(
new(queryStartKey,
new
{
OperationId = queryOperationId,
ConnectionId = connectionId
}));
interceptor.OnNext(
new(queryEndKey,
new
{
OperationId = queryOperationId,
ConnectionId = connectionId,
Command = new
{
CommandText = query
}
}));
interceptor.OnNext(
new(connectionCloseKey,
new
{
OperationId = connectionOperationIdClosed,
ConnectionId = connectionId
}));
childSpan.Finish();
// Assert
_fixture.Spans.Should().HaveCount(3);
var connectionSpan = _fixture.Spans.First(s => GetValidator(connectionOpenKey)(s));
var commandSpan = _fixture.Spans.First(s => GetValidator(queryStartKey)(s));
// Validate if all spans were finished.
// ReSharper disable once ParameterOnlyUsedForPreconditionCheck.Local
Assert.All(_fixture.Spans, span =>
{
Assert.True(span.IsFinished);
Assert.Equal(SpanStatus.Ok, span.Status);
});
// Check connections between spans.
Assert.Equal(childSpan.SpanId, connectionSpan.ParentSpanId);
Assert.Equal(childSpan.SpanId, commandSpan.ParentSpanId);
Assert.Equal(query, commandSpan.Description);
}
[Theory]
[InlineData(SqlMicrosoftWriteConnectionOpenBeforeCommand, SqlMicrosoftWriteConnectionOpenAfterCommand,
SqlMicrosoftWriteConnectionCloseAfterCommand)]
[InlineData(SqlDataWriteConnectionOpenBeforeCommand, SqlDataWriteConnectionOpenAfterCommand,
SqlDataWriteConnectionCloseAfterCommand)]
public void OnNext_TwoConnectionSpansWithSameId_FinishBothWithOk(string connectionBeforeKey,
string connectionUpdate, string connectionClose)
{
// Arrange
var hub = _fixture.Hub;
var interceptor = new SentrySqlListener(hub, new SentryOptions());
var connectionId = Guid.NewGuid();
var connectionOperationIds = new List<Guid>
{
Guid.NewGuid(),
Guid.NewGuid()
};
// Act
for (var i = 0; i < 2; i++)
{
interceptor.OnNext(
new(connectionBeforeKey,
new
{
OperationId = connectionOperationIds[i]
}));
// Connection Id is set.
interceptor.OnNext(
new(connectionUpdate,
new
{
OperationId = connectionOperationIds[i],
ConnectionId = connectionId
}));
interceptor.OnNext(
new(connectionClose,
new
{
OperationId = connectionOperationIds[i],
ConnectionId = connectionId
}));
}
// Assert
_fixture.Spans.Should().HaveCount(2);
// Validate if all spans were finished.
// ReSharper disable once ParameterOnlyUsedForPreconditionCheck.Local
Assert.All(_fixture.Spans, span =>
{
Assert.True(span.IsFinished);
Assert.Equal(SpanStatus.Ok, span.Status);
Assert.Equal(connectionId, (Guid)span.Extra[SqlKeys.DbConnectionId]!);
});
}
[Theory]
[InlineData(SqlMicrosoftWriteConnectionOpenBeforeCommand, SqlMicrosoftWriteConnectionOpenAfterCommand,
SqlMicrosoftWriteConnectionCloseAfterCommand, SqlMicrosoftBeforeExecuteCommand,
SqlMicrosoftAfterExecuteCommand)]
[InlineData(SqlDataWriteConnectionOpenBeforeCommand, SqlDataWriteConnectionOpenAfterCommand,
SqlDataWriteConnectionCloseAfterCommand, SqlDataBeforeExecuteCommand, SqlDataAfterExecuteCommand)]
public void OnNext_ExecuteQueryCalledBeforeConnectionId_ExecuteParentIsConnectionSpan(string connectionBeforeKey,
string connectionUpdate, string connectionClose, string executeBeforeKey, string executeAfterKey)
{
// Arrange
var hub = _fixture.Hub;
var interceptor = new SentrySqlListener(hub, new SentryOptions());
var query = "SELECT * FROM ...";
var connectionId = Guid.NewGuid();
var connectionOperationId = Guid.NewGuid();
var queryOperationId = Guid.NewGuid();
// Act
interceptor.OnNext(
new(connectionBeforeKey,
new
{
OperationId = connectionOperationId
}));
// Connection span has no connection ID and query will temporarily have Transaction as parent.
interceptor.OnNext(
new(executeBeforeKey,
new
{
OperationId = queryOperationId,
ConnectionId = connectionId
}));
// Connection Id is set.
interceptor.OnNext(
new(connectionUpdate,
new
{
OperationId = connectionOperationId,
ConnectionId = connectionId
}));
// Query sets ParentId to ConnectionId.
interceptor.OnNext(
new(executeAfterKey,
new
{
OperationId = queryOperationId,
ConnectionId = connectionId,
Command = new
{
CommandText = query
}
}));
interceptor.OnNext(
new(connectionClose,
new
{
OperationId = connectionOperationId,
ConnectionId = connectionId
}));
// Assert
_fixture.Spans.Should().HaveCount(2);
var connectionSpan = _fixture.Spans.First(s => GetValidator(connectionBeforeKey)(s));
var commandSpan = _fixture.Spans.First(s => GetValidator(executeBeforeKey)(s));
// Validate if all spans were finished.
// ReSharper disable once ParameterOnlyUsedForPreconditionCheck.Local
Assert.All(_fixture.Spans, span =>
{
Assert.True(span.IsFinished);
Assert.Equal(SpanStatus.Ok, span.Status);
});
// Check connections between spans.
Assert.Equal(_fixture.Tracer.SpanId, connectionSpan.ParentSpanId);
Assert.Equal(_fixture.Tracer.SpanId, commandSpan.ParentSpanId);
Assert.Equal(query, commandSpan.Description);
}
[Theory]
[InlineData(1)]
[InlineData(2)]
[InlineData(3)]
[InlineData(4)]
[InlineData(5)]
[InlineData(6)]
[InlineData(7)]
[InlineData(8)]
[InlineData(9)]
[InlineData(10)]
[InlineData(11)]
[InlineData(12)]
[InlineData(13)]
[InlineData(14)]
[InlineData(15)]
[InlineData(16)]
[InlineData(17)]
[InlineData(18)]
[InlineData(19)]
public async Task OnNext_ParallelExecution_IsValidAsync(int testNumber)
{
_ = testNumber;
// Arrange
var hub = _fixture.Hub;
var interceptor = new SentrySqlListener(hub, _fixture.Options);
var maxItems = 8;
var query = "SELECT * FROM ...";
var connectionsIds = Enumerable.Range(0, maxItems).Select(_ => Guid.NewGuid()).ToList();
var connectionOperationsIds = Enumerable.Range(0, maxItems).Select(_ => Guid.NewGuid()).ToList();
var connectionOperations2Ids = Enumerable.Range(0, maxItems).Select(_ => Guid.NewGuid()).ToList();
var queryOperationsIds = Enumerable.Range(0, maxItems).Select(_ => Guid.NewGuid()).ToList();
var queryOperations2Ids = Enumerable.Range(0, maxItems).Select(_ => Guid.NewGuid()).ToList();
var evt = new ManualResetEvent(false);
var ready = new ManualResetEvent(false);
var counter = 0;
// Act
var taskList = Enumerable.Range(1, maxItems).Select(_ => Task.Run(() =>
{
var threadId = Interlocked.Increment(ref counter) - 1;
if (threadId == maxItems - 1)
{
ready.Set();
}
evt.WaitOne();
// 1 repeated connection with 1 query where the first query will start before connection span gets the connectionId.
void SimulateDbRequest(List<Guid> connectionOperationIds, List<Guid> queryOperationIds)
{
interceptor.OpenConnectionStart(connectionOperationIds[threadId]);
interceptor.ExecuteQueryStart(queryOperationIds[threadId], connectionsIds[threadId]);
interceptor.OpenConnectionStarted(connectionOperationIds[threadId], connectionsIds[threadId]);
interceptor.ExecuteQueryFinish(queryOperationIds[threadId], connectionsIds[threadId], query);
interceptor.OpenConnectionClose(connectionOperationIds[threadId], connectionsIds[threadId]);
}
SimulateDbRequest(connectionOperationsIds, queryOperationsIds);
SimulateDbRequest(connectionOperations2Ids, queryOperations2Ids);
})).ToList();
ready.WaitOne();
evt.Set();
await Task.WhenAll(taskList);
// Assert
// 1 connection span and 1 query span, executed twice for 11 threads.
_fixture.Spans.Should().HaveCount(2 * 2 * maxItems);
var openSpans = _fixture.Spans.Where(span => !span.IsFinished);
var closedSpans = _fixture.Spans.Where(span => span.IsFinished);
var connectionSpans = _fixture.Spans.Where(span => span.Operation is "db.connection").ToList();
var closedConnectionSpans = connectionSpans.Where(span => span.IsFinished).ToList();
var querySpans = _fixture.Spans.Where(span => span.Operation is "db.query").ToList();
// We have two connections per thread, despite having the same ConnectionId, both will be closed.
closedConnectionSpans.Should().HaveCount(2 * maxItems);
querySpans.Should().HaveCount(2 * maxItems);
// Open Spans should not have any Connection key.
Assert.All(openSpans, span => Assert.False(span.Extra.ContainsKey(SqlKeys.DbConnectionId)));
Assert.All(closedSpans, span => Assert.Equal(SpanStatus.Ok, span.Status));
// Assert that all connectionIds is set and ParentId set to Trace.
// ReSharper disable once ParameterOnlyUsedForPreconditionCheck.Local
Assert.All(closedConnectionSpans, connectionSpan =>
{
Assert.NotNull(connectionSpan.Extra[SqlKeys.DbConnectionId]);
Assert.NotNull(connectionSpan.Extra[SqlKeys.DbOperationId]);
Assert.Equal(_fixture.Tracer.SpanId, connectionSpan.ParentSpanId);
});
// Assert all Query spans have the correct ParentId Set and record the Connection Id.
Assert.All(querySpans, querySpan =>
{
Assert.True(querySpan.IsFinished);
Assert.Equal(_fixture.Tracer.SpanId, querySpan.ParentSpanId);
var queryConnectionId = querySpan.Extra.TryGetValue<string, Guid?>(SqlKeys.DbConnectionId);
queryConnectionId.Should().NotBeNull();
});
_fixture.Logger.Entries.Should().BeEmpty();
}
[Fact]
public void OnNext_HappyPathWithError_TransactionWithErroredCommand()
{
// Arrange
var hub = _fixture.Hub;
var interceptor = new SentrySqlListener(hub, new SentryOptions());
var query = "SELECT * FROM ...";
var connectionId = Guid.NewGuid();
var connectionOperationId = Guid.NewGuid();
var connectionOperationIdClosed = Guid.NewGuid();
var queryOperationId = Guid.NewGuid();
// Act
interceptor.OnNext(
new(SqlMicrosoftWriteConnectionOpenBeforeCommand,
new
{
OperationId = connectionOperationId
}));
interceptor.OnNext(
new(SqlMicrosoftWriteConnectionOpenAfterCommand,
new
{
OperationId = connectionOperationId,
ConnectionId = connectionId
}));
interceptor.OnNext(
new(SqlMicrosoftBeforeExecuteCommand,
new
{
OperationId = queryOperationId,
ConnectionId = connectionId
}));
//Errored Query
interceptor.OnNext(
new(SqlMicrosoftWriteCommandError,
new
{
OperationId = queryOperationId,
ConnectionId = connectionId,
Command = new
{
CommandText = query
}
}));
interceptor.OnNext(
new(SqlMicrosoftWriteConnectionCloseAfterCommand,
new
{
OperationId = connectionOperationIdClosed,
ConnectionId = connectionId
}));
// Assert
_fixture.Spans.Should().HaveCount(2);
var connectionSpan = _fixture.Spans.First(s => GetValidator(SqlMicrosoftWriteConnectionOpenBeforeCommand)(s));
var commandSpan = _fixture.Spans.First(s => GetValidator(SqlMicrosoftBeforeExecuteCommand)(s));
Assert.True(connectionSpan.IsFinished);
Assert.Equal(SpanStatus.Ok, connectionSpan.Status);
// Assert the failed command.
Assert.True(commandSpan.IsFinished);
Assert.Equal(SpanStatus.InternalError, commandSpan.Status);
// Check connections between spans.
Assert.Equal(_fixture.Tracer.SpanId, connectionSpan.ParentSpanId);
Assert.Equal(_fixture.Tracer.SpanId, commandSpan.ParentSpanId);
Assert.Equal(query, commandSpan.Description);
}
[Fact]
public void OnNext_ThrowsException_ExceptionIsolated()
{
// Arrange
var hub = _fixture.Hub;
var interceptor = new SentrySqlListener(hub, new SentryOptions());
var exceptionReceived = false;
// Act
try
{
interceptor.OnNext(
new(SqlMicrosoftWriteConnectionOpenBeforeCommand,
new ThrowToOperationClass()));
}
catch (Exception)
{
exceptionReceived = true;
}
// Assert
Assert.False(exceptionReceived);
}
}