opentelemetry-dotnet icon indicating copy to clipboard operation
opentelemetry-dotnet copied to clipboard

Fix ASP.NET Core ExceptionFilter prevents recording exception

Open al-mac opened this issue 2 years ago • 8 comments

Fixes #2853

ASP.NET Core exception filters fire events before and after filter execution.

Before: https://github.com/dotnet/aspnetcore/blob/36df8e5dc487e27cb85674ef3f144dca07f05c20/src/Mvc/Mvc.Core/src/MvcCoreDiagnosticListenerExtensions.cs#L396-L402

After: https://github.com/dotnet/aspnetcore/blob/c85baf8db0c72ae8e68643029d514b2e737c9fae/src/Mvc/Mvc.Core/src/MvcCoreDiagnosticListenerExtensions.cs#L422-L434

The ASP.NET Core instrumentation's OnException event is invoked when these events fire. The payload of the event differs from that of the regular exception path and precludes the exception from being recorded (if enabled) and the span status from being set.

Changes

Please provide a brief description of the changes here.

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

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

al-mac avatar Jul 22 '22 20:07 al-mac

A few small CI failures to resolve

Static analysis enforces style guidelines. You should be able to produce this locally, but might need to do a release build

D:\a\opentelemetry-dotnet\opentelemetry-dotnet\test\OpenTelemetry.Instrumentation.AspNetCore.Tests\BasicTests.cs(665,29): error SA1204: Static members should appear before non-static members [D:\a\opentelemetry-dotnet\opentelemetry-dotnet\test\OpenTelemetry.Instrumentation.AspNetCore.Tests\OpenTelemetry.Instrumentation.AspNetCore.Tests.csproj]

We use markdownlint to enforce style in markdown files. You could run this locally too following instructions here, but in your case it looks you just need to add a newline after a header.

src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md:3 MD022/blanks-around-headings/blanks-around-headers Headings should be surrounded by blank lines [Expected: 1; Actual: 0; Below] [Context: "## Unreleased"]

alanwest avatar Jul 25 '22 23:07 alanwest

Fixed! Sorry about that.

al-mac avatar Jul 25 '22 23:07 al-mac

Codecov Report

Merging #3475 (dfd3202) into main (968dc52) will increase coverage by 0.03%. The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #3475      +/-   ##
==========================================
+ Coverage   87.26%   87.29%   +0.03%     
==========================================
  Files         277      277              
  Lines        9938     9944       +6     
==========================================
+ Hits         8672     8681       +9     
+ Misses       1266     1263       -3     
Impacted Files Coverage Δ
...DiagnosticSourceInstrumentation/PropertyFetcher.cs 92.50% <100.00%> (-4.56%) :arrow_down:
...tation/OpenTelemetryProtocolExporterEventSource.cs 75.00% <0.00%> (-20.00%) :arrow_down:
...porter.OpenTelemetryProtocol/OtlpMetricExporter.cs 72.72% <0.00%> (-13.64%) :arrow_down:
src/OpenTelemetry/BatchExportProcessor.cs 82.24% <0.00%> (-1.87%) :arrow_down:
...Telemetry/Internal/SelfDiagnosticsEventListener.cs 96.87% <0.00%> (-0.79%) :arrow_down:
...metryProtocol/Implementation/ActivityExtensions.cs 96.15% <0.00%> (+1.09%) :arrow_up:
...tation.AspNetCore/Implementation/HttpInListener.cs 91.41% <0.00%> (+1.22%) :arrow_up:
src/OpenTelemetry/Logs/OpenTelemetryLogger.cs 88.88% <0.00%> (+2.22%) :arrow_up:
...tpListener/Internal/PrometheusCollectionManager.cs 80.48% <0.00%> (+2.43%) :arrow_up:
... and 3 more

codecov[bot] avatar Jul 26 '22 00:07 codecov[bot]

Thanks for this PR @al-mac !

@alanwest Please do add this to SIG discussion. I was chatting offline with @vishweshbankwar for some potential improvements in this area as well. (Its related to how leveraging Activity.Current in non Start/End path could result in fetching unintended Activity.) Given we have reference to AspNetCore.App (for new runtimes..), we should discuss if we can use the Public types which are the payload, instead of relying on reflection.

cijothomas avatar Jul 26 '22 03:07 cijothomas

@alanwest thanks! Unfortunately I won't be able to attend the meeting. Nice catch on the potential change on the DiagnosticSourceListener.cs. The specific scenario that we worked on this PR will probably never happen if this changes to something like else if (string.Compare(value.Key, "OnException", StringComparison.OrdinalIgnoreCase) == 0). The only concern is if it could break some other use-case, but at this point is valid to consider adding some more else ifs to the code in order to cover those.

al-mac avatar Jul 26 '22 09:07 al-mac

Related issue: #2853

vishweshbankwar avatar Jul 26 '22 16:07 vishweshbankwar

Related issue: https://github.com/open-telemetry/opentelemetry-dotnet/issues/2853

👍 ah, good find! I've updated the description since this PR should resolve the issue.

alanwest avatar Jul 27 '22 17:07 alanwest

This PR was marked stale due to lack of activity and will be closed in 7 days. Commenting or Pushing will instruct the bot to automatically remove the label. This bot runs once per day.

github-actions[bot] avatar Aug 04 '22 03:08 github-actions[bot]

This PR was marked stale due to lack of activity and will be closed in 7 days. Commenting or Pushing will instruct the bot to automatically remove the label. This bot runs once per day.

github-actions[bot] avatar Aug 13 '22 03:08 github-actions[bot]

Closed as inactive. Feel free to reopen if this PR is still being worked on.

github-actions[bot] avatar Aug 20 '22 03:08 github-actions[bot]

@al-mac apologies for the delay on this PR! We just discussed it today and would like to get it merged. Would you mind resolving the conflicts?

alanwest avatar Aug 23 '22 19:08 alanwest

@alanwest not at all! i'll work on it later today

al-mac avatar Aug 23 '22 20:08 al-mac