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

[Instrumentation.Http] Support for enrich and filter delegates for http client metric #4148

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,17 @@ OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationOptions.FilterHttpWe
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationOptions.HttpClientInstrumentationOptions() -> void
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationOptions.RecordException.get -> bool
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationOptions.RecordException.set -> void
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationMeterOptions
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationMeterOptions.HttpClientInstrumentationMeterOptions() -> void
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationMeterOptions.HttpResponseMessageEnrichmentFunc
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationMeterOptions.EnrichWithHttpResponseMessage.get -> OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationMeterOptions.HttpResponseMessageEnrichmentFunc
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationMeterOptions.EnrichWithHttpResponseMessage.set -> void
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationMeterOptions.FilterHttpResponseMessage.get -> System.Func<System.Net.Http.HttpResponseMessage, bool>
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationMeterOptions.FilterHttpResponseMessage.set -> void
OpenTelemetry.Metrics.MeterProviderBuilderExtensions
OpenTelemetry.Trace.TracerProviderBuilderExtensions
static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.AddHttpClientInstrumentation(this OpenTelemetry.Metrics.MeterProviderBuilder builder) -> OpenTelemetry.Metrics.MeterProviderBuilder
static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.AddHttpClientInstrumentation(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Action<OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationMeterOptions> configureOptions) -> 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
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,17 @@ OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationOptions.FilterHttpWe
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationOptions.HttpClientInstrumentationOptions() -> void
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationOptions.RecordException.get -> bool
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationOptions.RecordException.set -> void
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationMeterOptions
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationMeterOptions.HttpClientInstrumentationMeterOptions() -> void
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationMeterOptions.HttpResponseMessageEnrichmentFunc
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationMeterOptions.EnrichWithHttpResponseMessage.get -> OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationMeterOptions.HttpResponseMessageEnrichmentFunc
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationMeterOptions.EnrichWithHttpResponseMessage.set -> void
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationMeterOptions.FilterHttpResponseMessage.get -> System.Func<System.Net.Http.HttpResponseMessage, bool>
OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationMeterOptions.FilterHttpResponseMessage.set -> void
OpenTelemetry.Metrics.MeterProviderBuilderExtensions
OpenTelemetry.Trace.TracerProviderBuilderExtensions
static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.AddHttpClientInstrumentation(this OpenTelemetry.Metrics.MeterProviderBuilder builder) -> OpenTelemetry.Metrics.MeterProviderBuilder
static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.AddHttpClientInstrumentation(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Action<OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationMeterOptions> configureOptions) -> 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// <copyright file="HttpClientInstrumentationMeterOptions.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

using System.Diagnostics;
using System.Net.Http;

namespace OpenTelemetry.Instrumentation.Http;

