AdminManager.Metrics.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System.Diagnostics.Metrics;
  2. using System.Runtime.InteropServices;
  3. using Content.Server.Afk;
  4. using Robust.Server.DataMetrics;
  5. namespace Content.Server.Administration.Managers;
  6. // Handles metrics reporting for active admin count and such.
  7. public sealed partial class AdminManager
  8. {
  9. private Dictionary<int, (int active, int afk, int deadminned)>? _adminOnlineCounts;
  10. private const int SentinelRankId = -1;
  11. [Dependency] private readonly IMetricsManager _metrics = default!;
  12. [Dependency] private readonly IAfkManager _afkManager = default!;
  13. [Dependency] private readonly IMeterFactory _meterFactory = default!;
  14. private void InitializeMetrics()
  15. {
  16. _metrics.UpdateMetrics += MetricsOnUpdateMetrics;
  17. var meter = _meterFactory.Create("SS14.AdminManager");
  18. meter.CreateObservableGauge(
  19. "admins_online_count",
  20. MeasureAdminCount,
  21. null,
  22. "The count of online admins");
  23. }
  24. private void MetricsOnUpdateMetrics()
  25. {
  26. _sawmill.Verbose("Updating metrics");
  27. var dict = new Dictionary<int, (int active, int afk, int deadminned)>();
  28. foreach (var (session, reg) in _admins)
  29. {
  30. var rankId = reg.RankId ?? SentinelRankId;
  31. ref var counts = ref CollectionsMarshal.GetValueRefOrAddDefault(dict, rankId, out _);
  32. if (reg.Data.Active)
  33. {
  34. if (_afkManager.IsAfk(session))
  35. counts.afk += 1;
  36. else
  37. counts.active += 1;
  38. }
  39. else
  40. {
  41. counts.deadminned += 1;
  42. }
  43. }
  44. // Neither prometheus-net nor dotnet-counters seem to handle stuff well if we STOP returning measurements.
  45. // i.e. if the last admin with a rank disconnects.
  46. // So if we have EVER reported a rank, always keep reporting it.
  47. if (_adminOnlineCounts != null)
  48. {
  49. foreach (var rank in _adminOnlineCounts.Keys)
  50. {
  51. CollectionsMarshal.GetValueRefOrAddDefault(dict, rank, out _);
  52. }
  53. }
  54. // Make sure "no rank" is always available. Avoid "no data".
  55. CollectionsMarshal.GetValueRefOrAddDefault(dict, SentinelRankId, out _);
  56. _adminOnlineCounts = dict;
  57. }
  58. private IEnumerable<Measurement<int>> MeasureAdminCount()
  59. {
  60. if (_adminOnlineCounts == null)
  61. yield break;
  62. foreach (var (rank, (active, afk, deadminned)) in _adminOnlineCounts)
  63. {
  64. yield return new Measurement<int>(
  65. active,
  66. new KeyValuePair<string, object?>("state", "active"),
  67. new KeyValuePair<string, object?>("rank", rank == SentinelRankId ? "none" : rank.ToString()));
  68. yield return new Measurement<int>(
  69. afk,
  70. new KeyValuePair<string, object?>("state", "afk"),
  71. new KeyValuePair<string, object?>("rank", rank == SentinelRankId ? "none" : rank.ToString()));
  72. yield return new Measurement<int>(
  73. deadminned,
  74. new KeyValuePair<string, object?>("state", "deadminned"),
  75. new KeyValuePair<string, object?>("rank", rank == SentinelRankId ? "none" : rank.ToString()));
  76. }
  77. }
  78. }