Skip to content

Commit f24cc8b

Browse files
[Instrumentation.AspNet] refine documentation (#3349)
Co-authored-by: Martin Costello <[email protected]>
1 parent ab01922 commit f24cc8b

File tree

2 files changed

+33
-75
lines changed
  • src
    • OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule
    • OpenTelemetry.Instrumentation.AspNet

2 files changed

+33
-75
lines changed

src/OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule/README.md

Lines changed: 20 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@
99
[![NuGet download count badge](https://img.shields.io/nuget/dt/OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule)](https://www.nuget.org/packages/OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule/)
1010
[![codecov.io](https://codecov.io/gh/open-telemetry/opentelemetry-dotnet-contrib/branch/main/graphs/badge.svg?flag=unittests-Instrumentation.AspNet)](https://app.codecov.io/gh/open-telemetry/opentelemetry-dotnet-contrib?flags[0]=unittests-Instrumentation.AspNet)
1111

12-
The ASP.NET Telemetry HttpModule enables distributed tracing of incoming ASP.NET
13-
requests using the OpenTelemetry API.
12+
The ASP.NET Telemetry HttpModule is a skeleton to enable distributed tracing
13+
and metrics of incoming ASP.NET requests using the OpenTelemetry API.
14+
15+
> [!NOTE]
16+
> This package is a [pre-release](https://github.com/open-telemetry/opentelemetry-dotnet/blob/main/VERSIONING.md#pre-releases).
17+
Until a [stable version](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/telemetry-stability.md)
18+
is released, there can be breaking changes.
1419

1520
## Usage
1621

@@ -19,7 +24,7 @@ requests using the OpenTelemetry API.
1924
If you are using the traditional `packages.config` reference style, a
2025
`web.config` transform should run automatically and configure the
2126
`TelemetryHttpModule` for you. If you are using the more modern PackageReference
22-
style, this may be needed to be done manually. For more information, see:
27+
style, this may need to be done manually. For more information, see:
2328
[Migrate from packages.config to
2429
PackageReference](https://docs.microsoft.com/nuget/consume-packages/migrate-packages-config-to-package-reference).
2530

@@ -37,65 +42,14 @@ To configure your `web.config` manually, add this:
3742
</system.webServer>
3843
```
3944

40-
### Step 2: Register a listener
45+
### Step 2: Register hooks
4146

42-
`TelemetryHttpModule` registers an
43-
[ActivitySource](https://docs.microsoft.com/dotnet/api/system.diagnostics.activitysource)
44-
with the name `OpenTelemetry.Instrumentation.AspNet`. By default, .NET
45-
`ActivitySource` will not generate any `Activity` objects unless there is
46-
a registered listener.
47+
`TelemetryHttpModule` provides hooks to create and manage activities and metrics.
4748

48-
To register a listener automatically using OpenTelemetry, please use the
49-
[OpenTelemetry.Instrumentation.AspNet](https://www.nuget.org/packages/OpenTelemetry.Instrumentation.AspNet/)
49+
To automatically register the entire infrastructure using OpenTelemetry, please
50+
use the [OpenTelemetry.Instrumentation.AspNet](https://www.nuget.org/packages/OpenTelemetry.Instrumentation.AspNet/)
5051
NuGet package.
5152

52-
To register a listener manually, use code such as the following:
53-
54-
```csharp
55-
using System.Diagnostics;
56-
using System.Web;
57-
using System.Web.Http;
58-
using System.Web.Mvc;
59-
using System.Web.Routing;
60-
using OpenTelemetry.Instrumentation.AspNet;
61-
62-
namespace Examples.AspNet;
63-
64-
public class WebApiApplication : HttpApplication
65-
{
66-
private ActivityListener aspNetActivityListener;
67-
68-
protected void Application_Start()
69-
{
70-
this.aspNetActivityListener = new ActivityListener
71-
{
72-
ShouldListenTo = (activitySource) =>
73-
{
74-
// Only listen to TelemetryHttpModule's ActivitySource.
75-
return activitySource.Name == "OpenTelemetry.Instrumentation.AspNet";
76-
},
77-
Sample = (ref ActivityCreationOptions<ActivityContext> options) =>
78-
{
79-
// Sample everything created by TelemetryHttpModule's ActivitySource.
80-
return ActivitySamplingResult.AllDataAndRecorded;
81-
},
82-
};
83-
84-
ActivitySource.AddActivityListener(this.aspNetActivityListener);
85-
86-
GlobalConfiguration.Configure(WebApiConfig.Register);
87-
88-
AreaRegistration.RegisterAllAreas();
89-
RouteConfig.RegisterRoutes(RouteTable.Routes);
90-
}
91-
92-
protected void Application_End()
93-
{
94-
this.aspNetActivityListener?.Dispose();
95-
}
96-
}
97-
```
98-
9953
## Options
10054

10155
`TelemetryHttpModule` provides a static options property
@@ -105,7 +59,7 @@ public class WebApiApplication : HttpApplication
10559
### TextMapPropagator
10660

10761
`TextMapPropagator` controls how trace context will be extracted from incoming
108-
Http request messages. By default, [W3C Trace
62+
HTTP request messages. By default, [W3C Trace
10963
Context](https://www.w3.org/TR/trace-context/) is enabled.
11064

11165
The OpenTelemetry API ships with a handful of [standard
@@ -134,10 +88,13 @@ default supports W3C Trace Context & Baggage.
13488

13589
### Events
13690

137-
`OnRequestStartedCallback`, `OnRequestStoppedCallback`, & `OnExceptionCallback`
91+
`OnRequestStartedCallback`, `OnRequestStoppedCallback`, and `OnExceptionCallback`
13892
are provided on `TelemetryHttpModuleOptions` and will be fired by the
13993
`TelemetryHttpModule` as requests are processed.
14094

141-
A typical use case for these events is to add information (tags, events, and/or
142-
links) to the created `Activity` based on the request, response, and/or
143-
exception event being fired.
95+
A typical use case for the `OnRequestStartedCallback` event is to create an activity
96+
based on the `HttpContextBase` and `ActivityContext`.
97+
98+
`OnRequestStoppedCallback` and `OnExceptionCallback` are needed to add
99+
information (tags, events, and/or links) to the created `Activity` based on the
100+
request, response, and/or exception event being fired.

src/OpenTelemetry.Instrumentation.AspNet/README.md

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
This is an [Instrumentation
1313
Library](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/glossary.md#instrumentation-library),
1414
which instruments [ASP.NET](https://docs.microsoft.com/aspnet/overview) and
15-
collect metrics and traces about incoming web requests.
15+
collects metrics and traces about incoming web requests.
1616

1717
> [!NOTE]
1818
> This package is a [pre-release](https://github.com/open-telemetry/opentelemetry-dotnet/blob/main/VERSIONING.md#pre-releases).
@@ -36,8 +36,8 @@ dotnet add package OpenTelemetry.Instrumentation.AspNet
3636
`OpenTelemetry.Instrumentation.AspNet` requires adding an additional HttpModule
3737
to your web server. This additional HttpModule is shipped as part of
3838
[`OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule`](https://www.nuget.org/packages/OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule/)
39-
which is implicitly brought by `OpenTelemetry.Instrumentation.AspNet`. The
40-
following shows changes required to your `Web.config` when using IIS web server.
39+
which is implicitly referenced by `OpenTelemetry.Instrumentation.AspNet`. The
40+
following shows the changes required to your `Web.config` when using IIS web server.
4141

4242
```xml
4343
<system.webServer>
@@ -146,7 +146,7 @@ data collection for anything NOT recorded by the sampler. The sampler approach
146146
will reduce the impact on the process being instrumented for all filtered
147147
requests.
148148

149-
This instrumentation by default collects all the incoming http requests. It
149+
This instrumentation by default collects all incoming HTTP requests. It
150150
allows filtering of requests by using the `Filter` function in
151151
`AspNetTraceInstrumentationOptions`. This defines the condition for allowable
152152
requests. The Filter receives the `HttpContextBase` of the incoming request, and
@@ -173,7 +173,7 @@ This instrumentation library provides `EnrichWithHttpRequest`,
173173
`EnrichWithHttpResponse` and `EnrichWithException` options that can be used to
174174
enrich the activity with additional information from the raw `HttpRequestBase`,
175175
`HttpResponseBase` and `Exception` objects respectively. These actions are called
176-
only when `activity.IsAllDataRequested` is `true`. It contains the activity
176+
only when `activity.IsAllDataRequested` is `true`. They contain the activity
177177
itself (which can be enriched) and the actual raw object.
178178

179179
The following code snippet shows how to enrich the activity using all 3
@@ -202,10 +202,11 @@ this.tracerProvider = Sdk.CreateTracerProviderBuilder()
202202
.Build();
203203
```
204204

205-
[Processor](https://github.com/open-telemetry/opentelemetry-dotnet/blob/main/docs/trace/extending-the-sdk/README.md#processor),
205+
[Processor](https://github.com/open-telemetry/opentelemetry-dotnet/blob/main/docs/trace/extending-the-sdk/README.md#processor)
206206
is the general extensibility point to add additional properties to any activity.
207-
The `Enrich` option is specific to this instrumentation, and is provided to get
208-
access to `HttpRequestBase` and `HttpResponseBase`.
207+
The `EnrichWithHttpRequest`, `EnrichWithHttpResponse`, and `EnrichWithException`
208+
options are specific to this instrumentation, and are provided to get
209+
access to `HttpRequestBase`, `HttpResponseBase`, and `Exception` respectively.
209210

210211
### RecordException
211212

@@ -221,13 +222,13 @@ This instrumentation can be configured to change the default behavior by using
221222
### Metric Enrich
222223

223224
This option allows one to enrich the metric with additional information from
224-
the `HttpContextBase`. The `Enrich` action is always called unless the metric was
225-
filtered. The callback allows for modifying the tag list. If the callback
226-
throws an exception the metric will still be recorded.
225+
the `HttpContextBase`. The `EnrichWithHttpContext` action is always called
226+
unless the metric was filtered. The callback allows for modifying the tag list.
227+
If the callback throws an exception the metric will still be recorded.
227228

228229
```csharp
229230
this.meterProvider = Sdk.CreateMeterProviderBuilder()
230-
.AddAspNetInstrumentation(options => options.Enrich =
231+
.AddAspNetInstrumentation(options => options.EnrichWithHttpContext =
231232
(HttpContextBase context, ref TagList tags) =>
232233
{
233234
// Add request content type to the metric tags.

0 commit comments

Comments
 (0)