Skip to content

Commit 263129e

Browse files
[EFCore] [SqlClient] Remove SetDbQueryParameters (#3081)
Co-authored-by: Piotr Kiełkowicz <[email protected]>
1 parent c8c60c4 commit 263129e

File tree

10 files changed

+106
-55
lines changed

10 files changed

+106
-55
lines changed

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ 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
75
OpenTelemetry.Instrumentation.EntityFrameworkCore.EntityFrameworkInstrumentationOptions.EnrichWithIDbCommand.get -> System.Action<System.Diagnostics.Activity!, System.Data.IDbCommand!>?
86
OpenTelemetry.Instrumentation.EntityFrameworkCore.EntityFrameworkInstrumentationOptions.EnrichWithIDbCommand.set -> void
97
OpenTelemetry.Trace.TracerProviderBuilderExtensions

src/OpenTelemetry.Instrumentation.EntityFrameworkCore/CHANGELOG.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
DB2, MongoDB, MySQL, Oracle, PostgreSQL and SQLite.
1717
([#3025](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/3025))
1818
* 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))
19+
the `OTEL_DOTNET_EXPERIMENTAL_EFCORE_ENABLE_TRACE_DB_QUERY_PARAMETERS`
20+
environment variable.
21+
([#3015](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/3015),
22+
[#3081](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/3081))
2123
* Add the `db.query.summary` attribute and use it for the trace span name when opted
2224
into using the `OTEL_SEMCONV_STABILITY_OPT_IN` environment variable.
2325
([#3022](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/3022))

src/OpenTelemetry.Instrumentation.EntityFrameworkCore/EntityFrameworkInstrumentationOptions.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ internal EntityFrameworkInstrumentationOptions(IConfiguration configuration)
2626
var databaseSemanticConvention = GetSemanticConventionOptIn(configuration);
2727
this.EmitOldAttributes = databaseSemanticConvention.HasFlag(DatabaseSemanticConvention.Old);
2828
this.EmitNewAttributes = databaseSemanticConvention.HasFlag(DatabaseSemanticConvention.New);
29+
30+
if (configuration["OTEL_DOTNET_EXPERIMENTAL_EFCORE_ENABLE_TRACE_DB_QUERY_PARAMETERS"] is { Length: > 0 } value &&
31+
bool.TryParse(value, out var setDbQueryParameters))
32+
{
33+
this.SetDbQueryParameters = setDbQueryParameters;
34+
}
2935
}
3036

3137
/// <summary>
@@ -70,7 +76,7 @@ internal EntityFrameworkInstrumentationOptions(IConfiguration configuration)
7076
/// contain any sensitive data.</b>
7177
/// </para>
7278
/// </remarks>
73-
public bool SetDbQueryParameters { get; set; }
79+
internal bool SetDbQueryParameters { get; set; }
7480

7581
/// <summary>
7682
/// Gets or sets a value indicating whether the old database attributes should be emitted.

src/OpenTelemetry.Instrumentation.EntityFrameworkCore/README.md

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -141,30 +141,25 @@ services.AddOpenTelemetry()
141141
.AddConsoleExporter());
142142
```
143143

144-
### SetDbQueryParameters
144+
## Experimental features
145145

146-
`SetDbQueryParameters` controls whether `db.query.parameter.<key>` attributes
147-
are emitted.
146+
> [!NOTE]
147+
> Experimental features are not enabled by default and can only be activated with
148+
> environment variables. They are subject to change or removal in future releases.
148149
149-
Query parameters may contain sensitive data, so only enable `SetDbQueryParameters`
150+
### DB query parameters
151+
152+
The `OTEL_DOTNET_EXPERIMENTAL_EFCORE_ENABLE_TRACE_DB_QUERY_PARAMETERS` environment
153+
variable controls whether `db.query.parameter.<key>` attributes are emitted.
154+
155+
Query parameters may contain sensitive data, so only enable this experimental feature
150156
if your queries and/or environment are appropriate for enabling this option.
151157

152-
`SetDbQueryParameters` is _false_ by default. When set to `true`, the
153-
instrumentation will set
158+
`OTEL_DOTNET_EXPERIMENTAL_EFCORE_ENABLE_TRACE_DB_QUERY_PARAMETERS` is implicitly
159+
`false` by default. When set to `true`, the instrumentation will set
154160
[`db.query.parameter.<key>`](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/database/database-spans.md#span-definition)
155161
attributes for each of the query parameters associated with a database command.
156162

157-
To enable capturing of parameter names and values use the
158-
following configuration.
159-
160-
```csharp
161-
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
162-
.AddEntityFrameworkCoreInstrumentation(
163-
options => options.SetDbQueryParameters = true)
164-
.AddConsoleExporter()
165-
.Build();
166-
```
167-
168163
## References
169164

170165
* [OpenTelemetry Project](https://opentelemetry.io/)

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ 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
119
OpenTelemetry.Instrumentation.SqlClient.SqlClientTraceInstrumentationOptions.SqlClientTraceInstrumentationOptions() -> void
1210
OpenTelemetry.Metrics.SqlClientMeterProviderBuilderExtensions
1311
OpenTelemetry.Trace.TracerProviderBuilderExtensions

src/OpenTelemetry.Instrumentation.SqlClient/CHANGELOG.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
## Unreleased
44

55
* Add `db.query.parameter.<key>` attribute(s) to query spans if opted into using
6-
the `SetDbQueryParameters` option. Not supported on .NET Framework.
7-
([#3015](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/3015))
6+
the `OTEL_DOTNET_EXPERIMENTAL_SQLCLIENT_ENABLE_TRACE_DB_QUERY_PARAMETERS`
7+
environment variable. Not supported on .NET Framework.
8+
([#3015](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/3015),
9+
[#3081](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/3081))
810

911
* Fix activities not being stopped on .NET Framework when using a global activity
1012
listener.

src/OpenTelemetry.Instrumentation.SqlClient/README.md

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -121,33 +121,6 @@ For an ASP.NET application, adding instrumentation is typically done in the
121121
This instrumentation can be configured to change the default behavior by using
122122
`SqlClientTraceInstrumentationOptions`.
123123

124-
### SetDbQueryParameters
125-
126-
> [!NOTE]
127-
> SetDbQueryParameters is not supported on .NET Framework.
128-
129-
`SetDbQueryParameters` controls whether `db.query.parameter.<key>` attributes
130-
are emitted.
131-
132-
Query parameters may contain sensitive data, so only enable `SetDbQueryParameters`
133-
if your queries and/or environment are appropriate for enabling this option.
134-
135-
`SetDbQueryParameters` is `false` by default. When set to `true`, the
136-
instrumentation will set
137-
[`db.query.parameter.<key>`](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/database/database-spans.md#span-definition)
138-
attributes for each of the query parameters associated with a database command.
139-
140-
To enable capturing of parameter names and values use the
141-
following configuration.
142-
143-
```csharp
144-
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
145-
.AddSqlClientInstrumentation(
146-
options => options.SetDbQueryParameters = true)
147-
.AddConsoleExporter()
148-
.Build();
149-
```
150-
151124
### Enrich
152125

153126
> [!NOTE]
@@ -251,6 +224,28 @@ command to set [traceparent](https://www.w3.org/TR/trace-context/#traceparent-he
251224
information for the current connection, which results in
252225
**an additional round-trip to the database**.
253226

227+
## Experimental features
228+
229+
> [!NOTE]
230+
> Experimental features are not enabled by default and can only be activated with
231+
> environment variables. They are subject to change or removal in future releases.
232+
233+
### DB query parameters
234+
235+
> [!NOTE]
236+
> This feature is available on .NET runtimes only.
237+
238+
The `OTEL_DOTNET_EXPERIMENTAL_SQLCLIENT_ENABLE_TRACE_DB_QUERY_PARAMETERS` environment
239+
variable controls whether `db.query.parameter.<key>` attributes are emitted.
240+
241+
Query parameters may contain sensitive data, so only enable this experimental feature
242+
if your queries and/or environment are appropriate for enabling this option.
243+
244+
`OTEL_DOTNET_EXPERIMENTAL_SQLCLIENT_ENABLE_TRACE_DB_QUERY_PARAMETERS` is implicitly
245+
`false` by default. When set to `true`, the instrumentation will set
246+
[`db.query.parameter.<key>`](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/database/database-spans.md#span-definition)
247+
attributes for each of the query parameters associated with a database command.
248+
254249
## Activity Duration calculation
255250

256251
`Activity.Duration` represents the time the underlying connection takes to

src/OpenTelemetry.Instrumentation.SqlClient/SqlClientTraceInstrumentationOptions.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ namespace OpenTelemetry.Instrumentation.SqlClient;
1919
public class SqlClientTraceInstrumentationOptions
2020
{
2121
internal const string ContextPropagationLevelEnvVar = "OTEL_DOTNET_EXPERIMENTAL_SQLCLIENT_ENABLE_TRACE_CONTEXT_PROPAGATION";
22+
internal const string SetDbQueryParametersEnvVar = "OTEL_DOTNET_EXPERIMENTAL_SQLCLIENT_ENABLE_TRACE_DB_QUERY_PARAMETERS";
2223

2324
/// <summary>
2425
/// Initializes a new instance of the <see cref="SqlClientTraceInstrumentationOptions"/> class.
@@ -44,6 +45,14 @@ internal SqlClientTraceInstrumentationOptions(IConfiguration configuration)
4445
{
4546
this.EnableTraceContextPropagation = enableTraceContextPropagation;
4647
}
48+
49+
if (configuration!.TryGetBoolValue(
50+
SqlClientInstrumentationEventSource.Log,
51+
SetDbQueryParametersEnvVar,
52+
out var setDbQueryParameters))
53+
{
54+
this.SetDbQueryParameters = setDbQueryParameters;
55+
}
4756
#endif
4857
}
4958

@@ -98,6 +107,7 @@ internal SqlClientTraceInstrumentationOptions(IConfiguration configuration)
98107
/// </remarks>
99108
public bool RecordException { get; set; }
100109

110+
#if !NETFRAMEWORK
101111
/// <summary>
102112
/// Gets or sets a value indicating whether or not the <see cref="SqlClientInstrumentation"/>
103113
/// should add the names and values of query parameters as the <c>db.query.parameter.{key}</c> tag.
@@ -110,10 +120,11 @@ internal SqlClientTraceInstrumentationOptions(IConfiguration configuration)
110120
/// contain any sensitive data.</b>
111121
/// </para>
112122
/// <para>
113-
/// <b>SetDbQueryParameters is only supported on .NET and .NET Core runtimes.</b>
123+
/// <b>SetDbQueryParameters is only supported on .NET runtimes.</b>
114124
/// </para>
115125
/// </remarks>
116-
public bool SetDbQueryParameters { get; set; }
126+
internal bool SetDbQueryParameters { get; set; }
127+
#endif
117128

118129
/// <summary>
119130
/// Gets or sets a value indicating whether the old database attributes should be emitted.

test/OpenTelemetry.Instrumentation.EntityFrameworkCore.Tests/EntityFrameworkInstrumentationOptionsTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,26 @@ public void ShouldEmitNewAttributesWhenStabilityOptInIsDatabase()
3939
Assert.False(options.EmitOldAttributes);
4040
Assert.True(options.EmitNewAttributes);
4141
}
42+
43+
[Fact]
44+
public void ShouldNotEmitDatabaseQueryParametersByDefault()
45+
{
46+
var configuration = new ConfigurationBuilder().Build();
47+
var options = new EntityFrameworkInstrumentationOptions(configuration);
48+
Assert.False(options.SetDbQueryParameters);
49+
}
50+
51+
[Theory]
52+
[InlineData("", false)]
53+
[InlineData("invalid", false)]
54+
[InlineData("false", false)]
55+
[InlineData("true", true)]
56+
public void ShouldAssignSetDatabaseQueryParametersFromEnvironmentVariable(string value, bool expected)
57+
{
58+
var configuration = new ConfigurationBuilder()
59+
.AddInMemoryCollection(new Dictionary<string, string?> { ["OTEL_DOTNET_EXPERIMENTAL_EFCORE_ENABLE_TRACE_DB_QUERY_PARAMETERS"] = value })
60+
.Build();
61+
var options = new EntityFrameworkInstrumentationOptions(configuration);
62+
Assert.Equal(expected, options.SetDbQueryParameters);
63+
}
4264
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,28 @@ public void ShouldNotCollectTelemetryAndShouldNotPropagateExceptionWhenFilterThr
216216

217217
Assert.Empty(activities);
218218
}
219+
220+
[Fact]
221+
public void ShouldNotEmitDatabaseQueryParametersByDefault()
222+
{
223+
var configuration = new ConfigurationBuilder().Build();
224+
var options = new SqlClientTraceInstrumentationOptions(configuration);
225+
Assert.False(options.SetDbQueryParameters);
226+
}
227+
228+
[Theory]
229+
[InlineData("", false)]
230+
[InlineData("invalid", false)]
231+
[InlineData("false", false)]
232+
[InlineData("true", true)]
233+
public void ShouldAssignSetDatabaseQueryParametersFromEnvironmentVariable(string value, bool expected)
234+
{
235+
var configuration = new ConfigurationBuilder()
236+
.AddInMemoryCollection(new Dictionary<string, string?> { ["OTEL_DOTNET_EXPERIMENTAL_SQLCLIENT_ENABLE_TRACE_DB_QUERY_PARAMETERS"] = value })
237+
.Build();
238+
var options = new SqlClientTraceInstrumentationOptions(configuration);
239+
Assert.Equal(expected, options.SetDbQueryParameters);
240+
}
219241
#endif
220242

221243
private static void ActivityEnrichment(Activity activity, string method, object obj)

0 commit comments

Comments
 (0)