elastic / elastic/apm-agent-python

When APM is disabled, OTEL's `start_span` causes exception

Open
#2,163 3 comments 0 reactions 0 assignees View on GitHub
agent-python community triage
Dominant language
Python
Stars
431
Forks
239
Avg merge
5d 10h
Merged PRs (30d)
7

Description

**Describe the bug**: ...
When `ELASTIC_APM_ENABLED` is set to `false`, `start_span()` should not result in an exception. This happens when the Tracer from `from elasticapm.contrib.opentelemetry.trace` is used.

**To Reproduce**

1. Set `ELASTIC_APM_ENABLED` to `false`
2. Call `start_as_current_span` or `start_span` (from `Tracer` of `elasticapm.contrib.opentelemetry.trace`)
3. Notice an exception is thrown

**Environment (please complete the following information)**
- OS: Mac OS, Linux
- Python version: 3.12
- Framework and version [e.g. Django 2.1]: N/A
- APM Server version: N/A
- Agent version: `6.23.0`

**Additional context**

Here's the exception:
```
with tracer.start_as_current_span(name) as span:\\n
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\\n
| File \\\"/usr/local/lib/python3.12/contextlib.py\\\", line 137, in __enter__\\n
| return next(self.gen)\\n
| ^^^^^^^^^^^^^^\\n
| File \\\"/app/.venv/lib/python3.12/site-packages/elasticapm/contrib/opentelemetry/trace.py\\\", line 230, in start_as_current_span\\n
| span = self.start_span(\\n
| ^^^^^^^^^^^^^^^^\\n
| File \\\"/app/.venv/lib/python3.12/site-packages/elasticapm/contrib/opentelemetry/trace.py\\\", line 150, in start_span\\n
| span = Span(\\n
| ^^^^^\\n
| File \\\"/app/.venv/lib/python3.12/site-packages/elasticapm/contrib/opentelemetry/span.py\\\", line 65, in __init__\\n
| elastic_span.otel_wrapper = self\\n
| ^^^^^^^^^^^^^^^^^^^^^^^^^\\n
| AttributeError: 'NoneType' object has no attribute 'otel_wrapper'\\n
```

- Agent config options

Click to expand

```
client = elasticapm.Client(
central_config=False,
transaction_sample_rate=1,
)

tracer = Tracer(__name__)
```

- `requirements.txt`:

Click to expand

```
elastic-apm = "^6.23.0"
opentelemetry-api = "^1.28.1"
opentelemetry-sdk = "^1.28.1"
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.