rework metrics

This commit is contained in:
Stanley Dimant
2022-08-26 02:22:19 +02:00
parent 7c1395df77
commit ace31926db
21 changed files with 179 additions and 244 deletions

View File

@@ -55,12 +55,9 @@ namespace MareSynchronosServer.Hubs
}, userEntry.CharacterIdentification).ConfigureAwait(false);
}
await _metricsClient.DecGaugeAsync(new GaugeRequest()
{ GaugeName = MetricsAPI.GaugePairs, Value = ownPairData.Count + otherPairData.Count }).ConfigureAwait(false);
await _metricsClient.DecGaugeAsync(new GaugeRequest()
{ GaugeName = MetricsAPI.GaugePairsPaused, Value = ownPairData.Count(c => c.IsPaused) }).ConfigureAwait(false);
await _metricsClient.DecGaugeAsync(new GaugeRequest()
{ GaugeName = MetricsAPI.GaugeUsersRegistered, Value = 1 }).ConfigureAwait(false);
_mareMetrics.DecGauge(MetricsAPI.GaugePairs, ownPairData.Count + otherPairData.Count);
_mareMetrics.DecGauge(MetricsAPI.GaugePairsPaused, ownPairData.Count(c => c.IsPaused));
_mareMetrics.IncCounter(MetricsAPI.CounterUsersRegisteredDeleted, 1);
_dbContext.RemoveRange(otherPairData);
_dbContext.Remove(userEntry);
@@ -162,17 +159,15 @@ namespace MareSynchronosServer.Hubs
await Clients.Users(otherEntries).SendAsync(Api.OnUserReceiveCharacterData, characterCache, user.CharacterIdentification).ConfigureAwait(false);
await _metricsClient.IncreaseCounterAsync(new IncreaseCounterRequest()
{ CounterName = MetricsAPI.CounterUserPushData, Value = 1 }).ConfigureAwait(false);
await _metricsClient.IncreaseCounterAsync(new IncreaseCounterRequest()
{ CounterName = MetricsAPI.CounterUserPushDataTo, Value = otherEntries.Count }).ConfigureAwait(false);
_mareMetrics.IncCounter(MetricsAPI.CounterUserPushData);
_mareMetrics.IncCounter(MetricsAPI.CounterUserPushDataTo, otherEntries.Count);
}
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
[HubMethodName(Api.SendUserPairedClientAddition)]
public async Task SendPairedClientAddition(string uid)
{
if (uid == AuthenticatedUserId) return;
if (uid == AuthenticatedUserId || string.IsNullOrWhiteSpace(uid)) return;
uid = uid.Trim();
var user = await _dbContext.Users.SingleAsync(u => u.UID == AuthenticatedUserId).ConfigureAwait(false);
@@ -223,7 +218,7 @@ namespace MareSynchronosServer.Hubs
}
}
await _metricsClient.IncGaugeAsync(new GaugeRequest() {GaugeName = MetricsAPI.GaugePairs, Value = 1}).ConfigureAwait(false);
_mareMetrics.IncGauge(MetricsAPI.GaugePairs);
}
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
@@ -263,11 +258,11 @@ namespace MareSynchronosServer.Hubs
if (isPaused)
{
await _metricsClient.IncGaugeAsync(new GaugeRequest() { GaugeName = MetricsAPI.GaugePairsPaused, Value = 1 }).ConfigureAwait(false);
_mareMetrics.IncGauge(MetricsAPI.GaugePairsPaused);
}
else
{
await _metricsClient.DecGaugeAsync(new GaugeRequest() { GaugeName = MetricsAPI.GaugePairsPaused, Value = 1 }).ConfigureAwait(false);
_mareMetrics.DecGauge(MetricsAPI.GaugePairsPaused);
}
}
@@ -311,7 +306,7 @@ namespace MareSynchronosServer.Hubs
}
}
await _metricsClient.DecGaugeAsync(new GaugeRequest() { GaugeName = MetricsAPI.GaugePairs, Value = 1 }).ConfigureAwait(false);
_mareMetrics.DecGauge(MetricsAPI.GaugePairs);
}
private ClientPair OppositeEntry(string otherUID) =>

View File

