add metrics to each shard
This commit is contained in:
@@ -77,6 +77,10 @@ public class Startup
|
|||||||
MetricsAPI.CounterUserPushData,
|
MetricsAPI.CounterUserPushData,
|
||||||
MetricsAPI.CounterUserPushDataTo,
|
MetricsAPI.CounterUserPushDataTo,
|
||||||
MetricsAPI.CounterUsersRegisteredDeleted,
|
MetricsAPI.CounterUsersRegisteredDeleted,
|
||||||
|
MetricsAPI.CounterAuthenticationCacheHits,
|
||||||
|
MetricsAPI.CounterAuthenticationFailures,
|
||||||
|
MetricsAPI.CounterAuthenticationRequests,
|
||||||
|
MetricsAPI.CounterAuthenticationSuccesses
|
||||||
}, new List<string>
|
}, new List<string>
|
||||||
{
|
{
|
||||||
MetricsAPI.GaugeAuthorizedConnections,
|
MetricsAPI.GaugeAuthorizedConnections,
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using MareSynchronosShared.Data;
|
using MareSynchronosShared.Data;
|
||||||
|
using MareSynchronosShared.Metrics;
|
||||||
using MareSynchronosShared.Utils;
|
using MareSynchronosShared.Utils;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
@@ -10,6 +11,7 @@ namespace MareSynchronosShared.Authentication;
|
|||||||
|
|
||||||
public class SecretKeyAuthenticatorService
|
public class SecretKeyAuthenticatorService
|
||||||
{
|
{
|
||||||
|
private readonly MareMetrics metrics;
|
||||||
private readonly IServiceScopeFactory _serviceScopeFactory;
|
private readonly IServiceScopeFactory _serviceScopeFactory;
|
||||||
private readonly ILogger<SecretKeyAuthenticatorService> _logger;
|
private readonly ILogger<SecretKeyAuthenticatorService> _logger;
|
||||||
private readonly ConcurrentDictionary<string, SecretKeyAuthReply> _cachedPositiveResponses = new(StringComparer.Ordinal);
|
private readonly ConcurrentDictionary<string, SecretKeyAuthReply> _cachedPositiveResponses = new(StringComparer.Ordinal);
|
||||||
@@ -18,7 +20,7 @@ public class SecretKeyAuthenticatorService
|
|||||||
private readonly int _tempBanMinutes;
|
private readonly int _tempBanMinutes;
|
||||||
private readonly List<string> _whitelistedIps;
|
private readonly List<string> _whitelistedIps;
|
||||||
|
|
||||||
public SecretKeyAuthenticatorService(IServiceScopeFactory serviceScopeFactory, IConfiguration configuration, ILogger<SecretKeyAuthenticatorService> logger)
|
public SecretKeyAuthenticatorService(MareMetrics metrics, IServiceScopeFactory serviceScopeFactory, IConfiguration configuration, ILogger<SecretKeyAuthenticatorService> logger)
|
||||||
{
|
{
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
var config = configuration.GetRequiredSection("MareSynchronos");
|
var config = configuration.GetRequiredSection("MareSynchronos");
|
||||||
@@ -31,13 +33,18 @@ public class SecretKeyAuthenticatorService
|
|||||||
{
|
{
|
||||||
logger.LogInformation("Whitelisted IP: " + ip);
|
logger.LogInformation("Whitelisted IP: " + ip);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.metrics = metrics;
|
||||||
_serviceScopeFactory = serviceScopeFactory;
|
_serviceScopeFactory = serviceScopeFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal async Task<SecretKeyAuthReply> AuthorizeAsync(string ip, string secretKey)
|
internal async Task<SecretKeyAuthReply> AuthorizeAsync(string ip, string secretKey)
|
||||||
{
|
{
|
||||||
|
metrics.IncCounter(MetricsAPI.CounterAuthenticationRequests);
|
||||||
|
|
||||||
if (_cachedPositiveResponses.TryGetValue(secretKey, out var cachedPositiveResponse))
|
if (_cachedPositiveResponses.TryGetValue(secretKey, out var cachedPositiveResponse))
|
||||||
{
|
{
|
||||||
|
metrics.IncCounter(MetricsAPI.CounterAuthenticationCacheHits);
|
||||||
return cachedPositiveResponse;
|
return cachedPositiveResponse;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,6 +75,8 @@ public class SecretKeyAuthenticatorService
|
|||||||
|
|
||||||
if (reply.Success)
|
if (reply.Success)
|
||||||
{
|
{
|
||||||
|
metrics.IncCounter(MetricsAPI.CounterAuthenticationSuccesses);
|
||||||
|
|
||||||
_cachedPositiveResponses[secretKey] = reply;
|
_cachedPositiveResponses[secretKey] = reply;
|
||||||
_ = Task.Run(async () =>
|
_ = Task.Run(async () =>
|
||||||
{
|
{
|
||||||
@@ -86,6 +95,8 @@ public class SecretKeyAuthenticatorService
|
|||||||
|
|
||||||
private SecretKeyAuthReply AuthenticationFailure(string ip)
|
private SecretKeyAuthReply AuthenticationFailure(string ip)
|
||||||
{
|
{
|
||||||
|
metrics.IncCounter(MetricsAPI.CounterAuthenticationFailures);
|
||||||
|
|
||||||
_logger.LogWarning("Failed authorization from {ip}", ip);
|
_logger.LogWarning("Failed authorization from {ip}", ip);
|
||||||
if (!_whitelistedIps.Any(w => ip.Contains(w, StringComparison.OrdinalIgnoreCase)))
|
if (!_whitelistedIps.Any(w => ip.Contains(w, StringComparison.OrdinalIgnoreCase)))
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -35,6 +35,8 @@ public class Startup
|
|||||||
|
|
||||||
bool isSecondary = mareSettings.GetValue<bool>("IsSecondaryInstance", false);
|
bool isSecondary = mareSettings.GetValue<bool>("IsSecondaryInstance", false);
|
||||||
|
|
||||||
|
//services.AddControllers();
|
||||||
|
|
||||||
var defaultMethodConfig = new MethodConfig
|
var defaultMethodConfig = new MethodConfig
|
||||||
{
|
{
|
||||||
Names = { MethodName.Default },
|
Names = { MethodName.Default },
|
||||||
@@ -52,6 +54,10 @@ public class Startup
|
|||||||
{
|
{
|
||||||
services.AddSingleton(new MareMetrics(new List<string>
|
services.AddSingleton(new MareMetrics(new List<string>
|
||||||
{
|
{
|
||||||
|
MetricsAPI.CounterAuthenticationCacheHits,
|
||||||
|
MetricsAPI.CounterAuthenticationFailures,
|
||||||
|
MetricsAPI.CounterAuthenticationRequests,
|
||||||
|
MetricsAPI.CounterAuthenticationSuccesses
|
||||||
}, new List<string>
|
}, new List<string>
|
||||||
{
|
{
|
||||||
MetricsAPI.GaugeFilesTotalSize,
|
MetricsAPI.GaugeFilesTotalSize,
|
||||||
@@ -113,6 +119,7 @@ public class Startup
|
|||||||
app.UseEndpoints(e =>
|
app.UseEndpoints(e =>
|
||||||
{
|
{
|
||||||
e.MapGrpcService<FileService>();
|
e.MapGrpcService<FileService>();
|
||||||
|
//e.MapControllers();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user