|
| 1 | +import base64 |
| 2 | +import json |
1 | 3 | from typing import TYPE_CHECKING |
2 | 4 |
|
3 | | -from sentry_sdk.integrations.boto3._instrumentation import _instrument_streaming_body |
| 5 | +from sentry_sdk.consts import SPANDATA |
4 | 6 | from sentry_sdk.integrations.boto3._services.base import _ServiceExtension |
5 | | -from sentry_sdk.integrations.boto3._utils import _get_aws_arn_attributes |
| 7 | +from sentry_sdk.integrations.boto3._utils import ( |
| 8 | + _get_aws_arn_attributes, |
| 9 | + _get_propagation_headers, |
| 10 | +) |
| 11 | +from sentry_sdk.integrations.boto3.consts import CLOUD_PROVIDER |
| 12 | +from sentry_sdk.traces import SpanStatus |
6 | 13 | from sentry_sdk.utils import capture_internal_exceptions |
7 | 14 |
|
8 | 15 | if TYPE_CHECKING: |
9 | | - from typing import Any |
| 16 | + from typing import Any, Dict, Optional |
10 | 17 |
|
11 | 18 | from sentry_sdk._types import Attributes |
12 | 19 | from sentry_sdk.integrations.boto3._context import AwsCallContext |
|
16 | 23 | class _LambdaExtension(_ServiceExtension): |
17 | 24 | def get_request_attributes(self, ctx: "AwsCallContext") -> "Attributes": |
18 | 25 | attributes: "Attributes" = {} |
| 26 | + |
| 27 | + # event source mapping apis. |
| 28 | + if "UUID" in ctx.params: |
| 29 | + attributes[SPANDATA.AWS_LAMBDA_RESOURCE_MAPPING_ID] = ctx.params["UUID"] |
| 30 | + if "FunctionName" not in ctx.params: |
| 31 | + return attributes |
| 32 | + |
| 33 | + # a name, a partial arn (`<account>:function:<name>`) or an |
| 34 | + # arn (`arn:<partition>:lambda:<region>:<account>:function:<name>`) |
| 35 | + # each optionally followed by `:<version or alias>`. |
| 36 | + function_name = ctx.params["FunctionName"] |
| 37 | + name = function_name.split(":function:")[-1].split(":", 1)[0] |
| 38 | + |
| 39 | + is_invoke = ctx.operation_name in ( |
| 40 | + "Invoke", |
| 41 | + "InvokeAsync", |
| 42 | + "InvokeWithResponseStream", |
| 43 | + ) |
| 44 | + if is_invoke: |
| 45 | + attributes[SPANDATA.FAAS_INVOKED_NAME] = name |
| 46 | + attributes[SPANDATA.FAAS_INVOKED_PROVIDER] = CLOUD_PROVIDER |
| 47 | + if ctx.region_name is not None: |
| 48 | + attributes[SPANDATA.FAAS_INVOKED_REGION] = ctx.region_name |
| 49 | + else: |
| 50 | + attributes[SPANDATA.AWS_LAMBDA_TARGET_FUNCTION_NAME] = name |
| 51 | + |
19 | 52 | with capture_internal_exceptions(): |
20 | | - attributes.update( |
21 | | - _get_aws_arn_attributes( |
22 | | - (ctx.params.get("FunctionName"),), |
23 | | - ("lambda",), |
24 | | - ) |
25 | | - ) |
| 53 | + arn_attributes = _get_aws_arn_attributes((function_name,), ("lambda",)) |
| 54 | + attributes.update(arn_attributes) |
| 55 | + if is_invoke and arn_attributes: |
| 56 | + attributes[SPANDATA.FAAS_INVOKED_REGION] = function_name.split(":")[3] |
| 57 | + |
26 | 58 | return attributes |
27 | 59 |
|
| 60 | + def get_request_params( |
| 61 | + self, ctx: "AwsCallContext", span: "Optional[Span]" |
| 62 | + ) -> "Optional[Dict[str, Any]]": |
| 63 | + # lambda only passes `ClientContext` to the function on synchronous invocations. |
| 64 | + # https://docs.aws.amazon.com/lambda/latest/api/API_Invoke.html#lambda-Invoke-request-ClientContext |
| 65 | + if ( |
| 66 | + ctx.operation_name not in ("Invoke", "InvokeWithResponseStream") |
| 67 | + or ctx.params.get("InvocationType", "RequestResponse") != "RequestResponse" |
| 68 | + ): |
| 69 | + return None |
| 70 | + |
| 71 | + headers = _get_propagation_headers(ctx, span) |
| 72 | + |
| 73 | + # https://docs.aws.amazon.com/lambda/latest/dg/python-context.html |
| 74 | + client_context = ( |
| 75 | + json.loads(base64.b64decode(ctx.params["ClientContext"])) |
| 76 | + if "ClientContext" in ctx.params |
| 77 | + else {} |
| 78 | + ) |
| 79 | + custom = client_context.get("custom", {}) |
| 80 | + # never mix with or replace trace data the caller propagates itself. |
| 81 | + if not headers or any(key in custom for key in headers): |
| 82 | + return None |
| 83 | + |
| 84 | + client_context["custom"] = {**custom, **headers} |
| 85 | + encoded = base64.b64encode( |
| 86 | + json.dumps(client_context, separators=(",", ":")).encode() |
| 87 | + ).decode() |
| 88 | + |
| 89 | + # lambda rejects more than 3,583 bytes of base64; skip propagation rather |
| 90 | + # than fail the call. |
| 91 | + if len(encoded) > 3583: |
| 92 | + return None |
| 93 | + |
| 94 | + return {**ctx.params, "ClientContext": encoded} |
| 95 | + |
28 | 96 | def on_success_response( |
29 | 97 | self, ctx: "AwsCallContext", response: "Any", span: "Span" |
30 | 98 | ) -> None: |
31 | | - streaming_body_instrumented = False |
32 | | - with capture_internal_exceptions(): |
33 | | - streaming_body_instrumented = _instrument_streaming_body(span, response) |
| 99 | + attributes: "Attributes" = {} |
| 100 | + |
| 101 | + if "UUID" in response: |
| 102 | + attributes[SPANDATA.AWS_LAMBDA_RESOURCE_MAPPING_ID] = response["UUID"] |
| 103 | + |
| 104 | + # `Invoke` returns `200 OK` when the function fails; reading it would leave |
| 105 | + # the caller with an empty body. |
| 106 | + if "FunctionError" in response: |
| 107 | + attributes[SPANDATA.ERROR_TYPE] = "lambda_function_error" |
| 108 | + |
| 109 | + span.set_attributes(attributes) |
| 110 | + if SPANDATA.ERROR_TYPE in attributes: |
| 111 | + span.status = SpanStatus.ERROR |
34 | 112 |
|
35 | | - # `StreamingBody`s finish their span when consumed or closed. |
36 | | - if not streaming_body_instrumented: |
37 | | - span.end() |
| 113 | + # `Invoke`'s `Payload` is a `StreamingBody`, but span |
| 114 | + # shouldn't be kept open until the caller reads it. |
| 115 | + span.end() |
0 commit comments