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("targets.hit", description: "number of targets hit"); var missedTargetsCounter = meter.CreateCounter("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(); } }