40 lines
1.4 KiB
C#
40 lines
1.4 KiB
C#
using System.Diagnostics.Metrics;
|
|
|
|
namespace HSchool.Server.Game;
|
|
|
|
/// <summary>Per-school tick counters surfaced in the Aspire dashboard.</summary>
|
|
internal sealed class GameMetrics : IDisposable
|
|
{
|
|
public const string MeterName = "HSchool.Server.Game";
|
|
|
|
private readonly Meter _meter;
|
|
private readonly Counter<long> _ticks;
|
|
private readonly Histogram<double> _tickDuration;
|
|
private readonly UpDownCounter<long> _connections;
|
|
|
|
private int _schools;
|
|
|
|
public GameMetrics(IMeterFactory meterFactory)
|
|
{
|
|
_meter = meterFactory.Create(MeterName);
|
|
_ticks = _meter.CreateCounter<long>("hschool.game.ticks", "{tick}", "Simulation steps executed.");
|
|
_tickDuration = _meter.CreateHistogram<double>("hschool.game.tick.duration", "ms", "Wall time of one simulation step.");
|
|
_connections = _meter.CreateUpDownCounter<long>("hschool.game.connections", "{connection}", "Open WebSocket connections.");
|
|
_meter.CreateObservableGauge("hschool.game.schools", () => Volatile.Read(ref _schools), "{school}", "Schools that currently exist.");
|
|
}
|
|
|
|
public void RecordTick(double durationMs)
|
|
{
|
|
_ticks.Add(1);
|
|
_tickDuration.Record(durationMs);
|
|
}
|
|
|
|
public void ClientConnected() => _connections.Add(1);
|
|
|
|
public void ClientDisconnected() => _connections.Add(-1);
|
|
|
|
public void SchoolsChanged(int count) => Volatile.Write(ref _schools, count);
|
|
|
|
public void Dispose() => _meter.Dispose();
|
|
}
|