Skip to content

Commit

Permalink
SDK(C#): Conditionally propagate W3C traceparent
Browse files Browse the repository at this point in the history
This is not available on .Net 4.5

Signed-off-by: Edwin Török <[email protected]>
  • Loading branch information
edwintorok committed Aug 12, 2024
1 parent 8ef759b commit 3f7d8e6
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions ocaml/sdk-gen/csharp/autogen/src/JsonRpc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,23 @@ protected virtual void PerformPostRequest(Stream postStream, Stream responseStre
webRequest.Headers.Add(header.Key, header.Value);
}

#if (NET462_OR_GREATER || NETSTANDARD2_0_OR_GREATER)
// propagate W3C traceparent and tracestate
// HttpClient would do this automatically on .NET 5,
// and .NET 6 would provide even more control over this: https://blog.ladeak.net/posts/opentelemetry-net6-httpclient
// the caller must ensure that the activity is in W3C format (by inheritance or direc setting)
var activity = Activity.Current;
if (activity != null && activity.IdFormat == ActivityIdFormat.W3C)
{
webRequest.Headers.Add("traceparent", activity.Id);
var state = activity.TraceStateString;
if (state?.Length > 0)
{
webRequest.Headers.Add("tracestate", state);
}
}
#endif

using (var str = webRequest.GetRequestStream())
{
postStream.CopyTo(str);
Expand Down

0 comments on commit 3f7d8e6

Please sign in to comment.