Skip to content

Commit

Permalink
Updates to implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ZRich97 committed Nov 4, 2023
1 parent 8676d02 commit b74ba53
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace OpenTelemetry.Instrumentation.AspNet;
Expand All @@ -12,17 +8,30 @@ public class AspNetMetricsInstrumentationOptions
/// <summary>
/// Delegate for enrichment of recorded metric with additional tags.
/// </summary>
/// <param name="metricName">The name of the metric being enriched.</param>
/// <param name="context"><see cref="HttpContext"/>: the HttpContext object. Both Request and Response are available.</param>
/// <param name="tags"><see cref="TagList"/>: List of current tags. You can add additional tags to this list. </param>
public delegate void AspNetMetricEnrichmentFunc(string name, HttpContext context, ref TagList tags);
public delegate void AspNetMetricEnrichmentFunc(string metricName, HttpContext context, ref TagList tags);

/// <summary>
/// Gets or sets a Filter function that determines whether or not to collect telemetry about requests on a per request basis.
/// The Filter gets the HttpContext, and should return a boolean.
/// If Filter returns true, the request is collected.
/// If Filter returns false or throw exception, the request is filtered out.
/// Gets or sets a filter function that determines whether or not to
/// collect telemetry on a per request basis.
/// </summary>
public Func<HttpContext, bool> Filter { get; set; }
/// <remarks>
/// Notes:
/// <list type="bullet">
/// <item>The first parameter is the name of the metric being
/// filtered.</item>
/// <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<string, HttpContext, bool> Filter { get; set; }

/// <summary>
/// Gets or sets an function to enrich a recorded metric with additional custom tags.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ namespace OpenTelemetry.Instrumentation.AspNet.Implementation;
internal sealed class HttpInMetricsListener : IDisposable
{
private readonly Histogram<double> httpServerDuration;
private readonly AspNetMetricsInstrumentationOptions options;
private readonly AspNetMetricsInstrumentationOptions options;
private readonly string httpServerDurationMetricName = "http.server.duration";
private readonly string onStopActivityEventName = "OnStopActivity";

public HttpInMetricsListener(Meter meter, AspNetMetricsInstrumentationOptions options)
{
this.options = options;
this.httpServerDuration = meter.CreateHistogram<double>("http.server.duration", "ms", "Measures the duration of inbound HTTP requests.");
this.httpServerDuration = meter.CreateHistogram<double>(this.httpServerDurationMetricName, "ms", "Measures the duration of inbound HTTP requests.");
TelemetryHttpModule.Options.OnRequestStoppedCallback += this.OnStopActivity;
}

Expand All @@ -44,7 +45,7 @@ private void OnStopActivity(Activity activity, HttpContext context)
{
try
{
if (this.options.Filter?.Invoke(context) == false && Activity.Current != null)
if (this.options.Filter?.Invoke(this.httpServerDurationMetricName, context) == false && Activity.Current != null)
{
AspNetInstrumentationEventSource.Log.RequestIsFilteredOut(Activity.Current.OperationName);
return;
Expand All @@ -69,7 +70,7 @@ private void OnStopActivity(Activity activity, HttpContext context)
{
try
{
this.options.Enrich(this.onStopActivityEventName, context, ref tags);
this.options.Enrich(this.httpServerDurationMetricName, context, ref tags);
}
catch (Exception ex)
{
Expand Down

0 comments on commit b74ba53

Please sign in to comment.