-
Notifications
You must be signed in to change notification settings - Fork 17
Open
Description
After taking a closer look at the UseEmfMiddleware - middleware responsable for measuring the time a request takes to be completed - I could notice that it is instanciating a new Stopwatch every request. As it can lead to potential performance issues in scenarios where the api is called hundreds or thousands of times I propose a change to use something like the ValueStopwatch struct used by the dotnet team to measure time without allocating a new object on the heap. I have already implemented those change on the PR #60 - Changes UseEmfMiddleware to avoid allocating a new stopwatch instance per request.
Middleware code as it is now:
public static void UseEmfMiddleware(this IApplicationBuilder app, Func<HttpContext, IMetricsLogger, Task> action)
{
app.Use(async (context, next) =>
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
await next.Invoke();
var config = context.RequestServices.GetRequiredService<EMF.Config.IConfiguration>();
var logger = context.RequestServices.GetRequiredService<IMetricsLogger>();
if (!String.IsNullOrEmpty(config.ServiceName))
{
logger.SetNamespace(config.ServiceName);
}
await action(context, logger);
stopWatch.Stop();
logger.PutMetric("Time", stopWatch.ElapsedMilliseconds, Model.Unit.MILLISECONDS);
});
}
Metadata
Metadata
Assignees
Labels
No labels