|
9 | 9 | from urllib.parse import urlencode |
10 | 10 |
|
11 | 11 | import sentry_sdk |
12 | | -from sentry_sdk.api import continue_trace |
13 | 12 | from sentry_sdk.consts import OP |
14 | 13 | from sentry_sdk.data_collection import _apply_key_value_collection_filtering |
15 | 14 | from sentry_sdk.integrations import Integration |
|
20 | 19 | ) |
21 | 20 | from sentry_sdk.scope import Scope, should_send_default_pii |
22 | 21 | from sentry_sdk.traces import SegmentNameSource |
23 | | -from sentry_sdk.tracing import TransactionSource |
24 | | -from sentry_sdk.tracing_utils import has_span_streaming_enabled |
25 | 22 | from sentry_sdk.utils import ( |
26 | 23 | AnnotatedValue, |
27 | 24 | TimeoutThread, |
|
45 | 42 | MILLIS_TO_SECONDS = 1000.0 |
46 | 43 |
|
47 | 44 |
|
| 45 | +def _get_user_from_event(aws_event: "dict[str, Any]") -> "dict[str, Any]": |
| 46 | + identity = aws_event.get("requestContext", {}).get("identity") |
| 47 | + if identity is None: |
| 48 | + return {} |
| 49 | + |
| 50 | + user_info: "dict[str, Any]" = {} |
| 51 | + |
| 52 | + user_arn = identity.get("userArn") |
| 53 | + if user_arn is not None: |
| 54 | + user_info["id"] = user_arn |
| 55 | + |
| 56 | + ip = identity.get("sourceIp") |
| 57 | + if ip is not None: |
| 58 | + user_info["ip_address"] = ip |
| 59 | + |
| 60 | + return user_info |
| 61 | + |
| 62 | + |
48 | 63 | def _wrap_init_error(init_error: "F") -> "F": |
49 | 64 | @ensure_integration_enabled(AwsLambdaIntegration, init_error) |
50 | 65 | def sentry_init_error(*args: "Any", **kwargs: "Any") -> "Any": |
@@ -177,52 +192,49 @@ def sentry_handler( |
177 | 192 | elif should_send_default_pii(): |
178 | 193 | additional_attributes["url.query"] = urlencode(qs) |
179 | 194 |
|
| 195 | + if not scope._user: |
| 196 | + if has_data_collection_enabled(client.options): |
| 197 | + if client.options["data_collection"]["user_info"]: |
| 198 | + user_info = _get_user_from_event(request_data) |
| 199 | + if user_info: |
| 200 | + scope.set_user(user_info) |
| 201 | + elif should_send_default_pii(): |
| 202 | + user_info = _get_user_from_event(request_data) |
| 203 | + if user_info: |
| 204 | + scope.set_user(user_info) |
| 205 | + |
180 | 206 | sampling_context = { |
181 | 207 | "aws_event": aws_event, |
182 | 208 | "aws_context": aws_context, |
183 | 209 | } |
184 | 210 |
|
185 | 211 | function_name = aws_context.function_name |
186 | 212 |
|
187 | | - if has_span_streaming_enabled(client.options): |
188 | | - sentry_sdk.traces.continue_trace(headers) |
189 | | - Scope.set_custom_sampling_context(sampling_context) |
190 | | - span_ctx = sentry_sdk.traces.start_span( |
191 | | - name=function_name, |
192 | | - parent_span=None, |
193 | | - attributes={ |
194 | | - "sentry.op": OP.FUNCTION_AWS, |
195 | | - "sentry.origin": AwsLambdaIntegration.origin, |
196 | | - "sentry.segment.name.source": SegmentNameSource.COMPONENT, |
197 | | - "cloud.region": aws_region, |
198 | | - "cloud.resource_id": aws_context.invoked_function_arn, |
199 | | - "cloud.platform": CLOUD_PLATFORM.AWS_LAMBDA, |
200 | | - "cloud.provider": CLOUD_PROVIDER.AWS, |
201 | | - "faas.name": function_name, |
202 | | - "faas.invocation_id": aws_context.aws_request_id, |
203 | | - "faas.version": aws_context.function_version, |
204 | | - "aws.lambda.invoked_arn": aws_context.invoked_function_arn, |
205 | | - "aws.log.group.names": [aws_context.log_group_name], |
206 | | - "aws.log.stream.names": [aws_context.log_stream_name], |
207 | | - "messaging.batch.message_count": batch_size, |
208 | | - **header_attributes, |
209 | | - **additional_attributes, |
210 | | - }, |
211 | | - ) |
212 | | - else: |
213 | | - transaction = continue_trace( |
214 | | - headers, |
215 | | - op=OP.FUNCTION_AWS, |
216 | | - name=function_name, |
217 | | - source=TransactionSource.COMPONENT, |
218 | | - origin=AwsLambdaIntegration.origin, |
219 | | - ) |
220 | | - |
221 | | - span_ctx = sentry_sdk.start_transaction( |
222 | | - transaction, custom_sampling_context=sampling_context |
223 | | - ) |
224 | | - |
225 | | - with span_ctx: |
| 213 | + sentry_sdk.traces.continue_trace(headers) |
| 214 | + Scope.set_custom_sampling_context(sampling_context) |
| 215 | + |
| 216 | + with sentry_sdk.traces.start_span( |
| 217 | + name=function_name, |
| 218 | + parent_span=None, |
| 219 | + attributes={ |
| 220 | + "sentry.op": OP.FUNCTION_AWS, |
| 221 | + "sentry.origin": AwsLambdaIntegration.origin, |
| 222 | + "sentry.segment.name.source": SegmentNameSource.COMPONENT, |
| 223 | + "cloud.region": aws_region, |
| 224 | + "cloud.resource_id": aws_context.invoked_function_arn, |
| 225 | + "cloud.platform": CLOUD_PLATFORM.AWS_LAMBDA, |
| 226 | + "cloud.provider": CLOUD_PROVIDER.AWS, |
| 227 | + "faas.name": function_name, |
| 228 | + "faas.invocation_id": aws_context.aws_request_id, |
| 229 | + "faas.version": aws_context.function_version, |
| 230 | + "aws.lambda.invoked_arn": aws_context.invoked_function_arn, |
| 231 | + "aws.log.group.names": [aws_context.log_group_name], |
| 232 | + "aws.log.stream.names": [aws_context.log_stream_name], |
| 233 | + "messaging.batch.message_count": batch_size, |
| 234 | + **header_attributes, |
| 235 | + **additional_attributes, |
| 236 | + }, |
| 237 | + ): |
226 | 238 | try: |
227 | 239 | return handler(aws_event, aws_context, *args, **kwargs) |
228 | 240 | except Exception: |
@@ -402,38 +414,22 @@ def event_processor( |
402 | 414 | client_options = sentry_sdk.get_client().options |
403 | 415 | if has_data_collection_enabled(client_options): |
404 | 416 | if client_options["data_collection"]["user_info"]: |
405 | | - user_info = sentry_event.setdefault("user", {}) |
406 | | - |
407 | | - identity = aws_event.get("requestContext", {}).get("identity") |
408 | | - if identity is None: |
409 | | - identity = {} |
410 | | - |
411 | | - id = identity.get("userArn") |
412 | | - if id is not None: |
413 | | - user_info.setdefault("id", id) |
414 | | - |
415 | | - ip = identity.get("sourceIp") |
416 | | - if ip is not None: |
417 | | - user_info.setdefault("ip_address", ip) |
| 417 | + extracted_user = _get_user_from_event(aws_event) |
| 418 | + if extracted_user: |
| 419 | + user_info = sentry_event.setdefault("user", {}) |
| 420 | + for key, value in extracted_user.items(): |
| 421 | + user_info.setdefault(key, value) |
418 | 422 |
|
419 | 423 | if "incoming_request" in client_options["data_collection"]["http_bodies"]: |
420 | 424 | if "body" in aws_event: |
421 | 425 | request["data"] = aws_event.get("body", "") |
422 | 426 |
|
423 | 427 | elif should_send_default_pii(): |
424 | | - user_info = sentry_event.setdefault("user", {}) |
425 | | - |
426 | | - identity = aws_event.get("requestContext", {}).get("identity") |
427 | | - if identity is None: |
428 | | - identity = {} |
429 | | - |
430 | | - id = identity.get("userArn") |
431 | | - if id is not None: |
432 | | - user_info.setdefault("id", id) |
433 | | - |
434 | | - ip = identity.get("sourceIp") |
435 | | - if ip is not None: |
436 | | - user_info.setdefault("ip_address", ip) |
| 428 | + extracted_user = _get_user_from_event(aws_event) |
| 429 | + if extracted_user: |
| 430 | + user_info = sentry_event.setdefault("user", {}) |
| 431 | + for key, value in extracted_user.items(): |
| 432 | + user_info.setdefault(key, value) |
437 | 433 |
|
438 | 434 | if "body" in aws_event: |
439 | 435 | request["data"] = aws_event.get("body", "") |
|
0 commit comments