Skip to content

Commit e222ecb

Browse files
[Instrumentation.Http][Instrumentation.AspNetCore] Fix url.full and url.query attribute values (#5532)
1 parent 0bbebb5 commit e222ecb

22 files changed

+359
-15
lines changed

OpenTelemetry.sln

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Shared", "Shared", "{A49299
270270
src\Shared\PeerServiceResolver.cs = src\Shared\PeerServiceResolver.cs
271271
src\Shared\PeriodicExportingMetricReaderHelper.cs = src\Shared\PeriodicExportingMetricReaderHelper.cs
272272
src\Shared\PooledList.cs = src\Shared\PooledList.cs
273+
src\Shared\RedactionHelper.cs = src\Shared\RedactionHelper.cs
273274
src\Shared\RequestMethodHelper.cs = src\Shared\RequestMethodHelper.cs
274275
src\Shared\ResourceSemanticConventions.cs = src\Shared\ResourceSemanticConventions.cs
275276
src\Shared\SemanticConventions.cs = src\Shared\SemanticConventions.cs

src/OpenTelemetry.Instrumentation.AspNetCore/AspNetCoreTraceInstrumentationOptions.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ internal AspNetCoreTraceInstrumentationOptions(IConfiguration configuration)
3232
{
3333
this.EnableGrpcAspNetCoreSupport = enableGrpcInstrumentation;
3434
}
35+
36+
if (configuration.TryGetBoolValue(
37+
AspNetCoreInstrumentationEventSource.Log,
38+
"OTEL_DOTNET_EXPERIMENTAL_ASPNETCORE_DISABLE_URL_QUERY_REDACTION",
39+
out var disableUrlQueryRedaction))
40+
{
41+
this.DisableUrlQueryRedaction = disableUrlQueryRedaction;
42+
}
3543
}
3644

3745
/// <summary>
@@ -94,4 +102,14 @@ internal AspNetCoreTraceInstrumentationOptions(IConfiguration configuration)
94102
/// https://github.com/open-telemetry/semantic-conventions/blob/main/docs/rpc/rpc-spans.md.
95103
/// </remarks>
96104
internal bool EnableGrpcAspNetCoreSupport { get; set; }
105+
106+
/// <summary>
107+
/// Gets or sets a value indicating whether the url query value should be redacted or not.
108+
/// </summary>
109+
/// <remarks>
110+
/// The query parameter values are redacted with value set as Redacted.
111+
/// e.g. `?key1=value1` is set as `?key1=Redacted`.
112+
/// The redaction can be disabled by setting this property to <see langword="true" />.
113+
/// </remarks>
114+
internal bool DisableUrlQueryRedaction { get; set; }
97115
}

src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
# Changelog
22

