add metrics to CachedFileProvider
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System.Collections.Concurrent;
|
||||
using MareSynchronosShared.Metrics;
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
namespace MareSynchronosStaticFilesServer;
|
||||
|
||||
@@ -6,15 +7,17 @@ public class CachedFileProvider
|
||||
{
|
||||
private readonly ILogger<CachedFileProvider> _logger;
|
||||
private readonly FileStatisticsService _fileStatisticsService;
|
||||
private readonly MareMetrics _metrics;
|
||||
private readonly Uri _remoteCacheSourceUri;
|
||||
private readonly string _basePath;
|
||||
private readonly ConcurrentDictionary<string, Task> _currentTransfers = new(StringComparer.Ordinal);
|
||||
private bool IsMainServer => _remoteCacheSourceUri == null;
|
||||
|
||||
public CachedFileProvider(IConfiguration configuration, ILogger<CachedFileProvider> logger, FileStatisticsService fileStatisticsService)
|
||||
public CachedFileProvider(IConfiguration configuration, ILogger<CachedFileProvider> logger, FileStatisticsService fileStatisticsService, MareMetrics metrics)
|
||||
{
|
||||
_logger = logger;
|
||||
_fileStatisticsService = fileStatisticsService;
|
||||
_metrics = metrics;
|
||||
var configurationSection = configuration.GetRequiredSection("MareSynchronos");
|
||||
_remoteCacheSourceUri = configurationSection.GetValue<Uri>("RemoteCacheSourceUri", null);
|
||||
_basePath = configurationSection["CacheDirectory"];
|
||||
@@ -60,6 +63,9 @@ public class CachedFileProvider
|
||||
await fileStream.WriteAsync(buffer.AsMemory(0, bytesRead)).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
_metrics.IncGauge(MetricsAPI.GaugeFilesTotal);
|
||||
_metrics.IncGauge(MetricsAPI.GaugeFilesTotalSize, FilePathUtil.GetFileInfoForHash(_basePath, hash).Length);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -35,11 +35,10 @@ public class FileCleanupService : IHostedService
|
||||
_cleanupCts = new();
|
||||
|
||||
DirectoryInfo dir = new(_cacheDir);
|
||||
var allFiles = dir.GetFiles();
|
||||
var allFiles = dir.GetFiles("*", SearchOption.AllDirectories);
|
||||
_metrics.SetGaugeTo(MetricsAPI.GaugeFilesTotalSize, allFiles.Sum(f => f.Length));
|
||||
_metrics.SetGaugeTo(MetricsAPI.GaugeFilesTotal, allFiles.Length);
|
||||
|
||||
|
||||
_ = CleanUpTask(_cleanupCts.Token);
|
||||
|
||||
return Task.CompletedTask;
|
||||
|
||||
@@ -11,9 +11,12 @@ namespace MareSynchronosStaticFilesServer;
|
||||
|
||||
public class Startup
|
||||
{
|
||||
private bool _isMain;
|
||||
public Startup(IConfiguration configuration)
|
||||
{
|
||||
Configuration = configuration;
|
||||
var mareSettings = Configuration.GetRequiredSection("MareSynchronos");
|
||||
_isMain = string.IsNullOrEmpty(mareSettings.GetValue("RemoteCacheSourceUri", string.Empty));
|
||||
}
|
||||
|
||||
public IConfiguration Configuration { get; }
|
||||
@@ -30,19 +33,6 @@ public class Startup
|
||||
|
||||
services.AddControllers();
|
||||
|
||||
var defaultMethodConfig = new MethodConfig
|
||||
{
|
||||
Names = { MethodName.Default },
|
||||
RetryPolicy = new RetryPolicy
|
||||
{
|
||||
MaxAttempts = 100,
|
||||
InitialBackoff = TimeSpan.FromSeconds(1),
|
||||
MaxBackoff = TimeSpan.FromSeconds(5),
|
||||
BackoffMultiplier = 1.5,
|
||||
RetryableStatusCodes = { Grpc.Core.StatusCode.Unavailable }
|
||||
}
|
||||
};
|
||||
|
||||
services.AddSingleton(m => new MareMetrics(m.GetService<ILogger<MareMetrics>>(), new List<string>
|
||||
{
|
||||
MetricsAPI.CounterAuthenticationCacheHits,
|
||||
@@ -93,8 +83,8 @@ public class Startup
|
||||
|
||||
app.UseRouting();
|
||||
|
||||
//var metricServer = new KestrelMetricServer(4981);
|
||||
//metricServer.Start();
|
||||
var metricServer = new KestrelMetricServer(4981);
|
||||
metricServer.Start();
|
||||
|
||||
app.UseHttpMetrics();
|
||||
|
||||
@@ -103,7 +93,8 @@ public class Startup
|
||||
|
||||
app.UseEndpoints(e =>
|
||||
{
|
||||
e.MapGrpcService<GrpcFileService>();
|
||||
if(_isMain)
|
||||
e.MapGrpcService<GrpcFileService>();
|
||||
e.MapControllers();
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user