From c260d3d93720551f5f6f4773a828d193ba97baa7 Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Wed, 4 Nov 2020 12:02:24 -0800 Subject: [PATCH 1/4] Doc update - adding Enrich e.g. in AspNet Readme --- .../README.md | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/OpenTelemetry.Instrumentation.AspNet/README.md b/src/OpenTelemetry.Instrumentation.AspNet/README.md index 1ad20d0af43..6164950ca49 100644 --- a/src/OpenTelemetry.Instrumentation.AspNet/README.md +++ b/src/OpenTelemetry.Instrumentation.AspNet/README.md @@ -102,6 +102,41 @@ instrumentation. OpenTelemetry has a concept of [Sampler](https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/sdk.md#sampling), and the `Filter` option does the filtering *before* the Sampler is invoked. +### Enrich + +This option allows one to enrich the activity with additional information +from the raw `HttpRequest`, `HttpResponse` objects. The `Enrich` action is +called only when `activity.IsAllDataRequested` is `true`. It contains the +activity itself (which can be enriched), the name of the event, and the +actual raw object. +For event name "OnStartActivity", the actual object will be `HttpRequest`. +For event name "OnStopActivity", the actual object will be `HttpResponse` + +The following code snippet shows how to add additional tags using `Enrich`. + +```csharp +this.tracerProvider = Sdk.CreateTracerProviderBuilder() + .AddAspNetInstrumentation(opt => opt.Enrich + = (activity, eventName, rawObject) => + { + if (eventName.Equals("OnStartActivity")) + { + if (rawObject is HttpRequest httpRequest) + { + activity.SetTag("physicalPath", httpRequest.PhysicalPath); + } + } + else if (eventName.Equals("OnStopActivity")) + { + if (rawObject is HttpResponse httpResponse) + { + activity.SetTag("responseType", httpResponse.ContentType); + } + } + }) + .Build(); +``` + ### Special topic - Enriching automatically collected telemetry ASP.NET instrumentation stores the `HttpRequest`, `HttpResponse` objects in the From 98f9808596a9647b1cea4fa9027f72a7d49b4b67 Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Wed, 4 Nov 2020 12:24:18 -0800 Subject: [PATCH 2/4] rmv special topic - enrich --- src/OpenTelemetry.Instrumentation.AspNet/README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/OpenTelemetry.Instrumentation.AspNet/README.md b/src/OpenTelemetry.Instrumentation.AspNet/README.md index 6164950ca49..4ebe711717e 100644 --- a/src/OpenTelemetry.Instrumentation.AspNet/README.md +++ b/src/OpenTelemetry.Instrumentation.AspNet/README.md @@ -137,8 +137,6 @@ this.tracerProvider = Sdk.CreateTracerProviderBuilder() .Build(); ``` -### Special topic - Enriching automatically collected telemetry - ASP.NET instrumentation stores the `HttpRequest`, `HttpResponse` objects in the `Activity`. These can be accessed in `BaseProcessor`, and can be used to further enrich the Activity as shown below. From 890a1ccfc6bc508caa915698aba20f860472e769 Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Wed, 4 Nov 2020 12:30:35 -0800 Subject: [PATCH 3/4] rmv old way of enrich in aspnet --- .../README.md | 51 ------------------- 1 file changed, 51 deletions(-) diff --git a/src/OpenTelemetry.Instrumentation.AspNet/README.md b/src/OpenTelemetry.Instrumentation.AspNet/README.md index 4ebe711717e..bdb4894e150 100644 --- a/src/OpenTelemetry.Instrumentation.AspNet/README.md +++ b/src/OpenTelemetry.Instrumentation.AspNet/README.md @@ -137,57 +137,6 @@ this.tracerProvider = Sdk.CreateTracerProviderBuilder() .Build(); ``` -ASP.NET instrumentation stores the `HttpRequest`, `HttpResponse` objects in the -`Activity`. These can be accessed in `BaseProcessor`, and can be used to -further enrich the Activity as shown below. - -The key name for HttpRequest custom property inside Activity is "OTel.AspNet.Request". - -The key name for HttpResponse custom property inside Activity is "OTel.AspNet.Response". - -```csharp -internal class MyAspNetEnrichingProcessor : BaseProcessor -{ - 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 or replace an existing tag. - 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. It is important to -add the enrichment processor before any exporters so that exporters see the -changes done by them. - -```csharp - this.tracerProvider = Sdk.CreateTracerProviderBuilder() - .AddAspNetInstrumentation() - .AddProcessor(new MyAspNetEnrichingProcessor()) - .AddJaegerExporter() - .Build(); -``` - ## References * [ASP.NET](https://dotnet.microsoft.com/apps/aspnet) From 46879fa7ce64d3d9760f3e0100f119a09dec71d2 Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Thu, 5 Nov 2020 12:26:38 -0800 Subject: [PATCH 4/4] adding processor option to enrich in aspnet readme --- src/OpenTelemetry.Instrumentation.AspNet/README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/OpenTelemetry.Instrumentation.AspNet/README.md b/src/OpenTelemetry.Instrumentation.AspNet/README.md index bdb4894e150..215c15b67a0 100644 --- a/src/OpenTelemetry.Instrumentation.AspNet/README.md +++ b/src/OpenTelemetry.Instrumentation.AspNet/README.md @@ -137,6 +137,11 @@ this.tracerProvider = Sdk.CreateTracerProviderBuilder() .Build(); ``` +[Processor](../../docs/trace/extending-the-sdk/README.md#processor), +is the general extensibility point to add additional properties to any activity. +The `Enrich` option is specific to this instrumentation, and is provided to +get access to `HttpRequest` and `HttpResponse`. + ## References * [ASP.NET](https://dotnet.microsoft.com/apps/aspnet)