Skip to content

Commit 558f104

Browse files
committed
Merge branch 'major/3.0' into pablo/boto3-add-sqs-extension
2 parents 438ded1 + 86b2e98 commit 558f104

7 files changed

Lines changed: 383 additions & 64 deletions

File tree

‎sentry_sdk/consts.py‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,18 @@ class SPANDATA:
375375
Example: "arn:aws:lambda:us-east-1:123456789012:function:my-function"
376376
"""
377377

378+
AWS_LAMBDA_RESOURCE_MAPPING_ID = "aws.lambda.resource_mapping.id"
379+
"""
380+
The UUID of the AWS Lambda event source mapping the request refers to.
381+
Example: "587b4f6c-1d0e-4d48-8a3f-2c6f9a1e0b7d"
382+
"""
383+
384+
AWS_LAMBDA_TARGET_FUNCTION_NAME = "aws.lambda.target_function.name"
385+
"""
386+
The name of the AWS Lambda function an AWS SDK call acts on, without a qualifier.
387+
Example: "billing"
388+
"""
389+
378390
AWS_LOG_GROUP_NAMES = "aws.log.group.names"
379391
"""
380392
The name(s) of the AWS log group(s) an application is writing to.
@@ -676,6 +688,24 @@ class SPANDATA:
676688
Example: "af9d5aa4-a685-4c5f-a22b-444f80b3cc28"
677689
"""
678690

691+
FAAS_INVOKED_NAME = "faas.invoked_name"
692+
"""
693+
The name of the invoked function, without a qualifier.
694+
Example: "billing"
695+
"""
696+
697+
FAAS_INVOKED_PROVIDER = "faas.invoked_provider"
698+
"""
699+
The cloud provider of the invoked function.
700+
Example: "aws"
701+
"""
702+
703+
FAAS_INVOKED_REGION = "faas.invoked_region"
704+
"""
705+
The cloud region of the invoked function.
706+
Example: "eu-north-1"
707+
"""
708+
679709
FAAS_NAME = "faas.name"
680710
"""
681711
The name of the serverless function.

