forked from Kieran/void.cat
Add prometheus metrics
This commit is contained in:
parent
2b75f829ed
commit
9d125b22f4
@ -1,3 +1,4 @@
|
||||
using Prometheus;
|
||||
using VoidCat.Model;
|
||||
using VoidCat.Services;
|
||||
|
||||
@ -18,7 +19,7 @@ services.AddMemoryCache();
|
||||
|
||||
services.AddScoped<IFileMetadataStore, LocalDiskFileMetadataStore>();
|
||||
services.AddScoped<IFileStore, LocalDiskFileIngressFactory>();
|
||||
services.AddScoped<IStatsCollector, InMemoryStatsCollector>();
|
||||
services.AddScoped<IStatsCollector, PrometheusStatsCollector>();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
@ -27,6 +28,7 @@ app.UseRouting();
|
||||
app.UseEndpoints(ep =>
|
||||
{
|
||||
ep.MapControllers();
|
||||
ep.MapMetrics();
|
||||
ep.MapFallbackToFile("index.html");
|
||||
});
|
||||
|
||||
|
37
VoidCat/Services/PrometheusStatsCollector.cs
Normal file
37
VoidCat/Services/PrometheusStatsCollector.cs
Normal file
@ -0,0 +1,37 @@
|
||||
using Prometheus;
|
||||
|
||||
namespace VoidCat.Services;
|
||||
|
||||
public class PrometheusStatsCollector : IStatsCollector
|
||||
{
|
||||
private readonly Counter _egress =
|
||||
Metrics.CreateCounter("egress", "Outgoing traffic from the site", new[] {"file"});
|
||||
|
||||
private readonly Counter _ingress =
|
||||
Metrics.CreateCounter("ingress", "Incoming traffic to the site", new[] {"file"});
|
||||
|
||||
public ValueTask TrackIngress(Guid id, ulong amount)
|
||||
{
|
||||
_ingress.Inc(amount);
|
||||
_ingress.WithLabels(id.ToString()).Inc(amount);
|
||||
return ValueTask.CompletedTask;
|
||||
}
|
||||
|
||||
public ValueTask TrackEgress(Guid id, ulong amount)
|
||||
{
|
||||
_egress.Inc(amount);
|
||||
_egress.WithLabels(id.ToString()).Inc(amount);
|
||||
return ValueTask.CompletedTask;
|
||||
}
|
||||
|
||||
public ValueTask<Bandwidth> GetBandwidth()
|
||||
{
|
||||
return ValueTask.FromResult<Bandwidth>(new((ulong) _ingress.Value, (ulong) _egress.Value));
|
||||
}
|
||||
|
||||
public ValueTask<Bandwidth> GetBandwidth(Guid id)
|
||||
{
|
||||
return ValueTask.FromResult<Bandwidth>(new((ulong) _ingress.Labels(id.ToString()).Value,
|
||||
(ulong) _egress.Labels(id.ToString()).Value));
|
||||
}
|
||||
}
|
@ -14,6 +14,7 @@
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.1" />
|
||||
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.14.0" />
|
||||
<PackageReference Include="NBitcoin" Version="6.0.19" />
|
||||
<PackageReference Include="prometheus-net.AspNetCore" Version="5.0.2" />
|
||||
<PackageReference Include="Seq.Extensions.Logging" Version="6.0.0" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
|
Loading…
Reference in New Issue
Block a user