lock metrics calls
This commit is contained in:
@@ -4,6 +4,7 @@ using System.Linq;
|
|||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using MareSynchronosServer.Data;
|
using MareSynchronosServer.Data;
|
||||||
|
using MareSynchronosServer.Metrics;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ using System.Threading;
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using MareSynchronos.API;
|
using MareSynchronos.API;
|
||||||
using MareSynchronosServer.Authentication;
|
using MareSynchronosServer.Authentication;
|
||||||
|
using MareSynchronosServer.Metrics;
|
||||||
using MareSynchronosServer.Models;
|
using MareSynchronosServer.Models;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.SignalR;
|
using Microsoft.AspNetCore.SignalR;
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ using System.Text;
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using MareSynchronos.API;
|
using MareSynchronos.API;
|
||||||
using MareSynchronosServer.Authentication;
|
using MareSynchronosServer.Authentication;
|
||||||
|
using MareSynchronosServer.Metrics;
|
||||||
using MareSynchronosServer.Models;
|
using MareSynchronosServer.Models;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.SignalR;
|
using Microsoft.AspNetCore.SignalR;
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ using System.Security.Cryptography;
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using MareSynchronos.API;
|
using MareSynchronos.API;
|
||||||
using MareSynchronosServer.Data;
|
using MareSynchronosServer.Data;
|
||||||
|
using MareSynchronosServer.Metrics;
|
||||||
using Microsoft.AspNetCore.SignalR;
|
using Microsoft.AspNetCore.SignalR;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
@@ -55,10 +56,6 @@ namespace MareSynchronosServer.Hubs
|
|||||||
IsAdmin = user.IsAdmin
|
IsAdmin = user.IsAdmin
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
MareMetrics.UnauthorizedConnections.Inc();
|
|
||||||
}
|
|
||||||
|
|
||||||
return new ConnectionDto()
|
return new ConnectionDto()
|
||||||
{
|
{
|
||||||
@@ -72,6 +69,12 @@ namespace MareSynchronosServer.Hubs
|
|||||||
return _systemInfoService.SystemInfoDto;
|
return _systemInfoService.SystemInfoDto;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override Task OnConnectedAsync()
|
||||||
|
{
|
||||||
|
MareMetrics.Connections.Inc();
|
||||||
|
return base.OnConnectedAsync();
|
||||||
|
}
|
||||||
|
|
||||||
public override async Task OnDisconnectedAsync(Exception exception)
|
public override async Task OnDisconnectedAsync(Exception exception)
|
||||||
{
|
{
|
||||||
var user = await _dbContext.Users.AsNoTracking().SingleOrDefaultAsync(u => u.UID == AuthenticatedUserId);
|
var user = await _dbContext.Users.AsNoTracking().SingleOrDefaultAsync(u => u.UID == AuthenticatedUserId);
|
||||||
@@ -99,10 +102,8 @@ namespace MareSynchronosServer.Hubs
|
|||||||
await Clients.All.SendAsync("UsersOnline",
|
await Clients.All.SendAsync("UsersOnline",
|
||||||
await _dbContext.Users.CountAsync(u => !string.IsNullOrEmpty(u.CharacterIdentification)));
|
await _dbContext.Users.CountAsync(u => !string.IsNullOrEmpty(u.CharacterIdentification)));
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
MareMetrics.Connections.Dec();
|
||||||
MareMetrics.UnauthorizedConnections.Dec();
|
|
||||||
}
|
|
||||||
|
|
||||||
await base.OnDisconnectedAsync(exception);
|
await base.OnDisconnectedAsync(exception);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,50 +0,0 @@
|
|||||||
using System.IO;
|
|
||||||
using System.Linq;
|
|
||||||
using MareSynchronosServer.Data;
|
|
||||||
using Microsoft.Extensions.Configuration;
|
|
||||||
using Prometheus;
|
|
||||||
|
|
||||||
namespace MareSynchronosServer
|
|
||||||
{
|
|
||||||
public class MareMetrics
|
|
||||||
{
|
|
||||||
public static readonly Counter InitializedConnections =
|
|
||||||
Metrics.CreateCounter("mare_initialized_connections", "Initialized Connections");
|
|
||||||
public static readonly Gauge UnauthorizedConnections =
|
|
||||||
Metrics.CreateGauge("mare_unauthorized_connections", "Unauthorized Connections");
|
|
||||||
public static readonly Gauge AuthorizedConnections =
|
|
||||||
Metrics.CreateGauge("mare_authorized_connections", "Authorized Connections");
|
|
||||||
|
|
||||||
public static readonly Gauge UsersRegistered = Metrics.CreateGauge("mare_users_registered", "Total Registrations");
|
|
||||||
|
|
||||||
public static readonly Gauge Pairs = Metrics.CreateGauge("mare_pairs", "Total Pairs");
|
|
||||||
public static readonly Gauge PairsPaused = Metrics.CreateGauge("mare_pairs_paused", "Total Paused Pairs");
|
|
||||||
|
|
||||||
public static readonly Gauge FilesTotal = Metrics.CreateGauge("mare_files", "Total uploaded files");
|
|
||||||
public static readonly Gauge FilesTotalSize =
|
|
||||||
Metrics.CreateGauge("mare_files_size", "Total uploaded files (bytes)");
|
|
||||||
|
|
||||||
public static readonly Counter UserPushData = Metrics.CreateCounter("mare_user_push", "Users pushing data");
|
|
||||||
public static readonly Counter UserPushDataTo =
|
|
||||||
Metrics.CreateCounter("mare_user_push_to", "Users Receiving Data");
|
|
||||||
|
|
||||||
public static readonly Counter UserDownloadedFiles = Metrics.CreateCounter("mare_user_downloaded_files", "Total Downloaded Files by Users");
|
|
||||||
public static readonly Counter UserDownloadedFilesSize = Metrics.CreateCounter("mare_user_downloaded_files_size", "Total Downloaded Files Size by Users");
|
|
||||||
|
|
||||||
public static readonly Gauge
|
|
||||||
CPUUsage = Metrics.CreateGauge("mare_cpu_usage", "Total calculated CPU usage in %");
|
|
||||||
public static readonly Gauge RAMUsage =
|
|
||||||
Metrics.CreateGauge("mare_ram_usage", "Total calculated RAM usage in bytes for Mare + MSSQL");
|
|
||||||
public static readonly Gauge NetworkOut = Metrics.CreateGauge("mare_network_out", "Network out in byte/s");
|
|
||||||
public static readonly Gauge NetworkIn = Metrics.CreateGauge("mare_network_in", "Network in in byte/s");
|
|
||||||
|
|
||||||
public static void InitializeMetrics(MareDbContext dbContext, IConfiguration configuration)
|
|
||||||
{
|
|
||||||
UsersRegistered.IncTo(dbContext.Users.Count());
|
|
||||||
Pairs.IncTo(dbContext.ClientPairs.Count());
|
|
||||||
PairsPaused.IncTo(dbContext.ClientPairs.Count(p => p.IsPaused));
|
|
||||||
FilesTotal.IncTo(dbContext.Files.Count());
|
|
||||||
FilesTotalSize.IncTo(Directory.EnumerateFiles(configuration["CacheDirectory"]).Sum(f => new FileInfo(f).Length));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
using Prometheus;
|
||||||
|
|
||||||
|
namespace MareSynchronosServer.Metrics
|
||||||
|
{
|
||||||
|
public class LockedProxyCounter
|
||||||
|
{
|
||||||
|
private readonly Counter _c;
|
||||||
|
|
||||||
|
public LockedProxyCounter(Counter c)
|
||||||
|
{
|
||||||
|
_c = c;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Inc(double inc = 1d)
|
||||||
|
{
|
||||||
|
lock (_c)
|
||||||
|
{
|
||||||
|
_c.Inc(inc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
using Prometheus;
|
||||||
|
|
||||||
|
namespace MareSynchronosServer.Metrics;
|
||||||
|
|
||||||
|
public class LockedProxyGauge
|
||||||
|
{
|
||||||
|
private readonly Gauge _g;
|
||||||
|
|
||||||
|
public LockedProxyGauge(Gauge g)
|
||||||
|
{
|
||||||
|
_g = g;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Inc(double inc = 1d)
|
||||||
|
{
|
||||||
|
lock (_g)
|
||||||
|
{
|
||||||
|
_g.Inc(inc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void IncTo(double incTo)
|
||||||
|
{
|
||||||
|
lock (_g)
|
||||||
|
{
|
||||||
|
_g.IncTo(incTo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dec(double decBy = 1d)
|
||||||
|
{
|
||||||
|
lock (_g)
|
||||||
|
{
|
||||||
|
_g.Dec(decBy);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Set(double setTo)
|
||||||
|
{
|
||||||
|
lock (_g)
|
||||||
|
{
|
||||||
|
_g.Set(setTo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public double Value => _g.Value;
|
||||||
|
}
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using MareSynchronosServer.Data;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Prometheus;
|
||||||
|
|
||||||
|
namespace MareSynchronosServer.Metrics
|
||||||
|
{
|
||||||
|
public class MareMetrics
|
||||||
|
{
|
||||||
|
public static readonly LockedProxyCounter InitializedConnections =
|
||||||
|
new(Prometheus.Metrics.CreateCounter("mare_initialized_connections", "Initialized Connections"));
|
||||||
|
public static readonly LockedProxyGauge Connections =
|
||||||
|
new(Prometheus.Metrics.CreateGauge("mare_unauthorized_connections", "Unauthorized Connections"));
|
||||||
|
public static readonly LockedProxyGauge AuthorizedConnections =
|
||||||
|
new(Prometheus.Metrics.CreateGauge("mare_authorized_connections", "Authorized Connections"));
|
||||||
|
|
||||||
|
public static readonly LockedProxyGauge UsersRegistered = new(Prometheus.Metrics.CreateGauge("mare_users_registered", "Total Registrations"));
|
||||||
|
|
||||||
|
public static readonly LockedProxyGauge Pairs = new(Prometheus.Metrics.CreateGauge("mare_pairs", "Total Pairs"));
|
||||||
|
public static readonly LockedProxyGauge PairsPaused = new(Prometheus.Metrics.CreateGauge("mare_pairs_paused", "Total Paused Pairs"));
|
||||||
|
|
||||||
|
public static readonly LockedProxyGauge FilesTotal = new(Prometheus.Metrics.CreateGauge("mare_files", "Total uploaded files"));
|
||||||
|
public static readonly LockedProxyGauge FilesTotalSize =
|
||||||
|
new(Prometheus.Metrics.CreateGauge("mare_files_size", "Total uploaded files (bytes)"));
|
||||||
|
|
||||||
|
public static readonly LockedProxyCounter UserPushData = new(Prometheus.Metrics.CreateCounter("mare_user_push", "Users pushing data"));
|
||||||
|
public static readonly LockedProxyCounter UserPushDataTo =
|
||||||
|
new(Prometheus.Metrics.CreateCounter("mare_user_push_to", "Users Receiving Data"));
|
||||||
|
|
||||||
|
public static readonly LockedProxyCounter UserDownloadedFiles =
|
||||||
|
new(Prometheus.Metrics.CreateCounter("mare_user_downloaded_files", "Total Downloaded Files by Users"));
|
||||||
|
public static readonly LockedProxyCounter UserDownloadedFilesSize =
|
||||||
|
new(Prometheus.Metrics.CreateCounter("mare_user_downloaded_files_size", "Total Downloaded Files Size by Users"));
|
||||||
|
|
||||||
|
public static readonly LockedProxyGauge
|
||||||
|
CPUUsage = new(Prometheus.Metrics.CreateGauge("mare_cpu_usage", "Total calculated CPU usage in %"));
|
||||||
|
public static readonly LockedProxyGauge RAMUsage =
|
||||||
|
new(Prometheus.Metrics.CreateGauge("mare_ram_usage", "Total calculated RAM usage in bytes for Mare + MSSQL"));
|
||||||
|
public static readonly LockedProxyGauge NetworkOut = new(Prometheus.Metrics.CreateGauge("mare_network_out", "Network out in byte/s"));
|
||||||
|
public static readonly LockedProxyGauge NetworkIn = new(Prometheus.Metrics.CreateGauge("mare_network_in", "Network in in byte/s"));
|
||||||
|
|
||||||
|
public static void InitializeMetrics(MareDbContext dbContext, IConfiguration configuration)
|
||||||
|
{
|
||||||
|
UsersRegistered.IncTo(dbContext.Users.Count());
|
||||||
|
Pairs.IncTo(dbContext.ClientPairs.Count());
|
||||||
|
PairsPaused.IncTo(dbContext.ClientPairs.Count(p => p.IsPaused));
|
||||||
|
FilesTotal.IncTo(dbContext.Files.Count());
|
||||||
|
FilesTotalSize.IncTo(Directory.EnumerateFiles(configuration["CacheDirectory"]).Sum(f => new FileInfo(f).Length));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,6 +7,7 @@ using Microsoft.EntityFrameworkCore;
|
|||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
using MareSynchronosServer.Metrics;
|
||||||
|
|
||||||
namespace MareSynchronosServer
|
namespace MareSynchronosServer
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ using System.Threading.Tasks;
|
|||||||
using MareSynchronos.API;
|
using MareSynchronos.API;
|
||||||
using MareSynchronosServer.Data;
|
using MareSynchronosServer.Data;
|
||||||
using MareSynchronosServer.Hubs;
|
using MareSynchronosServer.Hubs;
|
||||||
|
using MareSynchronosServer.Metrics;
|
||||||
using Microsoft.AspNetCore.SignalR;
|
using Microsoft.AspNetCore.SignalR;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|||||||
Reference in New Issue
Block a user