Added metrics to rss endpoints
Some checks failed
Docker Release / build (push) Has been cancelled
GitHub Release / Generate Release (push) Has been cancelled

This commit is contained in:
Mia Rose Winter 2024-04-11 14:00:43 +02:00
parent a64e16f681
commit ac9a2f3970
Signed by: miawinter
GPG key ID: 4B6F6A83178F595E
3 changed files with 29 additions and 1 deletions

View file

@ -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<Customization> customizations, ApplicationDbContext context, IOptions<Features> features) : ControllerBase {
public class RssController(IOptions<Customization> customizations, ApplicationDbContext context, IOptions<Features> features, RssMetrics metrics) : ControllerBase {
private ApplicationDbContext Context { get; } = context;
private IOptions<Customization> Customizations { get; } = customizations;
private IOptions<Features> Features { get; } = features;
@ -22,6 +23,8 @@ public class RssController(IOptions<Customization> customizations, ApplicationDb
public async Task<IActionResult> 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<Customization> customizations, ApplicationDb
public async Task<IActionResult> 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");

View file

@ -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<RssMetrics>();
builder.Services.AddSingleton<ApiMetrics>();
}

View file

@ -0,0 +1,21 @@
using System.Diagnostics.Metrics;
namespace Wave.Utilities.Metrics;
public class RssMetrics {
private Counter<int> RssRequestCounter { get; }
public RssMetrics(IMeterFactory meterFactory) {
var meter = meterFactory.Create("Wave.Rss");
RssRequestCounter = meter.CreateCounter<int>("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<string, object?>("wave.rss.type", type),
new KeyValuePair<string, object?>("wave.rss.category", category),
new KeyValuePair<string, object?>("wave.rss.author", author));
}
}