Skip to content

Commit a8d8c2c

Browse files
committed
CP-44752: SDK(C#): Conditionally propagate W3C traceparent
This is not available on .Net 4.5. Could use .Net Standard 2.0, .Net Framework 4.6.2 and .NET 8.0 as target, which matches the availability of the System.Diagnostics.DiagnosticSource builtin (and Nuget package for older versions): ``` NET462_OR_GREATER || NETSTANDARD2_0_OR_GREATER || NET8_0_OR_GREATER ``` However there are some bugs in the CI where the xenserver-samples XenSdkSample ends up in a zip, but without its dependency (the DiagnosticSource.dll). So for now just enable this on .Net8, where hopefully this package is part of the runtime already (although of course you'll still need to install the runtime, which for some reason you didn't have to do with .Net6, perhaps .Net6 would come preinstalled on Debian12?). Signed-off-by: Edwin Török <[email protected]>
1 parent 847334e commit a8d8c2c

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 (NET8_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 (NET8_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)