Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 35 additions & 29 deletions sentry_sdk/integrations/aws_lambda/_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from typing import TYPE_CHECKING
from urllib.parse import urlencode

from sentry_conventions.attributes import ATTRIBUTE_NAMES

import sentry_sdk
from sentry_sdk.consts import OP, SPANDATA
from sentry_sdk.data_collection import _apply_key_value_collection_filtering
Expand Down Expand Up @@ -99,7 +101,7 @@ def _get_producer_links(aws_event: "Any", trace_id: str) -> "list[SpanLink]":
link: "SpanLink" = {
"trace_id": str(data["trace_id"]),
"span_id": str(data["parent_span_id"]),
"attributes": {SPANDATA.SENTRY_LINK_TYPE: "message_origin"},
"attributes": {ATTRIBUTE_NAMES.SENTRY_LINK_TYPE: "message_origin"},
}
if data["parent_sampled"] is not None:
link["sampled"] = bool(data["parent_sampled"])
Expand Down Expand Up @@ -195,9 +197,9 @@ def sentry_handler(

additional_attributes: "dict[str, Any]" = {}
if "httpMethod" in request_data:
additional_attributes[SPANDATA.HTTP_REQUEST_METHOD] = request_data[
"httpMethod"
]
additional_attributes[ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD] = (
request_data["httpMethod"]
)

data_collection = client.options["data_collection"]
if "queryStringParameters" in request_data:
Expand All @@ -208,7 +210,7 @@ def sentry_handler(
behaviour=data_collection["url_query_params"],
)
if filtered_qs:
additional_attributes[SPANDATA.URL_QUERY] = urlencode(
additional_attributes[ATTRIBUTE_NAMES.URL_QUERY] = urlencode(
filtered_qs
)

Expand All @@ -225,18 +227,18 @@ def sentry_handler(
function_name = aws_context.function_name

attributes: "Attributes" = {
SPANDATA.SENTRY_OP: OP.FUNCTION_AWS,
SPANDATA.SENTRY_ORIGIN: ORIGIN,
SPANDATA.SENTRY_KIND: "server",
SPANDATA.SENTRY_SEGMENT_NAME_SOURCE: SegmentNameSource.COMPONENT,
SPANDATA.CLOUD_PROVIDER: CLOUD_PROVIDER,
SPANDATA.CLOUD_PLATFORM: CLOUD_PLATFORM,
ATTRIBUTE_NAMES.SENTRY_OP: OP.FUNCTION_AWS,
ATTRIBUTE_NAMES.SENTRY_ORIGIN: ORIGIN,
ATTRIBUTE_NAMES.SENTRY_KIND: "server",
ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE: SegmentNameSource.COMPONENT,
ATTRIBUTE_NAMES.CLOUD_PROVIDER: CLOUD_PROVIDER,
ATTRIBUTE_NAMES.CLOUD_PLATFORM: CLOUD_PLATFORM,
**header_attributes,
**additional_attributes,
}

with capture_internal_exceptions():
attributes[SPANDATA.FAAS_TRIGGER] = _get_trigger(aws_event)
attributes[ATTRIBUTE_NAMES.FAAS_TRIGGER] = _get_trigger(aws_event)

with capture_internal_exceptions():
invoked_function_arn = aws_context.invoked_function_arn
Expand All @@ -248,15 +250,19 @@ def sentry_handler(

attributes.update(
{
SPANDATA.CLOUD_REGION: environ["AWS_REGION"],
SPANDATA.CLOUD_ACCOUNT_ID: arn_parts[4],
SPANDATA.CLOUD_RESOURCE_ID: resource_id,
SPANDATA.FAAS_NAME: function_name,
SPANDATA.FAAS_VERSION: function_version,
SPANDATA.FAAS_INVOCATION_ID: aws_context.aws_request_id,
SPANDATA.AWS_LAMBDA_INVOKED_ARN: invoked_function_arn,
SPANDATA.AWS_LOG_GROUP_NAMES: [aws_context.log_group_name],
SPANDATA.AWS_LOG_STREAM_NAMES: [aws_context.log_stream_name],
ATTRIBUTE_NAMES.CLOUD_REGION: environ["AWS_REGION"],
ATTRIBUTE_NAMES.CLOUD_ACCOUNT_ID: arn_parts[4],
ATTRIBUTE_NAMES.CLOUD_RESOURCE_ID: resource_id,
ATTRIBUTE_NAMES.FAAS_NAME: function_name,
ATTRIBUTE_NAMES.FAAS_VERSION: function_version,
ATTRIBUTE_NAMES.FAAS_INVOCATION_ID: aws_context.aws_request_id,
ATTRIBUTE_NAMES.AWS_LAMBDA_INVOKED_ARN: invoked_function_arn,
ATTRIBUTE_NAMES.AWS_LOG_GROUP_NAMES: [
aws_context.log_group_name
],
ATTRIBUTE_NAMES.AWS_LOG_STREAM_NAMES: [
aws_context.log_stream_name
],
}
)

Expand Down Expand Up @@ -313,16 +319,16 @@ def sentry_handler(
process_span = sentry_sdk.start_span(
name=f"process {destination}",
attributes={
SPANDATA.SENTRY_OP: OP.QUEUE_PROCESS,
SPANDATA.SENTRY_ORIGIN: ORIGIN,
SPANDATA.SENTRY_KIND: "consumer",
SPANDATA.MESSAGING_SYSTEM: MESSAGING_SYSTEMS[
ATTRIBUTE_NAMES.SENTRY_OP: OP.QUEUE_PROCESS,
ATTRIBUTE_NAMES.SENTRY_ORIGIN: ORIGIN,
ATTRIBUTE_NAMES.SENTRY_KIND: "consumer",
ATTRIBUTE_NAMES.MESSAGING_SYSTEM: MESSAGING_SYSTEMS[
event_source
],
SPANDATA.MESSAGING_OPERATION_TYPE: "process",
SPANDATA.MESSAGING_OPERATION_NAME: "process",
SPANDATA.MESSAGING_DESTINATION_NAME: destination,
SPANDATA.MESSAGING_BATCH_MESSAGE_COUNT: len(
ATTRIBUTE_NAMES.MESSAGING_OPERATION_TYPE: "process",
ATTRIBUTE_NAMES.MESSAGING_OPERATION_NAME: "process",
ATTRIBUTE_NAMES.MESSAGING_DESTINATION_NAME: destination,
ATTRIBUTE_NAMES.MESSAGING_BATCH_MESSAGE_COUNT: len(
records
),
},
Expand Down
39 changes: 21 additions & 18 deletions sentry_sdk/integrations/boto3/_instrumentation.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from typing import TYPE_CHECKING, cast
from urllib.parse import urlsplit

from sentry_conventions.attributes import ATTRIBUTE_NAMES

import sentry_sdk
from sentry_sdk.consts import OP, SPANDATA
from sentry_sdk.integrations import DidNotEnable
Expand Down Expand Up @@ -59,8 +61,9 @@ def _get_server_attributes(endpoint_url: "Optional[str]") -> "Attributes":
# Infer the effective port when the configured HTTP(S) endpoint omits it.
# https://opentelemetry.io/docs/specs/semconv/rpc/rpc-spans/
return {
SPANDATA.SERVER_ADDRESS: parsed_url.hostname,
SPANDATA.SERVER_PORT: parsed_url.port or DEFAULT_PORTS[parsed_url.scheme],
ATTRIBUTE_NAMES.SERVER_ADDRESS: parsed_url.hostname,
ATTRIBUTE_NAMES.SERVER_PORT: parsed_url.port
or DEFAULT_PORTS[parsed_url.scheme],
}

except (TypeError, UnicodeError, ValueError):
Expand All @@ -75,10 +78,10 @@ def _get_client_attributes(

# `rpc.service` is deprecated in OTel, but js still uses it.
if ctx.service_id is not None:
attributes[SPANDATA.RPC_SERVICE] = ctx.service_id
attributes[ATTRIBUTE_NAMES.RPC_SERVICE] = ctx.service_id

if ctx.region_name:
attributes[SPANDATA.CLOUD_REGION] = ctx.region_name
attributes[ATTRIBUTE_NAMES.CLOUD_REGION] = ctx.region_name

attributes.update(_get_server_attributes(ctx.endpoint_url))
return attributes
Expand All @@ -92,11 +95,11 @@ def _get_response_attributes(response: "Mapping[str, Any]") -> "Attributes":
# https://github.com/boto/botocore/blob/358f8eec8c76201bb1a7a35644abcbc9036de7ed/botocore/parsers.py#L273-L284
status_code = metadata.get("HTTPStatusCode")
if status_code is not None:
attributes[SPANDATA.HTTP_STATUS_CODE] = status_code
attributes[ATTRIBUTE_NAMES.HTTP_RESPONSE_STATUS_CODE] = status_code

retry_attempts = metadata.get("RetryAttempts", 0)
if retry_attempts > 0:
attributes[SPANDATA.HTTP_REQUEST_RESEND_COUNT] = retry_attempts
attributes[ATTRIBUTE_NAMES.HTTP_REQUEST_RESEND_COUNT] = retry_attempts

headers = metadata.get("HTTPHeaders", {})

Expand All @@ -107,13 +110,13 @@ def _get_response_attributes(response: "Mapping[str, Any]") -> "Attributes":
or headers.get("x-amz-request-id")
)
if request_id:
attributes[SPANDATA.AWS_REQUEST_ID] = request_id
attributes[ATTRIBUTE_NAMES.AWS_REQUEST_ID] = request_id

# S3's `HostId` is the extended request ID returned in `x-amz-id-2`.
# https://docs.aws.amazon.com/AmazonS3/latest/developerguide/get-request-ids.html
extended_request_id = metadata.get("HostId") or headers.get("x-amz-id-2")
if extended_request_id:
attributes[SPANDATA.AWS_EXTENDED_REQUEST_ID] = extended_request_id
attributes[ATTRIBUTE_NAMES.AWS_EXTENDED_REQUEST_ID] = extended_request_id

return attributes

Expand Down Expand Up @@ -142,7 +145,7 @@ def _get_error_attributes(exception: "BaseException") -> "Attributes":
if isinstance(exception, ClientError):
attributes.update(_get_response_attributes(exception.response))

attributes[SPANDATA.ERROR_TYPE] = _get_error_type(exception)
attributes[ATTRIBUTE_NAMES.ERROR_TYPE] = _get_error_type(exception)
return attributes


Expand All @@ -159,12 +162,12 @@ def _start_client_span(
service_name = ctx.service_id or "unknown"
span_name = f"{service_name}.{ctx.operation_name}"
attributes: "Attributes" = {
SPANDATA.CLOUD_PROVIDER: CLOUD_PROVIDER,
SPANDATA.RPC_METHOD: ctx.operation_name,
SPANDATA.RPC_SYSTEM_NAME: AWS_RPC_SYSTEM_NAME,
ATTRIBUTE_NAMES.CLOUD_PROVIDER: CLOUD_PROVIDER,
ATTRIBUTE_NAMES.RPC_METHOD: ctx.operation_name,
ATTRIBUTE_NAMES.RPC_SYSTEM_NAME: AWS_RPC_SYSTEM_NAME,
# all client call spans are by default "client" spans.
# https://opentelemetry.io/docs/specs/semconv/cloud-providers/aws-sdk/#aws-sdk-spans
SPANDATA.SENTRY_KIND: "client",
ATTRIBUTE_NAMES.SENTRY_KIND: "client",
}
attributes.update(_get_client_attributes(ctx))

Expand All @@ -188,8 +191,8 @@ def _start_client_span(
# https://opentelemetry.io/docs/specs/semconv/rpc/rpc-spans/#rpc-client-span
attributes.update(
{
SPANDATA.SENTRY_OP: span_op,
SPANDATA.SENTRY_ORIGIN: ORIGIN,
ATTRIBUTE_NAMES.SENTRY_OP: span_op,
ATTRIBUTE_NAMES.SENTRY_ORIGIN: ORIGIN,
}
)
return sentry_sdk.start_span(
Expand Down Expand Up @@ -224,8 +227,8 @@ def _instrument_streaming_body(span: "Span", parsed: "Dict[str, Any]") -> bool:
# unrelated new spans attach to the stream span since it's the current span.
active=False,
attributes={
SPANDATA.SENTRY_OP: OP.HTTP_CLIENT_STREAM,
SPANDATA.SENTRY_ORIGIN: ORIGIN,
ATTRIBUTE_NAMES.SENTRY_OP: OP.HTTP_CLIENT_STREAM,
ATTRIBUTE_NAMES.SENTRY_ORIGIN: ORIGIN,
},
)

Expand Down Expand Up @@ -319,7 +322,7 @@ def _set_request_attributes(span: "Span", request: "AWSRequest") -> None:
span.set_attributes(_get_server_attributes(request.url))

span.set_attributes(get_url_attributes(client, parsed_url))
span.set_attribute(SPANDATA.HTTP_REQUEST_METHOD, cast(str, request.method))
span.set_attribute(ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD, cast(str, request.method))


def _add_request_breadcrumb(request: "AWSRequest") -> None:
Expand Down
Loading
Loading