Skip to content

Commit

Permalink
feat: add metrics extension data (applied from squash)
Browse files Browse the repository at this point in the history
  • Loading branch information
sighphyre committed Jul 9, 2024
1 parent 26071d5 commit 31f3cbc
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/Unleash/Internal/UnleashServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal class UnleashServices : IDisposable
private readonly CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
private readonly IUnleashScheduledTaskManager scheduledTaskManager;

const string supportedSpecVersion = "4.5.1";
public const string supportedSpecVersion = "4.5.1";

internal CancellationToken CancellationToken { get; }
internal IUnleashContextProvider ContextProvider { get; }
Expand Down Expand Up @@ -70,7 +70,7 @@ public UnleashServices(UnleashSettings settings, EventCallbackConfig eventConfig
}
else
{
// Mocked backend: fill instance collection
// Mocked backend: fill instance collection
apiClient = settings.UnleashApiClient;
}

Expand Down
29 changes: 29 additions & 0 deletions src/Unleash/Metrics/ClientMetrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,34 @@ internal class ClientMetrics
public string AppName { get; set; }
public string InstanceId { get; set; }
public MetricsBucket Bucket { get; set; }
public string PlatformName
{
get
{
return MetricsMetadata.GetPlatformName();

}
}
public string PlatformVersion
{
get
{
return MetricsMetadata.GetPlatformVersion();
}
}
public string YggdrasilVersion
{
get
{
return null;
}
}
public string SpecVersion
{
get
{
return UnleashServices.supportedSpecVersion;
}
}
}
}
29 changes: 29 additions & 0 deletions src/Unleash/Metrics/ClientRegistration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,34 @@ internal class ClientRegistration
public List<string> Strategies { get; set; }
public DateTimeOffset Started { get; set; }
public long Interval { get; set; }
public string PlatformName
{
get
{
return MetricsMetadata.GetPlatformName();

}
}
public string PlatformVersion
{
get
{
return MetricsMetadata.GetPlatformVersion();
}
}
public string YggdrasilVersion
{
get
{
return null;
}
}
public string SpecVersion
{
get
{
return UnleashServices.supportedSpecVersion;
}
}
}
}
34 changes: 34 additions & 0 deletions src/Unleash/Metrics/MetricsMetadata.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Text.RegularExpressions;

internal static class MetricsMetadata
{
internal static string GetPlatformName()
{
#if NETSTANDARD1_1_OR_GREATER || NETCOREAPP1_0_OR_GREATER
return GetPlatformName(System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription);
#else
return "PreDotNetCore";
#endif
}

internal static string GetPlatformName(string frameworkDescription)
{
var match = Regex.Match(frameworkDescription, @"^(?<name>.+?) \d");
return match.Success ? match.Groups["name"].Value : frameworkDescription;
}

internal static string GetPlatformVersion()
{
#if NETSTANDARD1_1_OR_GREATER || NETCOREAPP1_0_OR_GREATER
return GetPlatformVersion(System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription);
#else
return null;
#endif
}

internal static string GetPlatformVersion(string frameworkDescription)
{
var match = Regex.Match(frameworkDescription, @"(\d+\.\d+\.\d+)");
return match.Success ? match.Value : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public async Task RegisterClient_Success()
.WithPartialContent("\"interval\":1000")
.WithPartialContent("\"sdkVersion\":\"1.0.1\"")
.WithPartialContent("\"strategies\":[\"abc\"]")
.WithPartialContent("specVersion")
.WithPartialContent("platformName")
.WithPartialContent("platformVersion")
.WithPartialContent("\"yggdrasilVersion\":null")
.Respond("application/json", "{ 'status': 'ok' }");

var clientRegistration = new ClientRegistration()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ public async Task SendMetrics_Success()
.WithPartialContent("instanceId")
.WithPartialContent("\"no\":0")
.WithPartialContent("\"yes\":1")
.WithPartialContent("specVersion")
.WithPartialContent("platformName")
.WithPartialContent("platformVersion")
.WithPartialContent("\"yggdrasilVersion\":null")
.Respond("application/json", "{ 'status': 'ok' }");

var metricsBucket = new ThreadSafeMetricsBucket();
Expand Down
17 changes: 17 additions & 0 deletions tests/Unleash.Tests/Metrics/MetricsMetadataTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using FluentAssertions;
using NUnit.Framework;

public class MetricsMetadataTests
{
[TestCase(".NET Core 3.1.32", ".NET Core", "3.1.32")]
[TestCase(".NET 7.0.12", ".NET", "7.0.12")]
[TestCase(".NET", ".NET", null)] //apparently this can be a thing but the docs are very shady on when
public void GetPlatformName_ShouldReturnDotNet(string inputString, string expectedName, string expectedVersion)
{
var platformName = MetricsMetadata.GetPlatformName(inputString);
var platformVersion = MetricsMetadata.GetPlatformVersion(inputString);

platformName.Should().Be(expectedName);
platformVersion.Should().Be(expectedVersion);
}
}
2 changes: 1 addition & 1 deletion tests/Unleash.Tests/Strategy/Segments_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,4 @@ public static IUnleash CreateUnleash(string name, ToggleCollection state)
return unleash;
}
}
}
}

0 comments on commit 31f3cbc

Please sign in to comment.