Skip to content

ensure that the elasticsearch instrumentation handles DroppedSpans correctly #1190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
ensure that the elasticsearch instrumentation handles DroppedSpans co…
…rrectly

If the elasticsearch span is dropped for some reason, the context object
is None, which, if unhandled, leads to an exception

fixes #1188
  • Loading branch information
beniwohli committed Jul 12, 2021
commit 7ab6d946869af018b97454263b7268b73ea03c80
4 changes: 3 additions & 1 deletion elasticapm/instrumentation/packages/elasticsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

import elasticapm
from elasticapm.instrumentation.packages.base import AbstractInstrumentedModule
from elasticapm.traces import execution_context
from elasticapm.traces import DroppedSpan, execution_context
from elasticapm.utils.logging import get_logger

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

def call(self, module, method, wrapped, instance, args, kwargs):
span = execution_context.get_span()
if isinstance(span, DroppedSpan):
return wrapped(*args, **kwargs)

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

Expand Down
15 changes: 15 additions & 0 deletions tests/instrumentation/elasticsearch_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
from elasticsearch import Elasticsearch
from elasticsearch.serializer import JSONSerializer

import elasticapm
from elasticapm.conf.constants import TRANSACTION
from elasticapm.utils import compat

Expand Down Expand Up @@ -550,3 +551,17 @@ def test_custom_serializer(instrument, elasticapm_client, elasticsearch):
span = spans[0]
assert json.loads(span["context"]["db"]["statement"]) == json.loads('{"query":{"term":{"2":{"value":1}}}}')
assert span["context"]["http"]["status_code"] == 200


@pytest.mark.integrationtest
def test_dropped_span(instrument, elasticapm_client, elasticsearch):
elasticapm_client.begin_transaction("test")
with elasticapm.capture_span("test", leaf=True):
elasticsearch.ping()
elasticapm_client.end_transaction("test", "OK")

transaction = elasticapm_client.events[TRANSACTION][0]
spans = elasticapm_client.spans_for_transaction(transaction)
assert len(spans) == 1
span = spans[0]
assert span["name"] == "test"