/// <summary>
/// Options for HttpClient Meter instrumentation.
/// </summary>
public class HttpClientInstrumentationMeterOptions
{
/// <summary>
/// Delegate for enrichment of recorded metric with additional tags.
/// </summary>
/// <param name="name">The name of the metric being enriched.</param>
/// <param name="response"><see cref="HttpResponseMessage"/>: the HttpResponseMessage object.</param>
/// <param name="tags"><see cref="TagList"/>: List of current tags. You can add additional tags to this list. </param>
public delegate void HttpResponseMessageEnrichmentFunc(string name, HttpResponseMessage response, ref TagList tags);

/// <summary>
/// Gets or sets an action to enrich an tags collection with <see cref="HttpRequestMessage"/>.
/// </summary>
/// <remarks>
/// <para><b>
/// EnrichWithHttpRequestMessage is only executed on .NET and .NET
/// Core runtimes.
/// </b></para>
/// </remarks>
public HttpResponseMessageEnrichmentFunc EnrichWithHttpResponseMessage { get; set; }

/// <summary>
/// Gets or sets a filter function that determines whether or not to
/// collect telemetry on a per request basis.
/// </summary>
/// <remarks>
/// <para><b>
/// FilterHttpResponseMessage is only executed on .NET and .NET
/// Core runtimes.
/// </b></para>
/// Notes:
/// <list type="bullet">
/// <item>The return value for the filter function is interpreted as:
/// <list type="bullet">
/// <item>If filter returns <see langword="true" />, the request is
/// collected.</item>
/// <item>If filter returns <see langword="false" /> or throws an
/// exception the request is NOT collected.</item>
/// </list></item>
/// </list>
/// </remarks>
public Func<HttpResponseMessage, bool> FilterHttpResponseMessage { get; set; }
}
5 changes: 3 additions & 2 deletions src/OpenTelemetry.Instrumentation.Http/HttpClientMetrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ internal sealed class HttpClientMetrics : IDisposable
/// <summary>
/// Initializes a new instance of the <see cref="HttpClientMetrics"/> class.
/// </summary>
public HttpClientMetrics()
/// <param name="options">Options for HttpClient instrumentation.</param>
public HttpClientMetrics(HttpClientInstrumentationMeterOptions options)
{
this.meter = new Meter(InstrumentationName, InstrumentationVersion);
this.diagnosticSourceSubscriber = new DiagnosticSourceSubscriber(new HttpHandlerMetricsDiagnosticListener("HttpHandlerDiagnosticListener", this.meter), null);
this.diagnosticSourceSubscriber = new DiagnosticSourceSubscriber(new HttpHandlerMetricsDiagnosticListener("HttpHandlerDiagnosticListener", this.meter, options), null);
this.diagnosticSourceSubscriber.Subscribe();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,44 +26,110 @@ namespace OpenTelemetry.Instrumentation.Http.Implementation
internal sealed class HttpHandlerMetricsDiagnosticListener : ListenerHandler
{
internal const string OnStopEvent = "System.Net.Http.HttpRequestOut.Stop";
private const string HttpClientMetricName = "http.client.duration";

private readonly PropertyFetcher<HttpResponseMessage> stopResponseFetcher = new("Response");
private readonly Histogram<double> httpClientDuration;
private readonly HttpClientInstrumentationMeterOptions options;

public HttpHandlerMetricsDiagnosticListener(string name, Meter meter)
public HttpHandlerMetricsDiagnosticListener(string name, Meter meter, HttpClientInstrumentationMeterOptions options)
: base(name)
{
this.httpClientDuration = meter.CreateHistogram<double>("http.client.duration", "ms", "measures the duration of the outbound HTTP request");
this.options = options;
this.httpClientDuration = meter.CreateHistogram<double>(HttpClientMetricName, "ms", "measures the duration of the outbound HTTP request");
}

public override void OnEventWritten(string name, object payload)
{
if (name == OnStopEvent)
{
if (Sdk.SuppressInstrumentation)
{
return;
}
this.OnStopActivity(Activity.Current, payload);
}
}

var activity = Activity.Current;
if (this.stopResponseFetcher.TryFetch(payload, out HttpResponseMessage response) && response != null)
{
var request = response.RequestMessage;
private void OnStopActivity(Activity activity, object payload)
{
if (Sdk.SuppressInstrumentation)
{
return;
}

TagList tags = default;
tags.Add(new KeyValuePair<string, object>(SemanticConventions.AttributeHttpMethod, HttpTagHelper.GetNameForHttpMethod(request.Method)));
tags.Add(new KeyValuePair<string, object>(SemanticConventions.AttributeHttpScheme, request.RequestUri.Scheme));
tags.Add(new KeyValuePair<string, object>(SemanticConventions.AttributeHttpStatusCode, (int)response.StatusCode));
tags.Add(new KeyValuePair<string, object>(SemanticConventions.AttributeHttpFlavor, HttpTagHelper.GetFlavorTagValueFromProtocolVersion(request.Version)));
tags.Add(new KeyValuePair<string, object>(SemanticConventions.AttributeNetPeerName, request.RequestUri.Host));
if (!this.stopResponseFetcher.TryFetch(payload, out HttpResponseMessage response) || response == null)
{
// TODO: logging?
return;
}

if (!this.TryFilter(activity.OperationName, response))
{
return;
}

var request = response.RequestMessage;
TagList tags = default;
tags.Add(new(SemanticConventions.AttributeHttpMethod, HttpTagHelper.GetNameForHttpMethod(request.Method)));
tags.Add(new(SemanticConventions.AttributeHttpScheme, request.RequestUri.Scheme));
tags.Add(new(SemanticConventions.AttributeHttpStatusCode, (int)response.StatusCode));
tags.Add(new(SemanticConventions.AttributeHttpFlavor, HttpTagHelper.GetFlavorTagValueFromProtocolVersion(request.Version)));
tags.Add(new(SemanticConventions.AttributeNetPeerName, request.RequestUri.Host));

if (!request.RequestUri.IsDefaultPort)
{
tags.Add(new KeyValuePair<string, object>(SemanticConventions.AttributeNetPeerPort, request.RequestUri.Port));
}

if (!request.RequestUri.IsDefaultPort)
{
tags.Add(new KeyValuePair<string, object>(SemanticConventions.AttributeNetPeerPort, request.RequestUri.Port));
}
this.EnrichTags(ref tags, response);

this.httpClientDuration.Record(activity.Duration.TotalMilliseconds, tags);
this.httpClientDuration.Record(activity.Duration.TotalMilliseconds, tags);
}

/// <summary>
/// Gets or sets a filter function that determines whether or not to
/// collect telemetry on a per request basis.
/// </summary>
/// <param name="operationName">The name of operation.</param>
/// <param name="request">Proceed <see cref="HttpRequestMessage"/>.</param>
/// <returns>
/// <list type="bullet">
/// <item>The return value for the filter function is interpreted as:
/// <list type="bullet">
/// <item>If filter returns <see langword="true" />, the request is
/// collected.</item>
/// <item>If filter returns <see langword="false" /> or throws an
/// exception the request is NOT collected.</item>
/// </list></item>
/// </list>
/// </returns>
private bool TryFilter(string operationName, HttpResponseMessage request)
{
try
{
var shouldCollect = this.options.FilterHttpResponseMessage?.Invoke(request) ?? true;

if (shouldCollect)
{
return true;
}

HttpInstrumentationEventSource.Log.RequestIsFilteredOut(operationName);
return false;
}
catch (Exception ex)
{
HttpInstrumentationEventSource.Log.RequestFilterException(ex);
return false;
}
}

private void EnrichTags(ref TagList tagList, HttpResponseMessage response)
{
try
{
this.options.EnrichWithHttpResponseMessage?.Invoke(HttpClientMetricName, response, ref tagList);
}
catch (Exception ex)
{
HttpInstrumentationEventSource.Log.EnrichmentException(ex);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,31 @@ public static class MeterProviderBuilderExtensions
/// <param name="builder"><see cref="MeterProviderBuilder"/> being configured.</param>
/// <returns>The instance of <see cref="MeterProviderBuilder"/> to chain the calls.</returns>
public static MeterProviderBuilder AddHttpClientInstrumentation(
this MeterProviderBuilder builder)
{
Guard.ThrowIfNull(builder);
this MeterProviderBuilder builder) =>
builder.AddHttpClientInstrumentation(null);

/// <summary>
/// Enables HttpClient instrumentation.
/// </summary>
/// <param name="builder"><see cref="MeterProviderBuilder"/> being configured.</param>
/// <param name="configureOptions">Callback action for configuring <see cref="HttpClientInstrumentationMeterOptions"/>.</param>
/// <returns>The instance of <see cref="MeterProviderBuilder"/> to chain the calls.</returns>
public static MeterProviderBuilder AddHttpClientInstrumentation(
this MeterProviderBuilder builder,
Action<HttpClientInstrumentationMeterOptions> configureOptions)
{
// TODO: Implement an IDeferredMeterProviderBuilder

// TODO: Handle HttpClientInstrumentationOptions
// SetHttpFlavor - seems like this would be handled by views
// Filter - makes sense for metric instrumentation
// Enrich - do we want a similar kind of functionality for metrics?
// RecordException - probably doesn't make sense for metric instrumentation
Guard.ThrowIfNull(builder);

var options = new HttpClientInstrumentationMeterOptions();
configureOptions?.Invoke(options);

var instrumentation = new HttpClientMetrics();
var instrumentation = new HttpClientMetrics(options);
builder.AddMeter(HttpClientMetrics.InstrumentationName);
return builder.AddInstrumentation(() => instrumentation);
builder.AddInstrumentation(() => instrumentation);

return builder;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// <copyright file="MetricExtensions.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

using OpenTelemetry.Metrics;

namespace OpenTelemetry.Instrumentation.Http.Tests.Extensions;

internal static class MetricExtensions
{
public static IEnumerable<MetricPoint> AsEnumerable(this MetricPointsAccessor accessor)
{
foreach (var element in accessor)
{
yield return element;
}
}

public static IEnumerable<KeyValuePair<string, object>> AsEnumerable(this ReadOnlyTagCollection tags)
{
foreach (var tag in tags)
{
yield return tag;
}
}
}
Loading