Skip to content

Commit

Permalink
Add tracing instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
ogxd committed Mar 29, 2023
1 parent f1a4e92 commit a15f304
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
47 changes: 45 additions & 2 deletions AerospikeClient/Async/AsyncCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ public abstract class AsyncCommand : Command, IAsyncCommand, ITimeout
private bool compressed;
private bool inAuthenticate;
private bool inHeader = true;

private Activity activity;

/// <summary>
/// Default Constructor.
/// </summary>
Expand Down Expand Up @@ -203,14 +204,25 @@ private void ExecuteCore()
}
ExecuteCommand();
}

private void ExecuteCommand()
{
iteration++;

Exception exception = null;

StartActivity();

try
{
node = (AsyncNode)GetNode(cluster);

if (activity != null)
{
activity.SetTag("node.name", node.Name);
activity.SetTag("node.address", node.NodeAddress.ToString());
}

node.ValidateErrorCount();
conn = node.GetAsyncConnection();

Expand All @@ -229,29 +241,60 @@ private void ExecuteCommand()
}
catch (AerospikeException.Connection aec)
{
exception = aec;
ErrorCount++;
ConnectionFailed(aec);
}
catch (AerospikeException.Backoff aeb)
{
exception = aeb;
ErrorCount++;
Backoff(aeb);
}
catch (AerospikeException ae)
{
exception = ae;
ErrorCount++;
FailOnApplicationError(ae);
}
catch (SocketException se)
{
exception = se;
ErrorCount++;
OnSocketError(se.SocketErrorCode);
}
catch (Exception e)
{
exception = e;
ErrorCount++;
FailOnApplicationError(new AerospikeException(e));
}
finally
{
if (activity != null && exception != null)
{
activity.SetStatus(ActivityStatusCode.Error, exception.ToString());
}
}
}

private void StartActivity()
{
// Because of the retry mecanism, one operation can result in several I/Os, on several nodes.
// For this reason, we create a new Activity on every retry, so that we can set the actual node
// adress and the I/O status as tags of this current Activity.

// If we're retrying, stop the previous Activity before restarting a new one.
activity?.Dispose();
activity = Tracing.Source.StartActivity(GetType().Name, ActivityKind.Client);
if (activity != null)
{
// https://github.com/open-telemetry/opentelemetry-specification/blob/6ce62202e5407518e19c56c445c13682ef51a51d/specification/trace/semantic_conventions/span-general.md#general-remote-service-attributes
activity.SetTag("peer.service", Tracing.SERVICE_NAME);
// https://github.com/open-telemetry/opentelemetry-specification/blob/6ce62202e5407518e19c56c445c13682ef51a51d/specification/trace/semantic_conventions/database.md#connection-level-attributes
activity.SetTag("db.system", Tracing.SERVICE_NAME);
activity.SetTag("iteration", iteration);
}
}

public void OnConnected()
Expand Down
17 changes: 17 additions & 0 deletions AerospikeClient/Util/Tracing.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#if NET6_0_OR_GREATER

using System.Diagnostics;

namespace Aerospike.Client
{
public class Tracing
{
private const string SOURCE_NAME = "Aerospike.Client";

internal const string SERVICE_NAME = "Aerospike";

internal static readonly ActivitySource Source = new ActivitySource(SOURCE_NAME);
}
}

#endif

0 comments on commit a15f304

Please sign in to comment.