Skip to content

Commit

Permalink
fix: optimize meters
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanJosipovic committed Oct 31, 2023
1 parent 64ef5ec commit 7064310
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 20 deletions.
36 changes: 16 additions & 20 deletions src/oidc-guard/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
using oidc_guard.Services;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using System.Diagnostics.Metrics;
using System.Text.Json.Nodes;
using System.Text.RegularExpressions;

Expand Down Expand Up @@ -63,6 +62,8 @@ public static void Main(string[] args)

builder.Services.AddMetrics();

builder.Services.AddSingleton<Instrumentation>();

builder.Logging.AddFilter("Default", settings.LogLevel);
builder.Logging.AddFilter("Microsoft.AspNetCore", LogLevel.Warning);
builder.Logging.AddFilter("Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware", settings.LogLevel);
Expand Down Expand Up @@ -238,22 +239,17 @@ public static void Main(string[] args)
app.MapGet("/userinfo", (HttpContext httpContext) => httpContext.User.Claims.GroupBy(x => x.Type).ToDictionary(x => x.Key, y => y.Count() > 1 ? (object)y.Select(x => x.Value) : y.First().Value))
.RequireAuthorization();

app.MapGet("/auth", ([FromServices] Settings settings, [FromServices] IMeterFactory meterFactory, HttpContext httpContext) =>
app.MapGet("/auth", ([FromServices] Settings settings, [FromServices] Instrumentation meters, HttpContext httpContext) =>
{
meterFactory.Create("oidc_guard").CreateCounter<long>("oidc_guard_signin", description: "Number of Sign-in operations ongoing.").Add(1);

var meter = meterFactory.Create("oidc_guard");

var AuthorizedCounter = meter.CreateCounter<long>("oidc_guard_authorized", description: "Number of Authorized operations ongoing.");
var UnauthorizedCounter = meter.CreateCounter<long>("oidc_guard_unauthorized", description: "Number of Unauthorized operations ongoing.");
meters.SignInCounter.Add(1);

if (settings.SkipAuthPreflight &&
httpContext.Request.Headers[CustomHeaderNames.OriginalMethod][0] == HttpMethod.Options.Method &&
!StringValues.IsNullOrEmpty(httpContext.Request.Headers.AccessControlRequestHeaders) &&
!StringValues.IsNullOrEmpty(httpContext.Request.Headers.AccessControlRequestMethod) &&
!StringValues.IsNullOrEmpty(httpContext.Request.Headers.Origin))
{
AuthorizedCounter.Add(1);
meters.AuthorizedCounter.Add(1);
return Results.Ok();
}

Expand All @@ -276,15 +272,15 @@ public static void Main(string[] args)

if (method == originalMethod && Regex.IsMatch(originalUrl, regex))
{
AuthorizedCounter.Add(1);
meters.AuthorizedCounter.Add(1);
return Results.Ok();
}
}
else
{
if (Regex.IsMatch(originalUrl, item))
{
AuthorizedCounter.Add(1);
meters.AuthorizedCounter.Add(1);
return Results.Ok();
}
}
Expand All @@ -303,15 +299,15 @@ public static void Main(string[] args)

if (method != originalMethod && !Regex.IsMatch(originalUrl, regex))
{
AuthorizedCounter.Add(1);
meters.AuthorizedCounter.Add(1);
return Results.Ok();
}
}
else
{
if (!Regex.IsMatch(originalUrl, item))
{
AuthorizedCounter.Add(1);
meters.AuthorizedCounter.Add(1);
return Results.Ok();
}
}
Expand All @@ -321,7 +317,7 @@ public static void Main(string[] args)

if (httpContext.User.Identity?.IsAuthenticated == false)
{
UnauthorizedCounter.Add(1);
meters.UnauthorizedCounter.Add(1);
return Results.Unauthorized();
}

Expand Down Expand Up @@ -419,37 +415,37 @@ public static void Main(string[] args)
}
else if (!httpContext.User.Claims.Any(x => x.Type == item.Key && item.Value.Contains(x.Value)))
{
UnauthorizedCounter.Add(1);
meters.UnauthorizedCounter.Add(1);
//return Results.Unauthorized($"Claim {item.Key} does not match!");
return Results.Unauthorized();
}
}
}

AuthorizedCounter.Add(1);
meters.AuthorizedCounter.Add(1);
return Results.Ok();
});

app.MapGet("/signin", ([FromServices] Settings settings, [FromServices] IMeterFactory meterFactory, [FromQuery] Uri rd) =>
app.MapGet("/signin", ([FromServices] Settings settings, [FromServices] Instrumentation meters, [FromQuery] Uri rd) =>
{
if (!ValidateRedirect(rd, settings))
{
return Results.BadRequest();
}

meterFactory.Create("oidc_guard").CreateCounter<long>("oidc_guard_signin", description: "Number of Sign-in operations ongoing.").Add(1);
meters.SignInCounter.Add(1);

return Results.Challenge(new AuthenticationProperties { RedirectUri = rd.ToString() });
});

app.MapGet("/signout", ([FromServices] Settings settings, [FromServices] IMeterFactory meterFactory, [FromQuery] Uri rd) =>
app.MapGet("/signout", ([FromServices] Settings settings, [FromServices] Instrumentation meters, [FromQuery] Uri rd) =>
{
if (!ValidateRedirect(rd, settings))
{
return Results.BadRequest();
}

meterFactory.Create("oidc_guard").CreateCounter<long>("oidc_guard_signout", description: "Number of Sign-out operations ongoing.").Add(1);
meters.SignOutCounter.Add(1);

return Results.SignOut(new AuthenticationProperties { RedirectUri = rd.ToString() });
})
Expand Down
24 changes: 24 additions & 0 deletions src/oidc-guard/Services/Instrumentation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics.Metrics;

namespace oidc_guard.Services
{
public class Instrumentation
{
public Counter<long> SignInCounter { get; private set; }
public Counter<long> SignOutCounter { get; private set; }
public Counter<long> AuthorizedCounter { get; private set; }
public Counter<long> UnauthorizedCounter { get; private set; }

public Instrumentation(IMeterFactory meterFactory)
{
const string prefix = "oidc_guard";
var meter = meterFactory.Create(prefix);

SignInCounter = meter.CreateCounter<long>(prefix + "_signin", description: "Number of Sign-in operations ongoing.");
SignOutCounter = meter.CreateCounter<long>(prefix + "_signout", description: "Number of Sign-out operations ongoing.");
AuthorizedCounter = meter.CreateCounter<long>(prefix + "_authorized", description: "Number of Authorized operations ongoing.");
UnauthorizedCounter = meter.CreateCounter<long>(prefix + "_unauthorized", description: "Number of Unauthorized operations ongoing.");
}
}
}

0 comments on commit 7064310

Please sign in to comment.