11// Copyright (c) Microsoft Corporation.
22// Licensed under the MIT License.
33
4- // NOTE: Modified from https://github.com/Azure/durabletask/blob/main/src/DurableTask.Core/Tracing/DiagnosticActivityExtensions.cs
5-
64using System . Diagnostics ;
75using System . Linq . Expressions ;
86using System . Reflection ;
97
8+ // NOTE: Modified from https://github.com/Azure/durabletask/blob/main/src/DurableTask.Core/Tracing/DiagnosticActivityExtensions.cs
109namespace Microsoft . DurableTask . Tracing ;
1110
1211/// <summary>
13- /// Replica from System.Diagnostics.DiagnosticSource >= 6.0.0
12+ /// Replica from System.Diagnostics.DiagnosticSource >= 6.0.0.
1413/// </summary>
1514enum ActivityStatusCode
1615{
16+ /// <summary>
17+ /// The default value indicating the status code is not initialized.
18+ /// </summary>
1719 Unset = 0 ,
18- OK = 1 ,
20+
21+ /// <summary>
22+ /// Indicates the operation has been validated and completed successfully.
23+ /// </summary>
24+ Ok = 1 ,
25+
26+ /// <summary>
27+ /// Indicates an error was encountered during the operation.
28+ /// </summary>
1929 Error = 2 ,
2030}
2131
@@ -24,39 +34,56 @@ enum ActivityStatusCode
2434/// </summary>
2535static class DiagnosticActivityExtensions
2636{
27- static readonly Action < Activity , string > s_setSpanId ;
28- static readonly Action < Activity , ActivityStatusCode , string > s_setStatus ;
37+ static readonly Action < Activity , string > SetSpanIdMethod ;
38+ static readonly Action < Activity , ActivityStatusCode , string > SetStatusMethod ;
2939
3040 static DiagnosticActivityExtensions ( )
3141 {
3242 BindingFlags flags = BindingFlags . NonPublic | BindingFlags . Instance ;
33- s_setSpanId = ( typeof ( Activity ) . GetField ( "_spanId" , flags ) ?? throw new InvalidOperationException ( "The field Activity._spanId was not found." ) ) . CreateSetter < Activity , string > ( ) ;
34- s_setStatus = CreateSetStatus ( ) ;
43+ SetSpanIdMethod = ( typeof ( Activity ) . GetField ( "_spanId" , flags ) ?? throw new InvalidOperationException ( "The field Activity._spanId was not found." ) ) . CreateSetter < Activity , string > ( ) ;
44+ SetStatusMethod = CreateSetStatus ( ) ;
3545 }
3646
47+ /// <summary>
48+ /// Explicitly sets the span ID for the given activity.
49+ /// </summary>
50+ /// <param name="activity">The activity on which to set the span ID.</param>
51+ /// <param name="spanId">The span ID to set.</param>
3752 public static void SetSpanId ( this Activity activity , string spanId )
38- => s_setSpanId ( activity , spanId ) ;
53+ => SetSpanIdMethod ( activity , spanId ) ;
3954
55+ /// <summary>
56+ /// Explicitly sets the status code and description for the given activity.
57+ /// </summary>
58+ /// <param name="activity">The activity on which to set the span ID.</param>
59+ /// <param name="status">The status to set.</param>
60+ /// <param name="description">The description to set.</param>
4061 public static void SetStatus ( this Activity activity , ActivityStatusCode status , string description )
41- => s_setStatus ( activity , status , description ) ;
62+ => SetStatusMethod ( activity , status , description ) ;
4263
4364 static Action < Activity , ActivityStatusCode , string > CreateSetStatus ( )
4465 {
45- MethodInfo method = typeof ( Activity ) . GetMethod ( "SetStatus" ) ;
66+ MethodInfo ? method = typeof ( Activity ) . GetMethod ( "SetStatus" ) ;
67+
4668 if ( method is null )
4769 {
48- return ( activity , status , description ) => {
70+ return ( activity , status , description ) =>
71+ {
72+ #pragma warning disable CA1510
4973 if ( activity is null )
5074 {
5175 throw new ArgumentNullException ( nameof ( activity ) ) ;
5276 }
53- string str = status switch
77+ #pragma warning restore CA1510
78+
79+ string ? str = status switch
5480 {
5581 ActivityStatusCode . Unset => "UNSET" ,
56- ActivityStatusCode . OK => "OK" ,
82+ ActivityStatusCode . Ok => "OK" ,
5783 ActivityStatusCode . Error => "ERROR" ,
5884 _ => null ,
5985 } ;
86+
6087 activity . SetTag ( "otel.status_code" , str ) ;
6188 activity . SetTag ( "otel.status_description" , description ) ;
6289 } ;
0 commit comments