From 496b9293bcc2daecb23e6a54ed7e18686f87c1bb Mon Sep 17 00:00:00 2001 From: "agile.zhou" Date: Sun, 2 Jun 2024 19:46:51 +0800 Subject: [PATCH] Add cpu used metric --- .../AgileConfig.Server.Apisite.csproj | 133 +++++++++--------- .../Metrics/MeterService.cs | 32 ++++- src/AgileConfig.Server.Apisite/Startup.cs | 1 + 3 files changed, 101 insertions(+), 65 deletions(-) diff --git a/src/AgileConfig.Server.Apisite/AgileConfig.Server.Apisite.csproj b/src/AgileConfig.Server.Apisite/AgileConfig.Server.Apisite.csproj index 7c1a8e38..d1739b16 100644 --- a/src/AgileConfig.Server.Apisite/AgileConfig.Server.Apisite.csproj +++ b/src/AgileConfig.Server.Apisite/AgileConfig.Server.Apisite.csproj @@ -1,76 +1,81 @@  - - net8.0 - InProcess - 1.9.3 - 1.9.3 - 1.9.2 - Linux - 1.9.3 - kklldog - kklldog - + + net8.0 + InProcess + 1.9.3 + 1.9.3 + 1.9.2 + Linux + 1.9.3 + kklldog + kklldog + - - bin\Debug\AgileConfig.Server.Apisite.xml - 1701;1702;1591;1573 - + + bin\Debug\AgileConfig.Server.Apisite.xml + 1701;1702;1591;1573 + - - bin\Release\AgileConfig.Server.Apisite.xml - + + bin\Release\AgileConfig.Server.Apisite.xml + - - - + + + - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - + + + + + + + + + + + + + - - - Always - - - PreserveNewest - - + + + Always + + + PreserveNewest + + - - - - - + + + + + - + + + + + diff --git a/src/AgileConfig.Server.Apisite/Metrics/MeterService.cs b/src/AgileConfig.Server.Apisite/Metrics/MeterService.cs index 706b7133..de7438da 100644 --- a/src/AgileConfig.Server.Apisite/Metrics/MeterService.cs +++ b/src/AgileConfig.Server.Apisite/Metrics/MeterService.cs @@ -1,6 +1,9 @@ using AgileConfig.Server.Data.Entity; using AgileConfig.Server.IService; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Diagnostics.ResourceMonitoring; +using System; +using System.Diagnostics; using System.Diagnostics.Metrics; using System.Threading.Tasks; @@ -17,6 +20,8 @@ public class MeterService : IMeterService public ObservableGauge ServiceGauge { get; } public ObservableGauge ClientGauge { get; } public ObservableGauge NodeGauge { get; } + public ObservableGauge MemoryUsedGauge { get; } + public ObservableGauge CpuUsedGauge { get; } public Counter PullAppConfigCounter { get; } @@ -25,14 +30,17 @@ public class MeterService : IMeterService private readonly IServerNodeService _serverNodeService; private readonly IRemoteServerNodeProxy _remoteServer; private readonly IServiceInfoService _serviceInfoService; + private readonly IResourceMonitor _resourceMonitor; private int _appCount = 0; private int _configCount = 0; private int _clientCount = 0; private int _serverNodeCount = 0; private int _serviceCount = 0; + private long _memoryUsed = 0; + private double _cpuUsed = 0; - private const int _checkInterval = 30; + private const int _checkInterval = 5; static MeterService() { @@ -47,27 +55,43 @@ public MeterService(IServiceScopeFactory sf) _serverNodeService = sp.GetService(); _remoteServer = sp.GetService(); _serviceInfoService = sp.GetService(); + _resourceMonitor = sp.GetService(); AppGauge = AgileConfigMeter.CreateObservableGauge("AppCount", () => { return _appCount; }, "", "The number of enabled apps"); + ConfigGauge = AgileConfigMeter.CreateObservableGauge("ConfigCount", () => { return _configCount; }, "", "The number of enabled configuration items"); + ServiceGauge = AgileConfigMeter.CreateObservableGauge("ServiceCount", () => { return _serviceCount; }, "", "The number of registered services"); + ClientGauge = AgileConfigMeter.CreateObservableGauge("ClientCount", () => { return _clientCount; }, "", "The number of connected clients"); + NodeGauge = AgileConfigMeter.CreateObservableGauge("NodeCount", () => { return _serverNodeCount; }, "", "The number of nodes"); + + MemoryUsedGauge = AgileConfigMeter.CreateObservableGauge("MemoryUsed", () => + { + return _memoryUsed; + }, "MB", "Memory used (MB)"); + + CpuUsedGauge = AgileConfigMeter.CreateObservableGauge("CpuUsed", () => + { + return _cpuUsed; + }, "%", "CPU used percent"); + PullAppConfigCounter = AgileConfigMeter.CreateCounter("PullAppConfigCounter", "", "The number of times the app configuration was pulled"); } @@ -104,6 +128,12 @@ private Task StartCheckAppCountAsync() } } _clientCount = clientCount; + + // memory and cpu + var window = TimeSpan.FromSeconds(3); + var utilization = _resourceMonitor.GetUtilization(window); + _memoryUsed = (long)utilization.MemoryUsedInBytes / 1024 / 1024; + _cpuUsed = utilization.CpuUsedPercentage; } catch { diff --git a/src/AgileConfig.Server.Apisite/Startup.cs b/src/AgileConfig.Server.Apisite/Startup.cs index 3b2ed44a..33000c34 100644 --- a/src/AgileConfig.Server.Apisite/Startup.cs +++ b/src/AgileConfig.Server.Apisite/Startup.cs @@ -92,6 +92,7 @@ public void ConfigureServices(IServiceCollection services) services.AddOIDC(); + services.AddResourceMonitoring(); services.AddSingleton(); services.AddOpenTelemetry()