|
9 | 9 |
|
10 | 10 | import elasticapm |
11 | 11 | from elasticapm.base import Client, ClientState |
12 | | -from elasticapm.transport.base import Transport, TransportException |
13 | | -from elasticapm.utils import compat |
14 | | -from elasticapm.utils.compat import urlparse |
| 12 | +from elasticapm.conf.constants import KEYWORD_MAX_LENGTH |
| 13 | +from elasticapm.transport.base import Transport |
| 14 | +from elasticapm.utils import compat, encoding |
15 | 15 |
|
16 | 16 |
|
17 | 17 | def test_client_state_should_try_online(): |
@@ -404,7 +404,7 @@ def test_client_uses_sync_mode_when_master_process(is_master_process): |
404 | 404 | secret_token='secret', |
405 | 405 | async_mode=True, |
406 | 406 | ) |
407 | | - transport = client._get_transport(urlparse.urlparse('http://exampe.com')) |
| 407 | + transport = client._get_transport(compat.urlparse.urlparse('http://exampe.com')) |
408 | 408 | assert transport.async_mode is False |
409 | 409 |
|
410 | 410 |
|
@@ -702,3 +702,67 @@ def test_transaction_context_is_used_in_errors(elasticapm_client): |
702 | 702 | assert message['context']['tags'] == {'foo': 'baz'} |
703 | 703 | assert 'a' in transaction.context['custom'] |
704 | 704 | assert 'foo' not in transaction.context['custom'] |
| 705 | + |
| 706 | + |
| 707 | +def test_transaction_keyword_truncation(sending_elasticapm_client): |
| 708 | + too_long = 'x' * (KEYWORD_MAX_LENGTH + 1) |
| 709 | + expected = encoding.keyword_field(too_long) |
| 710 | + assert too_long != expected |
| 711 | + assert len(expected) == KEYWORD_MAX_LENGTH |
| 712 | + assert expected[-1] != 'x' |
| 713 | + sending_elasticapm_client.begin_transaction(too_long) |
| 714 | + elasticapm.tag(val=too_long) |
| 715 | + elasticapm.set_user_context(username=too_long, email=too_long, user_id=too_long) |
| 716 | + with elasticapm.capture_span(name=too_long, span_type=too_long): |
| 717 | + pass |
| 718 | + sending_elasticapm_client.end_transaction(too_long, too_long) |
| 719 | + sending_elasticapm_client.close() |
| 720 | + assert sending_elasticapm_client.httpserver.responses[0]['code'] == 202 |
| 721 | + transaction = sending_elasticapm_client.httpserver.payloads[0]['transactions'][0] |
| 722 | + span = transaction['spans'][0] |
| 723 | + |
| 724 | + assert transaction['name'] == expected |
| 725 | + assert transaction['type'] == expected |
| 726 | + assert transaction['result'] == expected |
| 727 | + |
| 728 | + assert transaction['context']['user']['id'] == expected |
| 729 | + assert transaction['context']['user']['username'] == expected |
| 730 | + assert transaction['context']['user']['email'] == expected |
| 731 | + |
| 732 | + assert transaction['context']['tags']['val'] == expected |
| 733 | + |
| 734 | + assert span['type'] == expected |
| 735 | + assert span['name'] == expected |
| 736 | + |
| 737 | + |
| 738 | +def test_error_keyword_truncation(sending_elasticapm_client): |
| 739 | + too_long = 'x' * (KEYWORD_MAX_LENGTH + 1) |
| 740 | + expected = encoding.keyword_field(too_long) |
| 741 | + |
| 742 | + # let's create a way too long Exception type with a way too long module name |
| 743 | + WayTooLongException = type(too_long.upper(), (Exception,), {}) |
| 744 | + WayTooLongException.__module__ = too_long |
| 745 | + try: |
| 746 | + raise WayTooLongException() |
| 747 | + except WayTooLongException: |
| 748 | + sending_elasticapm_client.capture_exception() |
| 749 | + error = sending_elasticapm_client.httpserver.payloads[0]['errors'][0] |
| 750 | + |
| 751 | + assert error['exception']['type'] == expected.upper() |
| 752 | + assert error['exception']['module'] == expected |
| 753 | + |
| 754 | + |
| 755 | +def test_message_keyword_truncation(sending_elasticapm_client): |
| 756 | + too_long = 'x' * (KEYWORD_MAX_LENGTH + 1) |
| 757 | + expected = encoding.keyword_field(too_long) |
| 758 | + sending_elasticapm_client.capture_message( |
| 759 | + param_message={'message': too_long, 'params': []}, |
| 760 | + logger_name=too_long, |
| 761 | + ) |
| 762 | + |
| 763 | + error = sending_elasticapm_client.httpserver.payloads[0]['errors'][0] |
| 764 | + |
| 765 | + assert error['log']['param_message'] == expected |
| 766 | + assert error['log']['message'] == too_long # message is not truncated |
| 767 | + |
| 768 | + assert error['log']['logger_name'] == expected |
0 commit comments