Skip to content

Commit

Permalink
Add AspNet metrics instrumentation (#2985)
Browse files Browse the repository at this point in the history
  • Loading branch information
vishweshbankwar authored Mar 9, 2022
1 parent 5bb9f26 commit efeaac9
Show file tree
Hide file tree
Showing 6 changed files with 265 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ OpenTelemetry.Instrumentation.AspNet.AspNetInstrumentationOptions.Filter.get ->
OpenTelemetry.Instrumentation.AspNet.AspNetInstrumentationOptions.Filter.set -> void
OpenTelemetry.Instrumentation.AspNet.AspNetInstrumentationOptions.RecordException.get -> bool
OpenTelemetry.Instrumentation.AspNet.AspNetInstrumentationOptions.RecordException.set -> void
OpenTelemetry.Metrics.MeterProviderBuilderExtensions
OpenTelemetry.Trace.TracerProviderBuilderExtensions
static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.AddAspNetInstrumentation(this OpenTelemetry.Metrics.MeterProviderBuilder builder) -> OpenTelemetry.Metrics.MeterProviderBuilder
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddAspNetInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder, System.Action<OpenTelemetry.Instrumentation.AspNet.AspNetInstrumentationOptions> configureAspNetInstrumentationOptions = null) -> OpenTelemetry.Trace.TracerProviderBuilder
53 changes: 53 additions & 0 deletions src/OpenTelemetry.Instrumentation.AspNet/AspNetMetrics.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// <copyright file="AspNetMetrics.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;
using System.Diagnostics.Metrics;
using System.Reflection;
using OpenTelemetry.Instrumentation.AspNet.Implementation;

namespace OpenTelemetry.Instrumentation.AspNet
{
/// <summary>
/// Asp.Net Requests instrumentation.
/// </summary>
internal class AspNetMetrics : IDisposable
{
internal static readonly AssemblyName AssemblyName = typeof(HttpInMetricsListener).Assembly.GetName();
internal static readonly string InstrumentationName = AssemblyName.Name;
internal static readonly string InstrumentationVersion = AssemblyName.Version.ToString();

private readonly Meter meter;

private readonly HttpInMetricsListener httpInMetricsListener;

/// <summary>
/// Initializes a new instance of the <see cref="AspNetMetrics"/> class.
/// </summary>
public AspNetMetrics()
{
this.meter = new Meter(InstrumentationName, InstrumentationVersion);
this.httpInMetricsListener = new HttpInMetricsListener(this.meter);
}

/// <inheritdoc/>
public void Dispose()
{
this.meter?.Dispose();
this.httpInMetricsListener?.Dispose();
}
}
}
2 changes: 2 additions & 0 deletions src/OpenTelemetry.Instrumentation.AspNet/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Changelog

