Skip to content

Conversation

lrafeei
Copy link
Contributor

@lrafeei lrafeei commented Aug 9, 2025

This PR adds three settings related to Django middleware instrumentation:

instrumentation.middleware.django.enabled
instrumentation.middleware.django.exclude
instrumentation.middleware.django.include

Where the exclude/include settings follow similar rules to attribute exclude/include: https://docs.newrelic.com/docs/apm/agents/python-agent/attributes/enabling-disabling-attributes-python/#rule-specific-wins

  1. If instrumentation.middleware.django.enabled is disabled, no middlewares are recorded, despite what may be in the include list
  2. Exclude always supersedes include
  3. Middleware names are case sensitive
  4. * denotes a wildcard. NOTE: This will not work as a pure wildcard, as it will only work at the end of a name and not in the middle or beginning.
  5. Most specific setting for a middleware name takes priority. That is, if exclude has middleware names using a wildcard but include does not and include's middleware name is within the set of exclude's wildcard middleware names, the middleware name in include will take precedence.
    i.e.
    Middleware seen in application: "foo", "bar", "baz"
    instrumentation.middleware.django.enabled = True
    instrumentation.middleware.django.exclude = ["ba*"]
    instrumentation.middleware.django.include = ["baz"]
    Middleware that is included in instrumentation: "foo", "baz"
    Middleware that is excluded in instrumentation: "bar"

Copy link

github-actions bot commented Aug 9, 2025

🦙 MegaLinter status: ✅ SUCCESS

Descriptor Linter Files Fixed Errors Warnings Elapsed time
✅ ACTION actionlint 6 0 0 0.74s
✅ MARKDOWN markdownlint 7 0 0 0 1.26s
✅ MARKDOWN markdown-link-check 7 0 0 21.49s
✅ PYTHON ruff 936 0 0 0 0.91s
✅ PYTHON ruff-format 936 0 0 0 0.36s
✅ YAML prettier 13 0 0 0 1.38s
✅ YAML v8r 13 0 0 6.39s
✅ YAML yamllint 13 0 0 0.61s

See detailed report in MegaLinter reports

MegaLinter is graciously provided by OX Security

@mergify mergify bot added the tests-failing Tests failing in CI. label Aug 9, 2025
@codecov-commenter
Copy link

codecov-commenter commented Aug 9, 2025

Codecov Report

❌ Patch coverage is 90.90909% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.42%. Comparing base (d1bcc83) to head (1029ae2).

Files with missing lines Patch % Lines
newrelic/hooks/framework_django.py 91.83% 2 Missing and 2 partials ⚠️
newrelic/core/config.py 83.33% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1444      +/-   ##
==========================================
+ Coverage   81.33%   81.42%   +0.09%     
==========================================
  Files         209      209              
  Lines       23650    23709      +59     
  Branches     3724     3740      +16     
==========================================
+ Hits        19236    19306      +70     
+ Misses       3153     3146       -7     
+ Partials     1261     1257       -4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@mergify mergify bot removed the tests-failing Tests failing in CI. label Aug 13, 2025
@mergify mergify bot added the tests-failing Tests failing in CI. label Aug 13, 2025
@mergify mergify bot removed the tests-failing Tests failing in CI. label Aug 18, 2025
@mergify mergify bot added tests-failing Tests failing in CI. and removed tests-failing Tests failing in CI. labels Aug 18, 2025
@lrafeei lrafeei marked this pull request as ready for review August 18, 2025 18:24
@lrafeei lrafeei requested a review from a team as a code owner August 18, 2025 18:25
@mergify mergify bot added the tests-failing Tests failing in CI. label Aug 18, 2025
@lrafeei lrafeei marked this pull request as draft August 21, 2025 16:36
@hmstepanek hmstepanek marked this pull request as ready for review August 26, 2025 18:54
@mergify mergify bot removed the tests-failing Tests failing in CI. label Aug 26, 2025
Copy link
Contributor

@hmstepanek hmstepanek left a comment

Choose a reason for hiding this comment

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

Posting a partial review-I need to look at the tests and the include/exclude implementation a little bit deeper.

@@ -644,6 +649,17 @@ def _parse_attributes(s):
return valid


# Called from newrelic.config.py to parse django_middleware lists
def _parse_django_middleware(s):
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we just point at this method rather than re-creating the exact same implementation again: _parse_attributes? Maybe just rename it to be generic or accept an additional argument for the log message.

if exclude_middleware.endswith("*"):
name = exclude_middleware.rstrip("*")
if callable_name.startswith(name):
if not include_logic(callable_name, name):
Copy link
Contributor

Choose a reason for hiding this comment

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

Cache the value so we aren't computing it twice.

Suggested change
if not include_logic(callable_name, name):
include = include_logic(callable_name, name)
if not include:
return False
else:
deny |= include

