Skip to content

Commit 7a2bb37

Browse files
committed
Add Prometheus support
1 parent 2ec4d5b commit 7a2bb37

File tree

5 files changed

+81
-1
lines changed

5 files changed

+81
-1
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using Microsoft.Extensions.Options;
2+
using MiniMediaMetadataAPI.Options;
3+
using Prometheus;
4+
5+
namespace MiniMediaMetadataAPI.Middlewares;
6+
7+
public class RequestMiddleware
8+
{
9+
private readonly RequestDelegate _next;
10+
private readonly ILogger _logger;
11+
private readonly PrometheusOptions _prometheusOptions;
12+
13+
public RequestMiddleware(
14+
RequestDelegate next
15+
, ILoggerFactory loggerFactory
16+
, IOptions<PrometheusOptions> prometheusOptions
17+
)
18+
{
19+
_next = next;
20+
_logger = loggerFactory.CreateLogger<RequestMiddleware>();
21+
_prometheusOptions = prometheusOptions.Value;
22+
}
23+
24+
public async Task Invoke(HttpContext httpContext)
25+
{
26+
var path = httpContext.Request.Path.Value;
27+
var method = httpContext.Request.Method;
28+
29+
var counter = Metrics.CreateCounter("MiniMediaMetadataAPI_request_total", "HTTP Requests Total", new CounterConfiguration
30+
{
31+
LabelNames = new[] { "path", "method", "status" }
32+
});
33+
34+
var statusCode = 200;
35+
36+
try
37+
{
38+
await _next.Invoke(httpContext);
39+
}
40+
catch (Exception)
41+
{
42+
statusCode = 500;
43+
counter.Labels(path, method, statusCode.ToString()).Inc();
44+
45+
throw;
46+
}
47+
48+
if (path != _prometheusOptions.MetricsUrl)
49+
{
50+
statusCode = httpContext.Response.StatusCode;
51+
counter.Labels(path, method, statusCode.ToString()).Inc();
52+
}
53+
}
54+
}
55+
56+
public static class RequestMiddlewareExtensions
57+
{
58+
public static IApplicationBuilder UseRequestMiddleware(this IApplicationBuilder builder)
59+
{
60+
return builder.UseMiddleware<RequestMiddleware>();
61+
}
62+
}

MiniMediaMetadataAPI/MiniMediaMetadataAPI.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
1919
<PackageReference Include="Npgsql" Version="9.0.3" />
2020
<PackageReference Include="Polly" Version="8.5.2" />
21+
<PackageReference Include="prometheus-net" Version="8.2.1" />
22+
<PackageReference Include="prometheus-net.AspNetCore" Version="8.2.1" />
2123
<PackageReference Include="Quartz" Version="3.14.0" />
2224
<PackageReference Include="Quartz.AspNetCore" Version="3.14.0" />
2325
<PackageReference Include="Quartz.Jobs" Version="3.14.0" />
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace MiniMediaMetadataAPI.Options;
2+
3+
public class PrometheusOptions
4+
{
5+
public string MetricsUrl { get; set; }
6+
}

MiniMediaMetadataAPI/Program.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
using MiniMediaMetadataAPI.Application.Configurations;
33
using MiniMediaMetadataAPI.Application.Repositories;
44
using MiniMediaMetadataAPI.Application.Services;
5+
using MiniMediaMetadataAPI.Middlewares;
6+
using MiniMediaMetadataAPI.Options;
7+
using Prometheus;
58

69
var builder = WebApplication.CreateBuilder(args);
710

@@ -13,6 +16,7 @@
1316
builder.Services.AddEndpointsApiExplorer();
1417
builder.Services.AddSwaggerGen();
1518
builder.Services.Configure<DatabaseConfiguration>(builder.Configuration.GetSection("DatabaseConfiguration"));
19+
builder.Services.Configure<PrometheusOptions>(builder.Configuration.GetSection("Prometheus"));
1620

1721
builder.Services.AddScoped<JobRepository>();
1822
builder.Services.AddScoped<MusicBrainzRepository>();
@@ -28,12 +32,15 @@
2832
// Configure the HTTP request pipeline.
2933
app.UseSwagger();
3034
app.UseSwaggerUI();
35+
app.UseRequestMiddleware();
3136

3237
//app.UseHttpsRedirection();
3338

3439
app.UseRouting();
3540
app.UseCors();
3641
app.UseEndpoints(endpoints => endpoints.MapControllers());
42+
app.UseMetricServer(url: builder.Configuration.GetSection("Prometheus")["MetricsUrl"]);
43+
3744

3845
app.Run();
3946

MiniMediaMetadataAPI/appsettings.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
}
77
},
88
"AllowedHosts": "*",
9-
"DatabaseConfiguration":{
9+
"DatabaseConfiguration": {
1010
"ConnectionString": ""
11+
},
12+
"Prometheus": {
13+
"MetricsUrl": "/metrics"
1114
}
1215
}

0 commit comments

Comments
 (0)