Skip to content

Commit b2c48e4

Browse files
[SqlClient] Support query parameter attributes
Add support for `db.query.parameter.<key>` for SqlClient and EFCore via new opt-in `SetDbQueryParameters` option. Resolves #2241.
1 parent c9913e8 commit b2c48e4

File tree

16 files changed

+402
-5
lines changed

16 files changed

+402
-5
lines changed

src/OpenTelemetry.Instrumentation.EntityFrameworkCore/.publicApi/PublicAPI.Unshipped.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ OpenTelemetry.Instrumentation.EntityFrameworkCore.EntityFrameworkInstrumentation
22
OpenTelemetry.Instrumentation.EntityFrameworkCore.EntityFrameworkInstrumentationOptions.EntityFrameworkInstrumentationOptions() -> void
33
OpenTelemetry.Instrumentation.EntityFrameworkCore.EntityFrameworkInstrumentationOptions.Filter.get -> System.Func<string?, System.Data.IDbCommand!, bool>?
44
OpenTelemetry.Instrumentation.EntityFrameworkCore.EntityFrameworkInstrumentationOptions.Filter.set -> void
5+
OpenTelemetry.Instrumentation.EntityFrameworkCore.EntityFrameworkInstrumentationOptions.SetDbQueryParameters.get -> bool
6+
OpenTelemetry.Instrumentation.EntityFrameworkCore.EntityFrameworkInstrumentationOptions.SetDbQueryParameters.set -> void
57
OpenTelemetry.Instrumentation.EntityFrameworkCore.EntityFrameworkInstrumentationOptions.SetDbStatementForStoredProcedure.get -> bool
68
OpenTelemetry.Instrumentation.EntityFrameworkCore.EntityFrameworkInstrumentationOptions.SetDbStatementForStoredProcedure.set -> void
79
OpenTelemetry.Instrumentation.EntityFrameworkCore.EntityFrameworkInstrumentationOptions.SetDbStatementForText.get -> bool

