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

add middleware_position feature in django #1903

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

TheAnshul756
Copy link
Contributor

@TheAnshul756 TheAnshul756 commented Aug 6, 2023

Description

Feature to add middleware at specific location. Added keyword argument middleware_position which one can set to the number of middleware they want to add before otel middleware to start instrumentation.

Fixes #1781

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update

How Has This Been Tested?

  • tox -e lint
  • tox -e test-instrumentation-django1
  • tox -e test-instrumentation-django2
  • tox -e test-instrumentation-django3

Does This PR Require a Core Repo Change?

  • 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

@TheAnshul756 TheAnshul756 marked this pull request as draft August 6, 2023 13:21
@TheAnshul756 TheAnshul756 changed the title [WIP ]add middleware_position feature in django [WIP] add middleware_position feature in django Aug 6, 2023
@TheAnshul756 TheAnshul756 marked this pull request as ready for review August 6, 2023 20:55
@TheAnshul756 TheAnshul756 requested a review from a team August 6, 2023 20:55
@TheAnshul756 TheAnshul756 changed the title [WIP] add middleware_position feature in django add middleware_position feature in django Aug 6, 2023
middleware_position = kwargs.pop("middleware_position", 0)
if len(settings_middleware) < middleware_position:
_logger.debug(
"The middleware_position you provided (%d) is less than the current number of middlewares (%d). \
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
"The middleware_position you provided (%d) is less than the current number of middlewares (%d). \
"The middleware_position you provided (%d) is greater than the current number of middlewares (%d). \

if len(settings_middleware) < middleware_position:
_logger.debug(
"The middleware_position you provided (%d) is less than the current number of middlewares (%d). \
Since the number of middlewares is less than the total, the Otel middleware will be appended at the end of the middleware chain.",
Copy link
Contributor

Choose a reason for hiding this comment

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

What's total here?

Copy link
Contributor

@ocelotl ocelotl left a comment

Choose a reason for hiding this comment

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

May be a good idea to reconsider implementation

@@ -53,6 +53,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#1879](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1879))
- Add optional distro and configurator selection for auto-instrumentation
([#1823](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1823))
- Add option to add Opentelemetry middleware at specific position in middleware chain
([#1903]https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1903)
Copy link
Member

Choose a reason for hiding this comment

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

Please move this to the Unreleased section.

@@ -355,10 +355,23 @@ def _instrument(self, **kwargs):

is_sql_commentor_enabled = kwargs.pop("is_sql_commentor_enabled", None)

middleware_position = kwargs.pop("middleware_position", 0)
Copy link
Member

Choose a reason for hiding this comment

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

Can you add an OTEL_PYTHON_DJANGO_MIDDLEWARE_POSITION env so that auto instrumentation people who use opentelemetry-instrument can also use this capability?

@ocelotl
Copy link
Contributor

ocelotl commented Aug 30, 2023

@TheAnshul756 This PR has enough approvals, seems we only need a few fixes in order to merge it. 👍

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.

DisallowedHost exception for Django instrumentation
4 participants