"""
if len(settings.instrumentation.django_middleware.include) == 0:
return True
for include_middleware in settings.instrumentation.django_middleware.include:
Copy link
Contributor

Choose a reason for hiding this comment

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

My brain is fried from trying to understand this...I'll come back to this later and hopefully it will make more sense. In theory I see what you are trying to do here but my brain is not following all of these implementation details.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fair--it gets a bit dicey. A lot of the complexity in this logic comes from attempting to sort out a case like this one:

Middleware = django.middleware.common:CommonMiddleware, django.middleware.csrf:CsrfViewMiddleware
exclude = ["django.middleware.c*"],
include = ["django.middleware.", "django.middleware.co"]

The django.middleware.* in the include list is effectively a no op. However, in the case where a user has this scenario, this should still filter correctly (based on the more specific include statement, django.middleware.co*, relative to the exclude statement django.middleware.c*

@mergify mergify bot added the tests-failing Tests failing in CI. label Aug 26, 2025
Copy link
Contributor

@hmstepanek hmstepanek left a comment

Choose a reason for hiding this comment

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

Ok I mainly just want some more thorough tests for the filtering-otherwise this looks good.

"instrumentation.django_middleware.enabled": True,
}

wildcard_exclude_specific_include_settings = (
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we get some more tests here:

  • there aren't *'d paths.
  • there is only an exclude rule
  • there is only an include rule
  • there are no dots in the paths
  • there are only dots in the include path
  • there are only dots in the exclude path
  • there is a path with a dot (no *) and a path with no dots and only a *
  • case sensitive test
    There's probably some additional cases I'm not thinking of?

I want to be really thorough when we test these cause there's a lot of complex logic there and I don't want to miss some corner case bug in the implementation. (I don't see any but it might be hard to catch without thorough testing.)

@mergify mergify bot removed the tests-failing Tests failing in CI. label Aug 26, 2025
@mergify mergify bot added the tests-failing Tests failing in CI. label Aug 28, 2025
@mergify mergify bot removed the tests-failing Tests failing in CI. label Aug 28, 2025
@mergify mergify bot added the tests-failing Tests failing in CI. label Aug 28, 2025
@lrafeei lrafeei requested a review from hmstepanek August 28, 2025 16:17
@mergify mergify bot removed the tests-failing Tests failing in CI. label Aug 28, 2025
@mergify mergify bot added tests-failing Tests failing in CI. and removed tests-failing Tests failing in CI. labels Aug 28, 2025
@lrafeei lrafeei enabled auto-merge (squash) August 29, 2025 19:45
@lrafeei lrafeei merged commit b6631c8 into main Sep 2, 2025
107 of 109 checks passed
@lrafeei lrafeei deleted the django-middleware-allow-deny branch September 2, 2025 21:51
TimPansino added a commit that referenced this pull request Sep 3, 2025
* Bump the github_actions group with 2 updates (#1466)

Bumps the github_actions group with 2 updates: [codecov/codecov-action](https://github.com/codecov/codecov-action) and [github/codeql-action](https://github.com/github/codeql-action).


Updates `codecov/codecov-action` from 5.4.3 to 5.5.0
- [Release notes](https://github.com/codecov/codecov-action/releases)
- [Changelog](https://github.com/codecov/codecov-action/blob/main/CHANGELOG.md)
- [Commits](codecov/codecov-action@18283e0...fdcc847)

Updates `github/codeql-action` from 3.29.10 to 3.29.11
- [Release notes](https://github.com/github/codeql-action/releases)
- [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md)
- [Commits](github/codeql-action@96f518a...3c3833e)

---
updated-dependencies:
- dependency-name: codecov/codecov-action
  dependency-version: 5.5.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
  dependency-group: github_actions
- dependency-name: github/codeql-action
  dependency-version: 3.29.11
  dependency-type: direct:production
  update-type: version-update:semver-patch
  dependency-group: github_actions
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>

* Safeguards for deepest unique path (#1450)

* Safeguards for deepest unique path

* Remove breakpoint

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>

* Add try-except to web request parsing (#1449)

* Add try-except to web request parsing

* Fix parsing logic

* Add clarifying comment

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>

* Use legacy bitnami for now (#1471)

* Use legacy bitnami for now

* Revert solr change

* Revert zookeeper change

* Add graphene-django instrumentation (#1451)

* Add graphene-django instrumentation

* Increase naming priority

* Remove unused import

* Add sychronous schema tests

* Clean up test files

* Remove commented out code

* Megalinter fixes

* Add operation & resolver tests

* Refine tests

* MegaLinter fixes

* Suggested reviewer changes

* Megalinter fixes

* Django middleware filtering settings (#1444)

* Reduce number of spans in django framework (#779)

* Do not wrap useless middlewares

* Fixup: use frozenset

* Add config settings

* Add middleware enable/disable options

* Add testing

* Testing exclude/include settings

* Add optional fixture scope argument

* Rewrite tests to use fixtures

* Add new fixture

* Fix tests

* Fix ruff errors

* MegaLinter Fixes

* Add config file tests

* MegaLinter fixes

* Reviewer changes

* MegaLinter fixes

* Add InstrumentationMiddlewareSettings

* More exclude/include filter tests

* Megalinter fixes

* Tests to increase coverage

* Megalinter fixes

* More coverage tests

* ANOTHER TEST:

---------

Co-authored-by: Hannah Stepanek <[email protected]>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>

* Pin bitnami images to bitnamilegacy (#1475)

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>

* Distributed CI Image Build (#1478)

* Distribute build of CI image across runners

* Add weekly CI image rebuild

* Add rust to toolchain

* Add human readable job names

* Linting

---------

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Co-authored-by: Lalleh Rafeei <[email protected]>
Co-authored-by: Hannah Stepanek <[email protected]>
@umaannamalai umaannamalai added this to the v10.17.0 milestone Sep 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants