Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Http] Update enrich callbacks for http #3792

Merged
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationOptions
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationOptions.Enrich.get -> System.Action<System.Diagnostics.Activity, string, object>
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationOptions.Enrich.set -> void
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationOptions.EnrichWithException.get -> System.Action<System.Diagnostics.Activity, System.Exception>
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationOptions.EnrichWithException.set -> void
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationOptions.EnrichWithHttpRequestMessage.get -> System.Action<System.Diagnostics.Activity, System.Net.Http.HttpRequestMessage>
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationOptions.EnrichWithHttpRequestMessage.set -> void
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationOptions.EnrichWithHttpResponseMessage.get -> System.Action<System.Diagnostics.Activity, System.Net.Http.HttpResponseMessage>
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationOptions.EnrichWithHttpResponseMessage.set -> void
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationOptions.Filter.get -> System.Func<System.Net.Http.HttpRequestMessage, bool>
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationOptions.Filter.set -> void
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationOptions.HttpClientInstrumentationOptions() -> void
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationOptions.RecordException.get -> bool
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationOptions.RecordException.set -> void
OpenTelemetry.Instrumentation.Http.HttpWebRequestInstrumentationOptions
OpenTelemetry.Instrumentation.Http.HttpWebRequestInstrumentationOptions.Enrich.get -> System.Action<System.Diagnostics.Activity, string, object>
OpenTelemetry.Instrumentation.Http.HttpWebRequestInstrumentationOptions.Enrich.set -> void
OpenTelemetry.Instrumentation.Http.HttpWebRequestInstrumentationOptions.EnrichWithException.get -> System.Action<System.Diagnostics.Activity, System.Exception>
OpenTelemetry.Instrumentation.Http.HttpWebRequestInstrumentationOptions.EnrichWithException.set -> void
OpenTelemetry.Instrumentation.Http.HttpWebRequestInstrumentationOptions.EnrichWithHttpWebRequest.get -> System.Action<System.Diagnostics.Activity, System.Net.HttpWebRequest>
OpenTelemetry.Instrumentation.Http.HttpWebRequestInstrumentationOptions.EnrichWithHttpWebRequest.set -> void
OpenTelemetry.Instrumentation.Http.HttpWebRequestInstrumentationOptions.EnrichWithHttpWebResponse.get -> System.Action<System.Diagnostics.Activity, System.Net.HttpWebResponse>
OpenTelemetry.Instrumentation.Http.HttpWebRequestInstrumentationOptions.EnrichWithHttpWebResponse.set -> void
OpenTelemetry.Instrumentation.Http.HttpWebRequestInstrumentationOptions.Filter.get -> System.Func<System.Net.HttpWebRequest, bool>
OpenTelemetry.Instrumentation.Http.HttpWebRequestInstrumentationOptions.Filter.set -> void
OpenTelemetry.Instrumentation.Http.HttpWebRequestInstrumentationOptions.HttpWebRequestInstrumentationOptions() -> void
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationOptions
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationOptions.Enrich.get -> System.Action<System.Diagnostics.Activity, string, object>
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationOptions.Enrich.set -> void
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationOptions.EnrichWithException.get -> System.Action<System.Diagnostics.Activity, System.Exception>
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationOptions.EnrichWithException.set -> void
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationOptions.EnrichWithHttpRequestMessage.get -> System.Action<System.Diagnostics.Activity, System.Net.Http.HttpRequestMessage>
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationOptions.EnrichWithHttpRequestMessage.set -> void
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationOptions.EnrichWithHttpResponseMessage.get -> System.Action<System.Diagnostics.Activity, System.Net.Http.HttpResponseMessage>
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationOptions.EnrichWithHttpResponseMessage.set -> void
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationOptions.Filter.get -> System.Func<System.Net.Http.HttpRequestMessage, bool>
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationOptions.Filter.set -> void
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationOptions.HttpClientInstrumentationOptions() -> void
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationOptions
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationOptions.Enrich.get -> System.Action<System.Diagnostics.Activity, string, object>
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationOptions.Enrich.set -> void
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationOptions.EnrichWithException.get -> System.Action<System.Diagnostics.Activity, System.Exception>
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationOptions.EnrichWithException.set -> void
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationOptions.EnrichWithHttpRequestMessage.get -> System.Action<System.Diagnostics.Activity, System.Net.Http.HttpRequestMessage>
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationOptions.EnrichWithHttpRequestMessage.set -> void
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationOptions.EnrichWithHttpResponseMessage.get -> System.Action<System.Diagnostics.Activity, System.Net.Http.HttpResponseMessage>
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationOptions.EnrichWithHttpResponseMessage.set -> void
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationOptions.Filter.get -> System.Func<System.Net.Http.HttpRequestMessage, bool>
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationOptions.Filter.set -> void
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationOptions.HttpClientInstrumentationOptions() -> void
Expand All @@ -11,4 +15,4 @@ OpenTelemetry.Trace.TracerProviderBuilderExtensions
static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.AddHttpClientInstrumentation(this OpenTelemetry.Metrics.MeterProviderBuilder builder) -> OpenTelemetry.Metrics.MeterProviderBuilder
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddHttpClientInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder) -> OpenTelemetry.Trace.TracerProviderBuilder
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddHttpClientInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder, string name, System.Action<OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationOptions> configureHttpClientInstrumentationOptions) -> OpenTelemetry.Trace.TracerProviderBuilder
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddHttpClientInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder, System.Action<OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationOptions> configureHttpClientInstrumentationOptions) -> OpenTelemetry.Trace.TracerProviderBuilder
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddHttpClientInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder, System.Action<OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationOptions> configureHttpClientInstrumentationOptions) -> OpenTelemetry.Trace.TracerProviderBuilder
16 changes: 16 additions & 0 deletions src/OpenTelemetry.Instrumentation.Http/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@

## Unreleased

* *Breaking change** The `Enrich` callback option has been removed. For better
usability, it has been replaced by three separate options: In case of
`HttpClient` the new options are `EnrichWithHttpRequestMessage`,
`EnrichWithHttpResponseMessage` and `EnrichWithException` and in case of
`HttpWebRequest` the new options are `EnrichWithHttpWebRequest`,
`EnrichWithHttpWebResponse` and `EnrichWithException`. Previously, the single
`Enrich` callback required the consumer to detect which event triggered the
callback to be invoked (e.g., request start, response end, or an exception)
and then cast the object received to the appropriate type:
`HttpRequestMessage`, `HttpResponsemessage`, or `Exception` in case of
`HttpClient` and `HttpWebRequest`,`HttpWebResponse` and `Exception` in case of
`HttpWebRequest`. The separate callbacks make it clear what event triggers
them and there is no longer the need to cast the argument to the expected
type.
([#3792](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3792))

* Added back `netstandard2.0` target.
([#3787](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3787))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,32 @@ public class HttpClientInstrumentationOptions
/// <remarks>
/// <para><see cref="Activity"/>: the activity being enriched.</para>
/// <para>string: the name of the event.</para>
/// <para>object: the raw object from which additional information can be extracted to enrich the activity.
/// <para><see cref="HttpRequestMessage"/> object from which additional information can be extracted to enrich the activity.
/// The type of this object depends on the event, which is given by the above parameter.</para>
/// </remarks>
public Action<Activity, string, object> Enrich { get; set; }
public Action<Activity, HttpRequestMessage> EnrichWithHttpRequestMessage { get; set; }

/// <summary>
/// Gets or sets an action to enrich an Activity.
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
/// <remarks>
/// <para><see cref="Activity"/>: the activity being enriched.</para>
/// <para>string: the name of the event.</para>
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved
/// <para><see cref="HttpResponseMessage"/> object from which additional information can be extracted to enrich the activity.
/// The type of this object depends on the event, which is given by the above parameter.</para>
/// </remarks>
public Action<Activity, HttpResponseMessage> EnrichWithHttpResponseMessage { get; set; }

/// <summary>
/// Gets or sets an action to enrich an Activity.
/// </summary>
/// <remarks>
/// <para><see cref="Activity"/>: the activity being enriched.</para>
/// <para>string: the name of the event.</para>
/// <para><see cref="Exception"/> object from which additional information can be extracted to enrich the activity.
/// The type of this object depends on the event, which is given by the above parameter.</para>
/// </remarks>
public Action<Activity, Exception> EnrichWithException { get; set; }

/// <summary>
/// Gets or sets a value indicating whether exception will be recorded as ActivityEvent or not.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,32 @@ public class HttpWebRequestInstrumentationOptions
/// <remarks>
/// <para><see cref="Activity"/>: the activity being enriched.</para>
/// <para>string: the name of the event.</para>
/// <para>object: the raw object from which additional information can be extracted to enrich the activity.
/// <para><see cref="HttpWebRequest"/> object from which additional information can be extracted to enrich the activity.
/// The type of this object depends on the event, which is given by the above parameter.</para>
/// </remarks>
public Action<Activity, string, object> Enrich { get; set; }
public Action<Activity, HttpWebRequest> EnrichWithHttpWebRequest { get; set; }

/// <summary>
/// Gets or sets an action to enrich an Activity.
/// </summary>
/// <remarks>
/// <para><see cref="Activity"/>: the activity being enriched.</para>
/// <para>string: the name of the event.</para>
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved
/// <para><see cref="HttpWebResponse"/> object from which additional information can be extracted to enrich the activity.
/// The type of this object depends on the event, which is given by the above parameter.</para>
/// </remarks>
public Action<Activity, HttpWebResponse> EnrichWithHttpWebResponse { get; set; }

/// <summary>
/// Gets or sets an action to enrich an Activity.
/// </summary>
/// <remarks>
/// <para><see cref="Activity"/>: the activity being enriched.</para>
/// <para>string: the name of the event.</para>
/// <para><see cref="Exception"/> object from which additional information can be extracted to enrich the activity.
/// The type of this object depends on the event, which is given by the above parameter.</para>
/// </remarks>
public Action<Activity, Exception> EnrichWithException { get; set; }

/// <summary>
/// Gets or sets a value indicating whether exception will be recorded as ActivityEvent or not.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public void OnStartActivity(Activity activity, object payload)

try
{
this.options.Enrich?.Invoke(activity, "OnStartActivity", request);
this.options.EnrichWithHttpRequestMessage?.Invoke(activity, request);
}
catch (Exception ex)
{
Expand Down Expand Up @@ -217,7 +217,7 @@ public void OnStopActivity(Activity activity, object payload)

try
{
this.options.Enrich?.Invoke(activity, "OnStopActivity", response);
this.options.EnrichWithHttpResponseMessage?.Invoke(activity, response);
}
catch (Exception ex)
{
Expand Down Expand Up @@ -258,7 +258,7 @@ public void OnException(Activity activity, object payload)

try
{
this.options.Enrich?.Invoke(activity, "OnException", exc);
this.options.EnrichWithException?.Invoke(activity, exc);
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private static void AddRequestTagsAndInstrumentRequest(HttpWebRequest request, A

try
{
Options.Enrich?.Invoke(activity, "OnStartActivity", request);
Options.EnrichWithHttpWebRequest?.Invoke(activity, request);
}
catch (Exception ex)
{
Expand All @@ -124,7 +124,7 @@ private static void AddResponseTags(HttpWebResponse response, Activity activity)

try
{
Options.Enrich?.Invoke(activity, "OnStopActivity", response);
Options.EnrichWithHttpWebResponse?.Invoke(activity, response);
}
catch (Exception ex)
{
Expand Down Expand Up @@ -190,7 +190,7 @@ private static void AddExceptionTags(Exception exception, Activity activity)

try
{
Options.Enrich?.Invoke(activity, "OnException", exception);
Options.EnrichWithException?.Invoke(activity, exception);
}
catch (Exception ex)
{
Expand Down
96 changes: 37 additions & 59 deletions src/OpenTelemetry.Instrumentation.Http/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,95 +112,73 @@ and the `Filter` option does the filtering *after* the Sampler is invoked.

### Enrich

This option allows one to enrich the activity with additional information from
the raw request and response 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. The object
type is different for `HttpClientInstrumentationOptions` vs
This instrumentation library provides options that can be used to
enrich the activity with additional information. These actions are called
only when `activity.IsAllDataRequested` is `true`. It contains the activity
itself (which can be enriched) and the actual raw object. The options
are different for `HttpClientInstrumentationOptions` vs
`HttpWebRequestInstrumentationOptions` and is detailed below.

#### HttpClientInstrumentationOptions

For event name "OnStartActivity", the actual object will be
`HttpRequestMessage`.

For event name "OnStopActivity", the actual object will be
`HttpResponseMessage`.

For event name "OnException", the actual object will be `Exception`.
HttpClientInstrumentationOptions provides 3 enrich options,
`EnrichWithHttpRequestMessage`, `EnrichWithHttpResponseMessage` and
`EnrichWithException`. These are based on the raw object that is passed in to
the action to enrich the activity.

Example:

```csharp
using System.Net.Http;

var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddHttpClientInstrumentation((options) => options.Enrich
= (activity, eventName, rawObject) =>
.AddHttpClientInstrumentation((options) =>
{
if (eventName.Equals("OnStartActivity"))
options.EnrichWithHttpRequestMessage = (activity, httpRequestMessage) =>
{
if (rawObject is HttpRequestMessage request)
{
activity.SetTag("requestVersion", request.Version);
}
}
else if (eventName.Equals("OnStopActivity"))
activity.SetTag("requestVersion", httpRequestMessage.Version);
};
options.EnrichWithHttpResponseMessage = (activity, httpResponseMessage) =>
{
if (rawObject is HttpResponseMessage response)
{
activity.SetTag("responseVersion", response.Version);
}
}
else if (eventName.Equals("OnException"))
activity.SetTag("responseVersion", httpResponseMessage.Version);
};
options.EnrichWithException = (activity, exception) =>
{
if (rawObject is Exception exception)
{
activity.SetTag("stackTrace", exception.StackTrace);
}
}
}).Build();
activity.SetTag("stackTrace", exception.StackTrace);
};
})
.Build();
```

#### HttpWebRequestInstrumentationOptions

For event name "OnStartActivity", the actual object will be `HttpWebRequest`.

For event name "OnStopActivity", the actual object will be `HttpWebResponse`.

For event name "OnException", the actual object will be `Exception`.
HttpClientInstrumentationOptions provides 3 enrich options,
`EnrichWithHttpWebRequest`, `EnrichWithHttpWebResponse` and
`EnrichWithException`. These are based on the raw object that is passed in to
the action to enrich the activity.

Example:

```csharp
using System.Net;

var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddHttpClientInstrumentation((options) => options.Enrich
= (activity, eventName, rawObject) =>
.AddHttpClientInstrumentation((options) =>
{
if (eventName.Equals("OnStartActivity"))
options.EnrichWithHttpWebRequest = (activity, httpWebRequest) =>
{
if (rawObject is HttpWebRequest request)
{
activity.SetTag("requestVersion", request.ProtocolVersion);
}
}
else if (eventName.Equals("OnStopActivity"))
activity.SetTag("requestVersion", httpWebRequest.Version);
};
options.EnrichWithHttpWebResponse = (activity, httpWebResponse) =>
{
if (rawObject is HttpWebResponse response)
{
activity.SetTag("responseVersion", response.ProtocolVersion);
}
}
else if (eventName.Equals("OnException"))
activity.SetTag("responseVersion", httpWebResponse.Version);
};
options.EnrichWithException = (activity, exception) =>
{
if (rawObject is Exception exception)
{
activity.SetTag("stackTrace", exception.StackTrace);
}
}
}).Build();
activity.SetTag("stackTrace", exception.StackTrace);
};
})
.Build();
```

[Processor](../../docs/trace/extending-the-sdk/README.md#processor), is the
Expand Down
Loading