Skip to content

Commit 442b0db

Browse files
authored
add tests to check update-ability of transaction_max_spans and capture_body (#589)
* test that updated capture_body setting is used by django and flask integrations * test that transaction_max_spans is updateable
1 parent 1ca6e36 commit 442b0db

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

tests/client/client_tests.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,29 @@ def test_transaction_max_spans(elasticapm_client):
733733
assert transaction["span_count"] == {"dropped": 10, "started": 5}
734734

735735

736+
def test_transaction_max_spans_dynamic(elasticapm_client):
737+
elasticapm_client.config.update(version=1, transaction_max_spans=1)
738+
elasticapm_client.begin_transaction("test_type")
739+
for i in range(5):
740+
with elasticapm.capture_span("span"):
741+
pass
742+
elasticapm_client.end_transaction("test")
743+
transaction = elasticapm_client.events[TRANSACTION][0]
744+
spans = elasticapm_client.spans_for_transaction(transaction)
745+
assert len(spans) == 1
746+
747+
elasticapm_client.config.update(version=2, transaction_max_spans=3)
748+
elasticapm_client.begin_transaction("test_type")
749+
for i in range(5):
750+
with elasticapm.capture_span("span"):
751+
pass
752+
753+
elasticapm_client.end_transaction("test")
754+
transaction = elasticapm_client.events[TRANSACTION][1]
755+
spans = elasticapm_client.spans_for_transaction(transaction)
756+
assert len(spans) == 3
757+
758+
736759
@pytest.mark.parametrize("elasticapm_client", [{"span_frames_min_duration": 20}], indirect=True)
737760
def test_transaction_span_frames_min_duration(elasticapm_client):
738761
elasticapm_client.begin_transaction("test_type")

tests/contrib/django/django_tests.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,6 +1349,38 @@ def test_capture_post_errors_dict(client, django_elasticapm_client):
13491349
assert error["context"]["request"]["body"] == "[REDACTED]"
13501350

13511351

1352+
def test_capture_body_config_is_dynamic_for_errors(client, django_elasticapm_client):
1353+
django_elasticapm_client.config.update(version="1", capture_body="all")
1354+
with pytest.raises(MyException):
1355+
client.post(reverse("elasticapm-raise-exc"), {"username": "john", "password": "smith"})
1356+
error = django_elasticapm_client.events[ERROR][0]
1357+
assert error["context"]["request"]["body"] == {"username": "john", "password": "smith"}
1358+
1359+
django_elasticapm_client.config.update(version="1", capture_body="off")
1360+
with pytest.raises(MyException):
1361+
client.post(reverse("elasticapm-raise-exc"), {"username": "john", "password": "smith"})
1362+
error = django_elasticapm_client.events[ERROR][1]
1363+
assert error["context"]["request"]["body"] == "[REDACTED]"
1364+
1365+
1366+
def test_capture_body_config_is_dynamic_for_transactions(client, django_elasticapm_client):
1367+
django_elasticapm_client.config.update(version="1", capture_body="all")
1368+
with override_settings(
1369+
**middleware_setting(django.VERSION, ["elasticapm.contrib.django.middleware.TracingMiddleware"])
1370+
):
1371+
client.post(reverse("elasticapm-no-error"), {"username": "john", "password": "smith"})
1372+
transaction = django_elasticapm_client.events[TRANSACTION][0]
1373+
assert transaction["context"]["request"]["body"] == {"username": "john", "password": "smith"}
1374+
1375+
django_elasticapm_client.config.update(version="1", capture_body="off")
1376+
with override_settings(
1377+
**middleware_setting(django.VERSION, ["elasticapm.contrib.django.middleware.TracingMiddleware"])
1378+
):
1379+
client.post(reverse("elasticapm-no-error"), {"username": "john", "password": "smith"})
1380+
transaction = django_elasticapm_client.events[TRANSACTION][1]
1381+
assert transaction["context"]["request"]["body"] == "[REDACTED]"
1382+
1383+
13521384
@pytest.mark.parametrize(
13531385
"django_elasticapm_client",
13541386
[{"capture_body": "errors"}, {"capture_body": "transactions"}, {"capture_body": "all"}, {"capture_body": "off"}],

tests/contrib/flask/flask_tests.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,34 @@ def test_post_files(flask_apm_client):
279279
assert event["context"]["request"]["body"] == "[REDACTED]"
280280

281281

282+
def test_capture_body_config_is_dynamic_for_errors(flask_apm_client):
283+
flask_apm_client.client.config.update(version="1", capture_body="all")
284+
resp = flask_apm_client.app.test_client().post("/an-error/", data={"foo": "bar"})
285+
resp.close()
286+
error = flask_apm_client.client.events[ERROR][0]
287+
assert error["context"]["request"]["body"] == {"foo": "bar"}
288+
289+
flask_apm_client.client.config.update(version="2", capture_body="off")
290+
resp = flask_apm_client.app.test_client().post("/an-error/", data={"foo": "bar"})
291+
resp.close()
292+
error = flask_apm_client.client.events[ERROR][1]
293+
assert error["context"]["request"]["body"] == "[REDACTED]"
294+
295+
296+
def test_capture_body_config_is_dynamic_for_transactions(flask_apm_client):
297+
flask_apm_client.client.config.update(version="1", capture_body="all")
298+
resp = flask_apm_client.app.test_client().post("/users/", data={"foo": "bar"})
299+
resp.close()
300+
error = flask_apm_client.client.events[TRANSACTION][0]
301+
assert error["context"]["request"]["body"] == {"foo": "bar"}
302+
303+
flask_apm_client.client.config.update(version="2", capture_body="off")
304+
resp = flask_apm_client.app.test_client().post("/users/", data={"foo": "bar"})
305+
resp.close()
306+
error = flask_apm_client.client.events[TRANSACTION][1]
307+
assert error["context"]["request"]["body"] == "[REDACTED]"
308+
309+
282310
@pytest.mark.parametrize("elasticapm_client", [{"capture_body": "transactions"}], indirect=True)
283311
def test_options_request(flask_apm_client):
284312
resp = flask_apm_client.app.test_client().options("/")

0 commit comments

Comments
 (0)