@@ -19,7 +19,7 @@ namespace MareSynchronosServer.Hubs
{
public partial class MareHub : Hub
{
private readonly MetricsService.MetricsServiceClient _metricsClient;
private readonly MareMetrics _mareMetrics;
private readonly AuthService.AuthServiceClient _authServiceClient;
private readonly FileService.FileServiceClient _fileServiceClient;
private readonly SystemInfoService _systemInfoService;
@@ -27,10 +27,10 @@ namespace MareSynchronosServer.Hubs
private readonly ILogger<MareHub> _logger;
private readonly MareDbContext _dbContext;
private readonly Uri cdnFullUri;
public MareHub(MetricsService.MetricsServiceClient metricsClient, AuthService.AuthServiceClient authServiceClient, FileService.FileServiceClient fileServiceClient,
public MareHub(MareMetrics mareMetrics, AuthService.AuthServiceClient authServiceClient, FileService.FileServiceClient fileServiceClient,
MareDbContext mareDbContext, ILogger<MareHub> logger, SystemInfoService systemInfoService, IConfiguration configuration, IHttpContextAccessor contextAccessor)
{
_metricsClient = metricsClient;
_mareMetrics = mareMetrics;
_authServiceClient = authServiceClient;
_fileServiceClient = fileServiceClient;
_systemInfoService = systemInfoService;
@@ -44,7 +44,7 @@ namespace MareSynchronosServer.Hubs
[Authorize(AuthenticationSchemes = SecretKeyGrpcAuthenticationHandler.AuthScheme)]
public async Task<ConnectionDto> Heartbeat(string characterIdentification)
{
await _metricsClient.IncreaseCounterAsync(new() { CounterName = MetricsAPI.CounterInitializedConnections, Value = 1 }).ConfigureAwait(false);
_mareMetrics.IncCounter(MetricsAPI.CounterInitializedConnections);
var userId = Context.User!.Claims.SingleOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value;
@@ -66,7 +66,7 @@ namespace MareSynchronosServer.Hubs
}
else if (string.IsNullOrEmpty(user.CharacterIdentification))
{
await _metricsClient.IncGaugeAsync(new GaugeRequest() { GaugeName = MetricsAPI.GaugeAuthorizedConnections, Value = 1 }).ConfigureAwait(false);
_mareMetrics.IncGauge(MetricsAPI.GaugeAuthorizedConnections);
}
user.LastLoggedIn = DateTime.UtcNow;
@@ -91,18 +91,18 @@ namespace MareSynchronosServer.Hubs
public override async Task OnConnectedAsync()
{
_logger.LogInformation("Connection from {ip}", contextAccessor.GetIpAddress());
await _metricsClient.IncGaugeAsync(new GaugeRequest() { GaugeName = MetricsAPI.GaugeConnections, Value = 1 }).ConfigureAwait(false);
_mareMetrics.IncGauge(MetricsAPI.GaugeConnections);
await base.OnConnectedAsync().ConfigureAwait(false);
}
public override async Task OnDisconnectedAsync(Exception exception)
{
await _metricsClient.DecGaugeAsync(new GaugeRequest() { GaugeName = MetricsAPI.GaugeConnections, Value = 1 }).ConfigureAwait(false);
_mareMetrics.DecGauge(MetricsAPI.GaugeConnections);
var user = await _dbContext.Users.SingleOrDefaultAsync(u => u.UID == AuthenticatedUserId).ConfigureAwait(false);
if (user != null && !string.IsNullOrEmpty(user.CharacterIdentification))
{
await _metricsClient.DecGaugeAsync(new GaugeRequest() { GaugeName = MetricsAPI.GaugeAuthorizedConnections, Value = 1 }).ConfigureAwait(false);
_mareMetrics.DecGauge(MetricsAPI.GaugeAuthorizedConnections);
_logger.LogInformation("Disconnect from {id}", AuthenticatedUserId);

View File

@@ -29,6 +29,7 @@
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="6.0.8" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="6.0.8" />
<PackageReference Include="Microsoft.Extensions.Hosting.Systemd" Version="6.0.0" />
<PackageReference Include="prometheus-net.AspNetCore" Version="6.0.0" />
</ItemGroup>
<ItemGroup>

View File

@@ -6,8 +6,8 @@ using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using MareSynchronosShared.Data;
using MareSynchronosShared.Metrics;
namespace MareSynchronosServer
{
@@ -36,6 +36,11 @@ namespace MareSynchronosServer
context.RemoveRange(unfinishedRegistrations);
context.RemoveRange(looseFiles);
context.SaveChanges();
var metrics = services.GetRequiredService<MareMetrics>();
metrics.SetGaugeTo(MetricsAPI.GaugePairs, context.ClientPairs.Count());
metrics.SetGaugeTo(MetricsAPI.GaugePairsPaused, context.ClientPairs.Count(p => p.IsPaused));
}
if (args.Length == 0 || args[0] != "dry")

View File

@@ -16,6 +16,9 @@ using MareSynchronosShared.Authentication;
using MareSynchronosShared.Data;
using MareSynchronosShared.Protos;
using Grpc.Net.Client.Configuration;
using Prometheus;
using MareSynchronosShared.Metrics;
using System.Collections.Generic;
namespace MareSynchronosServer
{
@@ -60,6 +63,22 @@ namespace MareSynchronosServer
}
};
services.AddSingleton(new MareMetrics(new List<string>
{
MetricsAPI.CounterInitializedConnections,
MetricsAPI.CounterUserPushData,
MetricsAPI.CounterUserPushDataTo,
MetricsAPI.CounterUsersRegisteredDeleted,
}, new List<string>
{
MetricsAPI.GaugeAuthorizedConnections,
MetricsAPI.GaugeConnections,
MetricsAPI.GaugePairs,
MetricsAPI.GaugePairsPaused,
MetricsAPI.GaugeAvailableIOWorkerThreads,
MetricsAPI.GaugeAvailableWorkerThreads
}));
services.AddGrpcClient<AuthService.AuthServiceClient>(c =>
{
c.Address = new Uri(mareConfig.GetValue<string>("ServiceAddress"));
@@ -67,13 +86,6 @@ namespace MareSynchronosServer
{
c.ServiceConfig = new ServiceConfig { MethodConfigs = { defaultMethodConfig } };
});
services.AddGrpcClient<MetricsService.MetricsServiceClient>(c =>
{
c.Address = new Uri(mareConfig.GetValue<string>("ServiceAddress"));
}).ConfigureChannel(c =>
{
c.ServiceConfig = new ServiceConfig { MethodConfigs = { defaultMethodConfig } };
});
services.AddGrpcClient<FileService.FileServiceClient>(c =>
{
c.Address = new Uri(mareConfig.GetValue<string>("StaticFileServiceAddress"));
@@ -134,6 +146,9 @@ namespace MareSynchronosServer
app.UseWebSockets();
var metricServer = new KestrelMetricServer(4981);
metricServer.Start();
app.UseAuthentication();
app.UseAuthorization();

View File

@@ -16,16 +16,16 @@ namespace MareSynchronosServer;
public class SystemInfoService : IHostedService, IDisposable
{
private readonly MetricsService.MetricsServiceClient metricsClient;
private readonly MareMetrics _mareMetrics;
private readonly IServiceProvider _services;
private readonly ILogger<SystemInfoService> _logger;
private readonly IHubContext<MareHub> _hubContext;
private Timer _timer;
public SystemInfoDto SystemInfoDto { get; private set; } = new();
public SystemInfoService(MetricsService.MetricsServiceClient metricsClient, IServiceProvider services, ILogger<SystemInfoService> logger, IHubContext<MareHub> hubContext)
public SystemInfoService(MareMetrics mareMetrics, IServiceProvider services, ILogger<SystemInfoService> logger, IHubContext<MareHub> hubContext)
{
this.metricsClient = metricsClient;
_mareMetrics = mareMetrics;
_services = services;
_logger = logger;
_hubContext = hubContext;
@@ -48,10 +48,8 @@ public class SystemInfoService : IHostedService, IDisposable
using var scope = _services.CreateScope();
using var db = scope.ServiceProvider.GetService<MareDbContext>()!;
metricsClient.SetGauge(new SetGaugeRequest()
{ GaugeName = MetricsAPI.GaugeAvailableWorkerThreads, Value = workerThreads });
metricsClient.SetGauge(new SetGaugeRequest()
{ GaugeName = MetricsAPI.GaugeAvailableIOWorkerThreads, Value = ioThreads });
_mareMetrics.SetGaugeTo(MetricsAPI.GaugeAvailableWorkerThreads, workerThreads);
_mareMetrics.SetGaugeTo(MetricsAPI.GaugeAvailableIOWorkerThreads, ioThreads);
var users = db.Users.Count(c => c.CharacterIdentification != null);