‎sentry_sdk/integrations/boto3/_client.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ def sentry_patched_make_api_call(
125125
service_ext = _StepFunctionsExtension()
126126

127127
span = _start_client_span(ctx, service_ext)
128+
if service_ext is not None:
129+
# override the request params for e.g. trace propagation.
130+
api_params = service_ext.get_request_params(ctx, span) or api_params
128131

129132
if span is None:
130133
return orig_make_api_call(self, operation_name, api_params)

‎sentry_sdk/integrations/boto3/_services/base.py‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import TYPE_CHECKING
22

33
if TYPE_CHECKING:
4-
from typing import Any, Optional
4+
from typing import Any, Dict, Optional
55

66
from sentry_sdk._types import Attributes
77
from sentry_sdk.integrations.boto3._context import AwsCallContext
@@ -11,9 +11,7 @@
1111
class _ServiceExtension:
1212
"""
1313
Optional hooks for adding service-specific behavior to AWS client
14-
span; non-overridden methods keep the generic instrumentation.
15-
Services without a registered extension in `_SERVICE_EXTENSIONS` continue
16-
to use the generic instrumentation.
14+
span. Non-overridden methods keep the generic instrumentation.
1715
"""
1816

1917
__slots__ = ()
@@ -33,6 +31,15 @@ def get_request_attributes(self, ctx: "AwsCallContext") -> "Attributes":
3331
"""Return request attributes to add before the AWS request is made."""
3432
return {}
3533

34+
def get_request_params(
35+
self, ctx: "AwsCallContext", span: "Optional[Span]"
36+
) -> "Optional[Dict[str, Any]]":
37+
"""
38+
Return params to send instead of `ctx.params`, e.g. with trace propagation added,
39+
or `None` to send them unchanged; must not mutate `ctx.params`.
40+
"""
41+
return None
42+
3643
def on_success_response(
3744
self,
3845
ctx: "AwsCallContext",
Lines changed: 93 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1+
import base64
2+
import json
13
from typing import TYPE_CHECKING
24

3-
from sentry_sdk.integrations.boto3._instrumentation import _instrument_streaming_body
5+
from sentry_sdk.consts import SPANDATA
46
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
613
from sentry_sdk.utils import capture_internal_exceptions
714

815
if TYPE_CHECKING:
9-
from typing import Any
16+
from typing import Any, Dict, Optional
1017

1118
from sentry_sdk._types import Attributes
1219
from sentry_sdk.integrations.boto3._context import AwsCallContext
@@ -16,22 +23,93 @@
1623
class _LambdaExtension(_ServiceExtension):
1724
def get_request_attributes(self, ctx: "AwsCallContext") -> "Attributes":
1825
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+
1952
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+
2658
return attributes
2759

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+
2896
def on_success_response(
2997
self, ctx: "AwsCallContext", response: "Any", span: "Span"
3098
) -> 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
34112

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()

‎sentry_sdk/integrations/boto3/_utils.py‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
from typing import TYPE_CHECKING
22

3+
import sentry_sdk
34
from sentry_sdk.consts import SPANDATA
5+
from sentry_sdk.traces import NoOpSpan
6+
from sentry_sdk.tracing_utils import should_propagate_trace
47

58
if TYPE_CHECKING:
69
from typing import Any, Dict, Iterable, Optional, Sequence, Tuple
710

811
from sentry_sdk._types import Attributes
12+
from sentry_sdk.integrations.boto3._context import AwsCallContext
13+
from sentry_sdk.traces import Span
914

1015
# tuple of (request param name, span attribute name).
1116
_AttributeSpec = Tuple[str, str]
@@ -53,3 +58,21 @@ def _get_batch_error_type(response: "Any") -> "Optional[str]":
5358
if error_codes:
5459
return "aws_batch_partial_failure"
5560
return None
61+
62+
63+
def _get_propagation_headers(
64+
ctx: "AwsCallContext", span: "Optional[Span]"
65+
) -> "Dict[str, str]":
66+
"""Return the `sentry-trace` and `baggage` headers a call propagates to its consumer."""
67+
if ctx.endpoint_url is None or not should_propagate_trace(
68+
sentry_sdk.get_client(), ctx.endpoint_url
69+
):
70+
return {}
71+
72+
# `NoOpSpan` is never sent, use scope's span instead.
73+
if isinstance(span, NoOpSpan):
74+
span = None
75+
76+
return dict(
77+
sentry_sdk.get_current_scope().iter_trace_propagation_headers(span=span)
78+
)

‎tests/integrations/boto3/test_client.py‎

Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -175,39 +175,11 @@ def record_client_span(request, **kwargs):
175175
assert span.get("end_timestamp") is not None
176176

177177

178-
@pytest.mark.parametrize(
179-
"service_name,method_name,api_params,payload_field",
180-
[
181-
pytest.param(
182-
"lambda",
183-
"invoke",
184-
{"FunctionName": "function"},
185-
"Payload",
186-
id="payload",
187-
),
188-
pytest.param(
189-
"s3",
190-
"get_object_annotation",
191-
{
192-
"Bucket": "bucket",
193-
"Key": "file.txt",
194-
"AnnotationName": "annotation",
195-
},
196-
"AnnotationPayload",
197-
id="annotation-payload",
198-
),
199-
],
200-
)
201-
def test_non_body_stream_delays_client_span(
202-
capture_items,
203-
client_factory,
204-
service_name,
205-
method_name,
206-
api_params,
207-
payload_field,
208-
):
209-
client = client_factory(service_name)
210-
require_botocore_model_fields(client, method_name, output_fields=(payload_field,))
178+
def test_non_body_stream_delays_client_span(capture_items, client_factory):
179+
client = client_factory("s3")
180+
require_botocore_model_fields(
181+
client, "get_object_annotation", output_fields=("AnnotationPayload",)
182+
)
211183
request_client_spans = []
212184

213185
def record_client_span(request, **kwargs):
@@ -218,8 +190,10 @@ def record_client_span(request, **kwargs):
218190
def invoke():
219191
parent = sentry_sdk.get_current_span()
220192
with MockResponse(client, 200, {"content-length": "1"}, b"x"):
221-
response = getattr(client, method_name)(**api_params)
222-
body = response[payload_field]
193+
response = client.get_object_annotation(
194+
Bucket="bucket", Key="file.txt", AnnotationName="annotation"
195+
)
196+
body = response["AnnotationPayload"]
223197
assert isinstance(body, StreamingBody)
224198
(request_client_span,) = request_client_spans
225199
assert request_client_span.end_timestamp is None

0 commit comments

Comments
 (0)