Skip to content

Commit 12477d4

Browse files
committed
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 66a573e commit 12477d4

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,23 @@ protected virtual void PerformPostRequest(Stream postStream, Stream responseStre
337337
webRequest.Headers.Add(header.Key, header.Value);
338338
}
339339

340+
#if NETSTANDARD2_0_OR_GREATER
341+
// propagate W3C traceparent and tracestate
342+
// HttpClient would do this automatically on .NET 5,
343+
// and .NET 6 would provide even more control over this: https://blog.ladeak.net/posts/opentelemetry-net6-httpclient
344+
// the caller must ensure that the activity is in W3C format (by inheritance or direc setting)
345+
var activity = Activity.Current;
346+
if (activity != null && activity.IdFormat == ActivityIdFormat.W3C)
347+
{
348+
webRequest.Headers.Add("traceparent", activity.Id);
349+
var state = activity.TraceStateString;
350+
if (state?.Length > 0)
351+
{
352+
webRequest.Headers.Add("tracestate", state);
353+
}
354+
}
355+
#endif
356+
340357
using (var str = webRequest.GetRequestStream())
341358
{
342359
postStream.CopyTo(str);

0 commit comments

Comments
 (0)