Skip to content

Commit

Permalink
migrated io metrics from Counter to ObservableCounter instrument
Browse files Browse the repository at this point in the history
  • Loading branch information
hunter1703 committed May 9, 2023
1 parent 05e900b commit 1b0139e
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 47 deletions.
11 changes: 4 additions & 7 deletions src/EventStore.Core.XUnit.Tests/Index/IndexTrackerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ public class IndexTrackerTests : IDisposable {
public IndexTrackerTests() {
var meter = new Meter($"{typeof(IndexTrackerTests)}");
_listener = new TestMeterListener<long>(meter);

var eventMetric = meter.CreateCounter<long>("eventstore-io", unit: "events");

_sut = new IndexTracker(
new CounterSubMetric<long>(
eventMetric,
new KeyValuePair<string, object>("activity", "written")));
new FastCounterMetric(
meter, "eventstore-io",
new KeyValuePair<string, object>("activity", "written"), "events"));
}

public void Dispose() {
Expand All @@ -49,8 +48,6 @@ public class IndexTrackerTests : IDisposable {
}

private void AssertMeasurements(long expectedEventsWritten) {
_listener.Observe();

Assert.Collection(
_listener.RetrieveMeasurements("eventstore-io-events"),
m => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ public class TFChunkTrackerTests : IDisposable {
public TFChunkTrackerTests() {
var meter = new Meter($"{typeof(TFChunkTrackerTests)}");
_listener = new TestMeterListener<long>(meter);
var byteMetric = meter.CreateCounter<long>("eventstore-io", unit: "bytes");
var eventMetric = meter.CreateCounter<long>("eventstore-io", unit: "events");

var readTag = new KeyValuePair<string, object>("activity", "read");
_sut = new TFChunkTracker(
readBytes: new CounterSubMetric<long>(byteMetric, readTag),
readEvents: new CounterSubMetric<long>(eventMetric, readTag));
readBytes: new FastCounterMetric(meter, "eventstore-io", readTag, "bytes"),
readEvents: new FastCounterMetric(meter, "eventstore-io", readTag, "events"));
}

public void Dispose() {
Expand All @@ -49,8 +47,8 @@ public class TFChunkTrackerTests : IDisposable {
_sut.OnRead(system);
_listener.Observe();

AssertEventsRead(null);
AssertBytesRead(null);
AssertEventsRead(0);
AssertBytesRead(0);
}

[Fact]
Expand All @@ -59,8 +57,8 @@ public class TFChunkTrackerTests : IDisposable {
_sut.OnRead(system);
_listener.Observe();

AssertEventsRead(null);
AssertBytesRead(null);
AssertEventsRead(0);
AssertBytesRead(0);
}

private void AssertEventsRead(long? expectedEventsRead) =>
Expand All @@ -70,7 +68,6 @@ public class TFChunkTrackerTests : IDisposable {
AssertMeasurements("eventstore-io-bytes", expectedBytesRead);

private void AssertMeasurements(string instrumentName, long? expectedValue) {
_listener.Observe();
var actual = _listener.RetrieveMeasurements(instrumentName);

if (expectedValue is null) {
Expand Down
4 changes: 2 additions & 2 deletions src/EventStore.Core/Index/IndexTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ public interface IIndexTracker {
}

public class IndexTracker : IIndexTracker {
private readonly CounterSubMetric<long> _indexedEvents;
private readonly FastCounterMetric _indexedEvents;

public IndexTracker(CounterSubMetric<long> indexedEvents) {
public IndexTracker(FastCounterMetric indexedEvents) {
_indexedEvents = indexedEvents;
}

Expand Down
12 changes: 5 additions & 7 deletions src/EventStore.Core/MetricsBootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ public static class MetricsBootstrapper {
var gossipProcessingMetric = new DurationMetric(coreMeter, "eventstore-gossip-processing-duration");
var queueQueueingDurationMaxMetric = new DurationMaxMetric(coreMeter, "eventstore-queue-queueing-duration-max");
var queueProcessingDurationMetric = new DurationMetric(coreMeter, "eventstore-queue-processing-duration");
var byteMetric = coreMeter.CreateCounter<long>("eventstore-io", unit: "bytes");
var eventMetric = coreMeter.CreateCounter<long>("eventstore-io", unit: "events");

// incoming grpc calls
var enabledCalls = conf.IncomingGrpcCalls.Where(kvp => kvp.Value).Select(kvp => kvp.Key).ToArray();
Expand All @@ -84,15 +82,15 @@ public static class MetricsBootstrapper {
if (conf.Events.TryGetValue(Conf.EventTracker.Read, out var readEnabled) && readEnabled) {
var readTag = new KeyValuePair<string, object>("activity", "read");
trackers.TransactionFileTracker = new TFChunkTracker(
readBytes: new CounterSubMetric<long>(byteMetric, readTag),
readEvents: new CounterSubMetric<long>(eventMetric, readTag));
readBytes: new FastCounterMetric(coreMeter, "eventstore-io", readTag, "bytes"),
readEvents: new FastCounterMetric(coreMeter, "eventstore-io", readTag, "events"));
}

// from a users perspective an event is written when it is indexed: thats when it can be read.
if (conf.Events.TryGetValue(Conf.EventTracker.Written, out var writtenEnabled) && writtenEnabled) {
trackers.IndexTracker = new IndexTracker(new CounterSubMetric<long>(
eventMetric,
new KeyValuePair<string, object>("activity", "written")));
trackers.IndexTracker = new IndexTracker(new FastCounterMetric(
coreMeter, "eventstore-io",
new KeyValuePair<string, object>("activity", "written"), "events"));
}

// gossip
Expand Down
18 changes: 0 additions & 18 deletions src/EventStore.Core/Telemetry/CounterSubMetric.cs

This file was deleted.

17 changes: 17 additions & 0 deletions src/EventStore.Core/Telemetry/FastCounterMetric.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Collections.Generic;
using System.Diagnostics.Metrics;
using System.Threading;

namespace EventStore.Core.Telemetry {
public class FastCounterMetric {
private long _counter;

public FastCounterMetric(Meter meter, string name, KeyValuePair<string, object> tags, string unit = null) {
meter.CreateObservableCounter(name, () => new Measurement<long>(Interlocked.Read(ref _counter), tags), unit);
}

public void Add(int delta) {
Interlocked.Add(ref _counter, delta);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
namespace EventStore.Core.TransactionLog.Chunks;

public class TFChunkTracker : ITransactionFileTracker {
private readonly CounterSubMetric<long> _readBytes;
private readonly CounterSubMetric<long> _readEvents;
private readonly FastCounterMetric _readBytes;
private readonly FastCounterMetric _readEvents;

public TFChunkTracker(
CounterSubMetric<long> readBytes,
CounterSubMetric<long> readEvents) {
FastCounterMetric readBytes,
FastCounterMetric readEvents) {

_readBytes = readBytes;
_readEvents = readEvents;
Expand Down

0 comments on commit 1b0139e

Please sign in to comment.