Skip to content

Commit

Permalink
add metrics to sdk (#133)
Browse files Browse the repository at this point in the history
* add metrics to sdk

* metrics for sends and receives

* format
  • Loading branch information
adamhathcock authored Oct 10, 2024
1 parent e14913b commit 1a9d28e
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/Speckle.Sdk/Api/Operations/Operations.Receive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public async Task<Base> Receive(
)
{
using var receiveActivity = activityFactory.Start("Operations.Receive");
metricsFactory.CreateCounter<long>("Receive").Add(1);

if (remoteTransport != null)
{
Expand Down
1 change: 1 addition & 0 deletions src/Speckle.Sdk/Api/Operations/Operations.Send.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public partial class Operations
}

// make sure all logs in the operation have the proper context
metricsFactory.CreateCounter<long>("Send").Add(1);
using var activity = activityFactory.Start();
activity?.SetTag("correlationId", Guid.NewGuid().ToString());
{
Expand Down
6 changes: 5 additions & 1 deletion src/Speckle.Sdk/Api/Operations/Operations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ namespace Speckle.Sdk.Api;
/// <para>Push/Pull (methods to serialize and send data to one or more servers)</para>
/// </summary>
[GenerateAutoInterface]
public partial class Operations(ILogger<Operations> logger, ISdkActivityFactory activityFactory) : IOperations;
public partial class Operations(
ILogger<Operations> logger,
ISdkActivityFactory activityFactory,
ISdkMetricsFactory metricsFactory
) : IOperations;
7 changes: 0 additions & 7 deletions src/Speckle.Sdk/Logging/ISdkActivityFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,3 @@ public interface ISdkActivityFactory : IDisposable
{
ISdkActivity? Start(string? name = default, [CallerMemberName] string source = "");
}

public sealed class NullActivityFactory : ISdkActivityFactory
{
public void Dispose() { }

public ISdkActivity? Start(string? name = default, string source = "") => null;
}
16 changes: 16 additions & 0 deletions src/Speckle.Sdk/Logging/ISdkCounter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Speckle.Sdk.Logging;

public interface ISdkCounter<T>
where T : struct
{
void Add(T value);
void Add(T value, KeyValuePair<string, object?> tag);
void Add(T value, KeyValuePair<string, object?> tag1, KeyValuePair<string, object?> tag2);
void Add(
T value,
KeyValuePair<string, object?> tag1,
KeyValuePair<string, object?> tag2,
KeyValuePair<string, object?> tag3
);
void Add(T value, params KeyValuePair<string, object?>[] tag);
}
7 changes: 7 additions & 0 deletions src/Speckle.Sdk/Logging/ISdkMetricsFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Speckle.Sdk.Logging;

public interface ISdkMetricsFactory
{
ISdkCounter<T> CreateCounter<T>(string name, string? unit = default, string? description = default)
where T : struct;
}
8 changes: 8 additions & 0 deletions src/Speckle.Sdk/Logging/NullActivityFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Speckle.Sdk.Logging;

public sealed class NullActivityFactory : ISdkActivityFactory
{
public void Dispose() { }

public ISdkActivity? Start(string? name = default, string source = "") => null;
}
20 changes: 20 additions & 0 deletions src/Speckle.Sdk/Logging/NullSdkCounter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Speckle.Sdk.Logging;

public sealed class NullSdkCounter<T> : ISdkCounter<T>
where T : struct
{
public void Add(T value) { }

public void Add(T value, KeyValuePair<string, object?> tag) { }

public void Add(T value, KeyValuePair<string, object?> tag1, KeyValuePair<string, object?> tag2) { }

public void Add(
T value,
KeyValuePair<string, object?> tag1,
KeyValuePair<string, object?> tag2,
KeyValuePair<string, object?> tag3
) { }

public void Add(T value, params KeyValuePair<string, object?>[] tag) { }
}
7 changes: 7 additions & 0 deletions src/Speckle.Sdk/Logging/NullSdkMetricsFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Speckle.Sdk.Logging;

public sealed class NullSdkMetricsFactory : ISdkMetricsFactory
{
public ISdkCounter<T> CreateCounter<T>(string name, string? unit = default, string? description = default)
where T : struct => new NullSdkCounter<T>();
}
3 changes: 2 additions & 1 deletion src/Speckle.Sdk/ServiceRegistration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ string speckleVersion
Slug = application.Slug,
}
);
serviceCollection.AddSingleton<ISdkActivityFactory, NullActivityFactory>();
serviceCollection.TryAddSingleton<ISdkActivityFactory, NullActivityFactory>();
serviceCollection.TryAddSingleton<ISdkMetricsFactory, NullSdkMetricsFactory>();
serviceCollection.AddMatchingInterfacesAsTransient(Assembly.GetExecutingAssembly());
return serviceCollection;
}
Expand Down

0 comments on commit 1a9d28e

Please sign in to comment.