diff --git a/.gitignore b/.gitignore index 5cdc66f..6da3193 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ obj/ riderModule.iml /_ReSharper.Caches/ /LinkRouter/data +*.DotSettings.user diff --git a/LinkRouter/App/Http/Controllers/RedirectController.cs b/LinkRouter/App/Http/Controllers/RedirectController.cs index 1bf3fbd..64f1c45 100644 --- a/LinkRouter/App/Http/Controllers/RedirectController.cs +++ b/LinkRouter/App/Http/Controllers/RedirectController.cs @@ -1,5 +1,7 @@ -using LinkRouter.App.Configuration; +using LinkRouter.App.Configuration; using Microsoft.AspNetCore.Mvc; +using Prometheus; + namespace LinkRouter.App.Http.Controllers; @@ -8,6 +10,26 @@ public class RedirectController : Controller { private readonly Config Config; + + + private readonly Counter RouteCounter = Metrics.CreateCounter( + "linkrouter_requests", + "Counts the number of requests to the link router", + new CounterConfiguration + { + LabelNames = new[] { "route" } + } + ); + + + private readonly Counter NotFoundCounter = Metrics.CreateCounter( + "linkrouter_404_requests", + "Counts the number of not found requests to the link router", + new CounterConfiguration + { + LabelNames = new[] { "route" } + } + ); public RedirectController(Config config) { @@ -20,8 +42,17 @@ public IActionResult RedirectToExternalUrl(string path) var redirectRoute = Config.Routes.FirstOrDefault(x => x.Route == path || x.Route == path + "/" || x.Route == "/" + path); if (redirectRoute != null) + { + RouteCounter + .WithLabels(redirectRoute.Route) + .Inc(); + return Redirect(redirectRoute.RedirectUrl); + } + NotFoundCounter + .WithLabels(path) + .Inc(); if (Config.NotFoundBehavior.RedirectOn404) return Redirect(Config.NotFoundBehavior.RedirectUrl); @@ -32,6 +63,10 @@ public IActionResult RedirectToExternalUrl(string path) [HttpGet("/")] public IActionResult GetRootRoute() { + RouteCounter + .WithLabels("/") + .Inc(); + string url = Config.RootRoute; return Redirect(url); diff --git a/LinkRouter/LinkRouter.csproj b/LinkRouter/LinkRouter.csproj index 13c742b..2eb2d1b 100644 --- a/LinkRouter/LinkRouter.csproj +++ b/LinkRouter/LinkRouter.csproj @@ -12,6 +12,7 @@ + diff --git a/LinkRouter/Program.cs b/LinkRouter/Program.cs index 4120bf1..e676022 100644 --- a/LinkRouter/Program.cs +++ b/LinkRouter/Program.cs @@ -5,6 +5,7 @@ using LinkRouter.App.Services; using MoonCore.Extensions; using MoonCore.Helpers; +using Prometheus; namespace LinkRouter; @@ -45,8 +46,14 @@ public static void Main(string[] args) builder.Services.AddSingleton(config); + builder.Services.AddMetricServer(options => + { + options.Port = 5000; + }); + var app = builder.Build(); + app.UseMetricServer(); app.MapControllers(); app.Run();