Description
Describe the bug:
I run into an issue, where the KafkaInstrumentation class throws an exception in line 146 of https://github.com/elastic/apm-agent-python/blob/main/elasticapm/instrumentation/packages/kafka.py:
AttributeError: 'NoneType' object has no attribute 'cancel'
this happens when iterating over KafkaConsumer. Indeed, looking at the code, in https://github.com/elastic/apm-agent-python/blob/main/elasticapm/instrumentation/packages/kafka.py, line 132 a context manager is created; the capture_span class calls handle_enter (in https://github.com/elastic/apm-agent-python/blob/main/elasticapm/traces.py, line 1085), which might return None,
but inside of the with block the check (if span) is done only in the subsequent line.
Environment (please complete the following information)
- OS: Linux
- Python version: 3.9.17
- Framework and version [e.g. Django 2.1]: Django 3.2.19
- Agent version: 6.15.1