feat: metrics test.
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
using Godot;
|
||||
using GodotHostTest.AetherBind;
|
||||
using GodotHostTest.Game.Level;
|
||||
using GodotHostTest.Game.Metrics;
|
||||
using GodotHostTest.Game.Score;
|
||||
using GodotHostTest.Game.Target;
|
||||
using GodotHostTest.Interfaces;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using OwofGames.GodotHost;
|
||||
using OwofGames.GodotHost.Observability;
|
||||
using OwofGames.GodotLume;
|
||||
|
||||
@@ -23,12 +25,16 @@ public partial class GameServiceSource : RootServiceSource
|
||||
// current level / level data management
|
||||
.AddInstance<ILevelDataProvider, LevelsData>(_levelsData)
|
||||
.AddSingleton<ICurrentLevel, CurrentLevel>()
|
||||
// metrics
|
||||
.AddSingleton<IHostLifetime, MetricsHandler>()
|
||||
// level scope services
|
||||
.AddLevelServices();
|
||||
}
|
||||
|
||||
protected override void ConfigureServices(IServiceCollection serviceCollection)
|
||||
{
|
||||
serviceCollection.AddObservability(builder => builder.AddGrpcExporter("http://localhost:4317"));
|
||||
serviceCollection.AddObservability(builder => builder
|
||||
.AddGrpcExporter("http://localhost:4317")
|
||||
.AddMeterName(MetricsHandler.MeterName));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
using System;
|
||||
using System.Diagnostics.Metrics;
|
||||
using System.Reflection;
|
||||
using GodotHostTest.Interfaces;
|
||||
using OwofGames.GodotHost;
|
||||
using R3;
|
||||
|
||||
namespace GodotHostTest.Game.Metrics;
|
||||
|
||||
public class MetricsHandler(IMeterFactory meterFactory, ITargetEventBus targetEventBus) : IHostLifetime
|
||||
{
|
||||
public static readonly string MeterName;
|
||||
private static readonly string? MeterVersion;
|
||||
private IDisposable? _disposables;
|
||||
|
||||
static MetricsHandler()
|
||||
{
|
||||
var assemblyName = Assembly.GetExecutingAssembly().GetName();
|
||||
MeterName = assemblyName.Name!;
|
||||
MeterVersion = assemblyName.Version?.ToString();
|
||||
}
|
||||
|
||||
public void Startup()
|
||||
{
|
||||
// create meter
|
||||
var assemblyName = Assembly.GetExecutingAssembly().GetName();
|
||||
var meterName = assemblyName.Name!;
|
||||
var meterVersion = assemblyName.Version?.ToString();
|
||||
var meter = meterFactory.Create(meterName, meterVersion);
|
||||
|
||||
// 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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://5ramu7es1104
|
||||
Reference in New Issue
Block a user