Skip to content

Commit

Permalink
CP-44752: 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 <edwin.torok@cloud.com>
  • Loading branch information
edwintorok committed Jan 23, 2025
1 parent 9c404ef commit 921dae5
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions ocaml/sdk-gen/csharp/autogen/src/JsonRpc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@

using System;
using System.Collections.Generic;
#if (NET462_OR_GREATER || NETSTANDARD2_0_OR_GREATER)
using System.Diagnostics;
#endif
using System.IO;
using System.Net;
using System.Net.Security;
Expand Down Expand Up @@ -293,6 +296,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 direct 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 921dae5

Please sign in to comment.