def test_receiver_task_pre_run(self): expected_request_uuid = "00000000-0000-0000-0000-000000000000" task_id = "11111111-1111-1111-1111-111111111111" expected_user_id = "1234" task = Mock() task.request = Mock() task.request.__django_structlog__ = { "request_id": expected_request_uuid, "user_id": expected_user_id, } with structlog.threadlocal.tmp_bind(self.logger): self.logger.bind(foo="bar") structlog.threadlocal.as_immutable(self.logger) immutable_logger = structlog.threadlocal.as_immutable(self.logger) context = immutable_logger._context self.assertDictEqual({"foo": "bar"}, context) receivers.receiver_task_pre_run(task_id, task) immutable_logger = structlog.threadlocal.as_immutable(self.logger) context = immutable_logger._context self.assertDictEqual( { "task_id": task_id, "request_id": expected_request_uuid, "user_id": expected_user_id, }, context, )
def test_signal(self): @receiver(signals.bind_extra_task_metadata) def receiver_bind_extra_request_metadata( sender, signal, task=None, logger=None ): logger.bind(correlation_id=task.request.correlation_id) expected_correlation_uuid = "00000000-0000-0000-0000-000000000000" task_id = "11111111-1111-1111-1111-111111111111" task = Mock() task.request = Mock() task.request.correlation_id = expected_correlation_uuid with structlog.threadlocal.tmp_bind(self.logger): self.logger.bind(foo="bar") structlog.threadlocal.as_immutable(self.logger) immutable_logger = structlog.threadlocal.as_immutable(self.logger) context = immutable_logger._context self.assertDictEqual({"foo": "bar"}, context) receivers.receiver_task_pre_run(task_id, task) immutable_logger = structlog.threadlocal.as_immutable(self.logger) context = immutable_logger._context self.assertEqual(context["correlation_id"], expected_correlation_uuid) self.assertEqual(context["task_id"], task_id)