Skip to content

Commit

Permalink
Refactor metrics hook tests
Browse files Browse the repository at this point in the history
Signed-off-by: André Silva <[email protected]>
  • Loading branch information
askpt committed Dec 14, 2023
1 parent 64e9e61 commit fcaea28
Showing 1 changed file with 42 additions and 4 deletions.
46 changes: 42 additions & 4 deletions test/OpenFeature.Contrib.Hooks.Otel.Test/MetricsHookTest.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using OpenFeature.Model;
using OpenTelemetry;
using OpenTelemetry.Exporter;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using Xunit;
Expand Down Expand Up @@ -42,7 +40,7 @@ public void After_Test()
// Assert metrics
Assert.NotEmpty(exportedItems);

// check if the metric feature_flag.evaluation_success_total is present in the exported items
// check if the metric is present in the exported items
var metric = exportedItems.FirstOrDefault(m => m.Name == metricName);
Assert.NotNull(metric);

Expand Down Expand Up @@ -78,7 +76,7 @@ public void Error_Test()
// Assert metrics
Assert.NotEmpty(exportedItems);

// check if the metric feature_flag.evaluation_success_total is present in the exported items
// check if the metric is present in the exported items
var metric = exportedItems.FirstOrDefault(m => m.Name == metricName);
Assert.NotNull(metric);

Expand Down Expand Up @@ -121,5 +119,45 @@ public void Finally_Test()
var noOtherMetric = exportedItems.All(m => m.Name == metricName);
Assert.True(noOtherMetric);
}

[Fact]
public void Before_Test()
{
// Arrange metrics collector
var exportedItems = new List<Metric>();
Sdk.CreateMeterProviderBuilder()
.AddMeter("*")
.ConfigureResource(r => r.AddService("openfeature"))
.AddInMemoryExporter(exportedItems, option => option.PeriodicExportingMetricReaderOptions = new PeriodicExportingMetricReaderOptions { ExportIntervalMilliseconds = 100 })
.Build();

// Arrange
const string metricName1 = "feature_flag.evaluation_active_count";
const string metricName2 = "feature_flag.evaluation_requests_total";
var otelHook = new MetricsHook();
var evaluationContext = EvaluationContext.Empty;
var ctx = new HookContext<string>("my-flag", "foo", Constant.FlagValueType.String, new ClientMetadata("my-client", "1.0"), new Metadata("my-provider"), evaluationContext);

// Act
var hookTask = otelHook.Before(ctx, new Dictionary<string, object>());
// Wait for the metrics to be exported
Thread.Sleep(150);

// Assert
Assert.True(hookTask.IsCompleted);

// Assert metrics
Assert.NotEmpty(exportedItems);

// check if the metric is present in the exported items
var metric1 = exportedItems.FirstOrDefault(m => m.Name == metricName1);
Assert.NotNull(metric1);

var metric2 = exportedItems.FirstOrDefault(m => m.Name == metricName2);
Assert.NotNull(metric2);

var noOtherMetric = exportedItems.All(m => m.Name == metricName1 || m.Name == metricName2);
Assert.True(noOtherMetric);
}
}
}

0 comments on commit fcaea28

Please sign in to comment.