Skip to content

Commit 9bb037d

Browse files
authored
[SqlClient] Refactor Enrich test (#2714)
1 parent 4eea25f commit 9bb037d

File tree

2 files changed

+106
-89
lines changed

2 files changed

+106
-89
lines changed

test/OpenTelemetry.Instrumentation.SqlClient.Tests/SqlClientTests.cs

Lines changed: 106 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,14 @@ public void SqlClient_NamedOptions()
7070
[MemberData(nameof(SqlTestData.SqlClientCallsAreCollectedSuccessfullyCases), MemberType = typeof(SqlTestData))]
7171
public void SqlClientCallsAreCollectedSuccessfully(
7272
string beforeCommand,
73-
string afterCommand,
7473
CommandType commandType,
7574
string commandText,
7675
bool captureTextCommandContent,
77-
bool shouldEnrich = true,
7876
bool emitOldAttributes = true,
7977
bool emitNewAttributes = false,
8078
bool tracingEnabled = true,
8179
bool metricsEnabled = true)
8280
{
83-
using var sqlConnection = new SqlConnection(TestConnectionString);
84-
using var sqlCommand = sqlConnection.CreateCommand();
85-
8681
var activities = new List<Activity>();
8782
var metrics = new List<Metric>();
8883

@@ -94,11 +89,6 @@ public void SqlClientCallsAreCollectedSuccessfully(
9489
(opt) =>
9590
{
9691
opt.SetDbStatementForText = captureTextCommandContent;
97-
if (shouldEnrich)
98-
{
99-
opt.Enrich = ActivityEnrichment;
100-
}
101-
10292
opt.EmitOldAttributes = emitOldAttributes;
10393
opt.EmitNewAttributes = emitNewAttributes;
10494
});
@@ -118,33 +108,7 @@ public void SqlClientCallsAreCollectedSuccessfully(
118108

119109
try
120110
{
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);
148112
}
149113
finally
150114
{
@@ -158,12 +122,12 @@ public void SqlClientCallsAreCollectedSuccessfully(
158122
{
159123
activity = Assert.Single(activities);
160124
VerifyActivityData(
161-
sqlCommand.CommandType,
162-
sqlCommand.CommandText,
125+
commandType,
126+
commandText,
163127
captureTextCommandContent,
164128
false,
165129
false,
166-
shouldEnrich,
130+
false,
167131
activity,
168132
emitOldAttributes,
169133
emitNewAttributes);
@@ -236,15 +200,10 @@ public void SqlClientAddsConnectionLevelAttributes(
236200
[MemberData(nameof(SqlTestData.SqlClientErrorsAreCollectedSuccessfullyCases), MemberType = typeof(SqlTestData))]
237201
public void SqlClientErrorsAreCollectedSuccessfully(
238202
string beforeCommand,
239-
string errorCommand,
240-
bool shouldEnrich = true,
241203
bool recordException = false,
242204
bool tracingEnabled = true,
243205
bool metricsEnabled = true)
244206
{
245-
using var sqlConnection = new SqlConnection(TestConnectionString);
246-
using var sqlCommand = sqlConnection.CreateCommand();
247-
248207
var activities = new List<Activity>();
249208
var metrics = new List<Metric>();
250209
var traceProviderBuilder = Sdk.CreateTracerProviderBuilder();
@@ -254,10 +213,6 @@ public void SqlClientErrorsAreCollectedSuccessfully(
254213
traceProviderBuilder.AddSqlClientInstrumentation(options =>
255214
{
256215
options.RecordException = recordException;
257-
if (shouldEnrich)
258-
{
259-
options.Enrich = ActivityEnrichment;
260-
}
261216
})
262217
.AddInMemoryExporter(activities);
263218
}
@@ -277,32 +232,7 @@ public void SqlClientErrorsAreCollectedSuccessfully(
277232

278233
try
279234
{
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);
306236
}
307237
finally
308238
{
@@ -316,12 +246,12 @@ public void SqlClientErrorsAreCollectedSuccessfully(
316246
{
317247
activity = Assert.Single(activities);
318248
VerifyActivityData(
319-
sqlCommand.CommandType,
320-
sqlCommand.CommandText,
249+
CommandType.StoredProcedure,
250+
"SP_GetOrders",
321251
false,
322252
true,
323253
recordException,
324-
shouldEnrich,
254+
false,
325255
activity);
326256
}
327257
else
@@ -358,6 +288,45 @@ public void MicrosoftDataStartsActivityWithExpectedAttributes(SqlClientTestCase
358288
this.RunSqlClientTestCase(testCase, SqlClientDiagnosticListener.SqlMicrosoftBeforeExecuteCommand, SqlClientDiagnosticListener.SqlMicrosoftAfterExecuteCommand);
359289
}
360290

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+
361330
[Fact]
362331
public void ShouldCollectTelemetryWhenFilterEvaluatesToTrue()
363332
{
@@ -691,6 +660,65 @@ private Activity[] RunCommandWithFilter(
691660

692661
return [.. activities];
693662
}
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+
}
694722
#endif
695723

696724
private class FakeSqlClientDiagnosticSource : IDisposable

test/OpenTelemetry.Instrumentation.SqlClient.Tests/SqlTestData.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,20 @@ public static IEnumerable<object[]> SqlClientCallsAreCollectedSuccessfullyCases(
1717
return from beforeCommand in new[] { SqlClientDiagnosticListener.SqlDataBeforeExecuteCommand, SqlClientDiagnosticListener.SqlMicrosoftBeforeExecuteCommand }
1818
from commandType in new[] { CommandType.StoredProcedure, CommandType.Text }
1919
from captureTextCommandContent in bools
20-
from shouldEnrich in bools
2120
from emitOldAttributes in bools
2221
from emitNewAttributes in bools
2322
from tracingEnabled in bools
2423
from metricsEnabled in bools
2524
where emitOldAttributes && emitNewAttributes
26-
let endCommand = beforeCommand == SqlClientDiagnosticListener.SqlDataBeforeExecuteCommand
27-
? SqlClientDiagnosticListener.SqlDataAfterExecuteCommand
28-
: SqlClientDiagnosticListener.SqlMicrosoftAfterExecuteCommand
2925
let commandText = commandType == CommandType.Text
3026
? "select * from sys.databases"
3127
: "SP_GetOrders"
3228
select new object[]
3329
{
3430
beforeCommand,
35-
endCommand,
3631
commandType,
3732
commandText,
3833
captureTextCommandContent,
39-
shouldEnrich,
4034
emitOldAttributes,
4135
emitNewAttributes,
4236
tracingEnabled,
@@ -52,14 +46,9 @@ from shouldEnrich in bools
5246
from recordException in bools
5347
from tracingEnabled in bools
5448
from metricsEnabled in bools
55-
let errorCommand = beforeCommand == SqlClientDiagnosticListener.SqlDataBeforeExecuteCommand
56-
? SqlClientDiagnosticListener.SqlDataWriteCommandError
57-
: SqlClientDiagnosticListener.SqlMicrosoftWriteCommandError
5849
select new object[]
5950
{
6051
beforeCommand,
61-
errorCommand,
62-
shouldEnrich,
6352
recordException,
6453
tracingEnabled,
6554
metricsEnabled,

0 commit comments

Comments
 (0)