diff --git a/Game/Game/GameServiceSource.cs b/Game/Game/GameServiceSource.cs index cebc18a..23826b6 100644 --- a/Game/Game/GameServiceSource.cs +++ b/Game/Game/GameServiceSource.cs @@ -35,6 +35,7 @@ public partial class GameServiceSource : RootServiceSource { serviceCollection.AddObservability(builder => builder .AddGrpcExporter("http://localhost:4317") - .AddMeterName()); + .AddMeterName() + .EnableObservabilityFeature(ObservabilityFeatures.Logging)); } } \ No newline at end of file diff --git a/Game/Level/LevelServiceSource.cs b/Game/Level/LevelServiceSource.cs index de3b1f2..b83fa0e 100644 --- a/Game/Level/LevelServiceSource.cs +++ b/Game/Level/LevelServiceSource.cs @@ -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 logger, + IOptional activitySourceFactoryWrapper, IValueSetter targetCollector, IValueSetter 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(); } } \ No newline at end of file diff --git a/Game/Metrics/MetricsHandler.cs b/Game/Metrics/MetricsHandler.cs index d983ac1..4ea5314 100644 --- a/Game/Metrics/MetricsHandler.cs +++ b/Game/Metrics/MetricsHandler.cs @@ -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 logger, + IOptional 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(); } } \ No newline at end of file diff --git a/godot-host-test.sln.DotSettings.user b/godot-host-test.sln.DotSettings.user index 4350b22..7437c2a 100644 --- a/godot-host-test.sln.DotSettings.user +++ b/godot-host-test.sln.DotSettings.user @@ -14,6 +14,7 @@ ForceIncluded ForceIncluded ForceIncluded + ForceIncluded ForceIncluded ForceIncluded ForceIncluded