Skip to content

Commit cbd8f95

Browse files
authored
chore(app): Add configurable exclusions for feature metric tracking (#2753)
## Related Issue(s) - #2376 ## Verification - [x] **Your** code builds clean without any errors or warnings - [x] Manual testing done (required) - [ ] Relevant automated test added (if you find this hard, leave it and we'll help out)
1 parent 1d91943 commit cbd8f95

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

src/Digdir.Domain.Dialogporten.WebApi/Common/FeatureMetric/FeatureMetricMiddleware.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
using Digdir.Domain.Dialogporten.Application.Common.Behaviours.FeatureMetric;
2+
using Microsoft.Extensions.Options;
23

34
namespace Digdir.Domain.Dialogporten.WebApi.Common.FeatureMetric;
45

56
/// <summary>
67
/// Middleware to handle feature metric delivery acknowledgments based on HTTP response status codes
78
/// </summary>
8-
public sealed class FeatureMetricMiddleware(RequestDelegate next)
9+
public sealed class FeatureMetricMiddleware(RequestDelegate next, IOptions<FeatureMetricOptions> options)
910
{
1011
private readonly RequestDelegate _next = next ?? throw new ArgumentNullException(nameof(next));
12+
private readonly FeatureMetricOptions _options = options.Value;
1113

1214
public async Task InvokeAsync(HttpContext context)
1315
{
16+
// Skip feature metric tracking for health check endpoints
17+
if (ShouldSkipFeatureMetrics(context))
18+
{
19+
await _next(context);
20+
return;
21+
}
22+
1423
// TODO: Analyze token to extract user and org info for feature metrics - do not log SSNs
1524
var deliveryContext = context.RequestServices.GetRequiredService<IFeatureMetricDeliveryContext>();
1625
try
@@ -34,6 +43,18 @@ public async Task InvokeAsync(HttpContext context)
3443

3544
private static bool IsSuccessStatusCode(int statusCode) => statusCode is >= 200 and < 300;
3645

46+
private bool ShouldSkipFeatureMetrics(HttpContext context)
47+
{
48+
var path = context.Request.Path.Value;
49+
if (string.IsNullOrEmpty(path))
50+
{
51+
return false;
52+
}
53+
54+
return _options.ExcludedPathPrefixes.Any(excludedPath =>
55+
path.StartsWith(excludedPath, StringComparison.OrdinalIgnoreCase));
56+
}
57+
3758
private static string GeneratePresentationTag(HttpContext context)
3859
{
3960
var method = context.Request.Method;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace Digdir.Domain.Dialogporten.WebApi.Common.FeatureMetric;
2+
3+
/// <summary>
4+
/// Configuration options for feature metric tracking.
5+
/// </summary>
6+
public sealed class FeatureMetricOptions
7+
{
8+
/// <summary>
9+
/// Gets or sets the list of path prefixes to exclude from feature metric tracking.
10+
/// </summary>
11+
public List<string> ExcludedPathPrefixes { get; set; } = new()
12+
{
13+
"/health",
14+
"/metrics",
15+
"/swagger",
16+
"/openapi"
17+
};
18+
}

src/Digdir.Domain.Dialogporten.WebApi/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ static void BuildAndRun(string[] args)
9696
.AddAspNetCoreInstrumentationExcludingHealthPaths())
9797
// Options setup
9898
.ConfigureOptions<AuthorizationOptionsSetup>()
99+
.Configure<FeatureMetricOptions>(builder.Configuration.GetSection("FeatureMetrics"))
99100

100101
// Clean architecture projects
101102
.AddApplication(builder.Configuration, builder.Environment)

0 commit comments

Comments
 (0)