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

feat(starlette): add Starlette integration #1441

Merged
merged 54 commits into from
Jul 20, 2022

Conversation

sl0thentr0py
Copy link
Member

@sl0thentr0py sl0thentr0py commented May 10, 2022

Integration for the Starlette Framwork: https://www.starlette.io/

Following features are implemented:

  • Transaction naming (url style and endpoint style)
  • Catch all exceptions from exceptions middleware
  • Get user information from auth middleware
  • RequestExtractor to get cookie, form, json, raw body data from request
  • Middleware Spans for cool waterfall charts in performance

Missing (will be done in a later release):

  • distributed tracing in background tasks
  • Jinja templates integration

Closes #1463

@sl0thentr0py sl0thentr0py force-pushed the neel/WEBBACKEND-167/starlette branch from bec5b4c to 2c325f5 Compare May 11, 2022 10:28
@github-actions
Copy link

github-actions bot commented Jun 2, 2022

This pull request has gone three weeks without activity. In another week, I will close it.

But! If you comment or otherwise update it, I will reset the clock, and if you label it Status: Backlog or Status: In Progress, I will leave it alone ... forever!


"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀

Unverified

This user has not yet uploaded their public signing key.
WEBBACKEND-162

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.

Partially verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
We cannot verify signatures from co-authors, and some of the co-authors attributed to this commit require their commits to be signed.

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
@sl0thentr0py sl0thentr0py force-pushed the neel/WEBBACKEND-167/starlette branch from 45a0192 to 545968a Compare June 2, 2022 15:02
@smeubank smeubank mentioned this pull request Jun 10, 2022
5 tasks

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
@antonpirker antonpirker force-pushed the neel/WEBBACKEND-167/starlette branch from af89c86 to ff1153a Compare June 15, 2022 12:09
@antonpirker antonpirker force-pushed the neel/WEBBACKEND-167/starlette branch from 0370bee to b188178 Compare June 20, 2022 14:05
@antonpirker antonpirker force-pushed the neel/WEBBACKEND-167/starlette branch from d952009 to d0308f7 Compare June 24, 2022 11:08
@antonpirker antonpirker marked this pull request as ready for review June 24, 2022 11:09
@antonpirker antonpirker force-pushed the neel/WEBBACKEND-167/starlette branch from b523595 to 5f42a1a Compare June 24, 2022 12:46
@vladanpaunovic vladanpaunovic changed the title Starlette Integration feat(starlette): add Starlette integration Jul 5, 2022
@antonpirker antonpirker self-assigned this Jul 13, 2022
@antonpirker antonpirker merged commit fabba69 into master Jul 20, 2022
@antonpirker antonpirker deleted the neel/WEBBACKEND-167/starlette branch July 20, 2022 14:23
Copy link
Contributor

@tiangolo tiangolo left a comment

Choose a reason for hiding this comment

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

This is awesome! 🚀

Just a couple of minor comments for the future.

def patch_middlewares():
# type: () -> None

old_build_middleware_stack = FastAPI.build_middleware_stack
Copy link
Contributor

Choose a reason for hiding this comment

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

The Starlette base middleware will probably change to fix a couple of issues, and this logic to build the middleware stack will probably change in the future. But I would expect the new version should be easier to patch too.

Copy link
Member

Choose a reason for hiding this comment

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

Good to know. (And yes, it was a bit cumbersome to instrument the middlewares :-) )
Any plans on when this will change?

Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure yet because it will probably have to be fixed in Starlette. There are several people trying to solve it, I still have to help review all that too. And then port/update it in FastAPI

Copy link

@Kludex Kludex Aug 28, 2022

Choose a reason for hiding this comment

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

The Starlette base middleware will probably change to fix a couple of issues

What issues? If you mean the BaseHTTPMiddleware yes, but if you mean the build_middleware_stack, there's nothing on Starlette's issues.

EDIT: Reading this again, I think you meant BaseHTTPMiddleware. Which doesn't affect the build_middleware_stack.

Comment on lines +172 to +173
if not _should_send_default_pii():
return
Copy link
Contributor

Choose a reason for hiding this comment

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

This is great for Starlette!

For FastAPI, the AuthenticationMiddleware is probably not commonly used because that is handled with dependencies, and there's no predefined expected/standardized user data/model.

It would probably make sense to explain in the docs how to manually and optionally record user data for FastAPI users in dependencies.

Copy link
Member

Choose a reason for hiding this comment

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

Good to know. What is the most used dependency for user handling?

We have something in the documentation on how to set the user: https://docs.sentry.io/platforms/python/enriching-events/identify-user/
I will make sure it lands in the FastAPI docs too!

Copy link
Contributor

Choose a reason for hiding this comment

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

That sounds perfect!


There's not an expected predefined user data model or dependency, so every application can use the data model they find more useful. It normally doesn't require adopting a predefined model or any type of dependency, because installing, learning, and using some package would require the same amount of effort and code as implementing it directly.

The documentation explaining how to handle users is here: https://fastapi.tiangolo.com/tutorial/security/get-current-user/

But again, it's just explaining the concepts, not enforcing a data model.


I think that documenting how to handle PII in the developers' code, as you suggest with the existing docs, would probably be the best approach here too. Instead of trying to solve it behind the scenes as there's nothing obvious to expect.

@antonpirker
Copy link
Member

Good feedback! I will look at your comments tomorrow!

@Kludex
Copy link

Kludex commented Aug 28, 2022

I don't know how to say this without sounding pedantic, but... Is it possible to change the name of the integration to FastAPIIntegration instead of FastApiIntegration? The name of the framework is FastAPI.

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.

Integration for Starlette
4 participants