-
Notifications
You must be signed in to change notification settings - Fork 638
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
378d917
commit f83d0ba
Showing
13 changed files
with
278 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
src/EventStore.Core.XUnit.Tests/Telemetry/MaxTrackerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
using System; | ||
using System.Diagnostics.Metrics; | ||
using System.Linq; | ||
using EventStore.Core.Telemetry; | ||
using Xunit; | ||
|
||
namespace EventStore.Core.XUnit.Tests.Telemetry; | ||
|
||
public class MaxTrackerTests : IDisposable { | ||
private readonly TestMeterListener<long> _listener; | ||
private readonly FakeClock _clock = new(); | ||
private readonly MaxTracker<long> _sut; | ||
|
||
public MaxTrackerTests() { | ||
var meter = new Meter($"{typeof(MaxTrackerTests)}"); | ||
_listener = new TestMeterListener<long>(meter); | ||
var metric = new MaxMetric<long>(meter, "the-metric"); | ||
_sut = new MaxTracker<long>( | ||
metric: metric, | ||
name: "the-tracker", | ||
expectedScrapeIntervalSeconds: 15, | ||
clock: _clock); | ||
} | ||
|
||
public void Dispose() { | ||
_listener.Dispose(); | ||
} | ||
|
||
[Fact] | ||
public void no_records() { | ||
AssertMeasurements(0); | ||
} | ||
|
||
[Fact] | ||
public void two_records_ascending() { | ||
AssertMeasurements(0); | ||
_sut.Record(1); | ||
AssertMeasurements(1); | ||
_sut.Record(2); | ||
AssertMeasurements(2); | ||
} | ||
|
||
[Fact] | ||
public void two_records_descending() { | ||
AssertMeasurements(0); | ||
_sut.Record(2); | ||
AssertMeasurements(2); | ||
_sut.Record(1); | ||
AssertMeasurements(2); | ||
} | ||
|
||
[Fact] | ||
public void removes_stale_data() { | ||
_sut.Record(1); | ||
_clock.AdvanceSeconds(19); | ||
AssertMeasurements(1); | ||
|
||
_clock.AdvanceSeconds(1); | ||
AssertMeasurements(0); | ||
} | ||
|
||
void AssertMeasurements(double expectedValue) { | ||
_listener.Observe(); | ||
|
||
Assert.Collection( | ||
_listener.RetrieveMeasurements("the-metric"), | ||
m => { | ||
Assert.Equal(expectedValue, m.Value); | ||
Assert.Collection( | ||
m.Tags.ToArray(), | ||
t => { | ||
Assert.Equal("name", t.Key); | ||
Assert.Equal("the-tracker", t.Value); | ||
}, | ||
t => { | ||
Assert.Equal("range", t.Key); | ||
Assert.Equal("16-20 seconds", t.Value); | ||
}); | ||
}); | ||
} | ||
|
||
[Fact] | ||
public void no_name() { | ||
using var meter = new Meter($"{typeof(MaxTrackerTests)}"); | ||
using var listener = new TestMeterListener<long>(meter); | ||
var sut = new MaxTracker<long>( | ||
metric: new MaxMetric<long>(meter, "the-metric"), | ||
name: null, | ||
expectedScrapeIntervalSeconds: 15); | ||
|
||
listener.Observe(); | ||
|
||
Assert.Collection( | ||
listener.RetrieveMeasurements("the-metric"), | ||
m => { | ||
Assert.Equal(0, m.Value); | ||
Assert.Collection( | ||
m.Tags.ToArray(), | ||
t => { | ||
Assert.Equal("range", t.Key); | ||
Assert.Equal("16-20 seconds", t.Value); | ||
}); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System.Collections.Generic; | ||
using System.Diagnostics.Metrics; | ||
|
||
namespace EventStore.Core.Telemetry; | ||
|
||
public class MaxMetric<T> where T : struct { | ||
private readonly List<MaxTracker<T>> _trackers = new(); | ||
|
||
public MaxMetric(Meter meter, string name) { | ||
// gauge rather than updowncounter because the dimensions wont make sense to sum, | ||
// because they are maxes and not necessarily from the same moment | ||
meter.CreateObservableGauge(name, Observe); | ||
} | ||
|
||
public void Add(MaxTracker<T> tracker) { | ||
lock (_trackers) { | ||
_trackers.Add(tracker); | ||
} | ||
} | ||
|
||
private IEnumerable<Measurement<T>> Observe() { | ||
lock (_trackers) { | ||
foreach (var tracker in _trackers) { | ||
yield return tracker.Observe(); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.