Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Robust.Client/ClientIoC.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics.Metrics;
using Robust.Client.Audio;
using Robust.Client.Audio.Midi;
using Robust.Client.Configuration;
Expand Down Expand Up @@ -37,6 +38,7 @@
using Robust.Shared.Configuration;
using Robust.Shared.Console;
using Robust.Shared.ContentPack;
using Robust.Shared.DataMetrics;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
Expand Down Expand Up @@ -114,6 +116,8 @@ public static void RegisterIoC(GameController.DisplayMode mode, IDependencyColle
deps.Register<ILocalizationManagerInternal, ClientLocalizationManager>();
deps.Register<LoadingScreenManager>();
deps.Register<ILoadingScreenManager, LoadingScreenManager>();
deps.Register<IMeterFactory, MeterFactory>();
deps.Register<IMeterFactoryInternal, MeterFactory>();

switch (mode)
{
Expand Down
4 changes: 4 additions & 0 deletions Robust.Client/GameController/GameController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
using Robust.Shared.Audio;
using Robust.Shared.Configuration;
using Robust.Shared.ContentPack;
using Robust.Shared.DataMetrics;
using Robust.Shared.Exceptions;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
Expand Down Expand Up @@ -102,6 +103,7 @@ internal sealed partial class GameController : IGameControllerInternal
[Dependency] private readonly LoadingScreenManager _loadscr = default!;
[Dependency] private readonly ITransferManager _transfer = default!;
[Dependency] private readonly ClientTransferTestManager _transferTest = default!;
[Dependency] private readonly IMeterFactoryInternal _meterFactory = default!;

private IWebViewManagerHook? _webViewHook;

Expand Down Expand Up @@ -426,6 +428,8 @@ internal bool StartupSystemSplash(
_configurationManager.OverrideConVars(_commandLineArgs.CVars);
}

_meterFactory.Initialize();

ProfileOptSetup.Setup(_configurationManager);

_prof.Initialize();
Expand Down
3 changes: 3 additions & 0 deletions Robust.Server.Testing/RobustServerSimulation.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics.Metrics;
using System.Reflection;
using JetBrains.Annotations;
using Moq;
Expand All @@ -22,6 +23,7 @@
using Robust.Shared.Console;
using Robust.Shared.Containers;
using Robust.Shared.ContentPack;
using Robust.Shared.DataMetrics;
using Robust.Shared.Exceptions;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
Expand Down Expand Up @@ -264,6 +266,7 @@ public ISimulation InitializeInstance()
container.Register<IParallelManagerInternal, TestingParallelManager>();
// Needed for grid fixture debugging.
container.Register<IConGroupController, ConGroupController>();
container.Register<IMeterFactory, MeterFactory>();
container.Register<EntityConsoleHost>();

// I just wanted to load pvs system
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ internal sealed partial class MetricsManager

private void InitializeUpdateMetrics()
{
_cfg.OnValueChanged(
Cfg.OnValueChanged(
CVars.MetricsUpdateInterval,
seconds =>
{
Expand Down
30 changes: 16 additions & 14 deletions Robust.Server/DataMetrics/MetricsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Robust.Shared;
using Robust.Shared.Asynchronous;
using Robust.Shared.Configuration;
using Robust.Shared.DataMetrics;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Log;
Expand Down Expand Up @@ -44,9 +45,8 @@ public interface IMetricsManager
event Action UpdateMetrics;
}

internal sealed partial class MetricsManager : IMetricsManagerInternal, IDisposable
internal sealed partial class MetricsManager : MeterFactory, IMetricsManagerInternal
{
[Dependency] private readonly IConfigurationManager _cfg = default!;
[Dependency] private readonly IEntitySystemManager _entitySystemManager = default!;
[Dependency] private readonly ILogManager _logManager = default!;
[Dependency] private readonly ITaskManager _taskManager = default!;
Expand All @@ -57,8 +57,10 @@ internal sealed partial class MetricsManager : IMetricsManagerInternal, IDisposa
private IDisposable? _runtimeCollector;
private ISawmill _sawmill = default!;

public void Initialize()
public override void Initialize()
{
base.Initialize();

_sawmill = _logManager.GetSawmill("metrics");

_initialized = true;
Expand All @@ -82,7 +84,7 @@ public void Initialize()

void ValueChanged<T>(CVarDef<T> cVar) where T : notnull
{
_cfg.OnValueChanged(cVar, _ => Reload());
Cfg.OnValueChanged(cVar, _ => Reload());
}

InitializeUpdateMetrics();
Expand All @@ -102,9 +104,9 @@ private async Task Stop()
_runtimeCollector = null;
}

async void IDisposable.Dispose()
protected override async void Dispose()
{
DisposeMeters();
base.Dispose();

await Stop();

Expand All @@ -120,16 +122,16 @@ private async void Reload()

await Stop();

var enabled = _cfg.GetCVar(CVars.MetricsEnabled);
var enabled = Cfg.GetCVar(CVars.MetricsEnabled);
_entitySystemManager.MetricsEnabled = enabled;

if (!enabled)
{
return;
}

var host = _cfg.GetCVar(CVars.MetricsHost);
var port = _cfg.GetCVar(CVars.MetricsPort);
var host = Cfg.GetCVar(CVars.MetricsHost);
var port = Cfg.GetCVar(CVars.MetricsPort);

_sawmill.Info("Prometheus metrics enabled, host: {1} port: {0}", port, host);
var sawmill = Logger.GetSawmill("metrics.server");
Expand All @@ -141,7 +143,7 @@ private async void Reload()
beforeCollect: BeforeCollectCallback);
_metricServer.Start();

if (_cfg.GetCVar(CVars.MetricsRuntime))
if (Cfg.GetCVar(CVars.MetricsRuntime))
{
_sawmill.Debug("Enabling runtime metrics");
_runtimeCollector = BuildRuntimeStats().StartCollecting();
Expand All @@ -160,7 +162,7 @@ private DotNetRuntimeStatsBuilder.Builder BuildRuntimeStats()

if (CapLevel(CVars.MetricsRuntimeContention) is { } contention)
{
var rate = _cfg.GetCVar(CVars.MetricsRuntimeContentionSampleRate);
var rate = Cfg.GetCVar(CVars.MetricsRuntimeContentionSampleRate);
builder.WithContentionStats(contention, (SampleEvery)rate);
}

Expand All @@ -174,7 +176,7 @@ private DotNetRuntimeStatsBuilder.Builder BuildRuntimeStats()

if (CapLevel(CVars.MetricsRuntimeJit) is { } jit)
{
var rate = _cfg.GetCVar(CVars.MetricsRuntimeJitSampleRate);
var rate = Cfg.GetCVar(CVars.MetricsRuntimeJitSampleRate);
builder.WithJitStats(jit, (SampleEvery) rate);
}

Expand All @@ -190,7 +192,7 @@ private DotNetRuntimeStatsBuilder.Builder BuildRuntimeStats()

CaptureLevel? CapLevel(CVarDef<string> cvar)
{
var val = _cfg.GetCVar(cvar);
var val = Cfg.GetCVar(cvar);
if (val != "")
return Enum.Parse<CaptureLevel>(val);

Expand All @@ -200,7 +202,7 @@ private DotNetRuntimeStatsBuilder.Builder BuildRuntimeStats()
// 🪣
double[] Buckets(CVarDef<string> cvar, double divide=1)
{
return _cfg.GetCVar(cvar)
return Cfg.GetCVar(cvar)
.Split(',')
.Select(x => double.Parse(x, CultureInfo.InvariantCulture) / divide)
.ToArray();
Expand Down
10 changes: 10 additions & 0 deletions Robust.Shared/CVars.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics.Metrics;
using System.Threading;
using Lidgren.Network;
using Robust.Shared.Audio;
Expand Down Expand Up @@ -540,6 +541,15 @@ protected CVars()
public static readonly CVarDef<float> MetricsUpdateInterval =
CVarDef.Create("metrics.update_interval", 0f, CVar.SERVERONLY);

/// <summary>
/// If set, an "<c>instance</c>" tag will be added to all metrics created via <see cref="IMeterFactory"/>.
/// </summary>
/// <remarks>
/// This is set by integration tests to distinguish instances.
/// </remarks>
public static readonly CVarDef<string> MetricsInstanceName =
CVarDef.Create("metrics.instance_name", "");

/// <summary>
/// Enable detailed runtime metrics. Empty to disable.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,34 @@
using System.Collections.Generic;
using System.Diagnostics.Metrics;
using System.Linq;
using System.Threading;
using Robust.Shared.Configuration;
using Robust.Shared.IoC;
using Robust.Shared.Utility;

namespace Robust.Server.DataMetrics;
namespace Robust.Shared.DataMetrics;

internal sealed partial class MetricsManager : IMeterFactory
internal interface IMeterFactoryInternal : IMeterFactory
{
void Initialize();
}

[Virtual]
internal class MeterFactory : IMeterFactoryInternal
{
[Dependency] protected readonly IConfigurationManager Cfg = null!;

private readonly Dictionary<string, List<CachedMeter>> _meterCache = new();
private readonly object _meterCacheLock = new();
private readonly Lock _meterCacheLock = new();

private string? _instanceName;

public virtual void Initialize()
{
_instanceName = Cfg.GetCVar(CVars.MetricsInstanceName);
if (string.IsNullOrEmpty(_instanceName))
_instanceName = null;
}

Meter IMeterFactory.Create(MeterOptions options)
{
Expand All @@ -21,9 +41,21 @@ Meter IMeterFactory.Create(MeterOptions options)
if (LockedFindCachedMeter(options) is { } cached)
return cached.Meter;

var meter = new Meter(options.Name, options.Version, options.Tags, this);
var tags = options.Tags;
if (_instanceName != null)
{
tags =
[
..options.Tags ?? [],
new KeyValuePair<string, object?>("instance", _instanceName)
];
}

// ReSharper disable once PossibleMultipleEnumeration
var meter = new Meter(options.Name, options.Version, tags, this);
var meterList = _meterCache.GetOrNew(options.Name);
meterList.Add(new CachedMeter(options.Version, TagsToDict(options.Tags), meter));
// ReSharper disable once PossibleMultipleEnumeration
meterList.Add(new CachedMeter(options.Version, TagsToDict(tags), meter));
return meter;
}
}
Expand Down Expand Up @@ -66,7 +98,12 @@ private static bool TagsMatch(Dictionary<string, object?> a, Dictionary<string,
return tags?.ToDictionary() ?? [];
}

private void DisposeMeters()
void IDisposable.Dispose()
{
Dispose();
}

protected virtual void Dispose()
{
lock (_meterCacheLock)
{
Expand Down
15 changes: 15 additions & 0 deletions Robust.Shared/GameObjects/EntityManager.Metrics.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Diagnostics.Metrics;
using Robust.Shared.IoC;

namespace Robust.Shared.GameObjects;

public abstract partial class EntityManager
{
[Dependency] private readonly IMeterFactory _meterFactory = null!;

private void InitMetrics()
{
var meter = _meterFactory.Create("Robust.EntityManager");
meter.CreateObservableUpDownCounter("entity_count", () => Entities.Count);
}
}
2 changes: 2 additions & 0 deletions Robust.Shared/GameObjects/EntityManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ public virtual void Initialize()
if (Initialized)
throw new InvalidOperationException("Initialize() called multiple times");

InitMetrics();

EventBusInternal = new EntityEventBus(this, _reflection);

InitializeComponents();
Expand Down
Loading
Loading