Repository navigation
Closed
Labels
api-approvedAPI was approved in API review, it can be implementedarea-System.Diagnostics.ActivityblockingMarks issues that we want to fast track in order to unblock other important workfeature-requestin-prThere is an active PR which will close this issue when it is merged
Milestone
Description
Edited by @tarekgh
Proposal
This proposal aims to enable the recording of exceptions during tracing operations, in accordance with the OpenTelemetry Record Exception specification
namespace System.Diagnostics;
public class Activity
{
public void RecordException(Exception exception, TagList tags = default, DateTimeOffset timestamp = default);
}
public delegate void ExceptionRecording(Activity activity, Exception exception, ref TagList tags);
public sealed class ActivityListener : IDisposable
{
// Will be invoked when Activity.RecordException is called.
public ExceptionRecording? ExceptionRecording { get; set; }
}Notes
RecordExceptionwill save details of the exception into anActivityEvententry, adhering to the guidelines of the OpenTelemetry semantic convention.RecordExceptioncaptures information such as the stack trace, exception type name, and exception message, but not the exception object itself. This approach is chosen because the exception might be rethrown, potentially altering the stack trace. Additionally, exception objects can be large and complex, leading to performance issues.- Anyone interested in accessing the actual exception object during recording can use
ActivityListener.ExceptionRecording, which provides a notification that includes the exception objects along with the tags to be recorded. When listeners are enabled, the behavior ofRecordExceptionwill be as follows:RecordExceptionwill iterate through all non-nullableExceptionRecordinglisteners one by one, passing the exception object and the input tags list provided toRecordException. Listeners can add or modify tags as necessary.- If multiple registered listeners modify the same tags, the changes made by the last listener will prevail and be recorded in the event.
- After calling all listeners,
RecordExceptionadds the populated event to theActivity.Eventslist. If any tags specified in the OpenTelemetry semantic convention are missing, they will be added to the tag list before the event is stored.
RecordExceptionwill always log the event, and there will be no option to suppress its addition.
The recorded event will have the name as exception and will include the following tags:
exception.message, with the value of Exception.Message.exception.stacktracewith the value of Exception.ToStringexception.typewith the value ofexceptionObject.GetType()
Usage Example
using ActivitySource aSource = new ActivitySource("SourceActivityListener");
using ActivityListener listener = new ActivityListener();
listener.ShouldListenTo = (activitySource) => object.ReferenceEquals(aSource, activitySource);
listener.Sample = (ref ActivityCreationOptions<ActivityContext> activityOptions) => ActivitySamplingResult.AllData;
listener.ExceptionRecording = (activity, exception, ref tags) => { /* access the exception and the tags */ };
ActivitySource.AddActivityListener(listener);
using Activity activity = aSource.StartActivity("AllDataRequestedActivity");
activity.RecordException(new Exception("Exception to be recorded"), new TagList { { "exception.escaped", "true" } }, DateTimeOffset.UtcNow );
ActivityEvent exceptionEvent = activity.Events.First();Original Proposal
Activity should have AddExceptionMethod, rather than having to set these tags everytime.
public static Activity AddException(this Activity activity, Exception ex)
{
return activity?.AddTag("event", "error")
.AddTag("error", "true")
.AddTag("error.kind", ex.GetType().Name)
.AddTag("error.message", ex.Message)
.AddTag("error.stack", ex.StackTrace);
}
This should also have constants for these strings, for performance.
Reacted by Kevin Hahn
Metadata
Metadata
Assignees
Labels
api-approvedAPI was approved in API review, it can be implementedarea-System.Diagnostics.ActivityblockingMarks issues that we want to fast track in order to unblock other important workfeature-requestin-prThere is an active PR which will close this issue when it is merged
Activity
tarekgh commentedon Jun 2, 2021
Why can't use
Activity.SetCustomProperty?Something like:
In general, for error reporting we followed up OpenTelemetry specs and provided Activity.SetStatus. So, you can also do:
I don't think adding a method for exception will be desirable for most scenarios as wouldn't be portable.
CC @noahfalk
noahfalk commentedon Jun 2, 2021
There is a semantic convention for exceptions but it is currently marked experimental. I do not recommend we codify it into the BCL until it is more stable, but once it was stable AddException() seems like a useful API.
DR9885 commentedon Jun 9, 2021
Theres also a few tags listed on microsoft docs:
https://docs.microsoft.com/en-us/dotnet/core/diagnostics/distributed-tracing-instrumentation-walkthroughs#optional-add-status
... However ... I Just realized this is done by SetStatus, so maybe redundant to an earlier comment
Based on all comments though, It might be better if SetStatus did all of this:
CodeBlanch commentedon Nov 12, 2021
@DR9885 Just to clarify this a bit, the OTel spec doesn't say to add tags for an exception, it says to add an event (with tags). Here is the extension method we have in the OTel.NET library for this: https://github.com/open-telemetry/opentelemetry-dotnet/blob/4c22707a98abf356fa4224ebbc872f3bad0d72c6/src/OpenTelemetry.Api/Trace/ActivityExtensions.cs#L95
But yes it would be great to have this API on activity so that users don't need the OpenTelemetry.Api dependency.
I was thinking the .NET API would be something simple like...
...or...
And then the OpenTelemetry library (or anyone interested) could worry about what to do with it.
/cc @cijothomas
cijothomas commentedon Nov 12, 2021
Related issue : open-telemetry/opentelemetry-dotnet-contrib#1794
(There are privacy concerns about storing stack as activity event)
49 remaining items
terrajobst commentedon May 28, 2024
Video
ersuffix for the delegateAddas the prefix as that's consistent with other methods onActivitytarekgh commentedon May 30, 2024
@terrajobst @bartonjs, as we have decided to use the method name
AddExceptioninstead ofRecordException, and considering the pattern of other methods in theActivityclass, theAddmethods always return theActivityinstead of void. I will follow this pattern and makeAddExceptionreturnActivityinstead of void. Please let me know if you have any concerns about this. Thanks!runtime/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Activity.cs
Line 459 in 992e027
runtime/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Activity.cs
Line 510 in 992e027
runtime/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Activity.cs
Line 529 in 992e027
runtime/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Activity.cs
Line 548 in 992e027
terrajobst commentedon May 31, 2024
Returning
Activitymakes sense, so:tarekgh commentedon Jun 3, 2024
@terrajobst @bartonjs sorry to ping again, there was a suggestion to mark the tags parameter with
inkeyword to avoid copying the TagList struct. what do you think?@CodeBlanch
bartonjs commentedon Jun 3, 2024
My initial thought is "using
ingoes against our guidelines". But that sounds familiar for this, and looking at the ref.cs it does appear that this struct is only ever passed asreforin; soinwould be consistent here, I guess.tarekgh commentedon Jun 3, 2024
Thanks @bartonjs!
I'll go ahead and apply that if @terrajobst has no objection.
tarekgh commentedon Jun 3, 2024
I created the PR #103009 to update the tags parameter modifier.
KalleOlaviNiemitalo commentedon Jun 4, 2024
It seems ActivitySource can call the ExceptionRecorder delegate multiple times with the same arguments, if another thread adds or removes listeners in parallel with Activity.AddException. This is more surprising for ExceptionRecorder, which is designed to have side effects on the TagList, than for ShouldListenTo. It will probably be OK but it should preferably be documented.