Skip to content

Commit

Permalink
Add cpu used metric
Browse files Browse the repository at this point in the history
  • Loading branch information
agile.zhou committed Jun 2, 2024
1 parent ac1f022 commit 496b929
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 65 deletions.
133 changes: 69 additions & 64 deletions src/AgileConfig.Server.Apisite/AgileConfig.Server.Apisite.csproj
Original file line number Diff line number Diff line change
@@ -1,76 +1,81 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
<AssemblyVersion>1.9.3</AssemblyVersion>
<Version>1.9.3</Version>
<PackageVersion>1.9.2</PackageVersion>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<FileVersion>1.9.3</FileVersion>
<Authors>kklldog</Authors>
<Company>kklldog</Company>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
<AssemblyVersion>1.9.3</AssemblyVersion>
<Version>1.9.3</Version>
<PackageVersion>1.9.2</PackageVersion>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<FileVersion>1.9.3</FileVersion>
<Authors>kklldog</Authors>
<Company>kklldog</Company>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DocumentationFile>bin\Debug\AgileConfig.Server.Apisite.xml</DocumentationFile>
<NoWarn>1701;1702;1591;1573</NoWarn>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DocumentationFile>bin\Debug\AgileConfig.Server.Apisite.xml</DocumentationFile>
<NoWarn>1701;1702;1591;1573</NoWarn>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<DocumentationFile>bin\Release\AgileConfig.Server.Apisite.xml</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<DocumentationFile>bin\Release\AgileConfig.Server.Apisite.xml</DocumentationFile>
</PropertyGroup>

<ItemGroup>
<None Remove="agile_config.db" />
</ItemGroup>
<ItemGroup>
<None Remove="agile_config.db" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.5" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="8.0.0" />
<PackageReference Include="Npgsql.OpenTelemetry" Version="8.0.3" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.8.1" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.8.1" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.8.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.8.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.8.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.8.0" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.5.0" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.5.0" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.5" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.ResourceMonitoring" Version="8.5.0" />
<PackageReference Include="Npgsql.OpenTelemetry" Version="8.0.3" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.8.1" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.8.1" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.8.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.8.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.8.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.8.0" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.5.0" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.5.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\AgileConfig.Server.Common\AgileConfig.Server.Common.csproj" />
<ProjectReference Include="..\AgileConfig.Server.Data.Abstraction\AgileConfig.Server.Data.Abstraction.csproj" />
<ProjectReference Include="..\AgileConfig.Server.Data.Repository.Freesql\AgileConfig.Server.Data.Repository.Freesql.csproj" />
<ProjectReference Include="..\AgileConfig.Server.Data.Repository.Mongodb\AgileConfig.Server.Data.Repository.Mongodb.csproj" />
<ProjectReference Include="..\Agile.Config.Protocol\Agile.Config.Protocol.csproj" />
<ProjectReference Include="..\AgileConfig.Server.Data.Entity\AgileConfig.Server.Data.Entity.csproj" />
<ProjectReference Include="..\AgileConfig.Server.Data.Freesql\AgileConfig.Server.Data.Freesql.csproj" />
<ProjectReference Include="..\AgileConfig.Server.Data.Repository.Selector\AgileConfig.Server.Data.Repository.Selector.csproj" />
<ProjectReference Include="..\AgileConfig.Server.Event\AgileConfig.Server.Event.csproj" />
<ProjectReference Include="..\AgileConfig.Server.OIDC\AgileConfig.Server.OIDC.csproj" />
<ProjectReference Include="..\AgileConfig.Server.Service\AgileConfig.Server.Service.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AgileConfig.Server.Common\AgileConfig.Server.Common.csproj" />
<ProjectReference Include="..\AgileConfig.Server.Data.Abstraction\AgileConfig.Server.Data.Abstraction.csproj" />
<ProjectReference Include="..\AgileConfig.Server.Data.Repository.Freesql\AgileConfig.Server.Data.Repository.Freesql.csproj" />
<ProjectReference Include="..\AgileConfig.Server.Data.Repository.Mongodb\AgileConfig.Server.Data.Repository.Mongodb.csproj" />
<ProjectReference Include="..\Agile.Config.Protocol\Agile.Config.Protocol.csproj" />
<ProjectReference Include="..\AgileConfig.Server.Data.Entity\AgileConfig.Server.Data.Entity.csproj" />
<ProjectReference Include="..\AgileConfig.Server.Data.Freesql\AgileConfig.Server.Data.Freesql.csproj" />
<ProjectReference Include="..\AgileConfig.Server.Data.Repository.Selector\AgileConfig.Server.Data.Repository.Selector.csproj" />
<ProjectReference Include="..\AgileConfig.Server.Event\AgileConfig.Server.Event.csproj" />
<ProjectReference Include="..\AgileConfig.Server.OIDC\AgileConfig.Server.OIDC.csproj" />
<ProjectReference Include="..\AgileConfig.Server.Service\AgileConfig.Server.Service.csproj" />
</ItemGroup>

