Skip to content

Commit d1e3207

Browse files
authored
Refactor tests validating configuration for capture of db.query.text (#2718)
1 parent 5aa6d86 commit d1e3207

File tree

2 files changed

+46
-10
lines changed

2 files changed

+46
-10
lines changed

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

+46-4
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,6 @@ public void SqlClient_NamedOptions()
7171
public void SqlClientCallsAreCollectedSuccessfully(
7272
string beforeCommand,
7373
CommandType commandType,
74-
string commandText,
75-
bool captureTextCommandContent,
7674
bool emitOldAttributes = true,
7775
bool emitNewAttributes = false)
7876
{
@@ -82,7 +80,7 @@ public void SqlClientCallsAreCollectedSuccessfully(
8280
using var traceProvider = Sdk.CreateTracerProviderBuilder()
8381
.AddSqlClientInstrumentation(options =>
8482
{
85-
options.SetDbStatementForText = captureTextCommandContent;
83+
options.SetDbStatementForText = true;
8684
options.EmitOldAttributes = emitOldAttributes;
8785
options.EmitNewAttributes = emitNewAttributes;
8886
})
@@ -94,6 +92,10 @@ public void SqlClientCallsAreCollectedSuccessfully(
9492
.AddInMemoryExporter(metrics)
9593
.Build();
9694

95+
var commandText = commandType == CommandType.Text
96+
? "select * from sys.databases"
97+
: "SP_GetOrders";
98+
9799
this.ExecuteCommand(commandType, commandText, false, beforeCommand);
98100

99101
traceProvider.ForceFlush();
@@ -105,7 +107,7 @@ public void SqlClientCallsAreCollectedSuccessfully(
105107
VerifyActivityData(
106108
commandType,
107109
commandText,
108-
captureTextCommandContent,
110+
true,
109111
false,
110112
false,
111113
false,
@@ -230,6 +232,46 @@ public void MicrosoftDataStartsActivityWithExpectedAttributes(SqlClientTestCase
230232
this.RunSqlClientTestCase(testCase, SqlClientDiagnosticListener.SqlMicrosoftBeforeExecuteCommand, SqlClientDiagnosticListener.SqlMicrosoftAfterExecuteCommand);
231233
}
232234

235+
[Theory]
236+
[InlineData(true)]
237+
[InlineData(false)]
238+
public void DbQueryTextCollectedWhenEnabled(bool captureTextCommandContent)
239+
{
240+
var activities = new List<Activity>();
241+
242+
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
243+
.AddSqlClientInstrumentation(options =>
244+
{
245+
options.SetDbStatementForText = captureTextCommandContent;
246+
options.EmitOldAttributes = true;
247+
options.EmitNewAttributes = true;
248+
})
249+
.AddInMemoryExporter(activities)
250+
.Build();
251+
252+
var commandText = "select * from sys.databases";
253+
this.ExecuteCommand(CommandType.Text, commandText, false, SqlClientDiagnosticListener.SqlDataBeforeExecuteCommand);
254+
this.ExecuteCommand(CommandType.Text, commandText, false, SqlClientDiagnosticListener.SqlMicrosoftBeforeExecuteCommand);
255+
256+
tracerProvider.ForceFlush();
257+
Assert.Equal(2, activities.Count);
258+
259+
if (captureTextCommandContent)
260+
{
261+
Assert.Equal(commandText, activities[0].GetTagValue(SemanticConventions.AttributeDbStatement));
262+
Assert.Equal(commandText, activities[0].GetTagValue(SemanticConventions.AttributeDbQueryText));
263+
Assert.Equal(commandText, activities[1].GetTagValue(SemanticConventions.AttributeDbStatement));
264+
Assert.Equal(commandText, activities[1].GetTagValue(SemanticConventions.AttributeDbQueryText));
265+
}
266+
else
267+
{
268+
Assert.Null(activities[0].GetTagValue(SemanticConventions.AttributeDbStatement));
269+
Assert.Null(activities[0].GetTagValue(SemanticConventions.AttributeDbQueryText));
270+
Assert.Null(activities[1].GetTagValue(SemanticConventions.AttributeDbStatement));
271+
Assert.Null(activities[1].GetTagValue(SemanticConventions.AttributeDbQueryText));
272+
}
273+
}
274+
233275
[Theory]
234276
[InlineData(true, false)]
235277
[InlineData(false, false)]

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

-6
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,13 @@ public static IEnumerable<object[]> SqlClientCallsAreCollectedSuccessfullyCases(
1616
var bools = new[] { true, false };
1717
return from beforeCommand in new[] { SqlClientDiagnosticListener.SqlDataBeforeExecuteCommand, SqlClientDiagnosticListener.SqlMicrosoftBeforeExecuteCommand }
1818
from commandType in new[] { CommandType.StoredProcedure, CommandType.Text }
19-
from captureTextCommandContent in bools
2019
from emitOldAttributes in bools
2120
from emitNewAttributes in bools
2221
where emitOldAttributes && emitNewAttributes
23-
let commandText = commandType == CommandType.Text
24-
? "select * from sys.databases"
25-
: "SP_GetOrders"
2622
select new object[]
2723
{
2824
beforeCommand,
2925
commandType,
30-
commandText,
31-
captureTextCommandContent,
3226
emitOldAttributes,
3327
emitNewAttributes,
3428
};

0 commit comments

Comments
 (0)