|
3 | 3 | SPDX-License-Identifier: Apache-2.0 |
4 | 4 | """ |
5 | 5 |
|
| 6 | +import importlib |
6 | 7 | import logging |
7 | 8 | import json |
8 | | -import re |
| 9 | +import os |
9 | 10 | import time |
10 | 11 | import unittest |
| 12 | +from unittest.mock import patch |
11 | 13 |
|
12 | | -from awslambdaric.lambda_runtime_log_utils import JsonFormatter |
13 | 14 |
|
14 | | - |
15 | | -class TestJsonFormatterTimestamp(unittest.TestCase): |
| 15 | +class TestJsonFormatterTimestampDefaultPrecision(unittest.TestCase): |
16 | 16 | def setUp(self): |
17 | | - self.formatter = JsonFormatter() |
18 | | - self.logger = logging.getLogger("test") |
19 | | - self.logger.setLevel(logging.INFO) |
| 17 | + env = os.environ.copy() |
| 18 | + env.pop("AWS_LAMBDA_LOG_TIMESTAMP_PRECISION", None) |
| 19 | + with patch.dict(os.environ, env, clear=True): |
| 20 | + import awslambdaric.lambda_runtime_log_utils as mod |
| 21 | + |
| 22 | + importlib.reload(mod) |
| 23 | + self.formatter = mod.JsonFormatter() |
20 | 24 |
|
21 | 25 | def test_timestamp_format_is_second_precision_with_z(self): |
22 | 26 | record = logging.LogRecord( |
@@ -53,9 +57,7 @@ def test_timestamp_value_is_accurate(self): |
53 | 57 | output = self.formatter.format(record) |
54 | 58 | log_entry = json.loads(output) |
55 | 59 |
|
56 | | - expected = time.strftime( |
57 | | - "%Y-%m-%dT%H:%M:%SZ", self.formatter.converter(record.created) |
58 | | - ) |
| 60 | + expected = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime(record.created)) |
59 | 61 | self.assertEqual(log_entry["timestamp"], expected) |
60 | 62 |
|
61 | 63 | def test_timestamp_does_not_include_milliseconds(self): |
@@ -102,3 +104,114 @@ def test_timestamps_same_within_same_second(self): |
102 | 104 | output2 = json.loads(self.formatter.format(record2)) |
103 | 105 |
|
104 | 106 | self.assertEqual(output1["timestamp"], output2["timestamp"]) |
| 107 | + |
| 108 | + |
| 109 | +class TestJsonFormatterTimestampMillisecondPrecision(unittest.TestCase): |
| 110 | + def setUp(self): |
| 111 | + env = os.environ.copy() |
| 112 | + env["AWS_LAMBDA_LOG_TIMESTAMP_PRECISION"] = "milliseconds" |
| 113 | + with patch.dict(os.environ, env, clear=True): |
| 114 | + import awslambdaric.lambda_runtime_log_utils as mod |
| 115 | + |
| 116 | + importlib.reload(mod) |
| 117 | + self.formatter = mod.JsonFormatter() |
| 118 | + |
| 119 | + def test_timestamp_includes_milliseconds(self): |
| 120 | + record = logging.LogRecord( |
| 121 | + name="test", |
| 122 | + level=logging.INFO, |
| 123 | + pathname="test.py", |
| 124 | + lineno=1, |
| 125 | + msg="hello", |
| 126 | + args=None, |
| 127 | + exc_info=None, |
| 128 | + ) |
| 129 | + output = self.formatter.format(record) |
| 130 | + log_entry = json.loads(output) |
| 131 | + timestamp = log_entry["timestamp"] |
| 132 | + |
| 133 | + pattern = r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$" |
| 134 | + self.assertRegex( |
| 135 | + timestamp, |
| 136 | + pattern, |
| 137 | + f"Timestamp '{timestamp}' does not match expected format YYYY-MM-DDTHH:MM:SS.mmmZ", |
| 138 | + ) |
| 139 | + |
| 140 | + def test_timestamp_milliseconds_are_accurate(self): |
| 141 | + record = logging.LogRecord( |
| 142 | + name="test", |
| 143 | + level=logging.INFO, |
| 144 | + pathname="test.py", |
| 145 | + lineno=1, |
| 146 | + msg="hello", |
| 147 | + args=None, |
| 148 | + exc_info=None, |
| 149 | + ) |
| 150 | + record.created = 1718838785.068 |
| 151 | + output = self.formatter.format(record) |
| 152 | + log_entry = json.loads(output) |
| 153 | + |
| 154 | + self.assertEqual(log_entry["timestamp"], "2024-06-19T23:13:05.068Z") |
| 155 | + |
| 156 | + def test_timestamp_zero_milliseconds(self): |
| 157 | + record = logging.LogRecord( |
| 158 | + name="test", |
| 159 | + level=logging.INFO, |
| 160 | + pathname="test.py", |
| 161 | + lineno=1, |
| 162 | + msg="hello", |
| 163 | + args=None, |
| 164 | + exc_info=None, |
| 165 | + ) |
| 166 | + record.created = 1718838785.0 |
| 167 | + output = self.formatter.format(record) |
| 168 | + log_entry = json.loads(output) |
| 169 | + |
| 170 | + self.assertEqual(log_entry["timestamp"], "2024-06-19T23:13:05.000Z") |
| 171 | + |
| 172 | + def test_timestamps_differ_within_same_second(self): |
| 173 | + record1 = logging.LogRecord( |
| 174 | + name="test", |
| 175 | + level=logging.INFO, |
| 176 | + pathname="test.py", |
| 177 | + lineno=1, |
| 178 | + msg="first", |
| 179 | + args=None, |
| 180 | + exc_info=None, |
| 181 | + ) |
| 182 | + record1.created = 1718838785.100 |
| 183 | + |
| 184 | + record2 = logging.LogRecord( |
| 185 | + name="test", |
| 186 | + level=logging.INFO, |
| 187 | + pathname="test.py", |
| 188 | + lineno=1, |
| 189 | + msg="second", |
| 190 | + args=None, |
| 191 | + exc_info=None, |
| 192 | + ) |
| 193 | + record2.created = 1718838785.200 |
| 194 | + |
| 195 | + output1 = json.loads(self.formatter.format(record1)) |
| 196 | + output2 = json.loads(self.formatter.format(record2)) |
| 197 | + |
| 198 | + self.assertNotEqual(output1["timestamp"], output2["timestamp"]) |
| 199 | + self.assertEqual(output1["timestamp"], "2024-06-19T23:13:05.100Z") |
| 200 | + self.assertEqual(output2["timestamp"], "2024-06-19T23:13:05.200Z") |
| 201 | + |
| 202 | + |
| 203 | +class TestTimestampPrecisionVersionGate(unittest.TestCase): |
| 204 | + """Fails if we bump to v5+ without removing the seconds-precision path.""" |
| 205 | + |
| 206 | + def test_v5_must_remove_timestamp_precision_env_var(self): |
| 207 | + from awslambdaric import __version__ |
| 208 | + |
| 209 | + major = int(__version__.split(".")[0]) |
| 210 | + if major >= 5: |
| 211 | + import awslambdaric.lambda_runtime_log_utils as mod |
| 212 | + |
| 213 | + self.assertFalse( |
| 214 | + hasattr(mod, "_TIMESTAMP_PRECISION_MILLIS"), |
| 215 | + "v5+: remove _TIMESTAMP_PRECISION_MILLIS and make milliseconds " |
| 216 | + "the default. See TODO(v5.0) in lambda_runtime_log_utils.py", |
| 217 | + ) |
0 commit comments