<ItemGroup>
<Content Update="appsettings.Development.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Update="nlog.config">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<Content Update="appsettings.Development.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Update="nlog.config">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<Folder Include="logs\" />
<Folder Include="Views\Shared" />
<Folder Include="wwwroot\ui\" />
</ItemGroup>
<ItemGroup>
<Folder Include="logs\" />
<Folder Include="Views\Shared" />
<Folder Include="wwwroot\ui\" />
</ItemGroup>

<ProjectExtensions><VisualStudio><UserProperties appsettings_1json__JsonSchema="" /></VisualStudio></ProjectExtensions>
<ProjectExtensions>
<VisualStudio>
<UserProperties appsettings_1json__JsonSchema="" />
</VisualStudio>
</ProjectExtensions>

</Project>
32 changes: 31 additions & 1 deletion src/AgileConfig.Server.Apisite/Metrics/MeterService.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -17,6 +20,8 @@ public class MeterService : IMeterService
public ObservableGauge<int> ServiceGauge { get; }
public ObservableGauge<int> ClientGauge { get; }
public ObservableGauge<int> NodeGauge { get; }
public ObservableGauge<long> MemoryUsedGauge { get; }
public ObservableGauge<double> CpuUsedGauge { get; }

public Counter<long> PullAppConfigCounter { get; }

Expand All @@ -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()
{
Expand All @@ -47,27 +55,43 @@ public MeterService(IServiceScopeFactory sf)
_serverNodeService = sp.GetService<IServerNodeService>();
_remoteServer = sp.GetService<IRemoteServerNodeProxy>();
_serviceInfoService = sp.GetService<IServiceInfoService>();
_resourceMonitor = sp.GetService<IResourceMonitor>();

AppGauge = AgileConfigMeter.CreateObservableGauge<int>("AppCount", () =>
{
return _appCount;
}, "", "The number of enabled apps");

ConfigGauge = AgileConfigMeter.CreateObservableGauge<int>("ConfigCount", () =>
{
return _configCount;
}, "", "The number of enabled configuration items");

ServiceGauge = AgileConfigMeter.CreateObservableGauge<int>("ServiceCount", () =>
{
return _serviceCount;
}, "", "The number of registered services");

ClientGauge = AgileConfigMeter.CreateObservableGauge<int>("ClientCount", () =>
{
return _clientCount;
}, "", "The number of connected clients");

NodeGauge = AgileConfigMeter.CreateObservableGauge<int>("NodeCount", () =>
{
return _serverNodeCount;
}, "", "The number of nodes");

MemoryUsedGauge = AgileConfigMeter.CreateObservableGauge<long>("MemoryUsed", () =>
{
return _memoryUsed;
}, "MB", "Memory used (MB)");

CpuUsedGauge = AgileConfigMeter.CreateObservableGauge<double>("CpuUsed", () =>
{
return _cpuUsed;
}, "%", "CPU used percent");

PullAppConfigCounter = AgileConfigMeter.CreateCounter<long>("PullAppConfigCounter", "", "The number of times the app configuration was pulled");
}

Expand Down Expand Up @@ -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
{
Expand Down
1 change: 1 addition & 0 deletions src/AgileConfig.Server.Apisite/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public void ConfigureServices(IServiceCollection services)

services.AddOIDC();

services.AddResourceMonitoring();
services.AddSingleton<IMeterService, MeterService>();

services.AddOpenTelemetry()
Expand Down

0 comments on commit 496b929

Please sign in to comment.