Skip to content

DurationMetric: do not use a member variable when decorating

Ed Baunton requested to merge edbaunton/bugfix/duration-metric-scope into master

Metrics are used both as decorators as well as a context. When being used a context the lifetime of the object is specific to that instantiation. When being used as a decorator however the metric is created at application startup time.

Previously at object construction we would start the stopwatch and then stop it after the decorator has had a function invoked. In practice this meant that the stopwatch was started at the wrong time.

This change updates the logic such when being as a decorated no member variables are used to record the start time. Only a locally scoped variable is needed since we return back to the same scope once the decorated function returns.

Added tests that demonstrate the issue: using a DurationMetric decorated in a nested context will trigger the issue as start_time was already set when the second call stopwatch is started.

Merge request reports