Skip to content

Commit

Permalink
SDK(C#): Conditional activity source for JsonRPC
Browse files Browse the repository at this point in the history
Doesn't exist on .Net45.

Signed-off-by: Edwin Török <[email protected]>
  • Loading branch information
edwintorok committed Aug 12, 2024
1 parent c222987 commit 8ef759b
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion 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 @@ -155,6 +158,11 @@ public partial class JsonRpcClient
{
private int _globalId;

#if (NET462_OR_GREATER || NETSTANDARD2_0_OR_GREATER)
// TODO: get proper version string from build
private static ActivitySource source = new ActivitySource("XenAPI.JsonRpcClient", "1.0");
#endif

public JsonRpcClient(string baseUrl)
{
Url = baseUrl;
Expand Down Expand Up @@ -207,6 +215,14 @@ protected virtual T Rpc<T>(string callName, JToken parameters, JsonSerializer se
// therefore the latter will be done only in DEBUG mode
using (var postStream = new MemoryStream())
{
#if (NET462_OR_GREATER || NETSTANDARD2_0_OR_GREATER)
using (Activity activity = source.CreateActivity("XenAPI" + callName, ActivityKind.Client))
{
// .NET 5 would use W3C format for the header by default but we build for .Net 4.x still
activity?.SetIdFormat(ActivityIdFormat.W3C);
activity?.Start();
activity?.SetTag("rpc.jsonrpc.request_id", id);
#endif
using (var sw = new StreamWriter(postStream))
{
#if DEBUG
Expand All @@ -225,6 +241,10 @@ protected virtual T Rpc<T>(string callName, JToken parameters, JsonSerializer se

using (var responseStream = new MemoryStream())
{
#if (NET462_OR_GREATER || NETSTANDARD2_0_OR_GREATER)
// https://opentelemetry.io/docs/specs/semconv/rpc/json-rpc/
activity?.SetTag("rpc.method", callName);
#endif
PerformPostRequest(postStream, responseStream);
responseStream.Position = 0;

Expand All @@ -239,12 +259,25 @@ protected virtual T Rpc<T>(string callName, JToken parameters, JsonSerializer se
#else
var res2 = (JsonResponseV2<T>)serializer.Deserialize(responseReader, typeof(JsonResponseV2<T>));
#endif

#if (NET462_OR_GREATER || NETSTANDARD2_0_OR_GREATER)
activity?.SetTag("rpc.jsonrpc.version", "2.0");
#endif
if (res2.Error != null)
{
var descr = new List<string> { res2.Error.Message };
descr.AddRange(res2.Error.Data.ToObject<string[]>());
#if (NET462_OR_GREATER || NETSTANDARD2_0_OR_GREATER)
activity?.SetTag("otel.status_code", "ERROR");
activity?.SetTag("rpc.jsonrpc.error_code", res2.Error.Code);
activity?.SetTag("rpc.jsonrpc.error_message", descr);
#endif
throw new Failure(descr);
}

#if (NET462_OR_GREATER || NETSTANDARD2_0_OR_GREATER)
activity?.SetTag("otel.status_code", "OK");
#endif
return res2.Result;
default:
#if DEBUG
Expand All @@ -256,14 +289,25 @@ protected virtual T Rpc<T>(string callName, JToken parameters, JsonSerializer se
if (res1.Error != null)
{
var errorArray = res1.Error.ToObject<string[]>();
if (errorArray != null)
if (errorArray != null) {
#if (NET462_OR_GREATER || NETSTANDARD2_0_OR_GREATER)
activity?.SetTag("otel.status_code", "ERROR");
//activity?.SetTag("rpc.jsonrpc.error_message", errorArray);
#endif
throw new Failure(errorArray);
}
}
#if (NET462_OR_GREATER || NETSTANDARD2_0_OR_GREATER)
activity?.SetTag("otel.status_code", "OK");
#endif
return res1.Result;
}
}
}
}
#if (NET462_OR_GREATER || NETSTANDARD2_0_OR_GREATER)
}
#endif
}
}

Expand Down

0 comments on commit 8ef759b

Please sign in to comment.