|
62 | 62 | from elasticapm.conf.constants import ERROR, SPAN, TRANSACTION |
63 | 63 | from elasticapm.contrib.django.apps import ElasticAPMConfig |
64 | 64 | from elasticapm.contrib.django.client import client, get_client |
| 65 | +from elasticapm.contrib.django.handlers import LoggingHandler |
65 | 66 | from elasticapm.contrib.django.middleware.wsgi import ElasticAPM |
66 | 67 | from elasticapm.utils.disttracing import TraceParent |
67 | 68 | from tests.contrib.django.conftest import BASE_TEMPLATE_DIR |
@@ -409,6 +410,25 @@ def test_ignored_exception_is_ignored(django_elasticapm_client, client): |
409 | 410 | assert len(django_elasticapm_client.events[ERROR]) == 0 |
410 | 411 |
|
411 | 412 |
|
| 413 | +def test_record_none_exc_info(django_elasticapm_client): |
| 414 | + # sys.exc_info can return (None, None, None) if no exception is being |
| 415 | + # handled anywhere on the stack. See: |
| 416 | + # http://docs.python.org/library/sys.html#sys.exc_info |
| 417 | + record = logging.LogRecord( |
| 418 | + "foo", logging.INFO, pathname=None, lineno=None, msg="test", args=(), exc_info=(None, None, None) |
| 419 | + ) |
| 420 | + handler = LoggingHandler() |
| 421 | + handler.emit(record) |
| 422 | + |
| 423 | + assert len(django_elasticapm_client.events[ERROR]) == 1 |
| 424 | + event = django_elasticapm_client.events[ERROR][0] |
| 425 | + |
| 426 | + assert event["log"]["param_message"] == "test" |
| 427 | + assert event["log"]["logger_name"] == "foo" |
| 428 | + assert event["log"]["level"] == "info" |
| 429 | + assert "exception" not in event |
| 430 | + |
| 431 | + |
412 | 432 | def test_404_middleware(django_elasticapm_client, client): |
413 | 433 | with override_settings( |
414 | 434 | **middleware_setting(django.VERSION, ["elasticapm.contrib.django.middleware.Catch404Middleware"]) |
@@ -1012,6 +1032,54 @@ def test_filter_matches_module_only(django_sending_elasticapm_client): |
1012 | 1032 | assert len(django_sending_elasticapm_client.httpserver.requests) == 1 |
1013 | 1033 |
|
1014 | 1034 |
|
| 1035 | +def test_django_logging_request_kwarg(django_elasticapm_client): |
| 1036 | + handler = LoggingHandler() |
| 1037 | + |
| 1038 | + logger = logging.getLogger(__name__) |
| 1039 | + logger.handlers = [] |
| 1040 | + logger.addHandler(handler) |
| 1041 | + |
| 1042 | + logger.error( |
| 1043 | + "This is a test error", |
| 1044 | + extra={ |
| 1045 | + "request": WSGIRequest( |
| 1046 | + environ={ |
| 1047 | + "wsgi.input": io.StringIO(), |
| 1048 | + "REQUEST_METHOD": "POST", |
| 1049 | + "SERVER_NAME": "testserver", |
| 1050 | + "SERVER_PORT": "80", |
| 1051 | + "CONTENT_TYPE": "application/json", |
| 1052 | + "ACCEPT": "application/json", |
| 1053 | + } |
| 1054 | + ) |
| 1055 | + }, |
| 1056 | + ) |
| 1057 | + |
| 1058 | + assert len(django_elasticapm_client.events[ERROR]) == 1 |
| 1059 | + event = django_elasticapm_client.events[ERROR][0] |
| 1060 | + assert "request" in event["context"] |
| 1061 | + request = event["context"]["request"] |
| 1062 | + assert request["method"] == "POST" |
| 1063 | + |
| 1064 | + |
| 1065 | +def test_django_logging_middleware(django_elasticapm_client, client): |
| 1066 | + handler = LoggingHandler() |
| 1067 | + |
| 1068 | + logger = logging.getLogger("logmiddleware") |
| 1069 | + logger.handlers = [] |
| 1070 | + logger.addHandler(handler) |
| 1071 | + logger.level = logging.INFO |
| 1072 | + |
| 1073 | + with override_settings( |
| 1074 | + **middleware_setting(django.VERSION, ["elasticapm.contrib.django.middleware.LogMiddleware"]) |
| 1075 | + ): |
| 1076 | + client.get(reverse("elasticapm-logging")) |
| 1077 | + assert len(django_elasticapm_client.events[ERROR]) == 1 |
| 1078 | + event = django_elasticapm_client.events[ERROR][0] |
| 1079 | + assert "request" in event["context"] |
| 1080 | + assert event["context"]["request"]["url"]["pathname"] == reverse("elasticapm-logging") |
| 1081 | + |
| 1082 | + |
1015 | 1083 | def client_get(client, url): |
1016 | 1084 | return client.get(url) |
1017 | 1085 |
|
|
0 commit comments