Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Partially incompatible with structlog configuration #1284

Open
james-certn opened this issue Jan 10, 2025 · 3 comments
Open

Partially incompatible with structlog configuration #1284

james-certn opened this issue Jan 10, 2025 · 3 comments

Comments

@james-certn
Copy link

Using structlog's "Rendering Using structlog-based Formatters Within logging" steps, new relic is unable to extract "event" -> "message"

Description
Log entries in New Relic are missing the "message" value

Expected Behavior
Log entries in New Relic have the "message" value populated correctly

Troubleshooting
Diagnosing the issue, I noticed:

  • newrelic appends a new_relic_event_consumer processor to the structlog processors list, and ensures it is at the tail end
  • new_relic_event_consumer inspects the type of the event parameter:
    • if str-like it directly copies it to message
    • if dict-like it copies the "event" value from the dict into message
  • this works for the 'simplified' structlog setup, but not for the 'ambitious' (structlog's wording, not mine!) setup (linked at the top)
    • in this setup, the last processor when configurating must be structlog.stdlib.ProcessorFormatter.wrap_for_formatter
    • wrap_for_formatter expects to always be the last processor prior to the ProcessorFormatter handling its output
      • as such, wrap_for_formatter knows it can safely change the shape of the data it returns to be compatible with ProcessorFormatter
      • this format is a tuple (specifically, the (*args, **kwargs}) that ProcessorFormatter expects to receive
    • however, because newrelic ensures its new_relic_event_consumer is always at the tail end of the processors list, it ends up:

Steps to Reproduce

  1. configure structlog in "Rendering Using structlog-based Formatters Within logging" fashion
    a. for us, we use django-structlog as well, but I don't think this is an influencing factor
  2. configure new relic to receive logs
  3. output some logs using structlog logging
  4. structlog logging output to the console is correct, however new relic does not receive "message" in records is unable to extract "event" in new_relic_event_consumer structlog processor

Your Environment

  • newrelic==10.3.1
  • django-structlog==8.1.0
  • structlog==24.4.0

Podman, linux container environment.

Additional context
Relates to and describes the issue in NR support ticket #00236985.

@workato-integration
Copy link

@TimPansino
Copy link
Contributor

Wow that's really specific, great writeup. I'll put this ticket into our queue and we'll see if we can do something about that.

@james-certn
Copy link
Author

james-certn commented Jan 10, 2025

If I add in the following to new_relic_event_consumer in the initial relevant if clause, it works:

        elif isinstance(event, tuple):
            _event = event[0][0]
            message = original_message = _event.get("event", "")
            event_attrs = {k: v for k, v in _event.items() if k != "event"}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants