Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: extend metrics with metadata #231

Merged
merged 1 commit into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading