Skip to content

Commit 24ab999

Browse files
authored
Fix starlette to not instrument when enabled=False (#1000)
* Fix starlette to not instrument when enabled=False * Add a test * CHANGELOG and lint
1 parent c0dd34d commit 24ab999

File tree

4 files changed

+27
-6
lines changed

4 files changed

+27
-6
lines changed

.pre-commit-config.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
repos:
22
- repo: https://github.com/pycqa/isort
3-
rev: 5.5.4
3+
rev: 5.7.0
44
hooks:
55
- id: isort
66
- repo: https://github.com/ambv/black
7-
rev: stable
7+
rev: 20.8b1
88
hooks:
99
- id: black
1010
language_version: python3
11-
- repo: https://github.com/pre-commit/pre-commit-hooks
12-
rev: v2.3.0
11+
- repo: https://gitlab.com/pycqa/flake8
12+
rev: 3.8.4
1313
hooks:
1414
- id: flake8
1515
exclude: elasticapm\/utils\/wrapt|build|src|tests|dist|conftest.py|setup.py

CHANGELOG.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ endif::[]
4444
* Fix for custom serializers in elasticsearch-py {pull}998[#998]
4545
* Fix large query truncation in psycopg2 {pull}994[#994]
4646
* Fix memory metrics reporting when `memory.usage_in_bytes` is unavailable {pull}987[#987]
47+
* Fix for Starlette/FastAPI integration to properly obey `enabled` config {pull}1000[#1000]
4748
4849
4950
[[release-notes-5.x]]

elasticapm/contrib/starlette/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def __init__(self, app: ASGIApp, client: Client):
115115
"""
116116
self.client = client
117117

118-
if self.client.config.instrument:
118+
if self.client.config.instrument and self.client.config.enabled:
119119
elasticapm.instrumentation.control.instrument()
120120

121121
super().__init__(app)

tests/contrib/asyncio/starlette_tests.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,19 @@
3232

3333
starlette = pytest.importorskip("starlette") # isort:skip
3434

35+
import types
36+
3537
import mock
38+
import urllib3
3639
from starlette.applications import Starlette
3740
from starlette.responses import PlainTextResponse
3841
from starlette.testclient import TestClient
3942

43+
import elasticapm
4044
from elasticapm import async_capture_span
4145
from elasticapm.conf import constants
4246
from elasticapm.contrib.starlette import ElasticAPM
47+
from elasticapm.utils import wrapt
4348
from elasticapm.utils.disttracing import TraceParent
4449

4550
pytestmark = [pytest.mark.starlette]
@@ -82,7 +87,9 @@ async def without_slash(request):
8287

8388
app.add_middleware(ElasticAPM, client=elasticapm_client)
8489

85-
return app
90+
yield app
91+
92+
elasticapm.uninstrument()
8693

8794

8895
def test_get(app, elasticapm_client):
@@ -271,3 +278,16 @@ def test_trailing_slash_redirect_detection(app, elasticapm_client, url, expected
271278
assert len(elasticapm_client.events[constants.TRANSACTION]) == 1
272279
for transaction in elasticapm_client.events[constants.TRANSACTION]:
273280
assert transaction["name"] == expected
281+
282+
283+
@pytest.mark.parametrize(
284+
"elasticapm_client",
285+
[
286+
{"enabled": False},
287+
],
288+
indirect=True,
289+
)
290+
def test_enabled_instrumentation(app, elasticapm_client):
291+
client = TestClient(app)
292+
293+
assert not isinstance(urllib3.connectionpool.HTTPConnectionPool.urlopen, wrapt.BoundFunctionWrapper)

0 commit comments

Comments
 (0)