diff --git a/Wave/Controllers/RssController.cs b/Wave/Controllers/RssController.cs index bed73ab..86b70b3 100644 --- a/Wave/Controllers/RssController.cs +++ b/Wave/Controllers/RssController.cs @@ -6,12 +6,13 @@ using Microsoft.Extensions.Options; using Wave.Data; using Wave.Utilities; +using Wave.Utilities.Metrics; namespace Wave.Controllers; [ApiController] [Route("/[controller]")] -public class RssController(IOptions customizations, ApplicationDbContext context, IOptions features) : ControllerBase { +public class RssController(IOptions customizations, ApplicationDbContext context, IOptions features, RssMetrics metrics) : ControllerBase { private ApplicationDbContext Context { get; } = context; private IOptions Customizations { get; } = customizations; private IOptions Features { get; } = features; @@ -22,6 +23,8 @@ public class RssController(IOptions customizations, ApplicationDb public async Task GetRssFeedAsync(string? category = null, Guid? author = null) { if (!Features.Value.Rss) return new JsonResult("RSS is disabled") {StatusCode = StatusCodes.Status401Unauthorized}; + metrics.RssRequestReceived("application/rss+xml", category, author?.ToString()); + if (category is not null || author.HasValue) Response.Headers.Append("x-robots-tag", "noindex"); @@ -36,6 +39,8 @@ public class RssController(IOptions customizations, ApplicationDb public async Task GetAtomFeedAsync(string? category = null, Guid? author = null) { if (!Features.Value.Rss) return new JsonResult("RSS is disabled") {StatusCode = StatusCodes.Status401Unauthorized}; + metrics.RssRequestReceived("application/atom+xml", category, author?.ToString()); + if (category is not null || author.HasValue) Response.Headers.Append("x-robots-tag", "noindex"); diff --git a/Wave/Program.cs b/Wave/Program.cs index 49b4a5e..d36e021 100644 --- a/Wave/Program.cs +++ b/Wave/Program.cs @@ -294,6 +294,7 @@ .AddMeter("Microsoft.AspNetCore.Http.Routing") .AddMeter("Microsoft.AspNetCore.Diagnostics") .AddMeter("Wave.Api") + .AddMeter("Wave.Rss") .AddPrometheusExporter()); // Jaeger etc. @@ -305,6 +306,7 @@ }); } + builder.Services.AddSingleton(); builder.Services.AddSingleton(); } diff --git a/Wave/Utilities/Metrics/RssMetrics.cs b/Wave/Utilities/Metrics/RssMetrics.cs new file mode 100644 index 0000000..f736d37 --- /dev/null +++ b/Wave/Utilities/Metrics/RssMetrics.cs @@ -0,0 +1,21 @@ +using System.Diagnostics.Metrics; + +namespace Wave.Utilities.Metrics; + +public class RssMetrics { + private Counter RssRequestCounter { get; } + + public RssMetrics(IMeterFactory meterFactory) { + var meter = meterFactory.Create("Wave.Rss"); + + RssRequestCounter = meter.CreateCounter("wave.rss.requests", "{requests}", + "Counts incoming requests processed on RSS endpoints"); + } + + public void RssRequestReceived(string type, string? category, string? author) { + RssRequestCounter.Add(1, + new KeyValuePair("wave.rss.type", type), + new KeyValuePair("wave.rss.category", category), + new KeyValuePair("wave.rss.author", author)); + } +} \ No newline at end of file