Add prometheus metrics

This commit is contained in:
Kieran 2022-02-10 16:36:53 +00:00
parent 2b75f829ed
commit 9d125b22f4
Signed by: Kieran
GPG Key ID: DE71CEB3925BE941
3 changed files with 41 additions and 1 deletions

View File

@ -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");
});

View 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));
}
}

View File

@ -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>