Skip to content

Commit 19f512e

Browse files
authored
Merge pull request #1 from mxritzdev/addMetrics
Add metrics
2 parents c9b5230 + aeac605 commit 19f512e

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ obj/
44
riderModule.iml
55
/_ReSharper.Caches/
66
/LinkRouter/data
7+
*.DotSettings.user

LinkRouter/App/Http/Controllers/RedirectController.cs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
using LinkRouter.App.Configuration;
1+
using LinkRouter.App.Configuration;
22
using Microsoft.AspNetCore.Mvc;
3+
using Prometheus;
4+
35

46
namespace LinkRouter.App.Http.Controllers;
57

@@ -8,6 +10,26 @@ public class RedirectController : Controller
810
{
911

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

1234
public RedirectController(Config config)
1335
{
@@ -20,8 +42,17 @@ public IActionResult RedirectToExternalUrl(string path)
2042
var redirectRoute = Config.Routes.FirstOrDefault(x => x.Route == path || x.Route == path + "/" || x.Route == "/" + path);
2143

2244
if (redirectRoute != null)
45+
{
46+
RouteCounter
47+
.WithLabels(redirectRoute.Route)
48+
.Inc();
49+
2350
return Redirect(redirectRoute.RedirectUrl);
51+
}
2452

53+
NotFoundCounter
54+
.WithLabels(path)
55+
.Inc();
2556

2657
if (Config.NotFoundBehavior.RedirectOn404)
2758
return Redirect(Config.NotFoundBehavior.RedirectUrl);
@@ -32,6 +63,10 @@ public IActionResult RedirectToExternalUrl(string path)
3263
[HttpGet("/")]
3364
public IActionResult GetRootRoute()
3465
{
66+
RouteCounter
67+
.WithLabels("/")
68+
.Inc();
69+
3570
string url = Config.RootRoute;
3671

3772
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
@@ -5,6 +5,7 @@
55
using LinkRouter.App.Services;
66
using MoonCore.Extensions;
77
using MoonCore.Helpers;
8+
using Prometheus;
89

910
namespace LinkRouter;
1011

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

4647
builder.Services.AddSingleton(config);
4748

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

56+
app.UseMetricServer();
5057
app.MapControllers();
5158

5259
app.Run();

0 commit comments

Comments
 (0)