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);
|
||||
}
|
||||
}
|
||||
@@ -2,8 +2,12 @@ using Microsoft.OpenApi.Models;
|
||||
using Moonlight.ApiServer.Configuration;
|
||||
using Moonlight.ApiServer.Database;
|
||||
using Moonlight.ApiServer.Implementations.Diagnose;
|
||||
using Moonlight.ApiServer.Implementations.Metrics;
|
||||
using Moonlight.ApiServer.Interfaces;
|
||||
using Moonlight.ApiServer.Plugins;
|
||||
using Moonlight.ApiServer.Services;
|
||||
using OpenTelemetry.Metrics;
|
||||
using OpenTelemetry.Trace;
|
||||
|
||||
namespace Moonlight.ApiServer.Implementations.Startup;
|
||||
|
||||
@@ -13,13 +17,13 @@ public class CoreStartup : IPluginStartup
|
||||
public Task BuildApplication(IServiceProvider serviceProvider, IHostApplicationBuilder builder)
|
||||
{
|
||||
var configuration = serviceProvider.GetRequiredService<AppConfiguration>();
|
||||
|
||||
|
||||
#region Api Docs
|
||||
|
||||
if (configuration.Development.EnableApiDocs)
|
||||
{
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
|
||||
|
||||
// Configure swagger api specification generator and set the document title for the api docs to use
|
||||
builder.Services.AddSwaggerGen(options =>
|
||||
{
|
||||
@@ -27,9 +31,9 @@ public class CoreStartup : IPluginStartup
|
||||
{
|
||||
Title = "Moonlight API"
|
||||
});
|
||||
|
||||
|
||||
options.CustomSchemaIds(x => x.FullName);
|
||||
|
||||
|
||||
options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
|
||||
{
|
||||
Name = "Authorization",
|
||||
@@ -54,22 +58,53 @@ public class CoreStartup : IPluginStartup
|
||||
builder.Services.AddSingleton<IDiagnoseProvider, LogsDiagnoseProvider>();
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region Prometheus
|
||||
|
||||
if (configuration.Metrics.Enable)
|
||||
{
|
||||
builder.Services.AddSingleton<MetricsBackgroundService>();
|
||||
builder.Services.AddHostedService(sp => sp.GetRequiredService<MetricsBackgroundService>());
|
||||
|
||||
builder.Services.AddSingleton<IMetric, ApplicationMetric>();
|
||||
builder.Services.AddSingleton<IMetric, UsersMetric>();
|
||||
|
||||
builder.Services.AddOpenTelemetry()
|
||||
.WithMetrics(providerBuilder =>
|
||||
{
|
||||
providerBuilder.AddPrometheusExporter();
|
||||
providerBuilder.AddAspNetCoreInstrumentation();
|
||||
|
||||
providerBuilder.AddMeter("moonlight");
|
||||
});
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task ConfigureApplication(IServiceProvider serviceProvider, IApplicationBuilder app)
|
||||
{
|
||||
var configuration = serviceProvider.GetRequiredService<AppConfiguration>();
|
||||
|
||||
#region Prometheus
|
||||
|
||||
if(configuration.Metrics.Enable)
|
||||
app.UseOpenTelemetryPrometheusScrapingEndpoint();
|
||||
|
||||
#endregion
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task ConfigureEndpoints(IServiceProvider serviceProvider, IEndpointRouteBuilder routeBuilder)
|
||||
{
|
||||
var configuration = serviceProvider.GetRequiredService<AppConfiguration>();
|
||||
|
||||
if(configuration.Development.EnableApiDocs)
|
||||
|
||||
if (configuration.Development.EnableApiDocs)
|
||||
routeBuilder.MapSwagger("/api/swagger/{documentName}");
|
||||
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user