3-
## Unreleased
3+
## 1.8.1
4+
5+
Released 2024-Apr-12
6+
7+
* **Breaking Change**: Fixed tracing instrumentation so that by default any
8+
values detected in the query string component of requests are replaced with
9+
the text `Redacted` when building the `url.query` tag. For example,
10+
`?key1=value1&key2=value2` becomes `?key1=Redacted&key2=Redacted`. You can
11+
disable this redaction by setting the environment variable
12+
`OTEL_DOTNET_EXPERIMENTAL_ASPNETCORE_DISABLE_URL_QUERY_REDACTION` to `true`.
13+
([#5532](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5532))
414

515
## 1.8.0
616

src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,14 @@ public void OnStartActivity(Activity activity, object payload)
193193

194194
if (request.QueryString.HasValue)
195195
{
196-
// QueryString should be sanitized. see: https://github.com/open-telemetry/opentelemetry-dotnet/issues/4571
197-
activity.SetTag(SemanticConventions.AttributeUrlQuery, request.QueryString.Value);
196+
if (this.options.DisableUrlQueryRedaction)
197+
{
198+
activity.SetTag(SemanticConventions.AttributeUrlQuery, request.QueryString.Value);
199+
}
200+
else
201+
{
202+
activity.SetTag(SemanticConventions.AttributeUrlQuery, RedactionHelper.GetRedactedQueryString(request.QueryString.Value));
203+
}
198204
}
199205

200206
RequestMethodHelper.SetHttpMethodTag(activity, request.Method);

src/OpenTelemetry.Instrumentation.AspNetCore/OpenTelemetry.Instrumentation.AspNetCore.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Instrumentation.GrpcNetClient\GrpcTagHelper.cs" Link="Includes\GrpcTagHelper.cs" />
1818
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Instrumentation.GrpcNetClient\StatusCanonicalCode.cs" Link="Includes\StatusCanonicalCode.cs" />
1919
<Compile Include="$(RepoRoot)\src\Shared\Guard.cs" Link="Includes\Guard.cs" />
20+
<Compile Include="$(RepoRoot)\src\Shared\RedactionHelper.cs" Link="Includes\RedactionHelper.cs" />
2021
<Compile Include="$(RepoRoot)\src\Shared\RequestMethodHelper.cs" Link="Includes\RequestMethodHelper.cs" />
2122
<Compile Include="$(RepoRoot)\src\Shared\Shims\NullableAttributes.cs" Link="Includes\Shims\NullableAttributes.cs" />
2223
<Compile Include="$(RepoRoot)\src\Shared\Options\*.cs" Link="Includes\Options\%(Filename).cs" />

src/OpenTelemetry.Instrumentation.AspNetCore/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@ for more details about each individual attribute:
7575
* `server.address`
7676
* `server.port`
7777
* `url.path`
78-
* `url.query`
78+
* `url.query` - By default, the values in the query component are replaced with
79+
the text `Redacted`. For example, `?key1=value1&key2=value2` becomes
80+
`?key1=Redacted&key2=Redacted`. You can disable this redaction by setting the
81+
environment variable
82+
`OTEL_DOTNET_EXPERIMENTAL_ASPNETCORE_DISABLE_URL_QUERY_REDACTION` to `true`.
7983
* `url.scheme`
8084

8185
[Enrich Api](#enrich) can be used if any additional attributes are

src/OpenTelemetry.Instrumentation.Http/CHANGELOG.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
# Changelog
22

3-
## Unreleased
3+
## 1.8.1
4+
5+
Released 2024-Apr-12
6+
7+
* **Breaking Change**: Fixed tracing instrumentation so that by default any
8+
values detected in the query string component of requests are replaced with
9+
the text `Redacted` when building the `url.full` tag. For example,
10+
`?key1=value1&key2=value2` becomes `?key1=Redacted&key2=Redacted`. You can
11+
disable this redaction by setting the environment variable
12+
`OTEL_DOTNET_EXPERIMENTAL_HTTPCLIENT_DISABLE_URL_QUERY_REDACTION` to `true`.
13+
([#5532](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5532))
414

515
## 1.8.0
616

src/OpenTelemetry.Instrumentation.Http/HttpClientInstrumentationTracerProviderBuilderExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ public static TracerProviderBuilder AddHttpClientInstrumentation(
5959
{
6060
services.Configure(name, configureHttpClientTraceInstrumentationOptions);
6161
}
62+
63+
services.RegisterOptionsFactory(configuration => new HttpClientTraceInstrumentationOptions(configuration));
6264
});
6365

6466
#if NETFRAMEWORK

src/OpenTelemetry.Instrumentation.Http/HttpClientTraceInstrumentationOptions.cs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33

44
using System.Diagnostics;
55
using System.Net;
6+
using System.Runtime.CompilerServices;
67
#if NETFRAMEWORK
78
using System.Net.Http;
89
#endif
9-
using System.Runtime.CompilerServices;
10+
using Microsoft.Extensions.Configuration;
1011
using OpenTelemetry.Instrumentation.Http.Implementation;
1112

1213
namespace OpenTelemetry.Instrumentation.Http;
@@ -16,6 +17,27 @@ namespace OpenTelemetry.Instrumentation.Http;
1617
/// </summary>
1718
public class HttpClientTraceInstrumentationOptions
1819
{
20+
/// <summary>
21+
/// Initializes a new instance of the <see cref="HttpClientTraceInstrumentationOptions"/> class.
22+
/// </summary>
23+
public HttpClientTraceInstrumentationOptions()
24+
: this(new ConfigurationBuilder().AddEnvironmentVariables().Build())
25+
{
26+
}
27+
28+
internal HttpClientTraceInstrumentationOptions(IConfiguration configuration)
29+
{
30+
Debug.Assert(configuration != null, "configuration was null");
31+
32+
if (configuration.TryGetBoolValue(
33+
HttpInstrumentationEventSource.Log,
34+
"OTEL_DOTNET_EXPERIMENTAL_HTTPCLIENT_DISABLE_URL_QUERY_REDACTION",
35+
out var disableUrlQueryRedaction))
36+
{
37+
this.DisableUrlQueryRedaction = disableUrlQueryRedaction;
38+
}
39+
}
40+
1941
/// <summary>
2042
/// Gets or sets a filter function that determines whether or not to
2143
/// collect telemetry on a per request basis.
@@ -125,6 +147,16 @@ public class HttpClientTraceInstrumentationOptions
125147
/// </remarks>
126148
public bool RecordException { get; set; }
127149

150+
/// <summary>
151+
/// Gets or sets a value indicating whether the url query value should be redacted or not.
152+
/// </summary>
153+
/// <remarks>
154+
/// The query parameter values are redacted with value set as Redacted.
155+
/// e.g. `?key1=value1` is set as `?key1=Redacted`.
156+
/// The redaction can be disabled by setting this property to <see langword="true" />.
157+
/// </remarks>
158+
internal bool DisableUrlQueryRedaction { get; set; }
159+
128160
[MethodImpl(MethodImplOptions.AggressiveInlining)]
129161
internal bool EventFilterHttpRequestMessage(string activityName, object arg1)
130162
{

src/OpenTelemetry.Instrumentation.Http/Implementation/HttpHandlerDiagnosticListener.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public void OnStartActivity(Activity activity, object payload)
149149
activity.SetTag(SemanticConventions.AttributeServerAddress, request.RequestUri.Host);
150150
activity.SetTag(SemanticConventions.AttributeServerPort, request.RequestUri.Port);
151151

152-
activity.SetTag(SemanticConventions.AttributeUrlFull, HttpTagHelper.GetUriTagValueFromRequestUri(request.RequestUri));
152+
activity.SetTag(SemanticConventions.AttributeUrlFull, HttpTagHelper.GetUriTagValueFromRequestUri(request.RequestUri, this.options.DisableUrlQueryRedaction));
153153

154154
try
155155
{

0 commit comments

Comments
 (0)