src/OpenTelemetry.Instrumentation.EntityFrameworkCore/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
* Extend `db.system.name` values to identity additional providers related to Couchbase,
1616
DB2, MongoDB, MySQL, Oracle, PostgreSQL and SQLite.
1717
([#3025](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/3025))
18+
* Add `db.query.parameter.<key>` attribute(s) to query spans if opted into using
19+
the `SetDbQueryParameters` option.
20+
([#3015](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/3015))
1821

1922
## 1.12.0-beta.2
2023

src/OpenTelemetry.Instrumentation.EntityFrameworkCore/EntityFrameworkInstrumentationOptions.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,20 @@ internal EntityFrameworkInstrumentationOptions(IConfiguration configuration)
6868
/// </remarks>
6969
public Func<string?, IDbCommand, bool>? Filter { get; set; }
7070

71+
/// <summary>
72+
/// Gets or sets a value indicating whether or not the <see cref="EntityFrameworkInstrumentation"/>
73+
/// should add the names and values of query parameters as the <c>db.query.parameter.{key}</c> tag.
74+
/// Default value: <see langword="false"/>.
75+
/// </summary>
76+
/// <remarks>
77+
/// <para>
78+
/// <b>WARNING: SetDbQueryParameters will capture the raw
79+
/// <c>Value</c>. Make sure your query parameters never
80+
/// contain any sensitive data.</b>
81+
/// </para>
82+
/// </remarks>
83+
public bool SetDbQueryParameters { get; set; }
84+
7185
/// <summary>
7286
/// Gets or sets a value indicating whether the old database attributes should be emitted.
7387
/// </summary>

src/OpenTelemetry.Instrumentation.EntityFrameworkCore/Implementation/EntityFrameworkDiagnosticListener.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,17 @@ public override void OnEventWritten(string name, object? payload)
125125
return;
126126
}
127127

128+
object? command = null;
129+
130+
if (this.options.SetDbQueryParameters)
131+
{
132+
command = this.commandFetcher.Fetch(payload);
133+
SqlParameterProcessor.AddQueryParameters(activity, command);
134+
}
135+
128136
if (activity.IsAllDataRequested)
129137
{
130-
var command = this.commandFetcher.Fetch(payload);
138+
command ??= this.commandFetcher.Fetch(payload);
131139

132140
try
133141
{

src/OpenTelemetry.Instrumentation.EntityFrameworkCore/OpenTelemetry.Instrumentation.EntityFrameworkCore.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
<Compile Include="$(RepoRoot)\src\Shared\PropertyFetcher.cs" Link="Includes\PropertyFetcher.cs" />
3333
<Compile Include="$(RepoRoot)\src\Shared\SemanticConventions.cs" Link="Includes\SemanticConventions.cs" />
3434
<Compile Include="$(RepoRoot)\src\Shared\SqlConnectionDetails.cs" Link="Includes\SqlConnectionDetails.cs" />
35+
<Compile Include="$(RepoRoot)\src\Shared\SqlParameterProcessor.cs" Link="Includes\SqlParameterProcessor.cs" />
3536
</ItemGroup>
3637

3738
</Project>

src/OpenTelemetry.Instrumentation.SqlClient/.publicApi/PublicAPI.Unshipped.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ OpenTelemetry.Instrumentation.SqlClient.SqlClientTraceInstrumentationOptions.Fil
66
OpenTelemetry.Instrumentation.SqlClient.SqlClientTraceInstrumentationOptions.Filter.set -> void
77
OpenTelemetry.Instrumentation.SqlClient.SqlClientTraceInstrumentationOptions.RecordException.get -> bool
88
OpenTelemetry.Instrumentation.SqlClient.SqlClientTraceInstrumentationOptions.RecordException.set -> void
9+
OpenTelemetry.Instrumentation.SqlClient.SqlClientTraceInstrumentationOptions.SetDbQueryParameters.get -> bool
10+
OpenTelemetry.Instrumentation.SqlClient.SqlClientTraceInstrumentationOptions.SetDbQueryParameters.set -> void
911
OpenTelemetry.Instrumentation.SqlClient.SqlClientTraceInstrumentationOptions.SetDbStatementForText.get -> bool
1012
OpenTelemetry.Instrumentation.SqlClient.SqlClientTraceInstrumentationOptions.SetDbStatementForText.set -> void
1113
OpenTelemetry.Instrumentation.SqlClient.SqlClientTraceInstrumentationOptions.SqlClientTraceInstrumentationOptions() -> void

src/OpenTelemetry.Instrumentation.SqlClient/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ Released 2025-Jul-15
2525
(see [SET CONTEXT_INFO](https://learn.microsoft.com/en-us/sql/t-sql/statements/set-context-info-transact-sql?view=sql-server-ver16)).
2626
Note that this option incurs an additional round-trip to the database.
2727

28+
* Add `db.query.parameter.<key>` attribute(s) to query spans if opted into using
29+
the `SetDbQueryParameters` option. Not supported on .NET Framework.
30+
([#3015](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/3015))
31+
2832
## 1.12.0-beta.1
2933

3034
Released 2025-May-06

src/OpenTelemetry.Instrumentation.SqlClient/Implementation/SqlClientDiagnosticListener.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ public override void OnEventWritten(string name, object? payload)
121121
}
122122
#endif
123123

124+
if (options.SetDbQueryParameters)
125+
{
126+
SqlParameterProcessor.AddQueryParameters(activity, command);
127+
}
128+
124129
if (activity.IsAllDataRequested)
125130
{
126131
try

src/OpenTelemetry.Instrumentation.SqlClient/OpenTelemetry.Instrumentation.SqlClient.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,13 @@
2828
<Compile Include="$(RepoRoot)\src\Shared\PropertyFetcher.cs" Link="Includes\PropertyFetcher.cs" />
2929
<Compile Include="$(RepoRoot)\src\Shared\SemanticConventions.cs" Link="Includes\SemanticConventions.cs" />
3030
<Compile Include="$(RepoRoot)\src\Shared\SqlConnectionDetails.cs" Link="Includes\SqlConnectionDetails.cs" />
31+
<Compile Include="$(RepoRoot)\src\Shared\SqlParameterProcessor.cs" Link="Includes\SqlParameterProcessor.cs" />
3132
<Compile Include="$(RepoRoot)\src\Shared\SqlProcessor.cs" Link="Includes\SqlProcessor.cs" />
3233
<Compile Include="$(RepoRoot)\src\Shared\SqlStatementInfo.cs" Link="Includes\SqlStatementInfo.cs" />
3334
</ItemGroup>
3435

3536
<ItemGroup>
36-
<PackageReference Include="Microsoft.Extensions.Configuration" Version="$(MicrosoftExtensionsConfigurationPkgVer)"/>
37+
<PackageReference Include="Microsoft.Extensions.Configuration" Version="$(MicrosoftExtensionsConfigurationPkgVer)" />
3738
<PackageReference Include="Microsoft.Extensions.Options" Version="$(MicrosoftExtensionsOptionsPkgVer)" />
3839
<PackageReference Include="OpenTelemetry.Api.ProviderBuilderExtensions" Version="$(OpenTelemetryCoreLatestVersion)" />
3940
</ItemGroup>

src/OpenTelemetry.Instrumentation.SqlClient/SqlClientTraceInstrumentationOptions.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,8 @@ internal SqlClientTraceInstrumentationOptions(IConfiguration configuration)
5050
}
5151

5252
/// <summary>
53-
/// Gets or sets a value indicating whether or not the <see
54-
/// cref="SqlClientInstrumentation"/> should add the text of commands as
55-
/// the <see cref="SemanticConventions.AttributeDbStatement"/> tag.
53+
/// Gets or sets a value indicating whether or not the <see cref="SqlClientInstrumentation"/>
54+
/// should add the text of commands as the <see cref="SemanticConventions.AttributeDbStatement"/> tag.
5655
/// Default value: <see langword="false"/>.
5756
/// </summary>
5857
/// <remarks>
@@ -134,6 +133,23 @@ internal SqlClientTraceInstrumentationOptions(IConfiguration configuration)
134133
/// </remarks>
135134
public bool RecordException { get; set; }
136135

136+
/// <summary>
137+
/// Gets or sets a value indicating whether or not the <see cref="SqlClientInstrumentation"/>
138+
/// should add the names and values of query parameters as the <c>db.query.parameter.{key}</c> tag.
139+
/// Default value: <see langword="false"/>.
140+
/// </summary>
141+
/// <remarks>
142+
/// <para>
143+
/// <b>WARNING: SetDbQueryParameters will capture the raw
144+
/// <c>Value</c>. Make sure your query parameters never
145+
/// contain any sensitive data.</b>
146+
/// </para>
147+
/// <para>
148+
/// <b>SetDbQueryParameters is only supported on .NET and .NET Core runtimes.</b>
149+
/// </para>
150+
/// </remarks>
151+
public bool SetDbQueryParameters { get; set; }
152+
137153
/// <summary>
138154
/// Gets or sets a value indicating whether the old database attributes should be emitted.
139155
/// </summary>

0 commit comments

Comments
 (0)