-
Notifications
You must be signed in to change notification settings - Fork 635
feat(chalice): Add span streaming support to Chalice integration #6503
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
Changes from 9 commits
208cf35
6df89ee
d637c2f
b4ad57a
e658841
65232d2
906158f
dcf2e10
51cf45a
596ad36
13010e5
c6d2360
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,9 +2,23 @@ | |
| from functools import wraps | ||
|
|
||
| import sentry_sdk | ||
| import sentry_sdk.traces | ||
| from sentry_sdk.consts import OP | ||
| from sentry_sdk.integrations import DidNotEnable, Integration | ||
| from sentry_sdk.integrations._wsgi_common import _filter_headers | ||
| from sentry_sdk.integrations.aws_lambda import _make_request_event_processor | ||
| from sentry_sdk.integrations.cloud_resource_context import ( | ||
| CLOUD_PLATFORM, | ||
| CLOUD_PROVIDER, | ||
| ) | ||
| from sentry_sdk.traces import ( | ||
| SegmentSource, | ||
| SpanStatus, | ||
| StreamedSpan, | ||
| get_current_span, | ||
| ) | ||
| from sentry_sdk.tracing import TransactionSource | ||
| from sentry_sdk.tracing_utils import has_span_streaming_enabled | ||
|
Check warning on line 21 in sentry_sdk/integrations/chalice.py
|
||
| from sentry_sdk.utils import ( | ||
| capture_internal_exceptions, | ||
| event_from_exception, | ||
|
|
@@ -63,38 +77,89 @@ | |
| with sentry_sdk.isolation_scope() as scope: | ||
| with capture_internal_exceptions(): | ||
| configured_time = app.lambda_context.get_remaining_time_in_millis() | ||
| scope.set_transaction_name( | ||
| app.lambda_context.function_name, | ||
| source=TransactionSource.COMPONENT, | ||
| ) | ||
|
|
||
| scope.add_event_processor( | ||
| _make_request_event_processor( | ||
| app.current_request.to_dict(), | ||
| app.lambda_context, | ||
| configured_time, | ||
| ) | ||
| ) | ||
| try: | ||
| return view_function(**function_args) | ||
| except Exception as exc: | ||
| if isinstance(exc, ChaliceViewError): | ||
|
|
||
| if has_span_streaming_enabled(client.options): | ||
| current_span = get_current_span() | ||
| segment = None | ||
| if type(current_span) is StreamedSpan: | ||
| # A segment already exists (created by the AWS Lambda | ||
| # integration), so decorate it with Chalice attributes | ||
| # The AWS Lambda integration owns the span lifecycle | ||
| # (end + flush), but Chalice converts unhandled view exceptions | ||
| # into 500 responses, so the error must be captured here. | ||
| aws_context = app.lambda_context | ||
| request_dict = app.current_request.to_dict() | ||
| headers = request_dict.get("headers", {}) | ||
|
|
||
| header_attrs: "Dict[str, Any]" = {} | ||
| for header, value in _filter_headers( | ||
| headers, use_annotated_value=False | ||
| ).items(): | ||
| header_attrs[f"http.request.header.{header.lower()}"] = value | ||
|
|
||
| additional_attrs: "Dict[str, Any]" = {} | ||
| if "method" in request_dict: | ||
| additional_attrs["http.request.method"] = request_dict["method"] | ||
|
|
||
| attributes = { | ||
| **_get_lambda_span_attributes(aws_context), | ||
| **header_attrs, | ||
| **additional_attrs, | ||
| } | ||
|
|
||
| segment = current_span._segment | ||
| segment.set_attributes(attributes) | ||
|
|
||
| try: | ||
| return view_function(**function_args) | ||
| except Exception as exc: | ||
| if isinstance(exc, ChaliceViewError): | ||
| raise | ||
| exc_info = sys.exc_info() | ||
| if segment: | ||
| segment.status = SpanStatus.ERROR.value | ||
| sentry_event, hint = event_from_exception( | ||
| exc_info, | ||
|
sentry[bot] marked this conversation as resolved.
|
||
| client_options=client.options, | ||
| mechanism={"type": "chalice", "handled": False}, | ||
| ) | ||
| sentry_sdk.capture_event(sentry_event, hint=hint) | ||
| if segment is None: | ||
| client.flush() | ||
| raise | ||
| exc_info = sys.exc_info() | ||
| event, hint = event_from_exception( | ||
| exc_info, | ||
| client_options=client.options, | ||
| mechanism={"type": "chalice", "handled": False}, | ||
| else: | ||
| scope.set_transaction_name( | ||
| app.lambda_context.function_name, | ||
| source=TransactionSource.COMPONENT, | ||
| ) | ||
| sentry_sdk.capture_event(event, hint=hint) | ||
| client.flush() | ||
| raise | ||
| try: | ||
| return view_function(**function_args) | ||
| except Exception as exc: | ||
| if isinstance(exc, ChaliceViewError): | ||
| raise | ||
| exc_info = sys.exc_info() | ||
| sentry_event, hint = event_from_exception( | ||
| exc_info, | ||
| client_options=client.options, | ||
| mechanism={"type": "chalice", "handled": False}, | ||
| ) | ||
| sentry_sdk.capture_event(sentry_event, hint=hint) | ||
| client.flush() | ||
| raise | ||
|
|
||
| return wrapped_view_function # type: ignore | ||
|
|
||
|
|
||
| class ChaliceIntegration(Integration): | ||
| identifier = "chalice" | ||
| origin = f"auto.function.{identifier}" | ||
|
|
||
| @staticmethod | ||
| def setup_once() -> None: | ||
|
|
@@ -129,3 +194,25 @@ | |
| RestAPIEventHandler._get_view_function_response = sentry_event_response | ||
| # for everything else (like events) | ||
| chalice.app.EventSourceHandler = EventSourceHandler | ||
|
|
||
|
|
||
| def _get_lambda_span_attributes(aws_context: "Any") -> "Dict[str, Any]": | ||
| invoked_arn = aws_context.invoked_function_arn | ||
| split_invoked_arn = invoked_arn.split(":") | ||
| aws_region = split_invoked_arn[3] if len(split_invoked_arn) > 3 else "unknown" | ||
|
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. Although AWS sets this value and it's unlikely to be malformed, these extra guards were added to ensure that if it were, we don't crash the user's application |
||
|
|
||
| return { | ||
| "sentry.op": OP.FUNCTION_AWS, | ||
| "sentry.origin": ChaliceIntegration.origin, | ||
| "sentry.span.source": SegmentSource.COMPONENT, | ||
| "cloud.platform": CLOUD_PLATFORM.AWS_LAMBDA, | ||
| "cloud.provider": CLOUD_PROVIDER.AWS, | ||
| "faas.name": aws_context.function_name, | ||
| "cloud.region": aws_region, | ||
| "cloud.resource_id": invoked_arn, | ||
| "aws.lambda.invoked_arn": invoked_arn, | ||
| "faas.invocation_id": aws_context.aws_request_id, | ||
| "faas.version": aws_context.function_version, | ||
| "aws.log.group.names": [aws_context.log_group_name], | ||
| "aws.log.stream.names": [aws_context.log_stream_name], | ||
| } | ||
|
Contributor
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. If the AWS Lambda integration is active, it's already attached all (?) of these to the segment, right? So this is for the case when the segment is not coming from the AWS Lambda integration, e.g. custom instrumentation? I'm fine adding this but unless I'm misunderstanding something it's not necessary for feature parity with the non-streaming code path |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: The Chalice integration can crash with an
AttributeErrorif the incoming request has a"headers"key with aNonevalue, as the code attempts to call.items()on it.Severity: HIGH
Suggested Fix
Add a check to ensure
headersis a dictionary before it's used. A pattern likeif not isinstance(headers, dict): headers = {}should be added after retrieving the headers fromrequest_dict, mirroring the safeguard present in the AWS Lambda integration.Prompt for AI Agent