-
Notifications
You must be signed in to change notification settings - Fork 778
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
Capture http.server.duration metric for ASP.NET Core instrumentation #1347
Closed
alanwest
wants to merge
22
commits into
open-telemetry:metrics
from
alanwest:alanwest/http-metrics-aspnetcore
Closed
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
fbed107
Capture http.server.duration metric for ASP.NET Core instrumentation
alanwest c029e26
Fix build
alanwest a5cbfa7
Use constants from SemanticConventions
alanwest d1181bb
Provide Meter through factory method
alanwest 9334355
Use StringComparer.Ordinal
alanwest af68d3c
Fix whitespace
alanwest 0cadfc7
Merge branch 'master' into alanwest/http-metrics-aspnetcore
cijothomas 3e7f067
Merge branch 'master' into alanwest/http-metrics-aspnetcore
alanwest 8551d7d
Dispose instrumentations added in MeterProviderSdk
alanwest 31d2f25
Merge branch 'master' into alanwest/http-metrics-aspnetcore
alanwest 9b32a0e
Move http.server.duration metric name to SemanticConventions
alanwest e213ec9
Delay creation of metric
alanwest 1809084
Fix namespace
alanwest f6596b6
Add unit tests
alanwest 45cf5f9
Merge branch 'master' into alanwest/http-metrics-aspnetcore
alanwest 81baa2a
Remove instrumentation options
alanwest 24d5baf
Merge branch 'master' into alanwest/http-metrics-aspnetcore
alanwest 7a5d1ac
Remove TODO
alanwest 3cb3749
Add TODO
alanwest 08f3274
Merge branch 'master' into alanwest/http-metrics-aspnetcore
alanwest ebaa2ae
Merge branch 'metrics' into alanwest/http-metrics-aspnetcore
cijothomas 35dfefc
Merge branch 'metrics' into alanwest/http-metrics-aspnetcore
cijothomas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
46 changes: 46 additions & 0 deletions
46
src/OpenTelemetry.Instrumentation.AspNetCore/AspNetCoreMetrics.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,46 @@ | ||
// <copyright file="AspNetCoreMetrics.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 OpenTelemetry.Instrumentation.AspNetCore.Implementation; | ||
using OpenTelemetry.Metrics; | ||
|
||
namespace OpenTelemetry.Instrumentation.AspNetCore | ||
{ | ||
/// <summary> | ||
/// Asp.Net Core Requests instrumentation. | ||
/// </summary> | ||
internal class AspNetCoreMetrics : IDisposable | ||
{ | ||
private readonly DiagnosticSourceSubscriber diagnosticSourceSubscriber; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="AspNetCoreMetrics"/> class. | ||
/// </summary> | ||
/// <param name="meter">The meter for obtaining metric instruments.</param> | ||
public AspNetCoreMetrics(Meter meter) | ||
{ | ||
this.diagnosticSourceSubscriber = new DiagnosticSourceSubscriber(new HttpInMetricsListener("Microsoft.AspNetCore", meter), null); | ||
this.diagnosticSourceSubscriber.Subscribe(); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public void Dispose() | ||
{ | ||
this.diagnosticSourceSubscriber?.Dispose(); | ||
} | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
src/OpenTelemetry.Instrumentation.AspNetCore/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,99 @@ | ||
// <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.Collections.Generic; | ||
using System.Diagnostics; | ||
using Microsoft.AspNetCore.Http; | ||
using OpenTelemetry.Metrics; | ||
using OpenTelemetry.Trace; | ||
|
||
namespace OpenTelemetry.Instrumentation.AspNetCore.Implementation | ||
{ | ||
internal class HttpInMetricsListener : ListenerHandler | ||
{ | ||
private readonly PropertyFetcher<HttpContext> stopContextFetcher = new PropertyFetcher<HttpContext>("HttpContext"); | ||
private readonly Meter meter; | ||
|
||
private MeasureMetric<double> httpServerDuration; | ||
|
||
public HttpInMetricsListener(string name, Meter meter) | ||
: base(name) | ||
{ | ||
this.meter = meter; | ||
} | ||
|
||
public override void OnStopActivity(Activity activity, object payload) | ||
{ | ||
HttpContext context = this.stopContextFetcher.Fetch(payload); | ||
if (context == null) | ||
{ | ||
AspNetCoreInstrumentationEventSource.Log.NullPayload(nameof(HttpInMetricsListener), nameof(this.OnStopActivity)); | ||
return; | ||
} | ||
|
||
// TODO: Prometheus pulls metrics by invoking the /metrics endpoint. Decide if it makes sense to suppress this. | ||
// Below is just a temporary way of achieving this suppression for metrics (we should consider suppressing traces too). | ||
// If we want to suppress activity from Prometheus then we should use SuppressInstrumentationScope. | ||
if (context.Request.Path.HasValue && context.Request.Path.Value.Contains("metrics")) | ||
{ | ||
return; | ||
} | ||
|
||
var labels = new Dictionary<string, string>(StringComparer.Ordinal); | ||
|
||
labels[SemanticConventions.AttributeHttpMethod] = context.Request.Method; | ||
alanwest marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
labels[SemanticConventions.AttributeHttpScheme] = context.Request.Scheme; | ||
|
||
if (context.Request.Host.HasValue) | ||
{ | ||
labels[SemanticConventions.AttributeHttpHost] = context.Request.Host.Value; | ||
labels[SemanticConventions.AttributeNetHostName] = context.Request.Host.Host; | ||
|
||
if (context.Request.Host.Port.HasValue) | ||
{ | ||
labels[SemanticConventions.AttributeNetHostPort] = context.Request.Host.Port.ToString(); | ||
} | ||
} | ||
|
||
labels[SemanticConventions.AttributeHttpStatusCode] = context.Response.StatusCode.ToString(); | ||
labels[SemanticConventions.AttributeHttpFlavor] = context.Request.Protocol; | ||
|
||
// TODO: Decide if/how to handle http.server_name. Spec seems to suggest | ||
// preference for net.host.name. | ||
// labels[SemanticConventions.AttributeHttpServerName] = string.Empty; | ||
|
||
// TODO: Decide if we want http.url. It seems awful from a cardinality perspective. | ||
// Retrieving the route data and setting http.target probably makes more sense. | ||
// labels[SemanticConventions.AttributeHttpUrl] = string.Empty; | ||
|
||
// TODO: Retrieve the route. | ||
// labels[SemanticConventions.AttributeHttpTarget] = string.Empty; | ||
|
||
// TODO: Ideally we could do this in the constructor. However, the instrumentation is usually instantiated | ||
// prior to invoking MeterProvider.SetDefault. Setting the default meter provider is required before metrics | ||
// can be created. | ||
// This should be safe in the meantime since CreateDoubleMeasure uses a ConcurrentDictionary behind the scenes. | ||
if (this.httpServerDuration == null) | ||
{ | ||
this.httpServerDuration = this.meter.CreateDoubleMeasure(SemanticConventions.MetricHttpServerDuration); | ||
} | ||
|
||
this.httpServerDuration.Record(new SpanContext(activity.Context), activity.Duration.TotalMilliseconds, labels); | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/OpenTelemetry.Instrumentation.AspNetCore/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,45 @@ | ||
// <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 System; | ||
using OpenTelemetry.Instrumentation.AspNetCore; | ||
|
||
namespace OpenTelemetry.Metrics | ||
{ | ||
/// <summary> | ||
/// Extension methods to simplify registering of ASP.NET Core request instrumentation. | ||
/// </summary> | ||
public static class MeterProviderBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Enables the incoming requests automatic data collection for ASP.NET Core. | ||
/// </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 AddAspNetCoreInstrumentation( | ||
this MeterProviderBuilder builder) | ||
{ | ||
if (builder == null) | ||
{ | ||
throw new ArgumentNullException(nameof(builder)); | ||
} | ||
|
||
builder.AddInstrumentation((meter) => new AspNetCoreMetrics(meter)); | ||
|
||
return builder; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
yes, eventually we can leverage the SupressInstrumentation API for this.