Skip to content

Simple ASP.NET Core instrumentation capturing request count metric#2159

Merged
cijothomas merged 2 commits intoopen-telemetry:metricsfrom
alanwest:alanwest/metrics-aspnetcore
Jul 20, 2021
Merged

Simple ASP.NET Core instrumentation capturing request count metric#2159
cijothomas merged 2 commits intoopen-telemetry:metricsfrom
alanwest:alanwest/metrics-aspnetcore

Conversation

@alanwest
Copy link
Member

Fixes #.

Changes

Please provide a brief description of the changes here.

For significant contributions please make sure you have completed the following items:

  • CHANGELOG.md updated for non-trivial changes
  • Design discussion issue #
  • Changes in public API reviewed

@alanwest alanwest requested a review from a team July 19, 2021 23:12
@codecov
Copy link

codecov bot commented Jul 19, 2021

Codecov Report

Merging #2159 (d0142e9) into metrics (a4d8b35) will decrease coverage by 0.62%.
The diff coverage is 0.00%.

Impacted file tree graph

@@             Coverage Diff             @@
##           metrics    #2159      +/-   ##
===========================================
- Coverage    75.15%   74.53%   -0.63%     
===========================================
  Files          216      219       +3     
  Lines         6910     6969      +59     
===========================================
+ Hits          5193     5194       +1     
- Misses        1717     1775      +58     
Impacted Files Coverage Δ
.../OpenTelemetry.Api/Metrics/MeterProviderBuilder.cs 0.00% <ø> (ø)
...ry.Instrumentation.AspNetCore/AspNetCoreMetrics.cs 0.00% <0.00%> (ø)
...AspNetCore/Implementation/HttpInMetricsListener.cs 0.00% <0.00%> (ø)
...ation.AspNetCore/MeterProviderBuilderExtensions.cs 0.00% <0.00%> (ø)
...c/OpenTelemetry/Metrics/MeterProviderBuilderSdk.cs 0.00% <0.00%> (ø)
src/OpenTelemetry/Metrics/MeterProviderSdk.cs 0.00% <0.00%> (ø)
...emetry.Api/Internal/OpenTelemetryApiEventSource.cs 82.35% <0.00%> (+2.94%) ⬆️

@cijothomas
Copy link
Member

Adding @shirhatti the Program Manager from Asp.Net Core diagnostics for review.
(Sourabh said he'd quit being a PM and become a developer to add metrics to Asp.Net Core :D)

}

// 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).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

@alanwest alanwest Jul 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yea the comment could probably be more clear - this comment was left over from my original PR from awhile back.

I meant suppressing invocations of the /metrics endpoint exposed via the ASP.NET Core middleware - pretty sure this would cause an activity to be generated. At least that's what I recall when running this with the old metrics code and exporting to prometheus, though I haven't tried out prometheus now that we've resurrected all the code.

In a follow up PR I'll expand the test app to export via Prometheus and OTLP and validate where things stand.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we haven't resurrected the middleware version of Prometheus. The in-proc server using HttpListener is resurrected, and it already does suppress. (but since there is no instrumentation for it, it shouldn't get captured anyway).
We can resurrect the asp.net core middleware based one and add the suppress flag there, so activity wont be recorded from that operation.

/// </summary>
internal class AspNetCoreMetrics : IDisposable
{
internal static readonly AssemblyName AssemblyName = typeof(HttpInListener).Assembly.GetName();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not blocking this PR, in general we might want to be defensive while having static initialization logic - TypeInitializationException could be very hard to troubleshoot.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea good point. This should be fixed elsewhere as well - here's another example

internal class HttpInListener : ListenerHandler
{
internal const string ActivityOperationName = "Microsoft.AspNetCore.Hosting.HttpRequestIn";
internal static readonly AssemblyName AssemblyName = typeof(HttpInListener).Assembly.GetName();
internal static readonly string ActivitySourceName = AssemblyName.Name;
internal static readonly Version Version = AssemblyName.Version;
.

I can grab this in a follow up.

Copy link
Member

@reyang reyang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

}

// TODO: Add IServiceCollection.AddOpenTelemetryMetrics extension method
var providerBuilder = Sdk.CreateMeterProviderBuilder()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

until we add Ext.Hosting support, we can simply add the meterprovider as a singleton in the services, to ensure it gets disposed at end.

@cijothomas
Copy link
Member

LGTM as our initial version! Thank you.

@cijothomas cijothomas merged commit d05b8d2 into open-telemetry:metrics Jul 20, 2021
@alanwest alanwest deleted the alanwest/metrics-aspnetcore branch October 22, 2021 16:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants