-
Notifications
You must be signed in to change notification settings - Fork 762
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
Add AspNet metrics instrumentation #2985
Changes from all commits
dfbe19f
fe9b0ee
63604ba
988b3b9
63294b9
3e22108
b54e83d
7c4e3f0
d911f81
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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(); | ||
} | ||
} | ||
} |
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to fix the issue of this depending on tracing being enabled for metrics to work. But this limitation already exists in aspnet core as well. It is okay to merge this for an initial release. This is better than nothing. |
||
} | ||
} | ||
} |
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); | ||
vishweshbankwar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need ? check?