Skip to content
Merged
Next Next commit
update enrich callbacks for http
  • Loading branch information
Vishwesh Bankwar committed Oct 19, 2022
commit a595afb0db70c88d2e1e7b2a8bcec65d6a9c622d
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
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.
/// </summary>
/// <remarks>
/// <para><see cref="Activity"/>: the activity being enriched.</para>
/// <para>string: the name of the event.</para>
/// <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>
/// <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
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Moq;
using OpenTelemetry.Context.Propagation;
using OpenTelemetry.Instrumentation.Http.Implementation;
Expand Down Expand Up @@ -96,7 +97,12 @@ public void AddHttpClientInstrumentation_BadArgs()
public async Task HttpClientInstrumentationInjectsHeadersAsync(bool shouldEnrich)
{
var processor = new Mock<BaseProcessor<Activity>>();
processor.Setup(x => x.OnStart(It.IsAny<Activity>())).Callback<Activity>(c => c.SetTag("enriched", "no"));
processor.Setup(x => x.OnStart(It.IsAny<Activity>())).Callback<Activity>(c =>
{
c.SetTag("enrichedWithHttpRequestMessage", "no");
c.SetTag("enrichedWithHttpResponseMessage", "no");
});

var request = new HttpRequestMessage
{
RequestUri = new Uri(this.url),
Expand Down Expand Up @@ -131,7 +137,15 @@ public async Task HttpClientInstrumentationInjectsHeadersAsync(bool shouldEnrich
{
if (shouldEnrich)
{
o.Enrich = ActivityEnrichmentSetTag;
o.EnrichWithHttpRequestMessage = (activity, httpRequestMessage) =>
{
activity.SetTag("enrichedWithHttpRequestMessage", "yes");
};

o.EnrichWithHttpResponseMessage = (activity, httpResponseMessage) =>
{
activity.SetTag("enrichedWithHttpResponseMessage", "yes");
};
}
})
.AddProcessor(processor.Object)
Expand All @@ -158,15 +172,20 @@ public async Task HttpClientInstrumentationInjectsHeadersAsync(bool shouldEnrich
Assert.Equal($"00-{activity.Context.TraceId}-{activity.Context.SpanId}-01", traceparents.Single());
Assert.Equal("k1=v1,k2=v2", tracestates.Single());

Assert.NotEmpty(activity.Tags.Where(tag => tag.Key == "enriched"));
Assert.Equal(shouldEnrich ? "yes" : "no", activity.Tags.Where(tag => tag.Key == "enriched").FirstOrDefault().Value);
Assert.NotEmpty(activity.Tags.Where(tag => tag.Key == "enrichedWithHttpRequestMessage"));
Assert.NotEmpty(activity.Tags.Where(tag => tag.Key == "enrichedWithHttpResponseMessage"));
Assert.Equal(shouldEnrich ? "yes" : "no", activity.Tags.Where(tag => tag.Key == "enrichedWithHttpRequestMessage").FirstOrDefault().Value);
Assert.Equal(shouldEnrich ? "yes" : "no", activity.Tags.Where(tag => tag.Key == "enrichedWithHttpResponseMessage").FirstOrDefault().Value);
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public async Task HttpClientInstrumentationInjectsHeadersAsync_CustomFormat(bool shouldEnrich)
{
bool enrichWithHttpRequestMessageCalled = false;
bool enrichWithHttpResponseMessageCalled = false;

var propagator = new Mock<TextMapPropagator>();
propagator.Setup(m => m.Inject(It.IsAny<PropagationContext>(), It.IsAny<HttpRequestMessage>(), It.IsAny<Action<HttpRequestMessage, string, string>>()))
.Callback<PropagationContext, HttpRequestMessage, Action<HttpRequestMessage, string, string>>((context, message, action) =>
Expand Down Expand Up @@ -196,7 +215,8 @@ public async Task HttpClientInstrumentationInjectsHeadersAsync_CustomFormat(bool
{
if (shouldEnrich)
{
opt.Enrich = ActivityEnrichment;
opt.EnrichWithHttpRequestMessage = (activity, httpRequestMessage) => { enrichWithHttpRequestMessageCalled = true; };
opt.EnrichWithHttpResponseMessage = (activity, httpResponseMessage) => { enrichWithHttpResponseMessageCalled = true; };
}
})
.AddProcessor(processor.Object)
Expand Down Expand Up @@ -227,6 +247,12 @@ public async Task HttpClientInstrumentationInjectsHeadersAsync_CustomFormat(bool
new TraceContextPropagator(),
new BaggagePropagator(),
}));

if (shouldEnrich)
{
Assert.True(enrichWithHttpRequestMessageCalled);
Assert.True(enrichWithHttpResponseMessageCalled);
}
}

[Fact]
Expand Down Expand Up @@ -400,6 +426,9 @@ public async Task HttpClientInstrumentationCorrelationAndBaggage()
{
var activityProcessor = new Mock<BaseProcessor<Activity>>();

bool enrichWithHttpRequestMessageCalled = false;
bool enrichWithHttpResponseMessageCalled = false;

using var parent = new Activity("w3c activity");
parent.SetIdFormat(ActivityIdFormat.W3C);
parent.AddBaggage("k1", "v1");
Expand All @@ -409,7 +438,11 @@ public async Task HttpClientInstrumentationCorrelationAndBaggage()
Baggage.SetBaggage("k2", "v2");

using (Sdk.CreateTracerProviderBuilder()
.AddHttpClientInstrumentation(options => options.Enrich = ActivityEnrichment)
.AddHttpClientInstrumentation(options =>
{
options.EnrichWithHttpRequestMessage = (activity, httpRequestMessage) => { enrichWithHttpRequestMessageCalled = true; };
options.EnrichWithHttpResponseMessage = (activity, httpResponseMessage) => { enrichWithHttpResponseMessageCalled = true; };
})
.AddProcessor(activityProcessor.Object)
.Build())
{
Expand All @@ -418,6 +451,8 @@ public async Task HttpClientInstrumentationCorrelationAndBaggage()
}

Assert.Equal(5, activityProcessor.Invocations.Count);
Assert.True(enrichWithHttpRequestMessageCalled);
Assert.True(enrichWithHttpResponseMessageCalled);
}

[Fact]
Expand Down
Loading