marcobreveglieri / dmvc-prometheus-metrics

Delphi MVC Framework middleware for exposing metrics through Prometheus Client library for Delphi
MIT License
4 stars 3 forks source link

Enhancement - Add timestamp for start of request #3

Open Tuxino opened 2 weeks ago

Tuxino commented 2 weeks ago

Its useful to be able to generate elapsed request time for successful and unsuccessful requests.

For a successful request, we can write event handlers for OnBeforeAction and OnAfterAction to determine elapsed time and add this to a Prometheus Histogram. However, if an error occurs, its possible that neither of these event handlers get called eg. for a 404.

I propose that we add a timestamp to the existing DMVC middleware

procedure TMetricsMiddleware.OnBeforeRouting(Context: TWebContext; var Handled: Boolean);
begin
  Context.Data.AddOrSetValue('RequestStartTime', FloatToStr(Now));
  // Check whether the current path request matches the metrics one.
  if not SameText(Context.Request.PathInfo, FPathInfo) then
    Exit;
...

This enables either the OnAfterAction or a global exception handler to figure out the elapsed time and add it to a histogram.

marcobreveglieri commented 6 days ago

Hi @Tuxino, thanks for your suggestion, I think it's really a good idea!

Would you be able to provide a simple pull request with a code sample to propose and evaluate?

It would be very much appreciated to have a real scenario to evaluate and possibly integrate into the main branch.

Thanks for your feedback! 💘 Marco.