adjust metrics
This commit is contained in:
@@ -24,6 +24,7 @@ using System.Net.Http;
|
|||||||
using MareSynchronosServer.Utils;
|
using MareSynchronosServer.Utils;
|
||||||
using MareSynchronosServer.RequirementHandlers;
|
using MareSynchronosServer.RequirementHandlers;
|
||||||
using MareSynchronosShared.Services;
|
using MareSynchronosShared.Services;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
namespace MareSynchronosServer;
|
namespace MareSynchronosServer;
|
||||||
|
|
||||||
@@ -71,7 +72,7 @@ public class Startup
|
|||||||
RetryPolicy = null
|
RetryPolicy = null
|
||||||
};
|
};
|
||||||
|
|
||||||
services.AddSingleton(new MareMetrics(new List<string>
|
services.AddSingleton<MareMetrics>(m => new MareMetrics(m.GetService<ILogger<MareMetrics>>(), new List<string>
|
||||||
{
|
{
|
||||||
MetricsAPI.CounterInitializedConnections,
|
MetricsAPI.CounterInitializedConnections,
|
||||||
MetricsAPI.CounterUserPushData,
|
MetricsAPI.CounterUserPushData,
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ using Microsoft.Extensions.DependencyInjection;
|
|||||||
using Prometheus;
|
using Prometheus;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using MareSynchronosServices.Identity;
|
using MareSynchronosServices.Identity;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
namespace MareSynchronosServices;
|
namespace MareSynchronosServices;
|
||||||
|
|
||||||
@@ -32,11 +33,7 @@ public class Startup
|
|||||||
options.EnableThreadSafetyChecks(false);
|
options.EnableThreadSafetyChecks(false);
|
||||||
}, Configuration.GetValue("DbContextPoolSize", 1024));
|
}, Configuration.GetValue("DbContextPoolSize", 1024));
|
||||||
|
|
||||||
services.AddSingleton(new MareMetrics(new List<string> {
|
services.AddSingleton(m => new MareMetrics(m.GetService<ILogger<MareMetrics>>(), new List<string> {
|
||||||
MetricsAPI.CounterAuthenticationRequests,
|
|
||||||
MetricsAPI.CounterAuthenticationFailures,
|
|
||||||
MetricsAPI.CounterAuthenticationCacheHits,
|
|
||||||
MetricsAPI.CounterAuthenticationSuccesses
|
|
||||||
}, new List<string>
|
}, new List<string>
|
||||||
{
|
{
|
||||||
MetricsAPI.GaugeUsersRegistered
|
MetricsAPI.GaugeUsersRegistered
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ namespace MareSynchronosShared.Authentication;
|
|||||||
|
|
||||||
public class SecretKeyAuthenticatorService
|
public class SecretKeyAuthenticatorService
|
||||||
{
|
{
|
||||||
private readonly MareMetrics metrics;
|
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);
|
||||||
@@ -34,17 +34,17 @@ public class SecretKeyAuthenticatorService
|
|||||||
logger.LogInformation("Whitelisted IP: " + ip);
|
logger.LogInformation("Whitelisted IP: " + ip);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.metrics = metrics;
|
_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);
|
_metrics.IncCounter(MetricsAPI.CounterAuthenticationRequests);
|
||||||
|
|
||||||
if (_cachedPositiveResponses.TryGetValue(secretKey, out var cachedPositiveResponse))
|
if (_cachedPositiveResponses.TryGetValue(secretKey, out var cachedPositiveResponse))
|
||||||
{
|
{
|
||||||
metrics.IncCounter(MetricsAPI.CounterAuthenticationCacheHits);
|
_metrics.IncCounter(MetricsAPI.CounterAuthenticationCacheHits);
|
||||||
return cachedPositiveResponse;
|
return cachedPositiveResponse;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,7 +75,7 @@ public class SecretKeyAuthenticatorService
|
|||||||
|
|
||||||
if (reply.Success)
|
if (reply.Success)
|
||||||
{
|
{
|
||||||
metrics.IncCounter(MetricsAPI.CounterAuthenticationSuccesses);
|
_metrics.IncCounter(MetricsAPI.CounterAuthenticationSuccesses);
|
||||||
|
|
||||||
_cachedPositiveResponses[secretKey] = reply;
|
_cachedPositiveResponses[secretKey] = reply;
|
||||||
_ = Task.Run(async () =>
|
_ = Task.Run(async () =>
|
||||||
@@ -95,7 +95,7 @@ public class SecretKeyAuthenticatorService
|
|||||||
|
|
||||||
private SecretKeyAuthReply AuthenticationFailure(string ip)
|
private SecretKeyAuthReply AuthenticationFailure(string ip)
|
||||||
{
|
{
|
||||||
metrics.IncCounter(MetricsAPI.CounterAuthenticationFailures);
|
_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)))
|
||||||
|
|||||||
@@ -1,18 +1,22 @@
|
|||||||
using Prometheus;
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Prometheus;
|
||||||
|
|
||||||
namespace MareSynchronosShared.Metrics;
|
namespace MareSynchronosShared.Metrics;
|
||||||
|
|
||||||
public class MareMetrics
|
public class MareMetrics
|
||||||
{
|
{
|
||||||
public MareMetrics(List<string> countersToServe, List<string> gaugesToServe)
|
public MareMetrics(ILogger<MareMetrics> logger, List<string> countersToServe, List<string> gaugesToServe)
|
||||||
{
|
{
|
||||||
foreach(var counter in countersToServe)
|
logger.LogInformation("Initializing MareMetrics");
|
||||||
|
foreach (var counter in countersToServe)
|
||||||
{
|
{
|
||||||
|
logger.LogInformation($"Creating Metric for Counter {counter}");
|
||||||
counters.Add(counter, Prometheus.Metrics.CreateCounter(counter, counter));
|
counters.Add(counter, Prometheus.Metrics.CreateCounter(counter, counter));
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach(var gauge in gaugesToServe)
|
foreach (var gauge in gaugesToServe)
|
||||||
{
|
{
|
||||||
|
logger.LogInformation($"Creating Metric for Counter {gauge}");
|
||||||
gauges.Add(gauge, Prometheus.Metrics.CreateGauge(gauge, gauge));
|
gauges.Add(gauge, Prometheus.Metrics.CreateGauge(gauge, gauge));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ using Microsoft.EntityFrameworkCore;
|
|||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.FileProviders;
|
using Microsoft.Extensions.FileProviders;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
using Prometheus;
|
using Prometheus;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
@@ -31,9 +32,9 @@ public class Startup
|
|||||||
|
|
||||||
services.AddTransient(_ => Configuration);
|
services.AddTransient(_ => Configuration);
|
||||||
|
|
||||||
var mareSettings = Configuration.GetRequiredSection("MareSynchronos");
|
services.AddLogging();
|
||||||
|
|
||||||
bool isSecondary = mareSettings.GetValue<bool>("IsSecondaryInstance", false);
|
var mareSettings = Configuration.GetRequiredSection("MareSynchronos");
|
||||||
|
|
||||||
//services.AddControllers();
|
//services.AddControllers();
|
||||||
|
|
||||||
@@ -50,21 +51,18 @@ public class Startup
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!isSecondary)
|
services.AddSingleton<MareMetrics>(m => new MareMetrics(m.GetService<ILogger<MareMetrics>>(), new List<string>
|
||||||
{
|
{
|
||||||
services.AddSingleton(new MareMetrics(new List<string>
|
MetricsAPI.CounterAuthenticationCacheHits,
|
||||||
{
|
MetricsAPI.CounterAuthenticationFailures,
|
||||||
MetricsAPI.CounterAuthenticationCacheHits,
|
MetricsAPI.CounterAuthenticationRequests,
|
||||||
MetricsAPI.CounterAuthenticationFailures,
|
MetricsAPI.CounterAuthenticationSuccesses
|
||||||
MetricsAPI.CounterAuthenticationRequests,
|
}, new List<string>
|
||||||
MetricsAPI.CounterAuthenticationSuccesses
|
{
|
||||||
}, new List<string>
|
MetricsAPI.GaugeFilesTotalSize,
|
||||||
{
|
MetricsAPI.GaugeFilesTotal
|
||||||
MetricsAPI.GaugeFilesTotalSize,
|
}));
|
||||||
MetricsAPI.GaugeFilesTotal
|
services.AddHostedService<CleanupService>();
|
||||||
}));
|
|
||||||
services.AddHostedService<CleanupService>();
|
|
||||||
}
|
|
||||||
|
|
||||||
services.AddSingleton<SecretKeyAuthenticatorService>();
|
services.AddSingleton<SecretKeyAuthenticatorService>();
|
||||||
services.AddDbContextPool<MareDbContext>(options =>
|
services.AddDbContextPool<MareDbContext>(options =>
|
||||||
@@ -91,17 +89,12 @@ public class Startup
|
|||||||
|
|
||||||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||||||
{
|
{
|
||||||
bool isSecondary = Configuration.GetSection("MareSynchronos").GetValue<bool>("IsSecondaryInstance", false);
|
|
||||||
|
|
||||||
app.UseHttpLogging();
|
app.UseHttpLogging();
|
||||||
|
|
||||||
app.UseRouting();
|
app.UseRouting();
|
||||||
|
|
||||||
if (!isSecondary)
|
var metricServer = new KestrelMetricServer(4981);
|
||||||
{
|
metricServer.Start();
|
||||||
var metricServer = new KestrelMetricServer(4981);
|
|
||||||
metricServer.Start();
|
|
||||||
}
|
|
||||||
|
|
||||||
app.UseAuthentication();
|
app.UseAuthentication();
|
||||||
app.UseAuthorization();
|
app.UseAuthorization();
|
||||||
@@ -111,16 +104,13 @@ public class Startup
|
|||||||
FileProvider = new PhysicalFileProvider(Configuration.GetRequiredSection("MareSynchronos")["CacheDirectory"]),
|
FileProvider = new PhysicalFileProvider(Configuration.GetRequiredSection("MareSynchronos")["CacheDirectory"]),
|
||||||
RequestPath = "/cache",
|
RequestPath = "/cache",
|
||||||
ServeUnknownFileTypes = true,
|
ServeUnknownFileTypes = true,
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!isSecondary)
|
app.UseEndpoints(e =>
|
||||||
{
|
{
|
||||||
app.UseEndpoints(e =>
|
e.MapGrpcService<FileService>();
|
||||||
{
|
//e.MapControllers();
|
||||||
e.MapGrpcService<FileService>();
|
});
|
||||||
//e.MapControllers();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user