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

@@ -5,7 +5,6 @@ using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using MareSynchronosServices.Metrics;
using MareSynchronosShared.Data;
using MareSynchronosShared.Metrics;
using MareSynchronosShared.Protos;

View File

@@ -1,5 +1,4 @@
using MareSynchronosServices.Authentication;
using MareSynchronosServices.Metrics;
using MareSynchronosShared.Data;
using MareSynchronosShared.Metrics;
using MareSynchronosShared.Models;
@@ -140,9 +139,9 @@ namespace MareSynchronosServices
.Where(u => u.OtherUser.UID == user.UID).ToList();
metrics.DecGaugeBy(MetricsAPI.GaugePairs, ownPairData.Count + otherPairData.Count);
metrics.DecGaugeBy(MetricsAPI.GaugePairsPaused, ownPairData.Count + ownPairData.Count(c => c.IsPaused));
metrics.DecGaugeBy(MetricsAPI.GaugeUsersRegistered, ownPairData.Count + 1);
metrics.DecGauge(MetricsAPI.GaugePairs, ownPairData.Count + otherPairData.Count);
metrics.DecGauge(MetricsAPI.GaugePairsPaused, ownPairData.Count + ownPairData.Count(c => c.IsPaused));
metrics.DecGauge(MetricsAPI.GaugeUsersRegistered, ownPairData.Count + 1);
dbContext.RemoveRange(otherPairData);
dbContext.Remove(auth);

View File

@@ -9,9 +9,7 @@ using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Discord;
using Discord.Rest;
using Discord.WebSocket;
using MareSynchronosServices.Metrics;
using MareSynchronosShared.Data;
using MareSynchronosShared.Metrics;
using MareSynchronosShared.Models;
@@ -262,7 +260,7 @@ public class DiscordBot : IHostedService
logger.LogInformation("User registered: {userUID}", user.UID);
metrics.IncGaugeBy(MetricsAPI.GaugeUsersRegistered, 1);
metrics.IncGauge(MetricsAPI.GaugeUsersRegistered, 1);
lodestoneAuth.StartedAt = null;
lodestoneAuth.User = user;

View File

@@ -1,85 +0,0 @@
using MareSynchronosShared.Data;
using MareSynchronosShared.Metrics;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Prometheus;
using System;
using System.Collections.Generic;
using System.Linq;
namespace MareSynchronosServices.Metrics;
public class MareMetrics
{
public MareMetrics(IServiceProvider services)
{
using var scope = services.CreateScope();
using var dbContext = scope.ServiceProvider.GetService<MareDbContext>();
gauges[MetricsAPI.GaugeUsersRegistered].IncTo(dbContext.Users.Count());
gauges[MetricsAPI.GaugePairs].IncTo(dbContext.ClientPairs.Count());
gauges[MetricsAPI.GaugePairsPaused].IncTo(dbContext.ClientPairs.Count(p => p.IsPaused));
gauges[MetricsAPI.GaugeFilesTotal].IncTo(dbContext.Files.Count());
}
private readonly Dictionary<string, Counter> counters = new()
{
{ MetricsAPI.CounterInitializedConnections, Prometheus.Metrics.CreateCounter(MetricsAPI.CounterInitializedConnections, "Initialized Connections") },
{ MetricsAPI.CounterUserPushData, Prometheus.Metrics.CreateCounter(MetricsAPI.CounterUserPushData, "Users pushing data") },
{ MetricsAPI.CounterUserPushDataTo, Prometheus.Metrics.CreateCounter(MetricsAPI.CounterUserPushDataTo, "Users Receiving Data") },
{ MetricsAPI.CounterAuthenticationRequests, Prometheus.Metrics.CreateCounter(MetricsAPI.CounterAuthenticationRequests, "Authentication Requests") },
{ MetricsAPI.CounterAuthenticationCacheHits, Prometheus.Metrics.CreateCounter(MetricsAPI.CounterAuthenticationCacheHits, "Authentication Requests Cache Hits") },
{ MetricsAPI.CounterAuthenticationFailures, Prometheus.Metrics.CreateCounter(MetricsAPI.CounterAuthenticationFailures, "Authentication Requests Failed") },
{ MetricsAPI.CounterAuthenticationSuccesses, Prometheus.Metrics.CreateCounter(MetricsAPI.CounterAuthenticationSuccesses, "Authentication Requests Success") },
};
private readonly Dictionary<string, Gauge> gauges = new()
{
{ MetricsAPI.GaugeConnections, Prometheus.Metrics.CreateGauge(MetricsAPI.GaugeConnections, "Unauthorized Connections") },
{ MetricsAPI.GaugeAuthorizedConnections, Prometheus.Metrics.CreateGauge(MetricsAPI.GaugeAuthorizedConnections, "Authorized Connections") },
{ MetricsAPI.GaugeAvailableIOWorkerThreads, Prometheus.Metrics.CreateGauge(MetricsAPI.GaugeAvailableIOWorkerThreads, "Available Threadpool IO Workers") },
{ MetricsAPI.GaugeAvailableWorkerThreads, Prometheus.Metrics.CreateGauge(MetricsAPI.GaugeAvailableWorkerThreads, "Aavailable Threadpool Workers") },
{ MetricsAPI.GaugeUsersRegistered, Prometheus.Metrics.CreateGauge(MetricsAPI.GaugeUsersRegistered, "Total Registrations") },
{ MetricsAPI.GaugePairs, Prometheus.Metrics.CreateGauge(MetricsAPI.GaugePairs, "Total Pairs") },
{ MetricsAPI.GaugePairsPaused, Prometheus.Metrics.CreateGauge(MetricsAPI.GaugePairsPaused, "Total Paused Pairs") },
{ MetricsAPI.GaugeFilesTotal, Prometheus.Metrics.CreateGauge(MetricsAPI.GaugeFilesTotal, "Total uploaded files") },
{ MetricsAPI.GaugeFilesTotalSize, Prometheus.Metrics.CreateGauge(MetricsAPI.GaugeFilesTotalSize, "Total uploaded files (bytes)") },
};
public void SetGaugeTo(string gaugeName, double value)
{
if (gauges.ContainsKey(gaugeName))
{
gauges[gaugeName].IncTo(value);
}
}
public void IncGaugeBy(string gaugeName, double value)
{
if (gauges.ContainsKey(gaugeName))
{
gauges[gaugeName].Inc(value);
}
}
public void DecGaugeBy(string gaugeName, double value)
{
if (gauges.ContainsKey(gaugeName))
{
gauges[gaugeName].Dec(value);
}
}
public void IncCounter(string counterName)
{
IncCounterBy(counterName, 1);
}
public void IncCounterBy(string counterName, double value)
{
if (counters.ContainsKey(counterName))
{
counters[counterName].Inc(value);
}
}
}

