Skip to content

Commit

Permalink
Doc for Asp.net core enrichment from request & response object (#1193)
Browse files Browse the repository at this point in the history
* Doc for Asp.net core enrichment from request & response object

* nit

* moin

* trailing space removal

* m

* m
  • Loading branch information
cijothomas authored Aug 28, 2020
1 parent dd4bd3e commit a09e1f8
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions src/OpenTelemetry.Instrumentation.AspNetCore/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,71 @@ This includes incoming gRPC requests using
dotnet add package OpenTelemetry.Instrumentation.AspNetCore
```

## Enable AspNetCore Instrumentation

`OpenTelemetry.Instrumentation.AspNetCore` must be enabled at application
startup, typically in the `ConfigureServices` method of your ASP.NET Core
application's `Startup.cs` class, as shown below.

```csharp
services.AddOpenTelemetryTracerProvider(
(builder) => builder
.AddAspNetCoreInstrumentation()
);
```

## Filtering

TODO.

## Enriching automaticaly collected activity with additional information

This instrumentation library stores the raw `HttpRequest`, `HttpResponse`
objects in the activity. This can be accessed in ActivityProcessors, and
can be used to further enrich the Activity with additional tags as shown
below.

```csharp
internal class MyAspNetCoreEnrichingProcessor : ActivityProcessor
{
public override void OnStart(Activity activity)
{
// Retrieve the HttpRequest object.
var httpRequest = activity.GetCustomProperty("OTel.AspNetCore.Request")
as HttpRequest;
if (httpRequest != null)
{
// Add more tags to the activity
activity.SetTag("mycustomtag", httpRequest.Headers["myheader"]);
}
}

public override void OnEnd(Activity activity)
{
// Retrieve the HttpResponse object.
var httpResponse = activity.GetCustomProperty("OTel.AspNetCore.Response")
as HttpResponse;
if (httpResponse != null)
{
var statusCode = httpResponse.StatusCode;
bool success = statusCode < 400;
// Add more tags to the activity or replace an existing tag.
activity.SetTag("myCustomSuccess", success);
}
}
}
```

The custom processor must be added to the provider as below.

```csharp
services.AddOpenTelemetryTracerProvider(
(builder) => builder
.AddAspNetCoreInstrumentation()
.AddProcessor(new MyAspNetCoreEnrichingProcessor())
);
```

## References

* [Introduction to ASP.NET
Expand Down

0 comments on commit a09e1f8

Please sign in to comment.