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

fix(starlette): fixes duplication due to changes path behavior w/latest starlette versions #8577

Closed
wants to merge 5 commits into from

Conversation

zarirhamza
Copy link
Contributor

root_path now contains duplicated info of the mounted path found in path for starlette > v0.34.

Previously, there was no duplication when mounting applications on starlette. Logic is now implemented in the asgi TraceMiddleware to check the root_path and path vs the raw_path to determine duplication.

Checklist

  • Change(s) are motivated and described in the PR description
  • Testing strategy is described if automated tests are not included in the PR
  • Risks are described (performance impact, potential for breakage, maintainability)
  • Change is maintainable (easy to change, telemetry, documentation)
  • Library release note guidelines are followed or label changelog/no-changelog is set
  • Documentation is included (in-code, generated user docs, public corp docs)
  • Backport labels are set (if applicable)
  • If this PR changes the public interface, I've notified @DataDog/apm-tees.
  • If change touches code that signs or publishes builds or packages, or handles credentials of any kind, I've requested a review from @DataDog/security-design-and-guidance.

Reviewer Checklist

  • Title is accurate
  • All changes are related to the pull request's stated goal
  • Description motivates each change
  • Avoids breaking API changes
  • Testing strategy adequately addresses listed risks
  • Change is maintainable (easy to change, telemetry, documentation)
  • Release note makes sense to a user of the library
  • Author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment
  • Backport labels are set in a manner that is consistent with the release branch maintenance policy

@zarirhamza zarirhamza changed the title fix(starlette): fixes duplication due to change in root_path and path behavior with latest starlette versions fix(starlette): fixes duplication due to changes path behavior w/latest starlette versions Mar 2, 2024
@datadog-dd-trace-py-rkomorn
Copy link

Datadog Report

Branch report: zarir/starlette-upgrade-version
Commit report: be07ae1
Test service: dd-trace-py

✅ 0 Failed, 171709 Passed, 1037 Skipped, 11m 7.1s Wall Time

@@ -85,6 +85,17 @@ def span_from_scope(scope: Mapping[str, Any]) -> Optional[Span]:
return scope.get("datadog", {}).get("request_spans", [None])[0]


def _extract_full_path(raw_path, root_path, path):
if root_path and raw_path and raw_path.startswith(root_path.encode()):
raw = raw_path.split(b"?")[0] if b"?" in raw_path else raw_path
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the raw path guaranteed to be a byte string? Can we use urlparse here? Removing the query args using the ? seems a bit brittle

if raw.decode() != check_path:
return path

return root_path + path
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
return root_path + path
return check_path

@@ -85,6 +85,17 @@ def span_from_scope(scope: Mapping[str, Any]) -> Optional[Span]:
return scope.get("datadog", {}).get("request_spans", [None])[0]


def _extract_full_path(raw_path, root_path, path):
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 include a doc string describing the new behavior?

"36": parse_version(sys.version) < (3, 7), # 3.6 has an extra request
"rest": parse_version(sys.version) >= (3, 7),
"old": starlette_version < (0, 20, 0), # below 0.20 has an extra request due to python 3.6 support
"new": starlette_version >= (0, 20, 0),
Copy link
Contributor

Choose a reason for hiding this comment

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

There seems to be two changes here:

  1. Updating the riofile to run starlette tests with <v.20.0
  2. Fixing a bug in how asgi middlewares set the http.url tag

Can we split up these changes?

Also would it be possible to avoid running snapshot tests on older versions of starlette? I think we have sufficient test coverage already.

@@ -542,8 +540,8 @@ def test_background_task(snapshot_client_with_tracer, tracer, test_spans):
)
@pytest.mark.snapshot(
variants={
"36": parse_version(sys.version) < (3, 7), # 3.6 has an extra request
"rest": parse_version(sys.version) >= (3, 7),
"old": starlette_version < (0, 20, 0), # below 0.20 has an extra request due to python 3.6 support
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 expand on this comment. It is not immediately clear why starlette_version<0.20 needs a unique snapshot file.

Copy link
Contributor

@mabdinur mabdinur left a comment

Choose a reason for hiding this comment

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

We will also need a release note for the asgi changes

@emmettbutler emmettbutler self-requested a review March 4, 2024 19:26
@zarirhamza zarirhamza closed this Mar 5, 2024
@zarirhamza zarirhamza reopened this Mar 5, 2024
@zarirhamza zarirhamza closed this Mar 6, 2024
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.

2 participants