View File

@@ -1,7 +1,11 @@
using MareSynchronosServices;
using MareSynchronosShared.Data;
using MareSynchronosShared.Metrics;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Linq;
public class Program
{
@@ -10,6 +14,15 @@ public class Program
var hostBuilder = CreateHostBuilder(args);
var host = hostBuilder.Build();
using (var scope = host.Services.CreateScope())
{
var services = scope.ServiceProvider;
using var dbContext = services.GetRequiredService<MareDbContext>();
var metrics = services.GetRequiredService<MareMetrics>();
metrics.SetGaugeTo(MetricsAPI.GaugeUsersRegistered, dbContext.Users.Count());
}
host.Run();
}

View File

@@ -1,40 +0,0 @@
using Grpc.Core;
using MareSynchronosServices.Metrics;
using MareSynchronosShared.Protos;
using System.Threading.Tasks;
namespace MareSynchronosServices.Services;
public class MetricsService : MareSynchronosShared.Protos.MetricsService.MetricsServiceBase
{
private readonly MareMetrics metrics;
public MetricsService(MareMetrics metrics)
{
this.metrics = metrics;
}
public override Task<Empty> IncreaseCounter(IncreaseCounterRequest request, ServerCallContext context)
{
metrics.IncCounterBy(request.CounterName, request.Value);
return Task.FromResult(new Empty());
}
public override Task<Empty> SetGauge(SetGaugeRequest request, ServerCallContext context)
{
metrics.SetGaugeTo(request.GaugeName, request.Value);
return Task.FromResult(new Empty());
}
public override Task<Empty> DecGauge(GaugeRequest request, ServerCallContext context)
{
metrics.DecGaugeBy(request.GaugeName, request.Value);
return Task.FromResult(new Empty());
}
public override Task<Empty> IncGauge(GaugeRequest request, ServerCallContext context)
{
metrics.IncGaugeBy(request.GaugeName, request.Value);
return Task.FromResult(new Empty());
}
}

View File

@@ -1,16 +1,15 @@
using MareSynchronosServer;
using MareSynchronosServices.Authentication;
using MareSynchronosServices.Discord;
using MareSynchronosServices.Metrics;
using MareSynchronosServices.Services;
using MareSynchronosShared.Authentication;
using MareSynchronosShared.Data;
using MareSynchronosShared.Metrics;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Prometheus;
using System.Collections.Generic;
namespace MareSynchronosServices;
@@ -34,7 +33,16 @@ public class Startup
options.EnableThreadSafetyChecks(false);
}, Configuration.GetValue("DbContextPoolSize", 1024));
services.AddSingleton<MareMetrics>();
services.AddSingleton(new MareMetrics(new List<string> {
MetricsAPI.CounterAuthenticationRequests,
MetricsAPI.CounterAuthenticationFailures,
MetricsAPI.CounterAuthenticationCacheHits,
MetricsAPI.CounterAuthenticationSuccesses
}, new List<string>
{
MetricsAPI.GaugeUsersRegistered
}));
services.AddSingleton<SecretKeyAuthenticationHandler>();
services.AddSingleton<CleanupService>();
services.AddTransient(_ => Configuration);
@@ -47,13 +55,12 @@ public class Startup
{
app.UseRouting();
var metricServer = new KestrelMetricServer(4980);
var metricServer = new KestrelMetricServer(4982);
metricServer.Start();
app.UseEndpoints(endpoints =>
{
endpoints.MapGrpcService<AuthenticationService>();
endpoints.MapGrpcService<MetricsService>();
});
}
}