* Added ASP.NET metrics instrumentation to collect `http.server.duration`.
([#2985](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2985))
* Fix: Http server span status is now unset for `400`-`499`.
([#2904](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2904))

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// <copyright file="HttpInMetricsListener.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;
using System.Diagnostics;
using System.Diagnostics.Metrics;
using System.Web;
using OpenTelemetry.Trace;

namespace OpenTelemetry.Instrumentation.AspNet.Implementation
{
internal sealed class HttpInMetricsListener : IDisposable
{
private readonly Histogram<double> httpServerDuration;

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

public void Dispose()
{
TelemetryHttpModule.Options.OnRequestStoppedCallback -= this.OnStopActivity;
}

private void OnStopActivity(Activity activity, HttpContext context)
{
// TODO: This is just a minimal set of attributes. See the spec for additional attributes:
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/semantic_conventions/http-metrics.md#http-server
var tags = new TagList
{
{ SemanticConventions.AttributeHttpMethod, context.Request.HttpMethod },
{ SemanticConventions.AttributeHttpScheme, context.Request.Url.Scheme },
{ SemanticConventions.AttributeHttpStatusCode, context.Response.StatusCode },
};

this.httpServerDuration.Record(activity.Duration.TotalMilliseconds, tags);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// <copyright file="MeterProviderBuilderExtensions.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.Instrumentation.AspNet;
using OpenTelemetry.Internal;

namespace OpenTelemetry.Metrics
{
/// <summary>
/// Extension methods to simplify registering of ASP.NET request instrumentation.
/// </summary>
public static class MeterProviderBuilderExtensions
{
/// <summary>
/// Enables the incoming requests automatic data collection for ASP.NET.
/// </summary>
/// <param name="builder"><see cref="MeterProviderBuilder"/> being configured.</param>
/// <returns>The instance of <see cref="MeterProviderBuilder"/> to chain the calls.</returns>
public static MeterProviderBuilder AddAspNetInstrumentation(
this MeterProviderBuilder builder)
{
Guard.ThrowIfNull(builder);

var instrumentation = new AspNetMetrics();
builder.AddMeter(AspNetMetrics.InstrumentationName);
return builder.AddInstrumentation(() => instrumentation);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// <copyright file="HttpInMetricsListenerTests.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.Collections.Generic;
using System.IO;
using System.Web;
using OpenTelemetry.Context.Propagation;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;
using Xunit;

namespace OpenTelemetry.Instrumentation.AspNet.Tests
{
public class HttpInMetricsListenerTests
{
[Fact]
public void HttpDurationMetricIsEmitted()
{
string url = "http://localhost/api/value";
double duration = 0;
HttpContext.Current = new HttpContext(
new HttpRequest(string.Empty, url, string.Empty),
new HttpResponse(new StringWriter()));

// This is to enable activity creation
// as it is created using activitysource inside TelemetryHttpModule
// TODO: This should not be needed once the dependency on activity is removed from metrics
using var traceprovider = Sdk.CreateTracerProviderBuilder()
.AddAspNetInstrumentation(opts => opts.Enrich
= (activity, eventName, rawObject) =>
{
if (eventName.Equals("OnStopActivity"))
{
duration = activity.Duration.TotalMilliseconds;
}
})
.Build();

var exportedItems = new List<Metric>();
using var meterprovider = Sdk.CreateMeterProviderBuilder()
.AddAspNetInstrumentation()
.AddInMemoryExporter(exportedItems)
.Build();

var activity = ActivityHelper.StartAspNetActivity(Propagators.DefaultTextMapPropagator, HttpContext.Current, TelemetryHttpModule.Options.OnRequestStartedCallback);
ActivityHelper.StopAspNetActivity(Propagators.DefaultTextMapPropagator, activity, HttpContext.Current, TelemetryHttpModule.Options.OnRequestStoppedCallback);

meterprovider.ForceFlush();

var metricPoints = new List<MetricPoint>();
foreach (var p in exportedItems[0].GetMetricPoints())
{
metricPoints.Add(p);
}

Assert.Single(metricPoints);

var metricPoint = metricPoints[0];

var count = metricPoint.GetHistogramCount();
var sum = metricPoint.GetHistogramSum();

Assert.Equal(MetricType.Histogram, exportedItems[0].MetricType);
Assert.Equal("http.server.duration", exportedItems[0].Name);
Assert.Equal(1L, count);
Assert.Equal(duration, sum);

Assert.Equal(3, metricPoints[0].Tags.Count);
string httpMethod = null;
int httpStatusCode = 0;
string httpScheme = null;

foreach (var tag in metricPoints[0].Tags)
{
if (tag.Key == SemanticConventions.AttributeHttpMethod)
{
httpMethod = (string)tag.Value;
continue;
}

if (tag.Key == SemanticConventions.AttributeHttpStatusCode)
{
httpStatusCode = (int)tag.Value;
continue;
}

if (tag.Key == SemanticConventions.AttributeHttpScheme)
{
httpScheme = (string)tag.Value;
continue;
}
}

Assert.Equal("GET", httpMethod);
Assert.Equal(200, httpStatusCode);
Assert.Equal("http", httpScheme);
}
}
}

0 comments on commit efeaac9

Please sign in to comment.