@@ -70,19 +70,14 @@ public void SqlClient_NamedOptions()
70
70
[ MemberData ( nameof ( SqlTestData . SqlClientCallsAreCollectedSuccessfullyCases ) , MemberType = typeof ( SqlTestData ) ) ]
71
71
public void SqlClientCallsAreCollectedSuccessfully (
72
72
string beforeCommand ,
73
- string afterCommand ,
74
73
CommandType commandType ,
75
74
string commandText ,
76
75
bool captureTextCommandContent ,
77
- bool shouldEnrich = true ,
78
76
bool emitOldAttributes = true ,
79
77
bool emitNewAttributes = false ,
80
78
bool tracingEnabled = true ,
81
79
bool metricsEnabled = true )
82
80
{
83
- using var sqlConnection = new SqlConnection ( TestConnectionString ) ;
84
- using var sqlCommand = sqlConnection . CreateCommand ( ) ;
85
-
86
81
var activities = new List < Activity > ( ) ;
87
82
var metrics = new List < Metric > ( ) ;
88
83
@@ -94,11 +89,6 @@ public void SqlClientCallsAreCollectedSuccessfully(
94
89
( opt ) =>
95
90
{
96
91
opt . SetDbStatementForText = captureTextCommandContent ;
97
- if ( shouldEnrich )
98
- {
99
- opt . Enrich = ActivityEnrichment ;
100
- }
101
-
102
92
opt . EmitOldAttributes = emitOldAttributes ;
103
93
opt . EmitNewAttributes = emitNewAttributes ;
104
94
} ) ;
@@ -118,33 +108,7 @@ public void SqlClientCallsAreCollectedSuccessfully(
118
108
119
109
try
120
110
{
121
- var operationId = Guid . NewGuid ( ) ;
122
- sqlCommand . CommandType = commandType ;
123
- #pragma warning disable CA2100
124
- sqlCommand . CommandText = commandText ;
125
- #pragma warning restore CA2100
126
-
127
- var beforeExecuteEventData = new
128
- {
129
- OperationId = operationId ,
130
- Command = sqlCommand ,
131
- Timestamp = ( long ? ) 1000000L ,
132
- } ;
133
-
134
- this . fakeSqlClientDiagnosticSource . Write (
135
- beforeCommand ,
136
- beforeExecuteEventData ) ;
137
-
138
- var afterExecuteEventData = new
139
- {
140
- OperationId = operationId ,
141
- Command = sqlCommand ,
142
- Timestamp = 2000000L ,
143
- } ;
144
-
145
- this . fakeSqlClientDiagnosticSource . Write (
146
- afterCommand ,
147
- afterExecuteEventData ) ;
111
+ this . ExecuteCommand ( commandType , commandText , false , beforeCommand ) ;
148
112
}
149
113
finally
150
114
{
@@ -158,12 +122,12 @@ public void SqlClientCallsAreCollectedSuccessfully(
158
122
{
159
123
activity = Assert . Single ( activities ) ;
160
124
VerifyActivityData (
161
- sqlCommand . CommandType ,
162
- sqlCommand . CommandText ,
125
+ commandType ,
126
+ commandText ,
163
127
captureTextCommandContent ,
164
128
false ,
165
129
false ,
166
- shouldEnrich ,
130
+ false ,
167
131
activity ,
168
132
emitOldAttributes ,
169
133
emitNewAttributes ) ;
@@ -236,15 +200,10 @@ public void SqlClientAddsConnectionLevelAttributes(
236
200
[ MemberData ( nameof ( SqlTestData . SqlClientErrorsAreCollectedSuccessfullyCases ) , MemberType = typeof ( SqlTestData ) ) ]
237
201
public void SqlClientErrorsAreCollectedSuccessfully (
238
202
string beforeCommand ,
239
- string errorCommand ,
240
- bool shouldEnrich = true ,
241
203
bool recordException = false ,
242
204
bool tracingEnabled = true ,
243
205
bool metricsEnabled = true )
244
206
{
245
- using var sqlConnection = new SqlConnection ( TestConnectionString ) ;
246
- using var sqlCommand = sqlConnection . CreateCommand ( ) ;
247
-
248
207
var activities = new List < Activity > ( ) ;
249
208
var metrics = new List < Metric > ( ) ;
250
209
var traceProviderBuilder = Sdk . CreateTracerProviderBuilder ( ) ;
@@ -254,10 +213,6 @@ public void SqlClientErrorsAreCollectedSuccessfully(
254
213
traceProviderBuilder . AddSqlClientInstrumentation ( options =>
255
214
{
256
215
options . RecordException = recordException ;
257
- if ( shouldEnrich )
258
- {
259
- options . Enrich = ActivityEnrichment ;
260
- }
261
216
} )
262
217
. AddInMemoryExporter ( activities ) ;
263
218
}
@@ -277,32 +232,7 @@ public void SqlClientErrorsAreCollectedSuccessfully(
277
232
278
233
try
279
234
{
280
- var operationId = Guid . NewGuid ( ) ;
281
- sqlCommand . CommandText = "SP_GetOrders" ;
282
- sqlCommand . CommandType = CommandType . StoredProcedure ;
283
-
284
- var beforeExecuteEventData = new
285
- {
286
- OperationId = operationId ,
287
- Command = sqlCommand ,
288
- Timestamp = ( long ? ) 1000000L ,
289
- } ;
290
-
291
- this . fakeSqlClientDiagnosticSource . Write (
292
- beforeCommand ,
293
- beforeExecuteEventData ) ;
294
-
295
- var commandErrorEventData = new
296
- {
297
- OperationId = operationId ,
298
- Command = sqlCommand ,
299
- Exception = new Exception ( "Boom!" ) ,
300
- Timestamp = 2000000L ,
301
- } ;
302
-
303
- this . fakeSqlClientDiagnosticSource . Write (
304
- errorCommand ,
305
- commandErrorEventData ) ;
235
+ this . ExecuteCommand ( CommandType . StoredProcedure , "SP_GetOrders" , true , beforeCommand ) ;
306
236
}
307
237
finally
308
238
{
@@ -316,12 +246,12 @@ public void SqlClientErrorsAreCollectedSuccessfully(
316
246
{
317
247
activity = Assert . Single ( activities ) ;
318
248
VerifyActivityData (
319
- sqlCommand . CommandType ,
320
- sqlCommand . CommandText ,
249
+ CommandType . StoredProcedure ,
250
+ "SP_GetOrders" ,
321
251
false ,
322
252
true ,
323
253
recordException ,
324
- shouldEnrich ,
254
+ false ,
325
255
activity ) ;
326
256
}
327
257
else
@@ -358,6 +288,45 @@ public void MicrosoftDataStartsActivityWithExpectedAttributes(SqlClientTestCase
358
288
this . RunSqlClientTestCase ( testCase , SqlClientDiagnosticListener . SqlMicrosoftBeforeExecuteCommand , SqlClientDiagnosticListener . SqlMicrosoftAfterExecuteCommand ) ;
359
289
}
360
290
291
+ [ Theory ]
292
+ [ InlineData ( true , false ) ]
293
+ [ InlineData ( false , false ) ]
294
+ [ InlineData ( true , true ) ]
295
+ [ InlineData ( false , true ) ]
296
+ public void ShouldEnrichWhenEnabled ( bool shouldEnrich , bool error )
297
+ {
298
+ var activities = new List < Activity > ( ) ;
299
+
300
+ using var traceProvider = Sdk . CreateTracerProviderBuilder ( )
301
+ . AddSqlClientInstrumentation (
302
+ ( opt ) =>
303
+ {
304
+ if ( shouldEnrich )
305
+ {
306
+ opt . Enrich = ActivityEnrichment ;
307
+ }
308
+ } )
309
+ . AddInMemoryExporter ( activities )
310
+ . Build ( ) ;
311
+
312
+ this . ExecuteCommand ( CommandType . Text , "SELECT * FROM Foo" , error , SqlClientDiagnosticListener . SqlDataBeforeExecuteCommand ) ;
313
+ this . ExecuteCommand ( CommandType . Text , "SELECT * FROM Foo" , error , SqlClientDiagnosticListener . SqlMicrosoftBeforeExecuteCommand ) ;
314
+
315
+ Assert . Equal ( 2 , activities . Count ) ;
316
+ if ( shouldEnrich )
317
+ {
318
+ Assert . Contains ( "enriched" , activities [ 0 ] . Tags . Select ( x => x . Key ) ) ;
319
+ Assert . Contains ( "enriched" , activities [ 1 ] . Tags . Select ( x => x . Key ) ) ;
320
+ Assert . Equal ( "yes" , activities [ 0 ] . Tags . FirstOrDefault ( tag => tag . Key == "enriched" ) . Value ) ;
321
+ Assert . Equal ( "yes" , activities [ 1 ] . Tags . FirstOrDefault ( tag => tag . Key == "enriched" ) . Value ) ;
322
+ }
323
+ else
324
+ {
325
+ Assert . DoesNotContain ( activities [ 0 ] . Tags , tag => tag . Key == "enriched" ) ;
326
+ Assert . DoesNotContain ( activities [ 1 ] . Tags , tag => tag . Key == "enriched" ) ;
327
+ }
328
+ }
329
+
361
330
[ Fact ]
362
331
public void ShouldCollectTelemetryWhenFilterEvaluatesToTrue ( )
363
332
{
@@ -691,6 +660,65 @@ private Activity[] RunCommandWithFilter(
691
660
692
661
return [ .. activities ] ;
693
662
}
663
+
664
+ private void ExecuteCommand ( CommandType commandType , string commandText , bool error , string beforeCommand )
665
+ {
666
+ var afterCommand = beforeCommand == SqlClientDiagnosticListener . SqlDataBeforeExecuteCommand
667
+ ? SqlClientDiagnosticListener . SqlDataAfterExecuteCommand
668
+ : SqlClientDiagnosticListener . SqlMicrosoftAfterExecuteCommand ;
669
+
670
+ var errorCommand = beforeCommand == SqlClientDiagnosticListener . SqlDataBeforeExecuteCommand
671
+ ? SqlClientDiagnosticListener . SqlDataWriteCommandError
672
+ : SqlClientDiagnosticListener . SqlMicrosoftWriteCommandError ;
673
+
674
+ using var sqlConnection = new SqlConnection ( TestConnectionString ) ;
675
+ using var sqlCommand = sqlConnection . CreateCommand ( ) ;
676
+
677
+ var operationId = Guid . NewGuid ( ) ;
678
+ sqlCommand . CommandType = commandType ;
679
+ #pragma warning disable CA2100
680
+ sqlCommand . CommandText = commandText ;
681
+ #pragma warning restore CA2100
682
+
683
+ var beforeExecuteEventData = new
684
+ {
685
+ OperationId = operationId ,
686
+ Command = sqlCommand ,
687
+ Timestamp = ( long ? ) 1000000L ,
688
+ } ;
689
+
690
+ this . fakeSqlClientDiagnosticSource . Write (
691
+ beforeCommand ,
692
+ beforeExecuteEventData ) ;
693
+
694
+ if ( error )
695
+ {
696
+ var commandErrorEventData = new
697
+ {
698
+ OperationId = operationId ,
699
+ Command = sqlCommand ,
700
+ Exception = new Exception ( "Boom!" ) ,
701
+ Timestamp = 2000000L ,
702
+ } ;
703
+
704
+ this . fakeSqlClientDiagnosticSource . Write (
705
+ errorCommand ,
706
+ commandErrorEventData ) ;
707
+ }
708
+ else
709
+ {
710
+ var afterExecuteEventData = new
711
+ {
712
+ OperationId = operationId ,
713
+ Command = sqlCommand ,
714
+ Timestamp = 2000000L ,
715
+ } ;
716
+
717
+ this . fakeSqlClientDiagnosticSource . Write (
718
+ afterCommand ,
719
+ afterExecuteEventData ) ;
720
+ }
721
+ }
694
722
#endif
695
723
696
724
private class FakeSqlClientDiagnosticSource : IDisposable
0 commit comments