Skip to content

Commit 4523eb3

Browse files
committed
CP-44752: SDK(C#): Conditionally propagate W3C traceparent
This is not available on .Net 4.5 Signed-off-by: Edwin Török <[email protected]>
1 parent aeafd6b commit 4523eb3

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

ocaml/sdk-gen/csharp/autogen/src/JsonRpc.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929

3030
using System;
3131
using System.Collections.Generic;
32+
#if (NET462_OR_GREATER || NETSTANDARD2_0_OR_GREATER)
33+
using System.Diagnostics;
34+
#endif
3235
using System.IO;
3336
using System.Net;
3437
using System.Net.Security;
@@ -293,6 +296,23 @@ protected virtual void PerformPostRequest(Stream postStream, Stream responseStre
293296
webRequest.Headers.Add(header.Key, header.Value);
294297
}
295298

299+
#if (NET462_OR_GREATER || NETSTANDARD2_0_OR_GREATER)
300+
// propagate W3C traceparent and tracestate
301+
// HttpClient would do this automatically on .NET 5,
302+
// and .NET 6 would provide even more control over this: https://blog.ladeak.net/posts/opentelemetry-net6-httpclient
303+
// the caller must ensure that the activity is in W3C format (by inheritance or direct setting)
304+
var activity = Activity.Current;
305+
if (activity != null && activity.IdFormat == ActivityIdFormat.W3C)
306+
{
307+
webRequest.Headers.Add("traceparent", activity.Id);
308+
var state = activity.TraceStateString;
309+
if (state?.Length > 0)
310+
{
311+
webRequest.Headers.Add("tracestate", state);
312+
}
313+
}
314+
#endif
315+
296316
using (var str = webRequest.GetRequestStream())
297317
{
298318
postStream.CopyTo(str);

0 commit comments

Comments
 (0)