Skip to content

Commit 9f99266

Browse files
authored
[Instrumentation.AspNet] Improve enrich methods (open-telemetry#1824)
1 parent b3f509a commit 9f99266

File tree

5 files changed

+60
-30
lines changed

5 files changed

+60
-30
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@ OpenTelemetry.Instrumentation.AspNet.AspNetMetricsInstrumentationOptions.Enrich.
55
OpenTelemetry.Instrumentation.AspNet.AspNetMetricsInstrumentationOptions.EnrichFunc
66
OpenTelemetry.Instrumentation.AspNet.AspNetTraceInstrumentationOptions
77
OpenTelemetry.Instrumentation.AspNet.AspNetTraceInstrumentationOptions.AspNetTraceInstrumentationOptions() -> void
8-
OpenTelemetry.Instrumentation.AspNet.AspNetTraceInstrumentationOptions.Enrich.get -> System.Action<System.Diagnostics.Activity!, string!, object!>?
9-
OpenTelemetry.Instrumentation.AspNet.AspNetTraceInstrumentationOptions.Enrich.set -> void
8+
OpenTelemetry.Instrumentation.AspNet.AspNetTraceInstrumentationOptions.EnrichWithException.get -> System.Action<System.Diagnostics.Activity!, System.Exception!>?
9+
OpenTelemetry.Instrumentation.AspNet.AspNetTraceInstrumentationOptions.EnrichWithException.set -> void
10+
OpenTelemetry.Instrumentation.AspNet.AspNetTraceInstrumentationOptions.EnrichWithHttpRequest.get -> System.Action<System.Diagnostics.Activity!, System.Web.HttpRequest!>?
11+
OpenTelemetry.Instrumentation.AspNet.AspNetTraceInstrumentationOptions.EnrichWithHttpRequest.set -> void
12+
OpenTelemetry.Instrumentation.AspNet.AspNetTraceInstrumentationOptions.EnrichWithHttpResponse.get -> System.Action<System.Diagnostics.Activity!, System.Web.HttpResponse!>?
13+
OpenTelemetry.Instrumentation.AspNet.AspNetTraceInstrumentationOptions.EnrichWithHttpResponse.set -> void
1014
OpenTelemetry.Instrumentation.AspNet.AspNetTraceInstrumentationOptions.Filter.get -> System.Func<System.Web.HttpContext!, bool>?
1115
OpenTelemetry.Instrumentation.AspNet.AspNetTraceInstrumentationOptions.Filter.set -> void
1216
OpenTelemetry.Instrumentation.AspNet.AspNetTraceInstrumentationOptions.RecordException.get -> bool

src/OpenTelemetry.Instrumentation.AspNet/AspNetTraceInstrumentationOptions.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,27 @@ public AspNetTraceInstrumentationOptions()
5555
/// </summary>
5656
/// <remarks>
5757
/// <para><see cref="Activity"/>: the activity being enriched.</para>
58-
/// <para>string: the name of the event.</para>
59-
/// <para>object: the raw object from which additional information can be extracted to enrich the activity.
60-
/// The type of this object depends on the event, which is given by the above parameter.</para>
58+
/// <para><see cref="HttpRequest"/>: the HttpRequest object from which additional information can be extracted to enrich the activity.</para>
6159
/// </remarks>
62-
public Action<Activity, string, object>? Enrich { get; set; }
60+
public Action<Activity, HttpRequest>? EnrichWithHttpRequest { get; set; }
61+
62+
/// <summary>
63+
/// Gets or sets an action to enrich an Activity.
64+
/// </summary>
65+
/// <remarks>
66+
/// <para><see cref="Activity"/>: the activity being enriched.</para>
67+
/// <para><see cref="HttpResponse"/>: the HttpResponse object from which additional information can be extracted to enrich the activity.</para>
68+
/// </remarks>
69+
public Action<Activity, HttpResponse>? EnrichWithHttpResponse { get; set; }
70+
71+
/// <summary>
72+
/// Gets or sets an action to enrich an Activity.
73+
/// </summary>
74+
/// <remarks>
75+
/// <para><see cref="Activity"/>: the activity being enriched.</para>
76+
/// <para><see cref="Exception"/>: the Exception object from which additional information can be extracted to enrich the activity.</para>
77+
/// </remarks>
78+
public Action<Activity, Exception>? EnrichWithException { get; set; }
6379

6480
/// <summary>
6581
/// Gets or sets a value indicating whether the exception will be recorded as ActivityEvent or not.

src/OpenTelemetry.Instrumentation.AspNet/CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
* **Breaking change** The `Enrich` callback option has been removed.
6+
For better usability, it has been replaced by three separate options:
7+
`EnrichWithHttpRequest`, `EnrichWithHttpResponse` and `EnrichWithException`.
8+
Previously, the single `Enrich` callback required the consumer to detect
9+
which event triggered the callback to be invoked (e.g., request start,
10+
response end, or an exception) and then cast the object received to the
11+
appropriate type: `HttpRequest`, `HttpResponse`, or `Exception`. The separate
12+
callbacks make it clear what event triggers them and there is no longer the
13+
need to cast the argument to the expected type.
14+
([#1824](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/1824))
15+
316
## 1.8.0-beta.2
417

518
Released 2024-Apr-17

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ private void OnStartActivity(Activity activity, HttpContext context)
102102

103103
try
104104
{
105-
this.options.Enrich?.Invoke(activity, "OnStartActivity", request);
105+
this.options.EnrichWithHttpRequest?.Invoke(activity, request);
106106
}
107107
catch (Exception ex)
108108
{
@@ -135,7 +135,7 @@ private void OnStopActivity(Activity activity, HttpContext context)
135135

136136
try
137137
{
138-
this.options.Enrich?.Invoke(activity, "OnStopActivity", response);
138+
this.options.EnrichWithHttpResponse?.Invoke(activity, response);
139139
}
140140
catch (Exception ex)
141141
{
@@ -158,7 +158,7 @@ private void OnException(Activity activity, HttpContext context, Exception excep
158158

159159
try
160160
{
161-
this.options.Enrich?.Invoke(activity, "OnException", exception);
161+
this.options.EnrichWithException?.Invoke(activity, exception);
162162
}
163163
catch (Exception ex)
164164
{

src/OpenTelemetry.Instrumentation.AspNet/README.md

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -164,35 +164,32 @@ and the `Filter` option does the filtering *before* the Sampler is invoked.
164164

165165
### Trace Enrich
166166

167-
This option allows one to enrich the activity with additional information from
168-
the raw `HttpRequest`, `HttpResponse` objects. The `Enrich` action is called
167+
This instrumentation library provides `EnrichWithHttpRequest`,
168+
`EnrichWithHttpResponse` and `EnrichWithException` options that can be used to
169+
enrich the activity with additional information from the raw `HttpRequest`,
170+
`HttpResponse` and `Exception` objects respectively. These actions are called
169171
only when `activity.IsAllDataRequested` is `true`. It contains the activity
170-
itself (which can be enriched), the name of the event, and the actual raw
171-
object. For event name "OnStartActivity", the actual object will be
172-
`HttpRequest`. For event name "OnStopActivity", the actual object will be
173-
`HttpResponse`
172+
itself (which can be enriched) and the actual raw object.
174173

175-
The following code snippet shows how to add additional tags using `Enrich`.
174+
The following code snippet shows how to enrich the activity using all 3
175+
different options.
176176

177177
```csharp
178178
this.tracerProvider = Sdk.CreateTracerProviderBuilder()
179-
.AddAspNetInstrumentation((options) => options.Enrich
180-
= (activity, eventName, rawObject) =>
179+
.AddAspNetInstrumentation(o =>
181180
{
182-
if (eventName.Equals("OnStartActivity"))
181+
o.EnrichWithHttpRequest = (activity, httpRequest) =>
183182
{
184-
if (rawObject is HttpRequest httpRequest)
185-
{
186-
activity.SetTag("physicalPath", httpRequest.PhysicalPath);
187-
}
188-
}
189-
else if (eventName.Equals("OnStopActivity"))
183+
activity.SetTag("physicalPath", httpRequest.PhysicalPath);
184+
};
185+
o.EnrichWithHttpResponse = (activity, httpResponse) =>
190186
{
191-
if (rawObject is HttpResponse httpResponse)
192-
{
193-
activity.SetTag("responseType", httpResponse.ContentType);
194-
}
195-
}
187+
activity.SetTag("responseType", httpResponse.ContentType);
188+
};
189+
o.EnrichWithException = (activity, exception) =>
190+
{
191+
activity.SetTag("exceptionType", exception.GetType().ToString());
192+
};
196193
})
197194
.Build();
198195
```

0 commit comments

Comments
 (0)