rocklan / prometheus-net.AspNet

Exposes prometheus metrics inside full framework asp.net applications using WebApi
MIT License
19 stars 4 forks source link

Possible to cleanly apply custom AuthorizeAttribute to PrometheusMetricsController? #6

Closed Waddlen closed 3 years ago

Waddlen commented 3 years ago

I want to protect the /metrics endpoint with my own auth attribute.

public class MyAuthAttribute : AuthorizeAttribute
{
    public override void OnAuthorization(HttpActionContext actionContext)
    {
        ... // stuff here
    }
}

However, I'm not sure how to cleanly apply this to PrometheusMetricsController. The cleanest I could come up with was inheriting it & applying it to the child:

[MyAuth]
public class TestController : PrometheusMetricsController
{
}

Then replacing the WebApiConfig.cs line PrometheusConfig.UseMetricsServer(config, "metrics"); with config.Routes.MapHttpRoute("Prometheus", "metrics", new { controller = "Test" });

Do you have any better ideas?

Thank you.

rocklan commented 3 years ago

Looks like a great solution to me. Great job!

rocklan commented 3 years ago

If you are implementing oAuth or something similar, consider creating a PR so that others can use it too :)