-
Notifications
You must be signed in to change notification settings - Fork 762
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add AspNet metrics instrumentation (#2985)
- Loading branch information
1 parent
5bb9f26
commit efeaac9
Showing
6 changed files
with
265 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/OpenTelemetry.Instrumentation.AspNet/Implementation/HttpInMetricsListener.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/OpenTelemetry.Instrumentation.AspNet/MeterProviderBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
112 changes: 112 additions & 0 deletions
112
test/OpenTelemetry.Instrumentation.AspNet.Tests/HttpInMetricsListenerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |