diff --git a/PluralKit.Bot/Services/PeriodicStatCollector.cs b/PluralKit.Bot/Services/PeriodicStatCollector.cs index a1c98451..0628754c 100644 --- a/PluralKit.Bot/Services/PeriodicStatCollector.cs +++ b/PluralKit.Bot/Services/PeriodicStatCollector.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Linq; +using System.Threading; using System.Threading.Tasks; using App.Metrics; using Discord; @@ -61,9 +62,42 @@ namespace PluralKit.Bot _metrics.Measure.Gauge.SetValue(CoreMetrics.MemberCount, await _members.Count()); _metrics.Measure.Gauge.SetValue(CoreMetrics.SwitchCount, await _switches.Count()); _metrics.Measure.Gauge.SetValue(CoreMetrics.MessageCount, await _messages.Count()); + + // Process info + var process = Process.GetCurrentProcess(); + _metrics.Measure.Gauge.SetValue(CoreMetrics.ProcessPhysicalMemory, process.WorkingSet64); + _metrics.Measure.Gauge.SetValue(CoreMetrics.ProcessVirtualMemory, process.VirtualMemorySize64); + _metrics.Measure.Gauge.SetValue(CoreMetrics.ProcessPrivateMemory, process.PrivateMemorySize64); + _metrics.Measure.Gauge.SetValue(CoreMetrics.ProcessThreads, process.Threads.Count); + _metrics.Measure.Gauge.SetValue(CoreMetrics.ProcessHandles, process.HandleCount); + _metrics.Measure.Gauge.SetValue(CoreMetrics.CpuUsage, await EstimateCpuUsage()); stopwatch.Stop(); _logger.Information("Updated metrics in {Time}", stopwatch.ElapsedDuration()); } + + private async Task EstimateCpuUsage() + { + // We get the current processor time, wait 5 seconds, then compare + // https://medium.com/@jackwild/getting-cpu-usage-in-net-core-7ef825831b8b + + _logger.Information("Estimating CPU usage..."); + var stopwatch = new Stopwatch(); + + stopwatch.Start(); + var cpuTimeBefore = Process.GetCurrentProcess().TotalProcessorTime; + + await Task.Delay(5000); + + stopwatch.Stop(); + var cpuTimeAfter = Process.GetCurrentProcess().TotalProcessorTime; + + var cpuTimePassed = cpuTimeAfter - cpuTimeBefore; + var timePassed = stopwatch.Elapsed; + + var percent = cpuTimePassed / timePassed; + _logger.Information("CPU usage measured as {Percent:P}", percent); + return percent; + } } } \ No newline at end of file diff --git a/PluralKit.Core/CoreMetrics.cs b/PluralKit.Core/CoreMetrics.cs index 8c7ae6ef..be35a59a 100644 --- a/PluralKit.Core/CoreMetrics.cs +++ b/PluralKit.Core/CoreMetrics.cs @@ -1,13 +1,21 @@ using App.Metrics; using App.Metrics.Gauge; +using App.Metrics.Meter; namespace PluralKit.Core { public static class CoreMetrics { - public static GaugeOptions SystemCount => new GaugeOptions { Name = "Systems", MeasurementUnit = Unit.Items}; - public static GaugeOptions MemberCount => new GaugeOptions { Name = "Members", MeasurementUnit = Unit.Items }; - public static GaugeOptions MessageCount => new GaugeOptions { Name = "Messages", MeasurementUnit = Unit.Items }; - public static GaugeOptions SwitchCount => new GaugeOptions { Name = "Switches", MeasurementUnit = Unit.Items }; + public static GaugeOptions SystemCount => new GaugeOptions { Name = "Systems", MeasurementUnit = Unit.Items, Context = "Core" }; + public static GaugeOptions MemberCount => new GaugeOptions { Name = "Members", MeasurementUnit = Unit.Items, Context = "Core" }; + public static GaugeOptions MessageCount => new GaugeOptions { Name = "Messages", MeasurementUnit = Unit.Items, Context = "Core" }; + public static GaugeOptions SwitchCount => new GaugeOptions { Name = "Switches", MeasurementUnit = Unit.Items, Context = "Core" }; + + public static GaugeOptions ProcessPhysicalMemory => new GaugeOptions { Name = "Process Physical Memory", MeasurementUnit = Unit.Bytes, Context = "Process" }; + public static GaugeOptions ProcessVirtualMemory => new GaugeOptions { Name = "Process Virtual Memory", MeasurementUnit = Unit.Bytes, Context = "Process" }; + public static GaugeOptions ProcessPrivateMemory => new GaugeOptions { Name = "Process Private Memory", MeasurementUnit = Unit.Bytes, Context = "Process" }; + public static GaugeOptions ProcessThreads => new GaugeOptions { Name = "Process Thread Count", MeasurementUnit = Unit.Threads, Context = "Process" }; + public static GaugeOptions ProcessHandles => new GaugeOptions { Name = "Process Handle Count", MeasurementUnit = Unit.Items, Context = "Process" }; + public static GaugeOptions CpuUsage => new GaugeOptions { Name = "CPU Usage", MeasurementUnit = Unit.Percent, Context = "Process" }; } } \ No newline at end of file