Skip to content

Commit 66a573e

Browse files
committed
SDK(C#): Conditional activity source for JsonRPC
Doesn't exist on .Net45. Signed-off-by: Edwin Török <[email protected]>
1 parent c222987 commit 66a573e

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

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

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929

3030
using System;
3131
using System.Collections.Generic;
32+
#if NETSTANDARD2_0_OR_GREATER
33+
using System.Diagnostics;
34+
#endif
3235
using System.IO;
3336
using System.Net;
3437
using System.Net.Security;
@@ -155,6 +158,11 @@ public partial class JsonRpcClient
155158
{
156159
private int _globalId;
157160

161+
#if NETSTANDARD2_0_OR_GREATER
162+
// TODO: get proper version string from build
163+
private static ActivitySource source = new ActivitySource("XenAPI.JsonRpcClient", "1.0");
164+
#endif
165+
158166
public JsonRpcClient(string baseUrl)
159167
{
160168
Url = baseUrl;
@@ -207,6 +215,14 @@ protected virtual T Rpc<T>(string callName, JToken parameters, JsonSerializer se
207215
// therefore the latter will be done only in DEBUG mode
208216
using (var postStream = new MemoryStream())
209217
{
218+
#if NETSTANDARD2_0_OR_GREATER
219+
using (Activity activity = source.CreateActivity("XenAPI" + callName, ActivityKind.Client))
220+
{
221+
// .NET 5 would use W3C format for the header by default but we build for .Net 4.x still
222+
activity?.SetIdFormat(ActivityIdFormat.W3C);
223+
activity?.Start();
224+
activity?.SetTag("rpc.jsonrpc.request_id", id);
225+
#endif
210226
using (var sw = new StreamWriter(postStream))
211227
{
212228
#if DEBUG
@@ -225,6 +241,10 @@ protected virtual T Rpc<T>(string callName, JToken parameters, JsonSerializer se
225241

226242
using (var responseStream = new MemoryStream())
227243
{
244+
#if NETSTANDARD2_0_OR_GREATER
245+
// https://opentelemetry.io/docs/specs/semconv/rpc/json-rpc/
246+
activity?.SetTag("rpc.method", callName);
247+
#endif
228248
PerformPostRequest(postStream, responseStream);
229249
responseStream.Position = 0;
230250

@@ -239,12 +259,25 @@ protected virtual T Rpc<T>(string callName, JToken parameters, JsonSerializer se
239259
#else
240260
var res2 = (JsonResponseV2<T>)serializer.Deserialize(responseReader, typeof(JsonResponseV2<T>));
241261
#endif
262+
263+
#if NETSTANDARD2_0_OR_GREATER
264+
activity?.SetTag("rpc.jsonrpc.version", "2.0");
265+
#endif
242266
if (res2.Error != null)
243267
{
244268
var descr = new List<string> { res2.Error.Message };
245269
descr.AddRange(res2.Error.Data.ToObject<string[]>());
270+
#if NETSTANDARD2_0_OR_GREATER
271+
activity?.SetTag("otel.status_code", "ERROR");
272+
activity?.SetTag("rpc.jsonrpc.error_code", res2.Error.Code);
273+
activity?.SetTag("rpc.jsonrpc.error_message", descr);
274+
#endif
246275
throw new Failure(descr);
247276
}
277+
278+
#if NETSTANDARD2_0_OR_GREATER
279+
activity?.SetTag("otel.status_code", "OK");
280+
#endif
248281
return res2.Result;
249282
default:
250283
#if DEBUG
@@ -256,14 +289,25 @@ protected virtual T Rpc<T>(string callName, JToken parameters, JsonSerializer se
256289
if (res1.Error != null)
257290
{
258291
var errorArray = res1.Error.ToObject<string[]>();
259-
if (errorArray != null)
292+
if (errorArray != null) {
293+
#if NETSTANDARD2_0_OR_GREATER
294+
activity?.SetTag("otel.status_code", "ERROR");
295+
//activity?.SetTag("rpc.jsonrpc.error_message", errorArray);
296+
#endif
260297
throw new Failure(errorArray);
298+
}
261299
}
300+
#if NETSTANDARD2_0_OR_GREATER
301+
activity?.SetTag("otel.status_code", "OK");
302+
#endif
262303
return res1.Result;
263304
}
264305
}
265306
}
266307
}
308+
#if NETSTANDARD2_0_OR_GREATER
309+
}
310+
#endif
267311
}
268312
}
269313

0 commit comments

Comments
 (0)