Files
godot-host-test/Game/Metrics/MetricsHandler.cs
T
2026-07-29 11:48:46 +02:00

38 lines
1.2 KiB
C#

using System;
using System.Diagnostics.Metrics;
using GodotHostTest.Interfaces;
using OwofGames.GodotHost;
using OwofGames.GodotHost.Observability;
using R3;
namespace GodotHostTest.Game.Metrics;
public class MetricsHandler(IMeterFactory meterFactory, ITargetEventBus targetEventBus) : IHostLifetime
{
private IDisposable? _disposables;
public void Startup()
{
// create meter
var meter = meterFactory.Create();
// create instruments
var hitTargetsCounter =
meter.CreateCounter<int>("targets.hit", description: "number of targets hit");
var missedTargetsCounter =
meter.CreateCounter<int>("targets.missed", description: "number of targets missed");
// feed instruments data, and contextually create the disposable to clean up at shutdown time
_disposables = Disposable.Combine(
targetEventBus.Hit.Subscribe(_ => hitTargetsCounter.Add(1)),
targetEventBus.TimedOut.Subscribe(_ => missedTargetsCounter.Add(1)),
meter
);
}
public void Shutdown()
{
_disposables?.Dispose();
meterFactory.Create("godot-host-test")?.Dispose();
}
}