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;
|
namespace MareSynchronosStaticFilesServer;
|
||||||
|
|
||||||
@@ -6,15 +7,17 @@ public class CachedFileProvider
|
|||||||
{
|
{
|
||||||
private readonly ILogger<CachedFileProvider> _logger;
|
private readonly ILogger<CachedFileProvider> _logger;
|
||||||
private readonly FileStatisticsService _fileStatisticsService;
|
private readonly FileStatisticsService _fileStatisticsService;
|
||||||
|
private readonly MareMetrics _metrics;
|
||||||
private readonly Uri _remoteCacheSourceUri;
|
private readonly Uri _remoteCacheSourceUri;
|
||||||
private readonly string _basePath;
|
private readonly string _basePath;
|
||||||
private readonly ConcurrentDictionary<string, Task> _currentTransfers = new(StringComparer.Ordinal);
|
private readonly ConcurrentDictionary<string, Task> _currentTransfers = new(StringComparer.Ordinal);
|
||||||
private bool IsMainServer => _remoteCacheSourceUri == null;
|
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;
|
_logger = logger;
|
||||||
_fileStatisticsService = fileStatisticsService;
|
_fileStatisticsService = fileStatisticsService;
|
||||||
|
_metrics = metrics;
|
||||||
var configurationSection = configuration.GetRequiredSection("MareSynchronos");
|
var configurationSection = configuration.GetRequiredSection("MareSynchronos");
|
||||||
_remoteCacheSourceUri = configurationSection.GetValue<Uri>("RemoteCacheSourceUri", null);
|
_remoteCacheSourceUri = configurationSection.GetValue<Uri>("RemoteCacheSourceUri", null);
|
||||||
_basePath = configurationSection["CacheDirectory"];
|
_basePath = configurationSection["CacheDirectory"];
|
||||||
@@ -60,6 +63,9 @@ public class CachedFileProvider
|
|||||||
await fileStream.WriteAsync(buffer.AsMemory(0, bytesRead)).ConfigureAwait(false);
|
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();
|
_cleanupCts = new();
|
||||||
|
|
||||||
DirectoryInfo dir = new(_cacheDir);
|
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.GaugeFilesTotalSize, allFiles.Sum(f => f.Length));
|
||||||
_metrics.SetGaugeTo(MetricsAPI.GaugeFilesTotal, allFiles.Length);
|
_metrics.SetGaugeTo(MetricsAPI.GaugeFilesTotal, allFiles.Length);
|
||||||
|
|
||||||
|
|
||||||
_ = CleanUpTask(_cleanupCts.Token);
|
_ = CleanUpTask(_cleanupCts.Token);
|
||||||
|
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
|
|||||||
@@ -11,9 +11,12 @@ namespace MareSynchronosStaticFilesServer;
|
|||||||
|
|
||||||
public class Startup
|
public class Startup
|
||||||
{
|
{
|
||||||
|
private bool _isMain;
|
||||||
public Startup(IConfiguration configuration)
|
public Startup(IConfiguration configuration)
|
||||||
{
|
{
|
||||||
Configuration = configuration;
|
Configuration = configuration;
|
||||||
|
var mareSettings = Configuration.GetRequiredSection("MareSynchronos");
|
||||||
|
_isMain = string.IsNullOrEmpty(mareSettings.GetValue("RemoteCacheSourceUri", string.Empty));
|
||||||
}
|
}
|
||||||
|
|
||||||
public IConfiguration Configuration { get; }
|
public IConfiguration Configuration { get; }
|
||||||
@@ -30,19 +33,6 @@ public class Startup
|
|||||||
|
|
||||||
services.AddControllers();
|
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>
|
services.AddSingleton(m => new MareMetrics(m.GetService<ILogger<MareMetrics>>(), new List<string>
|
||||||
{
|
{
|
||||||
MetricsAPI.CounterAuthenticationCacheHits,
|
MetricsAPI.CounterAuthenticationCacheHits,
|
||||||
@@ -93,8 +83,8 @@ public class Startup
|
|||||||
|
|
||||||
app.UseRouting();
|
app.UseRouting();
|
||||||
|
|
||||||
//var metricServer = new KestrelMetricServer(4981);
|
var metricServer = new KestrelMetricServer(4981);
|
||||||
//metricServer.Start();
|
metricServer.Start();
|
||||||
|
|
||||||
app.UseHttpMetrics();
|
app.UseHttpMetrics();
|
||||||
|
|
||||||
@@ -103,7 +93,8 @@ public class Startup
|
|||||||
|
|
||||||
app.UseEndpoints(e =>
|
app.UseEndpoints(e =>
|
||||||
{
|
{
|
||||||
e.MapGrpcService<GrpcFileService>();
|
if(_isMain)
|
||||||
|
e.MapGrpcService<GrpcFileService>();
|
||||||
e.MapControllers();
|
e.MapControllers();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user