up
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.Metrics;
|
||||
using System.Text.Json;
|
||||
using StellaOps.Concelier.Core.Diagnostics;
|
||||
using StellaOps.Concelier.Core.Linksets;
|
||||
using Xunit;
|
||||
|
||||
namespace StellaOps.Concelier.WebService.Tests;
|
||||
|
||||
public sealed class VulnExplorerTelemetryTests : IDisposable
|
||||
{
|
||||
private readonly MeterListener _listener;
|
||||
private readonly List<(string Name, double Value, KeyValuePair<string, object?>[] Tags)> _histogramMeasurements = new();
|
||||
private readonly List<(string Name, long Value, KeyValuePair<string, object?>[] Tags)> _counterMeasurements = new();
|
||||
|
||||
public VulnExplorerTelemetryTests()
|
||||
{
|
||||
_listener = new MeterListener
|
||||
{
|
||||
InstrumentPublished = (instrument, listener) =>
|
||||
{
|
||||
if (instrument.Meter.Name == VulnExplorerTelemetry.MeterName)
|
||||
{
|
||||
listener.EnableMeasurementEvents(instrument);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
_listener.SetMeasurementEventCallback<long>((instrument, measurement, tags, state) =>
|
||||
{
|
||||
if (instrument.Meter.Name == VulnExplorerTelemetry.MeterName)
|
||||
{
|
||||
_counterMeasurements.Add((instrument.Name, measurement, tags.ToArray()));
|
||||
}
|
||||
});
|
||||
|
||||
_listener.SetMeasurementEventCallback<double>((instrument, measurement, tags, state) =>
|
||||
{
|
||||
if (instrument.Meter.Name == VulnExplorerTelemetry.MeterName)
|
||||
{
|
||||
_histogramMeasurements.Add((instrument.Name, measurement, tags.ToArray()));
|
||||
}
|
||||
});
|
||||
|
||||
_listener.Start();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CountAliasCollisions_FiltersAliasConflicts()
|
||||
{
|
||||
var conflicts = new List<AdvisoryLinksetConflict>
|
||||
{
|
||||
new("aliases", "alias-inconsistency", Array.Empty<string>()),
|
||||
new("ranges", "range-divergence", Array.Empty<string>()),
|
||||
new("alias-field", "ALIAS-INCONSISTENCY", Array.Empty<string>())
|
||||
};
|
||||
|
||||
var count = VulnExplorerTelemetry.CountAliasCollisions(conflicts);
|
||||
|
||||
Assert.Equal(2, count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsWithdrawn_DetectsWithdrawnFlagsAndTimestamps()
|
||||
{
|
||||
using var json = JsonDocument.Parse("{\"withdrawn\":true,\"withdrawn_at\":\"2024-10-10T00:00:00Z\"}");
|
||||
Assert.True(VulnExplorerTelemetry.IsWithdrawn(json.RootElement));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RecordChunkLatency_EmitsHistogramMeasurement()
|
||||
{
|
||||
VulnExplorerTelemetry.RecordChunkLatency("tenant-a", "vendor-a", TimeSpan.FromMilliseconds(42));
|
||||
|
||||
var measurement = Assert.Single(_histogramMeasurements);
|
||||
Assert.Equal("vuln.chunk_latency_ms", measurement.Name);
|
||||
Assert.Equal(42, measurement.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RecordWithdrawnStatement_EmitsCounter()
|
||||
{
|
||||
VulnExplorerTelemetry.RecordWithdrawnStatement("tenant-b", "vendor-b");
|
||||
|
||||
var measurement = Assert.Single(_counterMeasurements);
|
||||
Assert.Equal("vuln.withdrawn_statements_total", measurement.Name);
|
||||
Assert.Equal(1, measurement.Value);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_listener.Dispose();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user