This code example was designed to increase the observability of how fast certain layers in the pipeline are executed. By seeing how long each layer was executed, you can localize the problem area, which you can try to speed up
Due to the fact that Diagnostic Context is injected into dependency injection as scoped, each of the pipelines will have a personal instance throughout its execution
builder.Services.AddScoped<IDiagnosticContext, DiagnosticContext>();
When the diagnostic context is first called, it will check inside itself whether the entry point has been set so that it can then be filtered by it, the entry point can be said as the name of the entire pipeline. For example, if we made an http request to our application, the controller will pick it up, and if we marked up the controller layer, then the entry point will be controllerName.ControllerMethodName
[HttpGet("MethodOne")]
public async Task<IActionResult> MethodOne()
{
using (_diagnosticContext.Measure($"{nameof(ControllerOne)}.{nameof(MethodOne)}"))
{
await _serviceOne.MethodOne();
await _serviceTwo.MethodOne();
}
return Ok();
}
public IDisposable Measure(string methodName)
{
_entryPoint ??= methodName;
var stopwatch = new Stopwatch();
stopwatch.Start();
return new DisposableAction(() =>
{
stopwatch.Stop();
var duration = stopwatch.ElapsedMilliseconds;
_metricGaugeDuration.WithLabels(methodName, _entryPoint).Set(duration);
});
}
Execute the docker-compose up
command, thus you will launch grafana & prometheus, then separately launch WebAPI first, then launch RequestSpammer.
Request Spammer is needed to create requests to our WebAPI application