Skip to content

Conversation

@rads-1996
Copy link
Contributor

@rads-1996 rads-1996 commented Sep 2, 2025

Description

Fixes #2808

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • Added tests for Django instrumentation

Does This PR Require a Core Repo Change?

  • Yes. - Link to PR:
  • No.

Checklist:

See contributing.md for styleguide, changelog guidelines, and more.

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

@rads-1996 rads-1996 requested a review from a team as a code owner September 2, 2025 23:11
Copy link
Contributor

@tammy-baylis-swi tammy-baylis-swi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks again @rads-1996 , this lgtm. Maintainers will also have a look.

@rads-1996
Copy link
Contributor Author

Thanks again @rads-1996 , this lgtm. Maintainers will also have a look.

Thank you so much. Really appreciate the review :)

@xrmx xrmx moved this from Ready for review to Reviewed PR that needs fixing in @xrmx's Python PR digest Sep 9, 2025
@xrmx xrmx moved this from Reviewed PR that needs fixing to Ready for review in @xrmx's Python PR digest Sep 9, 2025
@rads-1996 rads-1996 requested a review from xrmx September 10, 2025 20:05
@rads-1996 rads-1996 force-pushed the django-issue-fix branch 3 times, most recently from 0bce4fd to c79c917 Compare October 13, 2025 23:15
Copy link
Member

@emdneto emdneto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the research and the PR, but I'm not sure if the issue is fixed. I'm still able to reproduce it, because Django logging extension (django.request and django.server) is still adding request to extras, which causes logs to be emitted with a request attribute containing an invalid value (WSGIRequest).

I didn't dig into it in detail, but what I imagine can solve the issue is to use a logging filter like:

class RequestFilter(logging.Filter):
    def filter(self, record: logging.LogRecord) -> bool:
        request = getattr(record, "request", None)
        record.request = str(request)
        return True
        
logger = logging.getLogger("django.request")
logger.addFilter(RequestFilter())

@rads-1996
Copy link
Contributor Author

Thanks for the research and the PR, but I'm not sure if the issue is fixed. I'm still able to reproduce it, because Django logging extension (django.request and django.server) is still adding request to extras, which causes logs to be emitted with a request attribute containing an invalid value (WSGIRequest).

I didn't dig into it in detail, but what I imagine can solve the issue is to use a logging filter like:

class RequestFilter(logging.Filter):
    def filter(self, record: logging.LogRecord) -> bool:
        request = getattr(record, "request", None)
        record.request = str(request)
        return True
        
logger = logging.getLogger("django.request")
logger.addFilter(RequestFilter())

@emdneto Thank you for the suggestion, I have made the changes and the error is no longer being shown. Please refer to the before and after change screenshots -

BEFORE CHANGES -
image

AFTER CHANGE -
Screenshot 2025-10-24 113612

@rads-1996 rads-1996 requested review from emdneto and xrmx October 24, 2025 19:55
@rads-1996 rads-1996 requested a review from xrmx November 5, 2025 17:18
@rads-1996
Copy link
Contributor Author

As per discussion in the python SIG, moved the logic to logging API - open-telemetry/opentelemetry-python#4808

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

Labels

None yet

Projects

Status: Ready for review

Development

Successfully merging this pull request may close these issues.

Invalid type WSGIRequest for attribute 'request' value opentelemetry

4 participants