Skip to content

Commit ffa36d1

Browse files
authored
feat(dotnet): flatbuffers implementation of collect metrics (#84)
1 parent d1b2b3b commit ffa36d1

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

dotnet-engine/Yggdrasil.Engine.Tests/YggdrasilImpactMetricsTest.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.Text.Json.Nodes;
23
using NUnit.Framework;
34
using Yggdrasil;
45

@@ -86,4 +87,17 @@ public void ImpactMetrics_Methods_Can_Be_Used_Together()
8687
yggdrasilEngine.ObserveHistogram("test_histogram", 0.25);
8788
});
8889
}
90+
91+
[Test]
92+
public void CollectMetrics_Returns_Recorded_Metric()
93+
{
94+
var yggdrasilEngine = new YggdrasilEngine();
95+
yggdrasilEngine.DefineCounter("requests_total", "Total requests");
96+
Assert.DoesNotThrow(() => yggdrasilEngine.IncCounter("requests_total"));
97+
var metrics = yggdrasilEngine.CollectMetricsBucket();
98+
var counter = JsonNode.Parse(metrics!)!["impact_metrics"]!.AsArray()[0]!;
99+
var samples = counter["samples"]!.AsArray()![0]!;
100+
Assert.AreEqual("requests_total", counter["name"]!.GetValue<string>());
101+
Assert.AreEqual(1, samples["value"]!.GetValue<int>());
102+
}
89103
}

dotnet-engine/Yggdrasil.Engine/FFI.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ static FFI()
2828
free_response = Marshal.GetDelegateForFunctionPointer<FreeResponseDelegate>(NativeLibLoader.LoadFunctionPointer(_libHandle, "free_response"));
2929

3030
// everything else has some special details to it
31-
// if these get out of hand they'll need some refactor but for now 5 is fine
3231
take_state = Marshal.GetDelegateForFunctionPointer<TakeStateDelegate>(
3332
NativeLibLoader.LoadFunctionPointer(_libHandle, "flat_take_state"));
3433

@@ -43,6 +42,9 @@ static FFI()
4342

4443
get_metrics = Marshal.GetDelegateForFunctionPointer<GetMetricsDelegate>(
4544
NativeLibLoader.LoadFunctionPointer(_libHandle, "flat_get_metrics"));
45+
46+
collect_metrics = Marshal.GetDelegateForFunctionPointer<CollectMetricsDelegate>(
47+
NativeLibLoader.LoadFunctionPointer(_libHandle, "flat_collect_metrics"));
4648
}
4749

4850
// one delegate type to rule them all, lets us not have to deal with a ton of delegate types in the higher layers
@@ -64,6 +66,9 @@ static FFI()
6466
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
6567
private delegate Buf GetMetricsDelegate(IntPtr enginePtr);
6668

69+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
70+
private delegate Buf CollectMetricsDelegate(IntPtr enginePtr);
71+
6772
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
6873
private delegate IntPtr NewEngineDelegate();
6974

@@ -96,6 +101,7 @@ static FFI()
96101
private static readonly ListKnownTogglesDelegate list_known_toggles;
97102
private static readonly BuiltInStrategiesDelegate built_in_strategies;
98103
private static readonly GetMetricsDelegate get_metrics;
104+
private static readonly CollectMetricsDelegate collect_metrics;
99105

100106
internal static Buf TakeState(IntPtr ptr, string json)
101107
=> take_state(ptr, ToUtf8NullTerminated(json));
@@ -130,6 +136,8 @@ internal static Buf ObserveHistogram(IntPtr ptr, byte[] message)
130136

131137
internal static Buf GetMetrics(IntPtr ptr) => get_metrics(ptr);
132138

139+
internal static Buf CollectMetrics(IntPtr ptr) => collect_metrics(ptr);
140+
133141
internal static void FreeBuf(Buf buf) => free_buffer(buf);
134142

135143
internal static IntPtr NewEngine()

dotnet-engine/Yggdrasil.Engine/Flatbuffers.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,19 @@ internal static void ParseVoidAndThrow(Buf buf)
344344
}
345345
}
346346

347+
internal static string GetCollectedMetricsBucket(Buf buf)
348+
{
349+
var response = ReadBuffer(buf);
350+
351+
var collectResponse = CollectMetricsResponse.GetRootAsCollectMetricsResponse(new ByteBuffer(response));
352+
if (collectResponse.Error != null)
353+
{
354+
throw new YggdrasilEngineException(collectResponse.Error);
355+
}
356+
357+
return collectResponse.Response;
358+
}
359+
347360
private static Dictionary<string, FeatureCount> GetMetricsFeatureCounts(MetricsResponse response)
348361
{
349362
return Enumerable.Range(0, response.TogglesLength)

dotnet-engine/Yggdrasil.Engine/YggdrasilEngine.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,12 @@ public void ObserveHistogram(string name, double value, IDictionary<string, stri
185185
InvokeVoid(FFI.ObserveHistogram, msg);
186186
}
187187

188+
public string? CollectMetricsBucket()
189+
{
190+
EnsureNotDisposed();
191+
return InvokeNoMsg(FFI.CollectMetrics, Flatbuffers.GetCollectedMetricsBucket);
192+
}
193+
188194
public ICollection<FeatureDefinition> ListKnownToggles()
189195
{
190196
EnsureNotDisposed();

0 commit comments

Comments
 (0)