-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_logging.py
More file actions
150 lines (131 loc) · 4.07 KB
/
test_logging.py
File metadata and controls
150 lines (131 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import logging
import os
from datetime import datetime, timedelta
import pytest
from gunicorn.config import AccessLogFormat, Config # type: ignore[import-untyped]
from pytest_django.fixtures import SettingsWrapper
from pytest_mock import MockerFixture
from common.core.logging import JsonFormatter
from common.gunicorn.logging import (
GunicornAccessLogJsonFormatter,
GunicornJsonCapableLogger,
PrometheusGunicornLogger,
)
from common.test_tools import AssertMetricFixture
@pytest.mark.freeze_time("2023-12-08T06:05:47.320000+00:00")
def test_gunicorn_access_log_json_formatter__outputs_expected() -> None:
# Given
gunicorn_access_log_json_formatter = GunicornAccessLogJsonFormatter()
log_record = logging.LogRecord(
name="gunicorn.access",
level=logging.INFO,
pathname="",
lineno=1,
msg=AccessLogFormat.default,
args={
"a": "requests",
"b": "-",
"B": None,
"D": 1000000,
"f": "-",
"h": "192.168.0.1",
"H": None,
"l": "-",
"L": "1.0",
"m": "GET",
"M": 1000,
"p": "<42>",
"q": "foo=bar",
"R": "^test/",
"r": "GET",
"s": 200,
"T": 1,
"t": datetime.fromisoformat("2023-12-08T06:05:47.320000+00:00").strftime(
"[%d/%b/%Y:%H:%M:%S %z]"
),
"u": "-",
"U": "/test",
},
exc_info=None,
)
expected_pid = os.getpid()
# When
json_log = gunicorn_access_log_json_formatter.get_json_record(log_record)
# Then
assert json_log == {
"duration_in_ms": 1000,
"levelname": "INFO",
"logger_name": "gunicorn.access",
"message": '192.168.0.1 - - [08/Dec/2023:06:05:47 +0000] "GET" 200 - "-" "requests"',
"method": "GET",
"path": "/test?foo=bar",
"pid": expected_pid,
"remote_ip": "192.168.0.1",
"route": "^test/",
"status": "200",
"thread_name": "MainThread",
"time": "2023-12-08T06:05:47+00:00",
"timestamp": "2023-12-08 06:05:47,319",
"user_agent": "requests",
}
def test_gunicorn_prometheus_gunicorn_logger__expected_metrics(
mocker: MockerFixture,
assert_metric: AssertMetricFixture,
) -> None:
# Given
config = Config()
logger = PrometheusGunicornLogger(config)
response_mock = mocker.Mock()
response_mock.status = b"200 OK"
response_mock.status_code = 200
response_mock.response_length = 42
# When
logger.access(
response_mock,
mocker.Mock(),
{"wsgi.django_route": "/health", "REQUEST_METHOD": "GET"},
timedelta(milliseconds=101),
)
# Then
assert_metric(
name="flagsmith_http_server_requests_total",
value=1.0,
labels={"method": "GET", "route": "/health", "response_status": "200"},
)
assert_metric(
name="flagsmith_http_server_request_duration_seconds_sum",
value=0.101,
labels={"method": "GET", "route": "/health", "response_status": "200"},
)
assert_metric(
name="flagsmith_http_server_response_size_bytes_sum",
value=42.0,
labels={"method": "GET", "route": "/health", "response_status": "200"},
)
def test_gunicorn_json_capable_logger__sets_expected_formatters(
settings: SettingsWrapper,
) -> None:
# Given
settings.LOG_FORMAT = "json"
config = Config()
config.set("accesslog", "-")
# When
logger = GunicornJsonCapableLogger(config)
# Then
assert isinstance(
logger.error_log.handlers[0].formatter,
JsonFormatter,
)
assert isinstance(
logger.access_log.handlers[0].formatter,
GunicornAccessLogJsonFormatter,
)
def test_gunicorn_json_capable_logger__non_existent_setting__not_raises(
settings: SettingsWrapper,
) -> None:
# Given
del settings.LOG_FORMAT
config = Config()
# When & Then
from common.gunicorn.logging import GunicornJsonCapableLogger
GunicornJsonCapableLogger(config)