From 96bb8cec3665a23b1301c3a16cd33bc15899e4c1 Mon Sep 17 00:00:00 2001 From: Simon Hornby Date: Tue, 9 Jul 2024 13:37:10 +0200 Subject: [PATCH] feat: add metrics extension data (applied from squash) --- src/Unleash/Internal/UnleashServices.cs | 4 +-- src/Unleash/Metrics/ClientMetrics.cs | 29 ++++++++++++++++ src/Unleash/Metrics/ClientRegistration.cs | 29 ++++++++++++++++ src/Unleash/Metrics/MetricsMetadata.cs | 34 +++++++++++++++++++ .../UnleashApiClient_RegisterClient_Tests.cs | 4 +++ .../UnleashApiClient_SendMetrics_Tests.cs | 4 +++ .../Metrics/MetricsMetadataTests.cs | 17 ++++++++++ 7 files changed, 119 insertions(+), 2 deletions(-) create mode 100644 src/Unleash/Metrics/MetricsMetadata.cs create mode 100644 tests/Unleash.Tests/Metrics/MetricsMetadataTests.cs diff --git a/src/Unleash/Internal/UnleashServices.cs b/src/Unleash/Internal/UnleashServices.cs index 9f27ed8c..92576861 100644 --- a/src/Unleash/Internal/UnleashServices.cs +++ b/src/Unleash/Internal/UnleashServices.cs @@ -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; } @@ -70,7 +70,7 @@ public UnleashServices(UnleashSettings settings, EventCallbackConfig eventConfig } else { - // Mocked backend: fill instance collection + // Mocked backend: fill instance collection apiClient = settings.UnleashApiClient; } diff --git a/src/Unleash/Metrics/ClientMetrics.cs b/src/Unleash/Metrics/ClientMetrics.cs index a16e2b10..f9a3632d 100644 --- a/src/Unleash/Metrics/ClientMetrics.cs +++ b/src/Unleash/Metrics/ClientMetrics.cs @@ -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; + } + } } } \ No newline at end of file diff --git a/src/Unleash/Metrics/ClientRegistration.cs b/src/Unleash/Metrics/ClientRegistration.cs index 6e5bc758..0d43df04 100644 --- a/src/Unleash/Metrics/ClientRegistration.cs +++ b/src/Unleash/Metrics/ClientRegistration.cs @@ -11,5 +11,34 @@ internal class ClientRegistration public List 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; + } + } } } \ No newline at end of file diff --git a/src/Unleash/Metrics/MetricsMetadata.cs b/src/Unleash/Metrics/MetricsMetadata.cs new file mode 100644 index 00000000..82b87852 --- /dev/null +++ b/src/Unleash/Metrics/MetricsMetadata.cs @@ -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, @"^(?.+?) \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; + } +} \ No newline at end of file diff --git a/tests/Unleash.Tests/Communication/UnleashApiClient_RegisterClient_Tests.cs b/tests/Unleash.Tests/Communication/UnleashApiClient_RegisterClient_Tests.cs index ee59af34..1733f853 100644 --- a/tests/Unleash.Tests/Communication/UnleashApiClient_RegisterClient_Tests.cs +++ b/tests/Unleash.Tests/Communication/UnleashApiClient_RegisterClient_Tests.cs @@ -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() diff --git a/tests/Unleash.Tests/Communication/UnleashApiClient_SendMetrics_Tests.cs b/tests/Unleash.Tests/Communication/UnleashApiClient_SendMetrics_Tests.cs index 2c621bfb..1c791227 100644 --- a/tests/Unleash.Tests/Communication/UnleashApiClient_SendMetrics_Tests.cs +++ b/tests/Unleash.Tests/Communication/UnleashApiClient_SendMetrics_Tests.cs @@ -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(); diff --git a/tests/Unleash.Tests/Metrics/MetricsMetadataTests.cs b/tests/Unleash.Tests/Metrics/MetricsMetadataTests.cs new file mode 100644 index 00000000..1e2d8b99 --- /dev/null +++ b/tests/Unleash.Tests/Metrics/MetricsMetadataTests.cs @@ -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); + } +} \ No newline at end of file