@@ -107,7 +107,7 @@ public void AddAttribute<T>(string name, T value, Func<T, string> format)
107107 /// <param name="traceparent">The traceparent for the link.</param>
108108 /// <param name="tracestate">The tracestate for the link.</param>
109109 /// <param name="attributes">Optional attributes to associate with the link.</param>
110- public void AddLink ( string traceparent , string tracestate , IDictionary < string , string > ? attributes = null )
110+ public void AddLink ( string traceparent , string ? tracestate , IDictionary < string , string > ? attributes = null )
111111 {
112112 _activityAdapter ? . AddLink ( traceparent , tracestate , attributes ) ;
113113 }
@@ -124,12 +124,13 @@ public void SetStartTime(DateTime dateTime)
124124 }
125125
126126 /// <summary>
127- /// Sets the trace parent for the current scope.
127+ /// Sets the trace context for the current scope.
128128 /// </summary>
129129 /// <param name="traceparent">The trace parent to set for the current scope.</param>
130- public void SetTraceparent ( string traceparent )
130+ /// <param name="tracestate">The trace state to set for the current scope.</param>
131+ public void SetTraceContext ( string traceparent , string ? tracestate = default )
131132 {
132- _activityAdapter ? . SetTraceparent ( traceparent ) ;
133+ _activityAdapter ? . SetTraceContext ( traceparent , tracestate ) ;
133134 }
134135
135136 public void Dispose ( )
@@ -199,6 +200,7 @@ private class ActivityAdapter : IDisposable
199200 private DateTimeOffset _startTime ;
200201 private List < Activity > ? _links ;
201202 private string ? _traceparent ;
203+ private string ? _tracestate ;
202204
203205 public ActivityAdapter ( object ? activitySource , DiagnosticSource diagnosticSource , string activityName , ActivityKind kind , object ? diagnosticSourceArgs )
204206 {
@@ -258,7 +260,7 @@ public void AddTag(string name, object value)
258260 return linkCollection ;
259261 }
260262
261- public void AddLink ( string traceparent , string tracestate , IDictionary < string , string > ? attributes )
263+ public void AddLink ( string traceparent , string ? tracestate , IDictionary < string , string > ? attributes )
262264 {
263265 var linkedActivity = new Activity ( "LinkedActivity" ) ;
264266 linkedActivity . SetW3CFormat ( ) ;
@@ -334,6 +336,11 @@ public void AddLink(string traceparent, string tracestate, IDictionary<string, s
334336 _currentActivity . SetParentId ( _traceparent ) ;
335337 }
336338
339+ if ( _tracestate != null )
340+ {
341+ _currentActivity . SetTraceState ( _tracestate ) ;
342+ }
343+
337344 _currentActivity . Start ( ) ;
338345 }
339346
@@ -351,7 +358,8 @@ public void AddLink(string traceparent, string tracestate, IDictionary<string, s
351358 startTime : _startTime ,
352359 tags : _tagCollection ,
353360 links : GetActivitySourceLinkCollection ( ) ,
354- parentId : _traceparent ) ;
361+ traceparent : _traceparent ,
362+ tracestate : _tracestate ) ;
355363 }
356364
357365 public void SetStartTime ( DateTime startTime )
@@ -365,13 +373,14 @@ public void MarkFailed(Exception exception)
365373 _diagnosticSource ? . Write ( _activityName + ".Exception" , exception ) ;
366374 }
367375
368- public void SetTraceparent ( string traceparent )
376+ public void SetTraceContext ( string traceparent , string ? tracestate )
369377 {
370378 if ( _currentActivity != null )
371379 {
372380 throw new InvalidOperationException ( "Traceparent can not be set after the activity is started." ) ;
373381 }
374382 _traceparent = traceparent ;
383+ _tracestate = tracestate ;
375384 }
376385
377386 public void Dispose ( )
@@ -418,13 +427,14 @@ static ActivityExtensions()
418427 private static Action < Activity , string ? > ? SetTraceStateStringMethod ;
419428 private static Func < Activity , int > ? GetIdFormatMethod ;
420429 private static Action < Activity , string , object ? > ? ActivityAddTagMethod ;
421- private static Func < object , string , int , string ? , ICollection < KeyValuePair < string , object > > ? , IList ? , DateTimeOffset , Activity ? > ? ActivitySourceStartActivityMethod ;
430+ private static Func < object , string , int , object ? , ICollection < KeyValuePair < string , object > > ? , IList ? , DateTimeOffset , Activity ? > ? ActivitySourceStartActivityMethod ;
422431 private static Func < object , bool > ? ActivitySourceHasListenersMethod ;
423432 private static Func < string , string ? , ICollection < KeyValuePair < string , object > > ? , object ? > ? CreateActivityLinkMethod ;
424433 private static Func < ICollection < KeyValuePair < string , object > > ? > ? CreateTagsCollectionMethod ;
425434 private static Func < Activity , string , object ? > ? GetCustomPropertyMethod ;
426435 private static Action < Activity , string , object > ? SetCustomPropertyMethod ;
427436 private static readonly ParameterExpression ActivityParameter = Expression . Parameter ( typeof ( Activity ) ) ;
437+ private static MethodInfo ? ParseActivityContextMethod ;
428438
429439 public static object ? GetCustomProperty ( this Activity activity , string propertyName )
430440 {
@@ -687,13 +697,23 @@ public static bool ActivitySourceHasListeners(object? activitySource)
687697 return ActivitySourceHasListenersMethod . Invoke ( activitySource ) ;
688698 }
689699
690- public static Activity ? ActivitySourceStartActivity ( object ? activitySource , string activityName , int kind , DateTimeOffset startTime , ICollection < KeyValuePair < string , object > > ? tags , IList ? links , string ? parentId )
700+ public static Activity ? ActivitySourceStartActivity (
701+ object ? activitySource ,
702+ string activityName ,
703+ int kind ,
704+ DateTimeOffset startTime ,
705+ ICollection < KeyValuePair < string , object > > ? tags ,
706+ IList ? links ,
707+ string ? traceparent ,
708+ string ? tracestate )
691709 {
692710 if ( activitySource == null )
693711 {
694712 return null ;
695713 }
696714
715+ object ? activityContext = default ;
716+
697717 if ( ActivitySourceStartActivityMethod == null )
698718 {
699719 if ( ActivityLinkType == null ||
@@ -709,7 +729,7 @@ public static bool ActivitySourceHasListeners(object? activitySource)
709729 {
710730 typeof ( string ) ,
711731 ActivityKindType ,
712- typeof ( string ) ,
732+ ActivityContextType ,
713733 typeof ( IEnumerable < KeyValuePair < string , object > > ) ,
714734 typeof ( IEnumerable < > ) . MakeGenericType ( ActivityLinkType ) ,
715735 typeof ( DateTimeOffset )
@@ -724,27 +744,38 @@ public static bool ActivitySourceHasListeners(object? activitySource)
724744 var sourceParameter = Expression . Parameter ( typeof ( object ) ) ;
725745 var nameParameter = Expression . Parameter ( typeof ( string ) ) ;
726746 var kindParameter = Expression . Parameter ( typeof ( int ) ) ;
727- var parentIdParameter = Expression . Parameter ( typeof ( string ) ) ;
747+ var contextParameter = Expression . Parameter ( typeof ( object ) ) ;
728748 var startTimeParameter = Expression . Parameter ( typeof ( DateTimeOffset ) ) ;
729749 var tagsParameter = Expression . Parameter ( typeof ( ICollection < KeyValuePair < string , object > > ) ) ;
730750 var linksParameter = Expression . Parameter ( typeof ( IList ) ) ;
731751 var methodParameter = method . GetParameters ( ) ;
732- ActivitySourceStartActivityMethod = Expression . Lambda < Func < object , string , int , string ? , ICollection < KeyValuePair < string , object > > ? , IList ? , DateTimeOffset , Activity ? > > (
752+ ParseActivityContextMethod = ActivityContextType . GetMethod ( "Parse" , BindingFlags . Static | BindingFlags . Public ) ;
753+
754+ ActivitySourceStartActivityMethod = Expression . Lambda < Func < object , string , int , object ? , ICollection < KeyValuePair < string , object > > ? , IList ? , DateTimeOffset , Activity ? > > (
733755 Expression . Call (
734756 Expression . Convert ( sourceParameter , method . DeclaringType ! ) ,
735757 method ,
736758 nameParameter ,
737759 Expression . Convert ( kindParameter , methodParameter [ 1 ] . ParameterType ) ,
738- Expression . Convert ( parentIdParameter , methodParameter [ 2 ] . ParameterType ) ,
760+ Expression . Convert ( contextParameter , methodParameter [ 2 ] . ParameterType ) ,
739761 Expression . Convert ( tagsParameter , methodParameter [ 3 ] . ParameterType ) ,
740762 Expression . Convert ( linksParameter , methodParameter [ 4 ] . ParameterType ) ,
741763 Expression . Convert ( startTimeParameter , methodParameter [ 5 ] . ParameterType ) ) ,
742- sourceParameter , nameParameter , kindParameter , parentIdParameter , tagsParameter , linksParameter , startTimeParameter ) . Compile ( ) ;
764+ sourceParameter , nameParameter , kindParameter , contextParameter , tagsParameter , linksParameter , startTimeParameter ) . Compile ( ) ;
743765 }
744766 }
745767 }
746768
747- return ActivitySourceStartActivityMethod . Invoke ( activitySource , activityName , kind , parentId , tags , links , startTime ) ;
769+ if ( ActivityContextType != null && ParseActivityContextMethod != null )
770+ {
771+ if ( traceparent != null )
772+ activityContext = ParseActivityContextMethod . Invoke ( null , new [ ] { traceparent , tracestate } ) ! ;
773+ else
774+ // because ActivityContext is a struct, we need to create a default instance rather than allowing the argument to be null
775+ activityContext = Activator . CreateInstance ( ActivityContextType ) ;
776+ }
777+
778+ return ActivitySourceStartActivityMethod . Invoke ( activitySource , activityName , kind , activityContext , tags , links , startTime ) ;
748779 }
749780
750781 public static object ? CreateActivitySource ( string name )
0 commit comments