apm-agent-python
apm-agent-python copied to clipboard
Type Inconsistency: capture_span()
Describe the bug:
capture_span() types start as int instead of float
To Reproduce
- apm_client.start_transaction()
- t0 = time.time()
- do_something()
- t1 = time.time()
- dur = t1-t0
- do_something_else()
- span = elasticapm.capture_span(start=int(t0), duration=dur)
- with span as s: pass
- apm_client.end_transaction()
- 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().
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.
@Masterdanielsan Does https://github.com/elastic/apm-agent-python/pull/2335 fix the issue for you?