-
Notifications
You must be signed in to change notification settings - Fork 613
feat(flask): Add span streaming support and request body capture #6264
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
Draft
ericapisani
wants to merge
7
commits into
master
Choose a base branch
from
py-2323-migrate-flask
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c74acb1
feat(flask): Add span streaming support and request body capture
ericapisani c557b26
move common function between flask and starlette to more central place
ericapisani 2df2cd3
add comment back in that was accidentally removed, small cleanups
ericapisani c90dc55
Read the request body before sending the response to the user instead…
ericapisani 984a63a
Merge branch 'master' into py-2323-migrate-flask
ericapisani b7c0ec6
Improve handling of situation where a request body is never read in t…
ericapisani 3b71c16
fix for unbounded consumption of request wtihout a content-header spe…
ericapisani File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,34 @@ | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| import sentry_sdk | ||
| from sentry_sdk.integrations import _check_minimum_version, DidNotEnable, Integration | ||
| from sentry_sdk.integrations import DidNotEnable, Integration, _check_minimum_version | ||
| from sentry_sdk.integrations._wsgi_common import ( | ||
| _RAW_DATA_EXCEPTIONS, | ||
|
ericapisani marked this conversation as resolved.
Outdated
|
||
| DEFAULT_HTTP_METHODS_TO_CAPTURE, | ||
| RequestExtractor, | ||
| _serialize_request_body_data, | ||
| request_body_within_bounds, | ||
| ) | ||
| from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware | ||
| from sentry_sdk.scope import should_send_default_pii | ||
| from sentry_sdk.traces import StreamedSpan, _get_current_streamed_span | ||
| from sentry_sdk.tracing import SOURCE_FOR_STYLE | ||
| from sentry_sdk.tracing_utils import has_span_streaming_enabled | ||
| from sentry_sdk.utils import ( | ||
| AnnotatedValue, | ||
| capture_internal_exceptions, | ||
| ensure_integration_enabled, | ||
| event_from_exception, | ||
| package_version, | ||
| ) | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| if TYPE_CHECKING: | ||
| from typing import Any, Callable, Dict, Union | ||
|
|
||
| from werkzeug.datastructures import FileStorage, ImmutableMultiDict | ||
|
|
||
| from sentry_sdk._types import Event, EventProcessor | ||
| from sentry_sdk.integrations.wsgi import _ScopedResponse | ||
| from werkzeug.datastructures import FileStorage, ImmutableMultiDict | ||
|
|
||
|
|
||
| try: | ||
|
|
@@ -94,10 +101,9 @@ def setup_once() -> None: | |
| def sentry_patched_wsgi_app( | ||
| self: "Any", environ: "Dict[str, str]", start_response: "Callable[..., Any]" | ||
| ) -> "_ScopedResponse": | ||
| if sentry_sdk.get_client().get_integration(FlaskIntegration) is None: | ||
| return old_app(self, environ, start_response) | ||
|
|
||
| integration = sentry_sdk.get_client().get_integration(FlaskIntegration) | ||
| if integration is None: | ||
| return old_app(self, environ, start_response) | ||
|
|
||
| middleware = SentryWsgiMiddleware( | ||
| lambda *a, **kw: old_app(self, *a, **kw), | ||
|
|
@@ -158,6 +164,44 @@ def _request_started(app: "Flask", **kwargs: "Any") -> None: | |
| evt_processor = _make_request_event_processor(app, request, integration) | ||
| scope.add_event_processor(evt_processor) | ||
|
|
||
| client = sentry_sdk.get_client() | ||
| if has_span_streaming_enabled(client.options): | ||
| _set_request_body_data_on_streaming_segment(request, client) | ||
|
ericapisani marked this conversation as resolved.
|
||
|
|
||
|
|
||
| def _set_request_body_data_on_streaming_segment( | ||
| request: "Request", client: "sentry_sdk.client.BaseClient" | ||
| ) -> None: | ||
| current_span = _get_current_streamed_span() | ||
| if type(current_span) is not StreamedSpan: | ||
| return | ||
|
|
||
| with capture_internal_exceptions(): | ||
| content_length = int(request.content_length or 0) | ||
| extractor = FlaskRequestExtractor(request) | ||
|
|
||
| if not request_body_within_bounds(client, content_length): | ||
| data = AnnotatedValue.substituted_because_over_size_limit() | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This general logic mostly follows what happens with the request extractor, but Instead of removing the string entirely, it substitutes it with a value to explain why a value was removed, as opposed to providing only an empty string. |
||
| else: | ||
| raw_data = None | ||
| try: | ||
| raw_data = extractor.raw_data() | ||
| except _RAW_DATA_EXCEPTIONS: | ||
| pass | ||
|
|
||
| parsed_body = extractor.parsed_body() | ||
| if parsed_body is not None: | ||
| data = parsed_body | ||
| elif raw_data: | ||
| data = AnnotatedValue.substituted_because_raw_data() | ||
| else: | ||
| return | ||
|
|
||
| current_span._segment.set_attribute( | ||
| "http.request.body.data", | ||
| _serialize_request_body_data(data), | ||
| ) | ||
|
|
||
|
|
||
| class FlaskRequestExtractor(RequestExtractor): | ||
| def env(self) -> "Dict[str, str]": | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.