Started implementing metrics system
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
using System.Diagnostics.Metrics;
|
||||
using Moonlight.ApiServer.Interfaces;
|
||||
using Moonlight.ApiServer.Services;
|
||||
|
||||
namespace Moonlight.ApiServer.Implementations.Metrics;
|
||||
|
||||
public class ApplicationMetric : IMetric
|
||||
{
|
||||
private Gauge<long> MemoryUsage;
|
||||
private Gauge<int> CpuUsage;
|
||||
private Gauge<double> Uptime;
|
||||
|
||||
public Task Initialize(Meter meter)
|
||||
{
|
||||
MemoryUsage = meter.CreateGauge<long>("moonlight_memory_usage");
|
||||
CpuUsage = meter.CreateGauge<int>("moonlight_cpu_usage");
|
||||
Uptime = meter.CreateGauge<double>("moonlight_uptime");
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public async Task Run(IServiceProvider provider, CancellationToken cancellationToken)
|
||||
{
|
||||
var applicationService = provider.GetRequiredService<ApplicationService>();
|
||||
|
||||
var memory = await applicationService.GetMemoryUsage();
|
||||
MemoryUsage.Record(memory);
|
||||
|
||||
var uptime = await applicationService.GetUptime();
|
||||
Uptime.Record(uptime.TotalSeconds);
|
||||
|
||||
var cpu = await applicationService.GetCpuUsage();
|
||||
CpuUsage.Record(cpu);
|
||||
}
|
||||
}
|
||||
27
Moonlight.ApiServer/Implementations/Metrics/UsersMetric.cs
Normal file
27
Moonlight.ApiServer/Implementations/Metrics/UsersMetric.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using System.Diagnostics.Metrics;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MoonCore.Extended.Abstractions;
|
||||
using Moonlight.ApiServer.Database.Entities;
|
||||
using Moonlight.ApiServer.Interfaces;
|
||||
|
||||
namespace Moonlight.ApiServer.Implementations.Metrics;
|
||||
|
||||
public class UsersMetric : IMetric
|
||||
{
|
||||
private Gauge<int> Users;
|
||||
|
||||
public Task Initialize(Meter meter)
|
||||
{
|
||||
Users = meter.CreateGauge<int>("moonlight_users");
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public async Task Run(IServiceProvider provider, CancellationToken cancellationToken)
|
||||
{
|
||||
var usersRepo = provider.GetRequiredService<DatabaseRepository<User>>();
|
||||
var count = await usersRepo.Get().CountAsync(cancellationToken: cancellationToken);
|
||||
|
||||
Users.Record(count);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user