Skip to content

Commit 8c94536

Browse files
committed
Add Prometheus metrics for route and 404 requests
1 parent 0e09073 commit 8c94536

3 files changed

Lines changed: 42 additions & 0 deletions

File tree

LinkRouter/App/Http/Controllers/RedirectController.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using LinkRouter.App.Configuration;
44
using Microsoft.AspNetCore.Mvc;
55
using MoonCore.Services;
6+
using Prometheus;
67

78
namespace LinkRouter.App.Http.Controllers;
89

@@ -11,6 +12,26 @@ public class RedirectController : Controller
1112
{
1213

1314
private readonly Config Config;
15+
16+
17+
private readonly Counter RouteCounter = Metrics.CreateCounter(
18+
"linkrouter_requests",
19+
"Counts the number of requests to the link router",
20+
new CounterConfiguration
21+
{
22+
LabelNames = new[] { "route" }
23+
}
24+
);
25+
26+
27+
private readonly Counter NotFoundCounter = Metrics.CreateCounter(
28+
"linkrouter_404_requests",
29+
"Counts the number of not found requests to the link router",
30+
new CounterConfiguration
31+
{
32+
LabelNames = new[] { "route" }
33+
}
34+
);
1435

1536
public RedirectController(Config config)
1637
{
@@ -23,8 +44,17 @@ public IActionResult RedirectToExternalUrl(string path)
2344
var redirectRoute = Config.Routes.FirstOrDefault(x => x.Route == path || x.Route == path + "/" || x.Route == "/" + path);
2445

2546
if (redirectRoute != null)
47+
{
48+
RouteCounter
49+
.WithLabels(redirectRoute.Route)
50+
.Inc();
51+
2652
return Redirect(redirectRoute.RedirectUrl);
53+
}
2754

55+
NotFoundCounter
56+
.WithLabels(path)
57+
.Inc();
2858

2959
if (Config.NotFoundBehavior.RedirectOn404)
3060
return Redirect(Config.NotFoundBehavior.RedirectUrl);
@@ -35,6 +65,10 @@ public IActionResult RedirectToExternalUrl(string path)
3565
[HttpGet("/")]
3666
public IActionResult GetRootRoute()
3767
{
68+
RouteCounter
69+
.WithLabels("/")
70+
.Inc();
71+
3872
string url = Config.RootRoute;
3973

4074
return Redirect(url);

LinkRouter/LinkRouter.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.20"/>
1313
<PackageReference Include="MoonCore" Version="1.5.4" />
1414
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
15+
<PackageReference Include="prometheus-net.AspNetCore" Version="8.2.1" />
1516
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0"/>
1617
</ItemGroup>
1718

LinkRouter/Program.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using MoonCore.Extensions;
77
using MoonCore.Helpers;
88
using MoonCore.Services;
9+
using Prometheus;
910

1011
namespace LinkRouter;
1112

@@ -46,8 +47,14 @@ public static void Main(string[] args)
4647

4748
builder.Services.AddSingleton(config);
4849

50+
builder.Services.AddMetricServer(options =>
51+
{
52+
options.Port = 5000;
53+
});
54+
4955
var app = builder.Build();
5056

57+
app.UseMetricServer();
5158
app.MapControllers();
5259

5360
app.Run();

0 commit comments

Comments
 (0)