title: Executing Premetheus Middleware on /metrics path only in asp.net core App
body: <p>From the documentation of Prometheus, I implemented a middleware in order to create metrics. Prometheus out puts text file of these metrics by default in /metrics end point ... it works perfectly fine but the problem is that that middleware get called for each and every page hit which make app super slow...</p>
  
  <p>how can I make that middleware to be called only when user request for /metrics ?
  Im sorry if question is not that clear because this is my first experience with Prometheus on asp.net core app</p>
  
  <p>I used Prometheus-net.AspNetCore library</p>
  
  <p>MetricsMiddleware.cs</p>
  
  <pre><code>public class MetricsMiddleware
  {
          private readonly RequestDelegate _next;
  
          public MetricsMiddleware(RequestDelegate next)
          {
              this._next = next;
          }
  
          public async Task Invoke(HttpContext httpContext)
          {
                 await _next.Invoke(httpContext);
  
              //custome metrics created here
          }
  
          public static class MetricsMiddlewareExtensions
          {
            public static IApplicationBuilder UseRequestMiddleware(this IApplicationBuilder builder)
            {
              return builder.UseMiddleware&lt;RequestMiddleware&gt;();
            }
          }
  }
  </code></pre>
  
  <p>stratup.cs file: </p>
  
  <pre><code>public void Configure(IApplicationBuilder app, ....)
  
  {
  
  app.UseMetricServer();            
  app.UseMetricsMiddleware();
  
  }
  </code></pre>