Skip to content

Commit 7ab6d94

Browse files
committed
ensure that the elasticsearch instrumentation handles DroppedSpans correctly
If the elasticsearch span is dropped for some reason, the context object is None, which, if unhandled, leads to an exception fixes elastic#1188
1 parent b9f6aa7 commit 7ab6d94

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

elasticapm/instrumentation/packages/elasticsearch.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
import elasticapm
3636
from elasticapm.instrumentation.packages.base import AbstractInstrumentedModule
37-
from elasticapm.traces import execution_context
37+
from elasticapm.traces import DroppedSpan, execution_context
3838
from elasticapm.utils.logging import get_logger
3939

4040
logger = get_logger("elasticapm.instrument")
@@ -52,6 +52,8 @@ class ElasticsearchConnectionInstrumentation(AbstractInstrumentedModule):
5252

5353
def call(self, module, method, wrapped, instance, args, kwargs):
5454
span = execution_context.get_span()
55+
if isinstance(span, DroppedSpan):
56+
return wrapped(*args, **kwargs)
5557

5658
self._update_context_by_request_data(span.context, instance, args, kwargs)
5759

tests/instrumentation/elasticsearch_tests.py

+15
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
from elasticsearch import Elasticsearch
4040
from elasticsearch.serializer import JSONSerializer
4141

42+
import elasticapm
4243
from elasticapm.conf.constants import TRANSACTION
4344
from elasticapm.utils import compat
4445

@@ -550,3 +551,17 @@ def test_custom_serializer(instrument, elasticapm_client, elasticsearch):
550551
span = spans[0]
551552
assert json.loads(span["context"]["db"]["statement"]) == json.loads('{"query":{"term":{"2":{"value":1}}}}')
552553
assert span["context"]["http"]["status_code"] == 200
554+
555+
556+
@pytest.mark.integrationtest
557+
def test_dropped_span(instrument, elasticapm_client, elasticsearch):
558+
elasticapm_client.begin_transaction("test")
559+
with elasticapm.capture_span("test", leaf=True):
560+
elasticsearch.ping()
561+
elasticapm_client.end_transaction("test", "OK")
562+
563+
transaction = elasticapm_client.events[TRANSACTION][0]
564+
spans = elasticapm_client.spans_for_transaction(transaction)
565+
assert len(spans) == 1
566+
span = spans[0]
567+
assert span["name"] == "test"

0 commit comments

Comments
 (0)