tenacity icon indicating copy to clipboard operation
tenacity copied to clipboard

Statistic `delay_since_first_attempt` does not get updated if the last attempt is successful

Open DougMHu opened this issue 3 years ago • 2 comments

For some reason, tenacity's statistics don't calculate the elapsed time like I would expect. I would expect the below example to log that 3.5 sec elapsed, but delay_since_first_attempt says 2 sec. I think the statistics don't get updated with the time elapsed following the second to last attempt if the last attempt was successful.

In [40]: attempt_num_list = [1]

In [41]: @retry(stop=stop_after_delay(10), wait=wait_fixed(1))
    ...: def succeed_on_attempt(success_attempt_num, execution_time_s=0.5):
    ...:     print("Attempt {}".format(attempt_num_list[0]))
    ...:     print("Start {}".format(time.time()))
    ...:     time.sleep(execution_time_s)
    ...:     if attempt_num_list[0] != success_attempt_num:
    ...:         attempt_num_list[0] += 1
    ...:         print("Stop {}".format(time.time()))
    ...:         raise Exception
    ...:     print("Stop {}".format(time.time()))
    ...:

In [42]: succeed_on_attempt(3)
Attempt 1
Start 1623168327.531153
Stop 1623168328.031927
Attempt 2
Start 1623168329.0345838
Stop 1623168329.5350487
Attempt 3
Start 1623168330.536717
Stop 1623168331.038352

In [43]: print(succeed_on_attempt.retry.statistics)
{'start_time': 1459638.89, 'attempt_number': 3, 'idle_for': 2, 'delay_since_first_attempt': 2.0}

DougMHu avatar Jun 09 '21 16:06 DougMHu

From another thread:

Does line 371 in the listing below just need to move up to 361, so that we set this metric after successful attempts too?

https://github.com/jd/tenacity/blob/e31e0119aa37919d90a388177add3ba027ec0f3f/tenacity/init.py#L355-L392

Originally posted by @asqui in https://github.com/jd/tenacity/issues/84#issuecomment-857193820

I would set the delay metric immediately before the future result return and not remove the existing setting of the metric. You will create a breaking change for any user that has an after that takes time otherwise. I haven't used this library in a while and don't know if this statistic is being reported as intended or not. This is a really old unrelated issue, so I'm going to unsubscribe if you'd like a response from me please explicitly @.

Originally posted by @Brian-Williams in https://github.com/jd/tenacity/issues/84#issuecomment-857286274

DougMHu avatar Jun 09 '21 16:06 DougMHu