Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ This will be the last minor release to support the following versions:
* Use Span ID as parent ID in errors if an error happens inside a span {pull}669[#669]
* Added experimental support for API Key authentication {pull}679[#679]

[float]
===== Bug fixes

* introduced workaround to avoid instrumenting twice in rare cases (#708)


[[release-notes-5.x]]
=== Python Agent version 5.x
Expand Down
12 changes: 11 additions & 1 deletion elasticapm/instrumentation/packages/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@
logger = get_logger("elasticapm.instrument")


class ElasticAPMFunctionWrapper(wrapt.FunctionWrapper):
# used to differentiate between our own function wrappers and 1st/3rd party wrappers
pass


class AbstractInstrumentedModule(object):
"""
This class is designed to reduce the amount of code required to
Expand Down Expand Up @@ -136,8 +141,13 @@ def instrument(self):
# We jump through hoop here to get the original
# `module`/`method` in the call to `call_if_sampling`
parent, attribute, original = wrapt.resolve_path(module, method)
if isinstance(original, ElasticAPMFunctionWrapper):
logger.debug("%s.%s already instrumented, skipping", module, method)
continue
self.originals[(module, method)] = original
wrapper = wrapt.FunctionWrapper(original, functools.partial(self.call_if_sampling, module, method))
wrapper = ElasticAPMFunctionWrapper(
original, functools.partial(self.call_if_sampling, module, method)
)
wrapt.apply_patch(parent, attribute, wrapper)
instrumented_methods.append((module, method))
except ImportError:
Expand Down
23 changes: 22 additions & 1 deletion tests/instrumentation/base_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import pytest

import elasticapm
from elasticapm.conf import constants
from elasticapm.conf.constants import SPAN
from elasticapm.instrumentation.packages.base import AbstractInstrumentedModule
from elasticapm.utils import compat, wrapt
Expand Down Expand Up @@ -65,7 +66,8 @@ class _TestDummyInstrumentation(AbstractInstrumentedModule):
def call(self, module, method, wrapped, instance, args, kwargs):
kwargs = kwargs or {}
kwargs["call_args"] = (module, method)
return wrapped(*args, **kwargs)
with elasticapm.capture_span("dummy"):
return wrapped(*args, **kwargs)


def test_instrument_nonexisting_method_on_module():
Expand All @@ -79,6 +81,25 @@ def test_instrument_nonexisting_method(caplog):
assert "has no attribute" in record.message


def test_double_instrument(elasticapm_client):
elasticapm_client.begin_transaction("test")
inst = _TestDummyInstrumentation()
try:
inst.instrument()
assert hasattr(Dummy.dummy, "_self_wrapper")
Dummy().dummy()
elasticapm_client.end_transaction()
assert len(elasticapm_client.spans_for_transaction(elasticapm_client.events[constants.TRANSACTION][0])) == 1
inst.instrumented = False
inst.instrument()
elasticapm_client.begin_transaction("test")
Dummy().dummy()
elasticapm_client.end_transaction()
assert len(elasticapm_client.spans_for_transaction(elasticapm_client.events[constants.TRANSACTION][1])) == 1
finally:
inst.uninstrument()


@pytest.mark.skipif(compat.PY3, reason="different object model")
def test_uninstrument_py2(caplog):
assert isinstance(Dummy.dummy, types.MethodType)
Expand Down