apm-agent-python icon indicating copy to clipboard operation
apm-agent-python copied to clipboard

Type Inconsistency: capture_span()

Open Masterdanielsan opened this issue 6 months ago • 1 comments

Describe the bug:

capture_span() types start as int instead of float

To Reproduce

  1. apm_client.start_transaction()
  2. t0 = time.time()
  3. do_something()
  4. t1 = time.time()
  5. dur = t1-t0
  6. do_something_else()
  7. span = elasticapm.capture_span(start=int(t0), duration=dur)
  8. with span as s: pass
  9. apm_client.end_transaction()
  10. Note that span will not fall in transaction time frame correctly.

Environment (please complete the following information)

  • OS: mac
  • Python version: 3.12.10
  • Framework and version: FastApi 0.111.1
  • APM Server version: 6.23.0
  • Agent version: N/A

Additional context

elasticapm.capture_span() takes an optional argument start, which is typed to be an int. This works well enough with a time.time() float, but if you are using mypy, you will realize that it needs an int. Only problem is that capture_span() passes start to Span(BaseSpan), which calls super().__init__() which initializes start as a float, passing it through time_to_perf_counter(start). This method fails miserably with an int representation of time.time().

Masterdanielsan avatar May 01 '25 16:05 Masterdanielsan

Thanks for reporting and for the analysis, I think the typing is wrong since time_to_perf_counter expects the timestamp to be a float in seconds.

xrmx avatar May 20 '25 08:05 xrmx

@Masterdanielsan Does https://github.com/elastic/apm-agent-python/pull/2335 fix the issue for you?

xrmx avatar Jul 04 '25 13:07 xrmx