48 lines
1.4 KiB
C#
48 lines
1.4 KiB
C#
using System;
|
|
using System.Diagnostics.Metrics;
|
|
using GodotHostTest.Interfaces;
|
|
using Microsoft.Extensions.Logging;
|
|
using OwofGames.GodotHost;
|
|
using OwofGames.GodotHost.Observability;
|
|
using OwofGames.GodotLume;
|
|
using R3;
|
|
|
|
namespace GodotHostTest.Game.Metrics;
|
|
|
|
public class MetricsHandler(
|
|
ILogger<MetricsHandler> logger,
|
|
IOptional<IMeterFactory> meterFactoryWrapper,
|
|
ITargetEventBus targetEventBus) : IHostLifetime
|
|
{
|
|
private IDisposable? _disposables;
|
|
|
|
public void Startup()
|
|
{
|
|
if (!meterFactoryWrapper.TryGet(out var meterFactory))
|
|
{
|
|
logger.LogWarning("Metrics not enabled, cannot start local metrics");
|
|
return;
|
|
}
|
|
|
|
// 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();
|
|
}
|
|
} |