feat: adapt to enabled observability features.

This commit is contained in:
redglow
2026-07-29 12:49:42 +02:00
parent b3073ee1ba
commit ce6cfaccb2
4 changed files with 27 additions and 9 deletions
+2 -1
View File
@@ -35,6 +35,7 @@ public partial class GameServiceSource : RootServiceSource
{
serviceCollection.AddObservability(builder => builder
.AddGrpcExporter("http://localhost:4317")
.AddMeterName());
.AddMeterName()
.EnableObservabilityFeature(ObservabilityFeatures.Logging));
}
}
+12 -6
View File
@@ -4,6 +4,7 @@ using Godot;
using GodotHostTest.AetherBind;
using GodotHostTest.Game.Target;
using GodotHostTest.Interfaces;
using Microsoft.Extensions.Logging;
using OwofGames.AetherBind;
using OwofGames.GodotHost.Observability;
using OwofGames.GodotLume;
@@ -23,13 +24,18 @@ public partial class LevelServiceSource : ServiceSource
}
[WithProvider]
private void OnScopeCreated(IActivitySourceFactory activitySourceFactory,
private void OnScopeCreated(ILogger<LevelServiceSource> logger,
IOptional<IActivitySourceFactory> activitySourceFactoryWrapper,
IValueSetter<ITargetCollector> targetCollector, IValueSetter<ITargetSpawner> targetSpawner)
{
// ReSharper disable once ExplicitCallerInfoArgument - the caller name is not significant in this case
_levelActivity = activitySourceFactory.ActivitySource.StartActivity("Level") ??
throw new InvalidOperationException(
"No listeners for the activity - is OpenTelemetry running?");
if (activitySourceFactoryWrapper.TryGet(out var activitySourceFactory))
// ReSharper disable once ExplicitCallerInfoArgument - the caller name is not significant in this case
_levelActivity = activitySourceFactory.ActivitySource.StartActivity("Level") ??
throw new InvalidOperationException(
"No listeners for the activity - is OpenTelemetry running?");
else
logger.LogWarning("Tracing not enabled, cannot start the activity.");
targetCollector.Set(_targetSpawner);
targetSpawner.Set(_targetSpawner);
}
@@ -37,6 +43,6 @@ public partial class LevelServiceSource : ServiceSource
public override void _Ready()
{
// after 3 seconds, dispose the activity for simulation purposes
GetTree().CreateTimer(3).Timeout += _levelActivity!.Dispose;
GetTree().CreateTimer(3).Timeout += () => _levelActivity?.Dispose();
}
}
+12 -2
View File
@@ -1,18 +1,29 @@
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(IMeterFactory meterFactory, ITargetEventBus targetEventBus) : IHostLifetime
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();
@@ -33,6 +44,5 @@ public class MetricsHandler(IMeterFactory meterFactory, ITargetEventBus targetEv
public void Shutdown()
{
_disposables?.Dispose();
meterFactory.Create("godot-host-test")?.Dispose();
}
}