django-structlog: Signals do not appear to be working

I’m trying to add some additional request and tracing information via the bind_extra_request_metadata signal, but this does not appear to be working. Here’s a basic handler that can reproduce the issue:

@receiver(signals.bind_extra_request_metadata)
def bind_additional_values(request, logger, **kwargs):
    print("hello")

    logger.bind(test="hello")

I can see the print occurring, but the added key does not appear in the logs. I don’t actually understand how this ever would have worked because bind() returns a new logger with the bound values, it doesn’t actually modify logger. Similarly, I can see that none of the bound values attached in the middleware are logged for the same reason:

https://github.com/jrobichaud/django-structlog/blob/master/django_structlog/middlewares/request.py#L45 https://github.com/jrobichaud/django-structlog/blob/master/django_structlog/middlewares/request.py#L51

Neither request_id nor ip are included in the logs. Was there some older version of structlog where this may have worked? I’m currently using structlog 21.5.0

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 42 (42 by maintainers)

Commits related to this issue

Most upvoted comments

Started on this today and included #64. Got all the request middleware tests working, but still need to tweak the celery tests.

Absolutely. I suspect the killer here will be that the structlog.threadlocal.merge_threadlocal processor would now be required which I don’t believe it was before. I’ll check that first thing tomorrow. Signing off for the evening.

It works in the demo app. Let me try setting the Django and structlog versions in the demo app to more recent versions and see if it still works.