From 53001a853c7f8eaeb5bf9042037b24e51b89e636 Mon Sep 17 00:00:00 2001 From: srothh Date: Wed, 25 Nov 2026 16:07:20 +0100 Subject: [PATCH 1/2] ref(attributes): Enforce sentry-cnventions in cloud integrations Enforce sentry conventions attribute names in cloud integrations (aws_lambda, boto3, chalice, gcp) #8323 PY-3090 --- .../integrations/aws_lambda/_handler.py | 64 ++++++++++--------- .../integrations/boto3/_instrumentation.py | 39 +++++------ .../integrations/boto3/_services/bedrock.py | 61 ++++++++++-------- .../integrations/boto3/_services/kinesis.py | 7 +- .../integrations/boto3/_services/lambda_.py | 27 +++++--- sentry_sdk/integrations/boto3/_services/s3.py | 28 ++++---- .../boto3/_services/secretsmanager.py | 21 ++++-- .../integrations/boto3/_services/sns.py | 34 +++++----- .../integrations/boto3/_services/sqs.py | 44 +++++++------ .../boto3/_services/stepfunctions.py | 27 +++++--- sentry_sdk/integrations/boto3/_utils.py | 7 +- sentry_sdk/integrations/chalice.py | 8 ++- sentry_sdk/integrations/gcp.py | 28 +++++--- 13 files changed, 232 insertions(+), 163 deletions(-) diff --git a/sentry_sdk/integrations/aws_lambda/_handler.py b/sentry_sdk/integrations/aws_lambda/_handler.py index 7174171e84..3d99a84352 100644 --- a/sentry_sdk/integrations/aws_lambda/_handler.py +++ b/sentry_sdk/integrations/aws_lambda/_handler.py @@ -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 @@ -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"]) @@ -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: @@ -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 ) @@ -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 @@ -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 + ], } ) @@ -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 ), }, diff --git a/sentry_sdk/integrations/boto3/_instrumentation.py b/sentry_sdk/integrations/boto3/_instrumentation.py index 7ac437f51b..f99583a2fe 100644 --- a/sentry_sdk/integrations/boto3/_instrumentation.py +++ b/sentry_sdk/integrations/boto3/_instrumentation.py @@ -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 @@ -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): @@ -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 @@ -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", {}) @@ -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 @@ -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 @@ -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)) @@ -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( @@ -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, }, ) @@ -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: diff --git a/sentry_sdk/integrations/boto3/_services/bedrock.py b/sentry_sdk/integrations/boto3/_services/bedrock.py index 3efaf1a9a7..e3fd8561db 100644 --- a/sentry_sdk/integrations/boto3/_services/bedrock.py +++ b/sentry_sdk/integrations/boto3/_services/bedrock.py @@ -3,8 +3,10 @@ import time from typing import TYPE_CHECKING, cast +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk -from sentry_sdk.consts import OP, SPANDATA +from sentry_sdk.consts import OP from sentry_sdk.integrations import DidNotEnable from sentry_sdk.integrations.boto3._services.base import _ServiceExtension from sentry_sdk.traces import SpanStatus, StreamedSpan @@ -97,17 +99,18 @@ def sentry_patched_iter( input_tokens = usage["inputTokens"] span.set_attribute( - SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS, usage["outputTokens"] + ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS, + usage["outputTokens"], ) span.set_attribute( - SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS, usage["totalTokens"] + ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS, usage["totalTokens"] ) cache_read_tokens = usage.get("cacheReadInputTokens") if cache_read_tokens is not None: input_tokens += cache_read_tokens span.set_attribute( - SPANDATA.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS, + ATTRIBUTE_NAMES.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS, cache_read_tokens, ) @@ -115,12 +118,12 @@ def sentry_patched_iter( if cache_write_tokens is not None: input_tokens += cache_write_tokens span.set_attribute( - SPANDATA.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS, + ATTRIBUTE_NAMES.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS, cache_write_tokens, ) span.set_attribute( - SPANDATA.GEN_AI_USAGE_INPUT_TOKENS, + ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS, input_tokens, ) @@ -129,7 +132,7 @@ def sentry_patched_iter( if start_time is not None and "contentBlockDelta" in event: span.set_attribute( - SPANDATA.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK, + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK, time.perf_counter() - start_time, ) start_time = None @@ -137,7 +140,7 @@ def sentry_patched_iter( if "messageStop" in event: finish_reasons: "list[str]" = [event["messageStop"]["stopReason"]] span.set_attribute( - SPANDATA.GEN_AI_RESPONSE_FINISH_REASONS, finish_reasons + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_FINISH_REASONS, finish_reasons ) if not sentry_sdk.get_client().options["data_collection"]["gen_ai"][ @@ -154,7 +157,7 @@ def sentry_patched_iter( if "messageStop" in event and role is not None: span.set_attribute( - SPANDATA.GEN_AI_OUTPUT_MESSAGES, + ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES, json.dumps( [ { @@ -619,29 +622,30 @@ def get_request_attributes(self, ctx: "AwsCallContext") -> "Attributes": return {} attributes: "Attributes" = { - SPANDATA.GEN_AI_PROVIDER_NAME: "aws.bedrock", - SPANDATA.GEN_AI_OPERATION_NAME: "chat", - SPANDATA.GEN_AI_RESPONSE_STREAMING: ctx.operation_name == "ConverseStream", + ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME: "aws.bedrock", + ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME: "chat", + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING: ctx.operation_name + == "ConverseStream", } model_id = ctx.params.get("modelId") if model_id is not None: - attributes[SPANDATA.GEN_AI_REQUEST_MODEL] = model_id + attributes[ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] = model_id inference_config = ctx.params.get("inferenceConfig") if isinstance(inference_config, dict): max_tokens = inference_config.get("maxTokens") if max_tokens is not None: - attributes[SPANDATA.GEN_AI_REQUEST_MAX_TOKENS] = max_tokens + attributes[ATTRIBUTE_NAMES.GEN_AI_REQUEST_MAX_TOKENS] = max_tokens temperature = inference_config.get("temperature") if temperature is not None: - attributes[SPANDATA.GEN_AI_REQUEST_TEMPERATURE] = temperature + attributes[ATTRIBUTE_NAMES.GEN_AI_REQUEST_TEMPERATURE] = temperature top_p = inference_config.get("topP") if top_p is not None: - attributes[SPANDATA.GEN_AI_REQUEST_TOP_P] = top_p + attributes[ATTRIBUTE_NAMES.GEN_AI_REQUEST_TOP_P] = top_p client_options = sentry_sdk.get_client().options if not client_options["data_collection"]["gen_ai"]["inputs"]: @@ -649,13 +653,13 @@ def get_request_attributes(self, ctx: "AwsCallContext") -> "Attributes": system_instructions = ctx.params.get("system") if isinstance(system_instructions, (list, tuple)): - attributes[SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS] = json.dumps( + attributes[ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS] = json.dumps( _transform_system_instructions(system_instructions) ) input_messages = ctx.params.get("messages") if isinstance(input_messages, (list, tuple)): - attributes[SPANDATA.GEN_AI_INPUT_MESSAGES] = json.dumps( + attributes[ATTRIBUTE_NAMES.GEN_AI_INPUT_MESSAGES] = json.dumps( _transform_messages(input_messages) ) @@ -667,7 +671,7 @@ def get_request_attributes(self, ctx: "AwsCallContext") -> "Attributes": if not isinstance(tools, list): return attributes - attributes[SPANDATA.GEN_AI_TOOL_DEFINITIONS] = json.dumps( + attributes[ATTRIBUTE_NAMES.GEN_AI_TOOL_DEFINITIONS] = json.dumps( _transform_tool_definitions(tools) ) @@ -684,9 +688,11 @@ def on_success_response( usage = response["usage"] span.set_attribute( - SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS, usage["outputTokens"] + ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS, usage["outputTokens"] + ) + span.set_attribute( + ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS, usage["totalTokens"] ) - span.set_attribute(SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS, usage["totalTokens"]) input_tokens = usage["inputTokens"] @@ -694,21 +700,24 @@ def on_success_response( if cache_read_tokens is not None: input_tokens += cache_read_tokens span.set_attribute( - SPANDATA.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS, cache_read_tokens + ATTRIBUTE_NAMES.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS, + cache_read_tokens, ) cache_write_tokens = usage.get("cacheWriteInputTokens") if cache_write_tokens is not None: input_tokens += cache_write_tokens span.set_attribute( - SPANDATA.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS, + ATTRIBUTE_NAMES.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS, cache_write_tokens, ) - span.set_attribute(SPANDATA.GEN_AI_USAGE_INPUT_TOKENS, input_tokens) + span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS, input_tokens) finish_reasons: "list[str]" = [response["stopReason"]] - span.set_attribute(SPANDATA.GEN_AI_RESPONSE_FINISH_REASONS, finish_reasons) + span.set_attribute( + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_FINISH_REASONS, finish_reasons + ) if ( "message" in response["output"] @@ -717,7 +726,7 @@ def on_success_response( ] ): span.set_attribute( - SPANDATA.GEN_AI_OUTPUT_MESSAGES, + ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES, json.dumps(_transform_messages([response["output"]["message"]])), ) diff --git a/sentry_sdk/integrations/boto3/_services/kinesis.py b/sentry_sdk/integrations/boto3/_services/kinesis.py index 80bae7fc1d..68d9473657 100644 --- a/sentry_sdk/integrations/boto3/_services/kinesis.py +++ b/sentry_sdk/integrations/boto3/_services/kinesis.py @@ -1,6 +1,7 @@ from typing import TYPE_CHECKING -from sentry_sdk.consts import SPANDATA +from sentry_conventions.attributes import ATTRIBUTE_NAMES + from sentry_sdk.integrations.boto3._services.base import _ServiceExtension from sentry_sdk.integrations.boto3._utils import _get_aws_arn_attributes from sentry_sdk.traces import SpanStatus @@ -22,7 +23,7 @@ def get_request_attributes(self, ctx: "AwsCallContext") -> "Attributes": # `…:stream//consumer/:` for a consumer. for param in ("StreamName", "StreamARN", "ConsumerARN", "ResourceARN"): if param in ctx.params: - attributes[SPANDATA.AWS_KINESIS_STREAM_NAME] = ( + attributes[ATTRIBUTE_NAMES.AWS_KINESIS_STREAM_NAME] = ( ctx.params[param].split(":stream/", 1)[-1].split("/", 1)[0] ) break @@ -57,7 +58,7 @@ def on_success_response( if "ErrorCode" in record } span.set_attribute( - SPANDATA.ERROR_TYPE, + ATTRIBUTE_NAMES.ERROR_TYPE, error_codes.pop() if len(error_codes) == 1 else "aws_batch_partial_failure", ) span.status = SpanStatus.ERROR diff --git a/sentry_sdk/integrations/boto3/_services/lambda_.py b/sentry_sdk/integrations/boto3/_services/lambda_.py index 2b6a78c861..a225945372 100644 --- a/sentry_sdk/integrations/boto3/_services/lambda_.py +++ b/sentry_sdk/integrations/boto3/_services/lambda_.py @@ -2,7 +2,8 @@ import json from typing import TYPE_CHECKING -from sentry_sdk.consts import SPANDATA +from sentry_conventions.attributes import ATTRIBUTE_NAMES + from sentry_sdk.integrations.boto3._services.base import _ServiceExtension from sentry_sdk.integrations.boto3._utils import ( _get_aws_arn_attributes, @@ -26,7 +27,9 @@ def get_request_attributes(self, ctx: "AwsCallContext") -> "Attributes": # event source mapping apis. if "UUID" in ctx.params: - attributes[SPANDATA.AWS_LAMBDA_RESOURCE_MAPPING_ID] = ctx.params["UUID"] + attributes[ATTRIBUTE_NAMES.AWS_LAMBDA_RESOURCE_MAPPING_ID] = ctx.params[ + "UUID" + ] if "FunctionName" not in ctx.params: return attributes @@ -42,18 +45,20 @@ def get_request_attributes(self, ctx: "AwsCallContext") -> "Attributes": "InvokeWithResponseStream", ) if is_invoke: - attributes[SPANDATA.FAAS_INVOKED_NAME] = name - attributes[SPANDATA.FAAS_INVOKED_PROVIDER] = CLOUD_PROVIDER + attributes[ATTRIBUTE_NAMES.FAAS_INVOKED_NAME] = name + attributes[ATTRIBUTE_NAMES.FAAS_INVOKED_PROVIDER] = CLOUD_PROVIDER if ctx.region_name is not None: - attributes[SPANDATA.FAAS_INVOKED_REGION] = ctx.region_name + attributes[ATTRIBUTE_NAMES.FAAS_INVOKED_REGION] = ctx.region_name else: - attributes[SPANDATA.AWS_LAMBDA_TARGET_FUNCTION_NAME] = name + attributes[ATTRIBUTE_NAMES.AWS_LAMBDA_TARGET_FUNCTION_NAME] = name with capture_internal_exceptions(): arn_attributes = _get_aws_arn_attributes((function_name,), ("lambda",)) attributes.update(arn_attributes) if is_invoke and arn_attributes: - attributes[SPANDATA.FAAS_INVOKED_REGION] = function_name.split(":")[3] + attributes[ATTRIBUTE_NAMES.FAAS_INVOKED_REGION] = function_name.split( + ":" + )[3] return attributes @@ -99,15 +104,17 @@ def on_success_response( attributes: "Attributes" = {} if "UUID" in response: - attributes[SPANDATA.AWS_LAMBDA_RESOURCE_MAPPING_ID] = response["UUID"] + attributes[ATTRIBUTE_NAMES.AWS_LAMBDA_RESOURCE_MAPPING_ID] = response[ + "UUID" + ] # `Invoke` returns `200 OK` when the function fails; reading it would leave # the caller with an empty body. if "FunctionError" in response: - attributes[SPANDATA.ERROR_TYPE] = "lambda_function_error" + attributes[ATTRIBUTE_NAMES.ERROR_TYPE] = "lambda_function_error" span.set_attributes(attributes) - if SPANDATA.ERROR_TYPE in attributes: + if ATTRIBUTE_NAMES.ERROR_TYPE in attributes: span.status = SpanStatus.ERROR # `Invoke`'s `Payload` is a `StreamingBody`, but span diff --git a/sentry_sdk/integrations/boto3/_services/s3.py b/sentry_sdk/integrations/boto3/_services/s3.py index d5c58421f1..94f31eb118 100644 --- a/sentry_sdk/integrations/boto3/_services/s3.py +++ b/sentry_sdk/integrations/boto3/_services/s3.py @@ -2,7 +2,9 @@ from datetime import datetime from typing import TYPE_CHECKING -from sentry_sdk.consts import OP, SPANDATA +from sentry_conventions.attributes import ATTRIBUTE_NAMES + +from sentry_sdk.consts import OP from sentry_sdk.integrations.boto3._instrumentation import _instrument_streaming_body from sentry_sdk.integrations.boto3._services.base import _ServiceExtension from sentry_sdk.integrations.boto3._utils import ( @@ -51,9 +53,9 @@ # s3 request attributes that are extracted when present, regardless of operation. _REQUEST_ATTRIBUTES: "Sequence[_AttributeSpec]" = ( - ("Bucket", SPANDATA.AWS_S3_BUCKET), - ("Key", SPANDATA.AWS_S3_KEY), - ("UploadId", SPANDATA.AWS_S3_UPLOAD_ID), + ("Bucket", ATTRIBUTE_NAMES.AWS_S3_BUCKET), + ("Key", ATTRIBUTE_NAMES.AWS_S3_KEY), + ("UploadId", ATTRIBUTE_NAMES.AWS_S3_UPLOAD_ID), ) @@ -79,12 +81,12 @@ def get_request_attributes(self, ctx: "AwsCallContext") -> "Attributes": # https://docs.aws.amazon.com/boto3/latest/reference/services/s3/client/upload_part_copy.html copy_source = ctx.params["CopySource"] if isinstance(copy_source, str): - attributes[SPANDATA.AWS_S3_COPY_SOURCE] = copy_source + attributes[ATTRIBUTE_NAMES.AWS_S3_COPY_SOURCE] = copy_source else: value = f"{copy_source['Bucket']}/{copy_source['Key']}" if "VersionId" in copy_source: value += f"?versionId={copy_source['VersionId']}" - attributes[SPANDATA.AWS_S3_COPY_SOURCE] = value + attributes[ATTRIBUTE_NAMES.AWS_S3_COPY_SOURCE] = value # OTel defines `PartNumber` for `UploadPart` and `UploadPartCopy` only. # https://opentelemetry.io/docs/specs/semconv/object-stores/s3/#attributes @@ -92,11 +94,11 @@ def get_request_attributes(self, ctx: "AwsCallContext") -> "Attributes": ctx.operation_name in ("UploadPart", "UploadPartCopy") and "PartNumber" in ctx.params ): - attributes[SPANDATA.AWS_S3_PART_NUMBER] = ctx.params["PartNumber"] + attributes[ATTRIBUTE_NAMES.AWS_S3_PART_NUMBER] = ctx.params["PartNumber"] if "Delete" in ctx.params: with capture_internal_exceptions(): - attributes[SPANDATA.AWS_S3_DELETE] = json.dumps( + attributes[ATTRIBUTE_NAMES.AWS_S3_DELETE] = json.dumps( ctx.params["Delete"], default=lambda value: ( value.isoformat() if isinstance(value, datetime) else str(value) @@ -109,7 +111,7 @@ def get_request_attributes(self, ctx: "AwsCallContext") -> "Attributes": ctx.operation_name == "CompleteMultipartUpload" and "MpuObjectSize" in ctx.params ): - attributes[SPANDATA.FILE_SIZE] = ctx.params["MpuObjectSize"] + attributes[ATTRIBUTE_NAMES.FILE_SIZE] = ctx.params["MpuObjectSize"] return attributes @@ -123,12 +125,14 @@ def on_success_response( and "ContentLength" in response ): # `ContentLength` is the size of the HTTP body returned, which may be a range. - attributes[SPANDATA.HTTP_BODY_SIZE] = response["ContentLength"] + attributes[ATTRIBUTE_NAMES.HTTP_RESPONSE_BODY_SIZE] = response[ + "ContentLength" + ] # report the complete file size, not just the HTTP body size. file_size_field = _RESPONSE_FILE_SIZE_FIELDS.get(ctx.operation_name) if file_size_field is not None and file_size_field in response: - attributes[SPANDATA.FILE_SIZE] = response[file_size_field] + attributes[ATTRIBUTE_NAMES.FILE_SIZE] = response[file_size_field] if ( ctx.operation_name == "HeadObject" @@ -137,7 +141,7 @@ def on_success_response( and "ContentLength" in response ): # an un-ranged `HEAD` has no body, so `ContentLength` is the file size. - attributes[SPANDATA.FILE_SIZE] = response["ContentLength"] + attributes[ATTRIBUTE_NAMES.FILE_SIZE] = response["ContentLength"] span.set_attributes(attributes) diff --git a/sentry_sdk/integrations/boto3/_services/secretsmanager.py b/sentry_sdk/integrations/boto3/_services/secretsmanager.py index b3d5f7e99f..ce19ed06ff 100644 --- a/sentry_sdk/integrations/boto3/_services/secretsmanager.py +++ b/sentry_sdk/integrations/boto3/_services/secretsmanager.py @@ -1,7 +1,8 @@ import re from typing import TYPE_CHECKING -from sentry_sdk.consts import SPANDATA +from sentry_conventions.attributes import ATTRIBUTE_NAMES + from sentry_sdk.integrations.boto3._services.base import _ServiceExtension from sentry_sdk.integrations.boto3._utils import ( _extract_attributes, @@ -33,7 +34,9 @@ def get_request_attributes(self, ctx: "AwsCallContext") -> "Attributes": # only `CreateSecret` takes a `Name`; every other operations acts on # a secret by name or full/partial arn. if "Name" in ctx.params: - attributes[SPANDATA.AWS_SECRETSMANAGER_SECRET_NAME] = ctx.params["Name"] + attributes[ATTRIBUTE_NAMES.AWS_SECRETSMANAGER_SECRET_NAME] = ctx.params[ + "Name" + ] if "SecretId" in ctx.params: secret_id = ctx.params["SecretId"] @@ -45,12 +48,16 @@ def get_request_attributes(self, ctx: "AwsCallContext") -> "Attributes": ) attributes.update(arn_attributes) if arn_attributes: - attributes[SPANDATA.AWS_SECRETSMANAGER_SECRET_ARN] = secret_id - attributes[SPANDATA.AWS_SECRETSMANAGER_SECRET_NAME] = ( + attributes[ATTRIBUTE_NAMES.AWS_SECRETSMANAGER_SECRET_ARN] = ( + secret_id + ) + attributes[ATTRIBUTE_NAMES.AWS_SECRETSMANAGER_SECRET_NAME] = ( _get_name_from_arn(secret_id) ) else: - attributes[SPANDATA.AWS_SECRETSMANAGER_SECRET_NAME] = secret_id + attributes[ATTRIBUTE_NAMES.AWS_SECRETSMANAGER_SECRET_NAME] = ( + secret_id + ) return attributes @@ -63,8 +70,8 @@ def on_success_response( _extract_attributes( response, ( - ("ARN", SPANDATA.AWS_SECRETSMANAGER_SECRET_ARN), - ("Name", SPANDATA.AWS_SECRETSMANAGER_SECRET_NAME), + ("ARN", ATTRIBUTE_NAMES.AWS_SECRETSMANAGER_SECRET_ARN), + ("Name", ATTRIBUTE_NAMES.AWS_SECRETSMANAGER_SECRET_NAME), ), ) ) diff --git a/sentry_sdk/integrations/boto3/_services/sns.py b/sentry_sdk/integrations/boto3/_services/sns.py index ac4fd17cd0..d751849bf6 100644 --- a/sentry_sdk/integrations/boto3/_services/sns.py +++ b/sentry_sdk/integrations/boto3/_services/sns.py @@ -1,7 +1,9 @@ from typing import TYPE_CHECKING +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk -from sentry_sdk.consts import OP, SPANDATA +from sentry_sdk.consts import OP from sentry_sdk.integrations.boto3._services.base import _ServiceExtension from sentry_sdk.integrations.boto3._utils import ( _get_aws_arn_attributes, @@ -34,9 +36,9 @@ def get_span_name( if ctx.operation_name not in _PUBLISH_OPERATIONS: return None - destination = attributes.get(SPANDATA.MESSAGING_DESTINATION_NAME) - if destination is None and SPANDATA.SERVER_ADDRESS in attributes: - destination = f"{attributes[SPANDATA.SERVER_ADDRESS]}:{attributes[SPANDATA.SERVER_PORT]}" + destination = attributes.get(ATTRIBUTE_NAMES.MESSAGING_DESTINATION_NAME) + if destination is None and ATTRIBUTE_NAMES.SERVER_ADDRESS in attributes: + destination = f"{attributes[ATTRIBUTE_NAMES.SERVER_ADDRESS]}:{attributes[ATTRIBUTE_NAMES.SERVER_PORT]}" # for example: `send ` when `messaging.operation.type=send` (always send). # https://opentelemetry.io/docs/specs/semconv/messaging/messaging-spans/#span-name @@ -64,29 +66,31 @@ def get_request_attributes(self, ctx: "AwsCallContext") -> "Attributes": ) if topic_arn: - attributes[SPANDATA.AWS_SNS_TOPIC_ARN] = topic_arn + attributes[ATTRIBUTE_NAMES.AWS_SNS_TOPIC_ARN] = topic_arn # `arn:aws:sns:eu-north-1:123456789012:alerts` -> `alerts` topic_name = topic_arn.split(":")[-1] - attributes[SPANDATA.MESSAGING_DESTINATION_NAME] = topic_name + attributes[ATTRIBUTE_NAMES.MESSAGING_DESTINATION_NAME] = topic_name elif ctx.operation_name == "CreateTopic" and "Name" in ctx.params: - attributes[SPANDATA.MESSAGING_DESTINATION_NAME] = ctx.params["Name"] + attributes[ATTRIBUTE_NAMES.MESSAGING_DESTINATION_NAME] = ctx.params[ + "Name" + ] if ctx.operation_name in _PUBLISH_OPERATIONS: attributes.update( { - SPANDATA.SENTRY_KIND: "producer", - SPANDATA.MESSAGING_SYSTEM: "aws.sns", - SPANDATA.MESSAGING_OPERATION_TYPE: "send", - SPANDATA.MESSAGING_OPERATION_NAME: "publish", + ATTRIBUTE_NAMES.SENTRY_KIND: "producer", + ATTRIBUTE_NAMES.MESSAGING_SYSTEM: "aws.sns", + ATTRIBUTE_NAMES.MESSAGING_OPERATION_TYPE: "send", + ATTRIBUTE_NAMES.MESSAGING_OPERATION_NAME: "publish", } ) # phone numbers and device endpoints are PII. if "PhoneNumber" in ctx.params or endpoint_arn: - attributes[SPANDATA.MESSAGING_DESTINATION_NAME] = "[Filtered]" + attributes[ATTRIBUTE_NAMES.MESSAGING_DESTINATION_NAME] = "[Filtered]" if "PublishBatchRequestEntries" in ctx.params: with capture_internal_exceptions(): - attributes[SPANDATA.MESSAGING_BATCH_MESSAGE_COUNT] = len( + attributes[ATTRIBUTE_NAMES.MESSAGING_BATCH_MESSAGE_COUNT] = len( ctx.params["PublishBatchRequestEntries"] ) @@ -121,12 +125,12 @@ def on_success_response( # `Stubber` or an SNS-compatible service, must only lose these attributes. with capture_internal_exceptions(): if ctx.operation_name == "Publish" and "MessageId" in response: - attributes[SPANDATA.MESSAGING_MESSAGE_ID] = response["MessageId"] + attributes[ATTRIBUTE_NAMES.MESSAGING_MESSAGE_ID] = response["MessageId"] error_type = _get_batch_error_type(response) if error_type is not None: span.status = SpanStatus.ERROR - attributes[SPANDATA.ERROR_TYPE] = error_type + attributes[ATTRIBUTE_NAMES.ERROR_TYPE] = error_type span.set_attributes(attributes) span.end() diff --git a/sentry_sdk/integrations/boto3/_services/sqs.py b/sentry_sdk/integrations/boto3/_services/sqs.py index 82b59fda02..c954e99c73 100644 --- a/sentry_sdk/integrations/boto3/_services/sqs.py +++ b/sentry_sdk/integrations/boto3/_services/sqs.py @@ -3,7 +3,9 @@ from typing import TYPE_CHECKING from urllib.parse import unquote, urlsplit -from sentry_sdk.consts import OP, SPANDATA +from sentry_conventions.attributes import ATTRIBUTE_NAMES + +from sentry_sdk.consts import OP from sentry_sdk.integrations.boto3._services.base import _ServiceExtension from sentry_sdk.integrations.boto3._utils import ( _get_aws_arn_attributes, @@ -67,7 +69,7 @@ def _message_link(message: "Dict[str, Any]") -> "Optional[SpanLink]": "trace_id": str(data["trace_id"]), "span_id": str(data["parent_span_id"]), # Sentry drops other link attributes for now; add `messaging.message.id` in the future. - "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"]) @@ -88,7 +90,7 @@ def get_span_name( # for example: `send ` when `messaging.operation.type=send`. # https://opentelemetry.io/docs/specs/semconv/messaging/messaging-spans/#span-name operation_type = _MESSAGING_OPERATIONS[ctx.operation_name][1] - destination_name = attributes.get(SPANDATA.MESSAGING_DESTINATION_NAME) + destination_name = attributes.get(ATTRIBUTE_NAMES.MESSAGING_DESTINATION_NAME) return ( f"{operation_type} {destination_name}" if destination_name @@ -107,26 +109,28 @@ def get_request_attributes(self, ctx: "AwsCallContext") -> "Attributes": queue_url = ctx.params.get("QueueUrl") if queue_url is not None: - attributes[SPANDATA.AWS_SQS_QUEUE_URL] = queue_url + attributes[ATTRIBUTE_NAMES.AWS_SQS_QUEUE_URL] = queue_url with capture_internal_exceptions(): # `https://sqs.eu-north-1.amazonaws.com/123456789012/orders` => ['123456789012', 'orders'] path = unquote(urlsplit(queue_url).path).strip("/").split("/") - attributes[SPANDATA.MESSAGING_DESTINATION_NAME] = path[-1] + attributes[ATTRIBUTE_NAMES.MESSAGING_DESTINATION_NAME] = path[-1] if re.fullmatch(r"[0-9]{12}", path[0]): - attributes[SPANDATA.CLOUD_ACCOUNT_ID] = path[0] + attributes[ATTRIBUTE_NAMES.CLOUD_ACCOUNT_ID] = path[0] elif "QueueName" in ctx.params: # `CreateQueue` & `GetQueueUrl` operations. - attributes[SPANDATA.MESSAGING_DESTINATION_NAME] = ctx.params["QueueName"] + attributes[ATTRIBUTE_NAMES.MESSAGING_DESTINATION_NAME] = ctx.params[ + "QueueName" + ] if "QueueOwnerAWSAccountId" in ctx.params: - attributes[SPANDATA.CLOUD_ACCOUNT_ID] = ctx.params[ + attributes[ATTRIBUTE_NAMES.CLOUD_ACCOUNT_ID] = ctx.params[ "QueueOwnerAWSAccountId" ] elif "SourceArn" in ctx.params: # `arn:aws:sqs:eu-north-1:123456789012:orders` => `orders` - attributes[SPANDATA.MESSAGING_DESTINATION_NAME] = ctx.params[ + attributes[ATTRIBUTE_NAMES.MESSAGING_DESTINATION_NAME] = ctx.params[ "SourceArn" ].split(":")[-1] @@ -134,16 +138,16 @@ def get_request_attributes(self, ctx: "AwsCallContext") -> "Attributes": operation_type = _MESSAGING_OPERATIONS[ctx.operation_name][1] attributes.update( { - SPANDATA.MESSAGING_SYSTEM: "aws_sqs", - SPANDATA.MESSAGING_OPERATION_TYPE: operation_type, - SPANDATA.MESSAGING_OPERATION_NAME: operation_type, + ATTRIBUTE_NAMES.MESSAGING_SYSTEM: "aws_sqs", + ATTRIBUTE_NAMES.MESSAGING_OPERATION_TYPE: operation_type, + ATTRIBUTE_NAMES.MESSAGING_OPERATION_NAME: operation_type, } ) if operation_type == "send": - attributes[SPANDATA.SENTRY_KIND] = "producer" + attributes[ATTRIBUTE_NAMES.SENTRY_KIND] = "producer" if "Entries" in ctx.params: with capture_internal_exceptions(): - attributes[SPANDATA.MESSAGING_BATCH_MESSAGE_COUNT] = len( + attributes[ATTRIBUTE_NAMES.MESSAGING_BATCH_MESSAGE_COUNT] = len( ctx.params["Entries"] ) return attributes @@ -157,18 +161,22 @@ def on_success_response( # `Stubber` or an SQS-compatible service, must only lose these attributes. with capture_internal_exceptions(): if ctx.operation_name == "SendMessage" and "MessageId" in response: - attributes[SPANDATA.MESSAGING_MESSAGE_ID] = response["MessageId"] + attributes[ATTRIBUTE_NAMES.MESSAGING_MESSAGE_ID] = response["MessageId"] elif ctx.operation_name == "ReceiveMessage": # `Messages` is left out when no message was received. messages = response.get("Messages", ()) - attributes[SPANDATA.MESSAGING_BATCH_MESSAGE_COUNT] = len(messages) + attributes[ATTRIBUTE_NAMES.MESSAGING_BATCH_MESSAGE_COUNT] = len( + messages + ) if len(messages) == 1 and "MessageId" in messages[0]: - attributes[SPANDATA.MESSAGING_MESSAGE_ID] = messages[0]["MessageId"] + attributes[ATTRIBUTE_NAMES.MESSAGING_MESSAGE_ID] = messages[0][ + "MessageId" + ] error_type = _get_batch_error_type(response) if error_type is not None: span.status = SpanStatus.ERROR - attributes[SPANDATA.ERROR_TYPE] = error_type + attributes[ATTRIBUTE_NAMES.ERROR_TYPE] = error_type if ctx.operation_name == "ReceiveMessage": with capture_internal_exceptions(): diff --git a/sentry_sdk/integrations/boto3/_services/stepfunctions.py b/sentry_sdk/integrations/boto3/_services/stepfunctions.py index 37c4896032..7ee7d1d2b8 100644 --- a/sentry_sdk/integrations/boto3/_services/stepfunctions.py +++ b/sentry_sdk/integrations/boto3/_services/stepfunctions.py @@ -1,6 +1,7 @@ from typing import TYPE_CHECKING -from sentry_sdk.consts import SPANDATA +from sentry_conventions.attributes import ATTRIBUTE_NAMES + from sentry_sdk.integrations.boto3._services.base import _ServiceExtension from sentry_sdk.integrations.boto3._utils import ( _extract_attributes, @@ -19,9 +20,9 @@ # set on both request and response if available. _ARN_ATTRIBUTES: "Sequence[_AttributeSpec]" = ( - ("activityArn", SPANDATA.AWS_STEP_FUNCTIONS_ACTIVITY_ARN), - ("executionArn", SPANDATA.AWS_STEP_FUNCTIONS_EXECUTION_ARN), - ("stateMachineArn", SPANDATA.AWS_STEP_FUNCTIONS_STATE_MACHINE_ARN), + ("activityArn", ATTRIBUTE_NAMES.AWS_STEP_FUNCTIONS_ACTIVITY_ARN), + ("executionArn", ATTRIBUTE_NAMES.AWS_STEP_FUNCTIONS_EXECUTION_ARN), + ("stateMachineArn", ATTRIBUTE_NAMES.AWS_STEP_FUNCTIONS_STATE_MACHINE_ARN), ) @@ -33,9 +34,9 @@ def get_request_attributes(self, ctx: "AwsCallContext") -> "Attributes": if "name" in ctx.params: name = ctx.params["name"] if ctx.operation_name == "CreateActivity": - attributes[SPANDATA.AWS_STEP_FUNCTIONS_ACTIVITY_NAME] = name + attributes[ATTRIBUTE_NAMES.AWS_STEP_FUNCTIONS_ACTIVITY_NAME] = name elif ctx.operation_name == "CreateStateMachine": - attributes[SPANDATA.AWS_STEP_FUNCTIONS_STATE_MACHINE_NAME] = name + attributes[ATTRIBUTE_NAMES.AWS_STEP_FUNCTIONS_STATE_MACHINE_NAME] = name for param in ( "activityArn", @@ -62,9 +63,11 @@ def get_request_attributes(self, ctx: "AwsCallContext") -> "Attributes": resource_type, resource = arn.split(":", 6)[5:] name = resource.split(":", 1)[0].split("/", 1)[0] if resource_type == "activity": - attributes[SPANDATA.AWS_STEP_FUNCTIONS_ACTIVITY_NAME] = name + attributes[ATTRIBUTE_NAMES.AWS_STEP_FUNCTIONS_ACTIVITY_NAME] = name else: - attributes[SPANDATA.AWS_STEP_FUNCTIONS_STATE_MACHINE_NAME] = name + attributes[ + ATTRIBUTE_NAMES.AWS_STEP_FUNCTIONS_STATE_MACHINE_NAME + ] = name return attributes @@ -78,10 +81,14 @@ def on_success_response( if ctx.operation_name == "StartSyncExecution": if response.get("status") == "FAILED": span.status = SpanStatus.ERROR - attributes[SPANDATA.ERROR_TYPE] = "stepfunctions_execution_failed" + attributes[ATTRIBUTE_NAMES.ERROR_TYPE] = ( + "stepfunctions_execution_failed" + ) elif response.get("status") == "TIMED_OUT": span.status = SpanStatus.ERROR - attributes[SPANDATA.ERROR_TYPE] = "stepfunctions_execution_timed_out" + attributes[ATTRIBUTE_NAMES.ERROR_TYPE] = ( + "stepfunctions_execution_timed_out" + ) span.set_attributes(attributes) span.end() diff --git a/sentry_sdk/integrations/boto3/_utils.py b/sentry_sdk/integrations/boto3/_utils.py index 0773fe2d8b..96ffb0ebc6 100644 --- a/sentry_sdk/integrations/boto3/_utils.py +++ b/sentry_sdk/integrations/boto3/_utils.py @@ -1,7 +1,8 @@ from typing import TYPE_CHECKING +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk -from sentry_sdk.consts import SPANDATA from sentry_sdk.traces import NoOpSpan from sentry_sdk.tracing_utils import should_propagate_trace @@ -46,9 +47,9 @@ def _get_aws_arn_attributes( if arn_parts[2] not in services: continue - attributes: "Attributes" = {SPANDATA.CLOUD_RESOURCE_ID: value} + attributes: "Attributes" = {ATTRIBUTE_NAMES.CLOUD_RESOURCE_ID: value} if arn_parts[4]: - attributes[SPANDATA.CLOUD_ACCOUNT_ID] = arn_parts[4] + attributes[ATTRIBUTE_NAMES.CLOUD_ACCOUNT_ID] = arn_parts[4] return attributes return {} diff --git a/sentry_sdk/integrations/chalice.py b/sentry_sdk/integrations/chalice.py index 7d2b072b57..a163ca9066 100644 --- a/sentry_sdk/integrations/chalice.py +++ b/sentry_sdk/integrations/chalice.py @@ -1,6 +1,8 @@ import sys from functools import wraps +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk from sentry_sdk.integrations import DidNotEnable, Integration, _check_minimum_version from sentry_sdk.integrations._wsgi_common import _filter_headers @@ -98,10 +100,12 @@ def wrapped_view_function(**function_args: "Any") -> "Any": additional_attrs: "Dict[str, Any]" = {} if "method" in request_dict: - additional_attrs["http.request.method"] = request_dict["method"] + additional_attrs[ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD] = ( + request_dict["method"] + ) attributes = { - "sentry.origin": ChaliceIntegration.origin, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: ChaliceIntegration.origin, **header_attrs, **additional_attrs, } diff --git a/sentry_sdk/integrations/gcp.py b/sentry_sdk/integrations/gcp.py index 7534e35e64..8b4a63b7bc 100644 --- a/sentry_sdk/integrations/gcp.py +++ b/sentry_sdk/integrations/gcp.py @@ -5,6 +5,8 @@ from os import environ from typing import TYPE_CHECKING +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk from sentry_sdk.consts import OP from sentry_sdk.data_collection import _apply_data_collection_filtering_to_query_string @@ -92,7 +94,9 @@ def sentry_func( additional_attributes = {} if hasattr(gcp_event, "method"): - additional_attributes["http.request.method"] = gcp_event.method + additional_attributes[ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD] = ( + gcp_event.method + ) if hasattr(gcp_event, "query_string"): query_string = gcp_event.query_string.decode("utf-8", errors="replace") @@ -102,7 +106,7 @@ def sentry_func( behaviour=client.options["data_collection"]["url_query_params"], ) if filtered_qs: - additional_attributes["url.query"] = filtered_qs + additional_attributes[ATTRIBUTE_NAMES.URL_QUERY] = filtered_qs sampling_context = { "gcp_env": { @@ -118,15 +122,19 @@ def sentry_func( function_name = environ.get("FUNCTION_NAME", "") if environ.get("GCP_PROJECT"): - additional_attributes["gcp.project.id"] = environ.get("GCP_PROJECT") + additional_attributes[ATTRIBUTE_NAMES.GCP_PROJECT_ID] = environ.get( + "GCP_PROJECT" + ) if environ.get("FUNCTION_IDENTITY"): - additional_attributes["faas.identity"] = environ.get( + additional_attributes[ATTRIBUTE_NAMES.FAAS_IDENTITY] = environ.get( "FUNCTION_IDENTITY" ) if environ.get("ENTRY_POINT"): - additional_attributes["faas.entry_point"] = environ.get("ENTRY_POINT") + additional_attributes[ATTRIBUTE_NAMES.FAAS_ENTRY_POINT] = environ.get( + "ENTRY_POINT" + ) sentry_sdk.continue_trace(headers) Scope.set_custom_sampling_context(sampling_context) @@ -135,11 +143,11 @@ def sentry_func( name=function_name, parent_span=None, attributes={ - "sentry.op": OP.FUNCTION_GCP, - "sentry.origin": GcpIntegration.origin, - "sentry.segment.name.source": SegmentNameSource.COMPONENT, - "cloud.provider": CLOUD_PROVIDER.GCP, - "faas.name": function_name, + ATTRIBUTE_NAMES.SENTRY_OP: OP.FUNCTION_GCP, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: GcpIntegration.origin, + ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE: SegmentNameSource.COMPONENT, + ATTRIBUTE_NAMES.CLOUD_PROVIDER: CLOUD_PROVIDER.GCP, + ATTRIBUTE_NAMES.FAAS_NAME: function_name, **header_attributes, **additional_attributes, }, From 0e66ee66760138229372fefb2cc268f32bb92961 Mon Sep 17 00:00:00 2001 From: srothh Date: Thu, 26 Nov 2026 11:31:13 +0100 Subject: [PATCH 2/2] test(attributes): Enforce sentry-conventions in cloud tests Enforce sentry-conventions attribute names in cloud integration tests (aws_lambda, boto3, chalice, gcp). #8323 PY-3090 Co-Authored-By: Claude Opus 5.5 --- .../aws_lambda/test_aws_lambda.py | 152 +++++++++-------- tests/integrations/boto3/helpers.py | 6 +- tests/integrations/boto3/test_bedrock.py | 155 +++++++++++------- tests/integrations/boto3/test_client.py | 105 ++++++------ tests/integrations/boto3/test_dynamodb.py | 11 +- tests/integrations/boto3/test_kinesis.py | 22 +-- tests/integrations/boto3/test_lambda.py | 21 +-- tests/integrations/boto3/test_s3.py | 108 ++++++------ .../integrations/boto3/test_secretsmanager.py | 17 +- tests/integrations/boto3/test_sns.py | 44 ++--- tests/integrations/boto3/test_sqs.py | 51 +++--- .../integrations/boto3/test_stepfunctions.py | 38 +++-- tests/integrations/chalice/test_chalice.py | 20 ++- tests/integrations/gcp/test_gcp.py | 106 ++++++++---- 14 files changed, 489 insertions(+), 367 deletions(-) diff --git a/tests/integrations/aws_lambda/test_aws_lambda.py b/tests/integrations/aws_lambda/test_aws_lambda.py index 9c254e31c1..12b06b71a4 100644 --- a/tests/integrations/aws_lambda/test_aws_lambda.py +++ b/tests/integrations/aws_lambda/test_aws_lambda.py @@ -8,6 +8,7 @@ import pytest import yaml from aws_cdk import App +from sentry_conventions.attributes import ATTRIBUTE_NAMES from sentry_sdk.consts import OP, SPANDATA from sentry_sdk.integrations.aws_lambda.consts import ( @@ -336,8 +337,8 @@ def test_request_data_with_default_data_collection(lambda_client, test_environme assert len(segment_spans) == 1 attrs = segment_spans[0]["attributes"] - assert _get_span_attr(attrs, SPANDATA.HTTP_REQUEST_METHOD) == "GET" - assert _get_span_attr(attrs, SPANDATA.URL_QUERY) == "bonkers=true" + assert _get_span_attr(attrs, ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD) == "GET" + assert _get_span_attr(attrs, ATTRIBUTE_NAMES.URL_QUERY) == "bonkers=true" USER_INFO_PAYLOAD = b""" @@ -378,7 +379,7 @@ def test_user_info_with_data_collection_user_info_on(lambda_client, test_environ assert len(segment_spans) == 1 attrs = segment_spans[0]["attributes"] - assert _get_span_attr(attrs, SPANDATA.USER_ID) == "42" + assert _get_span_attr(attrs, ATTRIBUTE_NAMES.USER_ID) == "42" def test_request_data_with_data_collection_allowlist(lambda_client, test_environment): @@ -421,7 +422,7 @@ def test_request_data_with_data_collection_allowlist(lambda_client, test_environ assert len(segment_spans) == 1 attrs = segment_spans[0]["attributes"] - assert _get_span_attr(attrs, SPANDATA.HTTP_REQUEST_METHOD) == "GET" + assert _get_span_attr(attrs, ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD) == "GET" # Allowlisted, non-sensitive headers pass through. assert _get_span_attr(attrs, f"{SPANDATA.HTTP_REQUEST_HEADER}.user-agent") == [ "custom" @@ -486,7 +487,7 @@ def test_request_data_with_data_collection_denylist(lambda_client, test_environm assert len(segment_spans) == 1 attrs = segment_spans[0]["attributes"] - assert _get_span_attr(attrs, SPANDATA.HTTP_REQUEST_METHOD) == "GET" + assert _get_span_attr(attrs, ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD) == "GET" # Not denied by any term -> pass through. assert _get_span_attr(attrs, f"{SPANDATA.HTTP_REQUEST_HEADER}.host") == [ "iwsz2c7uwi.execute-api.us-east-1.amazonaws.com" @@ -550,7 +551,7 @@ def test_request_data_with_data_collection_off(lambda_client, test_environment): assert len(segment_spans) == 1 attrs = segment_spans[0]["attributes"] - assert _get_span_attr(attrs, SPANDATA.HTTP_REQUEST_METHOD) == "GET" + assert _get_span_attr(attrs, ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD) == "GET" # With request headers collection turned off, no header attributes are collected. header_attrs = [ k for k in attrs if k.startswith(f"{SPANDATA.HTTP_REQUEST_HEADER}.") @@ -602,7 +603,7 @@ def test_url_query_params_with_data_collection_allowlist( # Not allowlisted -> substituted. # Allowlisted but sensitive -> still filtered. assert ( - _get_span_attr(attrs, SPANDATA.URL_QUERY) + _get_span_attr(attrs, ATTRIBUTE_NAMES.URL_QUERY) == "page=2&tracking=%5BFiltered%5D&token=%5BFiltered%5D" ) @@ -645,7 +646,7 @@ def test_url_query_params_with_data_collection_off(lambda_client, test_environme attrs = segment_spans[0]["attributes"] # With url_query_params collection turned off, no query string is collected. - assert SPANDATA.URL_QUERY not in attrs + assert ATTRIBUTE_NAMES.URL_QUERY not in attrs @pytest.mark.parametrize( @@ -689,7 +690,8 @@ def test_span_origin(lambda_client, test_environment): segment_spans = [s for s in span_items if s.get("is_segment")] assert len(segment_spans) == 1 assert ( - _get_span_attr(segment_spans[0]["attributes"], SPANDATA.SENTRY_ORIGIN) == ORIGIN + _get_span_attr(segment_spans[0]["attributes"], ATTRIBUTE_NAMES.SENTRY_ORIGIN) + == ORIGIN ) @@ -765,35 +767,37 @@ def test_no_error(lambda_client, test_environment): attrs = segment_span["attributes"] - assert _get_span_attr(attrs, SPANDATA.SENTRY_OP) == OP.FUNCTION_AWS - assert _get_span_attr(attrs, SPANDATA.SENTRY_ORIGIN) == ORIGIN - assert _get_span_attr(attrs, SPANDATA.SENTRY_KIND) == "server" + assert _get_span_attr(attrs, ATTRIBUTE_NAMES.SENTRY_OP) == OP.FUNCTION_AWS + assert _get_span_attr(attrs, ATTRIBUTE_NAMES.SENTRY_ORIGIN) == ORIGIN + assert _get_span_attr(attrs, ATTRIBUTE_NAMES.SENTRY_KIND) == "server" assert ( - _get_span_attr(attrs, SPANDATA.SENTRY_SEGMENT_NAME_SOURCE) + _get_span_attr(attrs, ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE) == SegmentNameSource.COMPONENT ) - assert _get_span_attr(attrs, SPANDATA.CLOUD_PROVIDER) == CLOUD_PROVIDER - assert _get_span_attr(attrs, SPANDATA.CLOUD_PLATFORM) == CLOUD_PLATFORM - assert _get_span_attr(attrs, SPANDATA.CLOUD_ACCOUNT_ID) == "012345678912" - assert _get_span_attr(attrs, SPANDATA.CLOUD_REGION) == "us-east-1" + assert _get_span_attr(attrs, ATTRIBUTE_NAMES.CLOUD_PROVIDER) == CLOUD_PROVIDER + assert _get_span_attr(attrs, ATTRIBUTE_NAMES.CLOUD_PLATFORM) == CLOUD_PLATFORM + assert _get_span_attr(attrs, ATTRIBUTE_NAMES.CLOUD_ACCOUNT_ID) == "012345678912" + assert _get_span_attr(attrs, ATTRIBUTE_NAMES.CLOUD_REGION) == "us-east-1" assert ( - _get_span_attr(attrs, SPANDATA.CLOUD_RESOURCE_ID) + _get_span_attr(attrs, ATTRIBUTE_NAMES.CLOUD_RESOURCE_ID) == "arn:aws:lambda:us-east-1:012345678912:function:BasicOkSpanStreaming" ) - assert _get_span_attr(attrs, SPANDATA.FAAS_NAME) == "BasicOkSpanStreaming" - assert _get_span_attr(attrs, SPANDATA.FAAS_VERSION) == LATEST_FUNCTION_VERSION - assert SPANDATA.FAAS_INVOCATION_ID in attrs + assert _get_span_attr(attrs, ATTRIBUTE_NAMES.FAAS_NAME) == "BasicOkSpanStreaming" assert ( - _get_span_attr(attrs, SPANDATA.AWS_LAMBDA_INVOKED_ARN) + _get_span_attr(attrs, ATTRIBUTE_NAMES.FAAS_VERSION) == LATEST_FUNCTION_VERSION + ) + assert ATTRIBUTE_NAMES.FAAS_INVOCATION_ID in attrs + assert ( + _get_span_attr(attrs, ATTRIBUTE_NAMES.AWS_LAMBDA_INVOKED_ARN) == "arn:aws:lambda:us-east-1:012345678912:function:BasicOkSpanStreaming" ) - assert _get_span_attr(attrs, SPANDATA.AWS_LOG_GROUP_NAMES) == [ + assert _get_span_attr(attrs, ATTRIBUTE_NAMES.AWS_LOG_GROUP_NAMES) == [ "aws/lambda/BasicOkSpanStreaming" ] - assert _get_span_attr(attrs, SPANDATA.AWS_LOG_STREAM_NAMES) == [ + assert _get_span_attr(attrs, ATTRIBUTE_NAMES.AWS_LOG_STREAM_NAMES) == [ LATEST_FUNCTION_VERSION ] - assert _get_span_attr(attrs, SPANDATA.FAAS_TRIGGER) == "other" + assert _get_span_attr(attrs, ATTRIBUTE_NAMES.FAAS_TRIGGER) == "other" def test_error(lambda_client, test_environment): @@ -822,32 +826,32 @@ def test_error(lambda_client, test_environment): attrs = segment_span["attributes"] - assert _get_span_attr(attrs, SPANDATA.SENTRY_OP) == OP.FUNCTION_AWS - assert _get_span_attr(attrs, SPANDATA.SENTRY_ORIGIN) == ORIGIN + assert _get_span_attr(attrs, ATTRIBUTE_NAMES.SENTRY_OP) == OP.FUNCTION_AWS + assert _get_span_attr(attrs, ATTRIBUTE_NAMES.SENTRY_ORIGIN) == ORIGIN assert ( - _get_span_attr(attrs, SPANDATA.SENTRY_SEGMENT_NAME_SOURCE) + _get_span_attr(attrs, ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE) == SegmentNameSource.COMPONENT ) - assert _get_span_attr(attrs, SPANDATA.CLOUD_PROVIDER) == CLOUD_PROVIDER - assert _get_span_attr(attrs, SPANDATA.CLOUD_PLATFORM) == CLOUD_PLATFORM - assert _get_span_attr(attrs, SPANDATA.FAAS_NAME) == "RaiseErrorSpanStreaming" + assert _get_span_attr(attrs, ATTRIBUTE_NAMES.CLOUD_PROVIDER) == CLOUD_PROVIDER + assert _get_span_attr(attrs, ATTRIBUTE_NAMES.CLOUD_PLATFORM) == CLOUD_PLATFORM + assert _get_span_attr(attrs, ATTRIBUTE_NAMES.FAAS_NAME) == "RaiseErrorSpanStreaming" assert ( - _get_span_attr(attrs, SPANDATA.CLOUD_RESOURCE_ID) + _get_span_attr(attrs, ATTRIBUTE_NAMES.CLOUD_RESOURCE_ID) == "arn:aws:lambda:us-east-1:012345678912:function:RaiseErrorSpanStreaming:5" ) - assert _get_span_attr(attrs, SPANDATA.FAAS_VERSION) == "5" - assert SPANDATA.FAAS_INVOCATION_ID in attrs + assert _get_span_attr(attrs, ATTRIBUTE_NAMES.FAAS_VERSION) == "5" + assert ATTRIBUTE_NAMES.FAAS_INVOCATION_ID in attrs assert ( - _get_span_attr(attrs, SPANDATA.AWS_LAMBDA_INVOKED_ARN) + _get_span_attr(attrs, ATTRIBUTE_NAMES.AWS_LAMBDA_INVOKED_ARN) == "arn:aws:lambda:us-east-1:012345678912:function:RaiseErrorSpanStreaming:5" ) - assert _get_span_attr(attrs, SPANDATA.AWS_LOG_GROUP_NAMES) == [ + assert _get_span_attr(attrs, ATTRIBUTE_NAMES.AWS_LOG_GROUP_NAMES) == [ "aws/lambda/RaiseErrorSpanStreaming" ] - assert _get_span_attr(attrs, SPANDATA.AWS_LOG_STREAM_NAMES) == [ + assert _get_span_attr(attrs, ATTRIBUTE_NAMES.AWS_LOG_STREAM_NAMES) == [ LATEST_FUNCTION_VERSION ] - assert _get_span_attr(attrs, SPANDATA.FAAS_TRIGGER) == "other" + assert _get_span_attr(attrs, ATTRIBUTE_NAMES.FAAS_TRIGGER) == "other" @pytest.mark.parametrize( @@ -913,7 +917,7 @@ def test_trigger(lambda_client, test_environment, aws_event, trigger): s for s in test_environment["server"].span_items if s["is_segment"] ] attrs = segment_span["attributes"] - assert _get_span_attr(attrs, SPANDATA.FAAS_TRIGGER) == trigger + assert _get_span_attr(attrs, ATTRIBUTE_NAMES.FAAS_TRIGGER) == trigger @pytest.mark.parametrize( @@ -938,19 +942,22 @@ def test_messaging_process_span( (process_span,) = [ s for s in span_items - if _get_span_attr(s["attributes"], SPANDATA.SENTRY_OP) == OP.QUEUE_PROCESS + if _get_span_attr(s["attributes"], ATTRIBUTE_NAMES.SENTRY_OP) + == OP.QUEUE_PROCESS ] assert process_span["parent_span_id"] == segment_span["span_id"] assert process_span["trace_id"] == segment_span["trace_id"] assert process_span["name"] == f"process {destination}" attrs = process_span["attributes"] - assert _get_span_attr(attrs, SPANDATA.SENTRY_KIND) == "consumer" - assert _get_span_attr(attrs, SPANDATA.MESSAGING_SYSTEM) == system - assert _get_span_attr(attrs, SPANDATA.MESSAGING_OPERATION_TYPE) == "process" - assert _get_span_attr(attrs, SPANDATA.MESSAGING_OPERATION_NAME) == "process" - assert _get_span_attr(attrs, SPANDATA.MESSAGING_DESTINATION_NAME) == destination - assert _get_span_attr(attrs, SPANDATA.MESSAGING_BATCH_MESSAGE_COUNT) == len( + assert _get_span_attr(attrs, ATTRIBUTE_NAMES.SENTRY_KIND) == "consumer" + assert _get_span_attr(attrs, ATTRIBUTE_NAMES.MESSAGING_SYSTEM) == system + assert _get_span_attr(attrs, ATTRIBUTE_NAMES.MESSAGING_OPERATION_TYPE) == "process" + assert _get_span_attr(attrs, ATTRIBUTE_NAMES.MESSAGING_OPERATION_NAME) == "process" + assert ( + _get_span_attr(attrs, ATTRIBUTE_NAMES.MESSAGING_DESTINATION_NAME) == destination + ) + assert _get_span_attr(attrs, ATTRIBUTE_NAMES.MESSAGING_BATCH_MESSAGE_COUNT) == len( aws_event["Records"] ) @@ -984,7 +991,8 @@ def test_process_span_error(lambda_client, test_environment): (process_span,) = [ s for s in span_items - if _get_span_attr(s["attributes"], SPANDATA.SENTRY_OP) == OP.QUEUE_PROCESS + if _get_span_attr(s["attributes"], ATTRIBUTE_NAMES.SENTRY_OP) + == OP.QUEUE_PROCESS ] assert process_span["parent_span_id"] == segment_span["span_id"] assert process_span["status"] == "error" @@ -1020,21 +1028,21 @@ def test_trace_continuation(lambda_client, test_environment): assert segment_span["trace_id"] == trace_id assert segment_span["name"] == "RaiseErrorSpanStreaming" attrs = segment_span["attributes"] - assert _get_span_attr(attrs, SPANDATA.SENTRY_OP) == OP.FUNCTION_AWS - assert _get_span_attr(attrs, SPANDATA.SENTRY_ORIGIN) == ORIGIN + assert _get_span_attr(attrs, ATTRIBUTE_NAMES.SENTRY_OP) == OP.FUNCTION_AWS + assert _get_span_attr(attrs, ATTRIBUTE_NAMES.SENTRY_ORIGIN) == ORIGIN assert ( - _get_span_attr(attrs, SPANDATA.SENTRY_SEGMENT_NAME_SOURCE) + _get_span_attr(attrs, ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE) == SegmentNameSource.COMPONENT ) - assert _get_span_attr(attrs, SPANDATA.CLOUD_PROVIDER) == CLOUD_PROVIDER - assert _get_span_attr(attrs, SPANDATA.CLOUD_PLATFORM) == CLOUD_PLATFORM - assert _get_span_attr(attrs, SPANDATA.FAAS_NAME) == "RaiseErrorSpanStreaming" - assert _get_span_attr(attrs, SPANDATA.FAAS_VERSION) == "5" - assert SPANDATA.FAAS_INVOCATION_ID in attrs + assert _get_span_attr(attrs, ATTRIBUTE_NAMES.CLOUD_PROVIDER) == CLOUD_PROVIDER + assert _get_span_attr(attrs, ATTRIBUTE_NAMES.CLOUD_PLATFORM) == CLOUD_PLATFORM + assert _get_span_attr(attrs, ATTRIBUTE_NAMES.FAAS_NAME) == "RaiseErrorSpanStreaming" + assert _get_span_attr(attrs, ATTRIBUTE_NAMES.FAAS_VERSION) == "5" + assert ATTRIBUTE_NAMES.FAAS_INVOCATION_ID in attrs MESSAGE_ORIGIN_LINK = { - SPANDATA.SENTRY_LINK_TYPE: {"value": "message_origin", "type": "string"} + ATTRIBUTE_NAMES.SENTRY_LINK_TYPE: {"value": "message_origin", "type": "string"} } @@ -1165,7 +1173,8 @@ def test_producer_links(lambda_client, test_environment, aws_event, links): (process_span,) = [ s for s in span_items - if _get_span_attr(s["attributes"], SPANDATA.SENTRY_OP) == OP.QUEUE_PROCESS + if _get_span_attr(s["attributes"], ATTRIBUTE_NAMES.SENTRY_OP) + == OP.QUEUE_PROCESS ] # messages are linked from the process span, never the parent of the invocation. assert "parent_span_id" not in segment_span @@ -1209,7 +1218,8 @@ def test_trace_continuation_from_client_context( (process_span,) = [ s for s in span_items - if _get_span_attr(s["attributes"], SPANDATA.SENTRY_OP) == OP.QUEUE_PROCESS + if _get_span_attr(s["attributes"], ATTRIBUTE_NAMES.SENTRY_OP) + == OP.QUEUE_PROCESS ] assert segment_span["trace_id"] == "471a43a4192642f0b136d5159a501701" assert segment_span["parent_span_id"] == "6e8f22c393e68f19" @@ -1245,9 +1255,9 @@ def test_request_attributes(lambda_client, test_environment): segment_span = segment_spans[0] attrs = segment_span["attributes"] - assert _get_span_attr(attrs, SPANDATA.HTTP_REQUEST_METHOD) == "POST" + assert _get_span_attr(attrs, ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD) == "POST" assert ( - _get_span_attr(attrs, SPANDATA.URL_QUERY) + _get_span_attr(attrs, ATTRIBUTE_NAMES.URL_QUERY) == "foo=bar&a-complicated-value=a%3Db%26c%3Dd" ) assert _get_span_attr(attrs, f"{SPANDATA.HTTP_REQUEST_HEADER}.content-type") == [ @@ -1257,25 +1267,25 @@ def test_request_attributes(lambda_client, test_environment): "text/html" ] assert ( - _get_span_attr(attrs, SPANDATA.FAAS_NAME) + _get_span_attr(attrs, ATTRIBUTE_NAMES.FAAS_NAME) == "BasicOkSpanStreamingDataCollection" ) - assert _get_span_attr(attrs, SPANDATA.CLOUD_PROVIDER) == CLOUD_PROVIDER - assert _get_span_attr(attrs, SPANDATA.CLOUD_PLATFORM) == CLOUD_PLATFORM + assert _get_span_attr(attrs, ATTRIBUTE_NAMES.CLOUD_PROVIDER) == CLOUD_PROVIDER + assert _get_span_attr(attrs, ATTRIBUTE_NAMES.CLOUD_PLATFORM) == CLOUD_PLATFORM assert ( - _get_span_attr(attrs, SPANDATA.AWS_LAMBDA_INVOKED_ARN) + _get_span_attr(attrs, ATTRIBUTE_NAMES.AWS_LAMBDA_INVOKED_ARN) == "arn:aws:lambda:us-east-1:012345678912:function:BasicOkSpanStreamingDataCollection:prod" ) assert ( - _get_span_attr(attrs, SPANDATA.CLOUD_RESOURCE_ID) + _get_span_attr(attrs, ATTRIBUTE_NAMES.CLOUD_RESOURCE_ID) == "arn:aws:lambda:us-east-1:012345678912:function:BasicOkSpanStreamingDataCollection:5" ) - assert _get_span_attr(attrs, SPANDATA.FAAS_VERSION) == "5" - assert SPANDATA.FAAS_INVOCATION_ID in attrs - assert _get_span_attr(attrs, SPANDATA.AWS_LOG_GROUP_NAMES) == [ + assert _get_span_attr(attrs, ATTRIBUTE_NAMES.FAAS_VERSION) == "5" + assert ATTRIBUTE_NAMES.FAAS_INVOCATION_ID in attrs + assert _get_span_attr(attrs, ATTRIBUTE_NAMES.AWS_LOG_GROUP_NAMES) == [ "aws/lambda/BasicOkSpanStreamingDataCollection" ] - assert _get_span_attr(attrs, SPANDATA.AWS_LOG_STREAM_NAMES) == [ + assert _get_span_attr(attrs, ATTRIBUTE_NAMES.AWS_LOG_STREAM_NAMES) == [ LATEST_FUNCTION_VERSION ] @@ -1306,7 +1316,7 @@ def test_url_query_params_with_data_collection(lambda_client, test_environment): # "page" passes through; "tracking" is denied by a custom term and "token" # by the built-in sensitive denylist. assert ( - _get_span_attr(attrs, SPANDATA.URL_QUERY) + _get_span_attr(attrs, ATTRIBUTE_NAMES.URL_QUERY) == "page=2&tracking=%5BFiltered%5D&token=%5BFiltered%5D" ) @@ -1322,7 +1332,7 @@ def test_user_info_with_data_collection_user_info_off(lambda_client, test_enviro assert len(segment_spans) == 1 attrs = segment_spans[0]["attributes"] - assert SPANDATA.USER_ID not in attrs + assert ATTRIBUTE_NAMES.USER_ID not in attrs @pytest.mark.parametrize( diff --git a/tests/integrations/boto3/helpers.py b/tests/integrations/boto3/helpers.py index ec9d146c4d..2d73ccefbf 100644 --- a/tests/integrations/boto3/helpers.py +++ b/tests/integrations/boto3/helpers.py @@ -4,9 +4,9 @@ import boto3 import pytest from botocore.config import Config +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk -from sentry_sdk.consts import SPANDATA from sentry_sdk.integrations.boto3 import Boto3Integration from sentry_sdk.integrations.boto3.consts import ORIGIN @@ -108,8 +108,8 @@ def capture_spans_by_op( spans_by_op: Dict[Optional[str], List["SpanJSON"]] = {} for item in items: span = item.payload - if span["attributes"].get(SPANDATA.SENTRY_ORIGIN) == expected_origin: + if span["attributes"].get(ATTRIBUTE_NAMES.SENTRY_ORIGIN) == expected_origin: spans_by_op.setdefault( - span["attributes"].get(SPANDATA.SENTRY_OP), [] + span["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP), [] ).append(span) return spans_by_op diff --git a/tests/integrations/boto3/test_bedrock.py b/tests/integrations/boto3/test_bedrock.py index 8ccb8803a2..6d09b36c53 100644 --- a/tests/integrations/boto3/test_bedrock.py +++ b/tests/integrations/boto3/test_bedrock.py @@ -15,9 +15,10 @@ from botocore.parsers import EventStreamJSONParser from botocore.stub import Stubber from packaging.version import Version +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk -from sentry_sdk.consts import OP, SPANDATA +from sentry_sdk.consts import OP from sentry_sdk.integrations.boto3 import Boto3Integration BOTOCORE_VERSION = Version(botocore.__version__) @@ -454,30 +455,39 @@ def test_converse( sentry_sdk.flush() spans = [item.payload for item in items] (span,) = ( - span for span in spans if span["attributes"].get("sentry.op") == OP.GEN_AI_CHAT + span + for span in spans + if span["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == OP.GEN_AI_CHAT ) - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "aws.bedrock" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is False + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "aws.bedrock" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is False assert ( - span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "anthropic.claude-3-sonnet-20240229-v1:0" ) - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MAX_TOKENS] == 1024 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_TEMPERATURE] == 0.7 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_TOP_P] == 0.9 - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_FINISH_REASONS] == ["end_turn"] + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MAX_TOKENS] == 1024 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_TEMPERATURE] == 0.7 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_TOP_P] == 0.9 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_FINISH_REASONS] == [ + "end_turn" + ] - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 20 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 30 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 30 if BOTOCORE_VERSION < Version("1.37.24"): - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 return - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 110 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS] == 80 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 110 + assert ( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS] == 80 + ) + assert ( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS] + == 20 + ) def test_converse_error( @@ -528,24 +538,26 @@ def raise_client_error(**kwargs): sentry_sdk.flush() spans = [item.payload for item in items] (span,) = ( - span for span in spans if span["attributes"].get("sentry.op") == OP.GEN_AI_CHAT + span + for span in spans + if span["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == OP.GEN_AI_CHAT ) - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "aws.bedrock" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "aws.bedrock" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" assert ( - span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "anthropic.claude-3-sonnet-20240229-v1:0" ) - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MAX_TOKENS] == 1024 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_TEMPERATURE] == 0.7 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_TOP_P] == 0.9 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MAX_TOKENS] == 1024 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_TEMPERATURE] == 0.7 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_TOP_P] == 0.9 - assert span["attributes"][SPANDATA.ERROR_TYPE] == "AccessDeniedException" - assert span["attributes"][SPANDATA.HTTP_STATUS_CODE] == 403 - assert span["attributes"][SPANDATA.AWS_REQUEST_ID] == "test-id" - assert span["attributes"][SPANDATA.HTTP_REQUEST_RESEND_COUNT] == 1 + assert span["attributes"][ATTRIBUTE_NAMES.ERROR_TYPE] == "AccessDeniedException" + assert span["attributes"][ATTRIBUTE_NAMES.HTTP_RESPONSE_STATUS_CODE] == 403 + assert span["attributes"][ATTRIBUTE_NAMES.AWS_REQUEST_ID] == "test-id" + assert span["attributes"][ATTRIBUTE_NAMES.HTTP_REQUEST_RESEND_COUNT] == 1 assert span["status"] == "error" @@ -611,11 +623,13 @@ def test_converse_tool_definitions( sentry_sdk.flush() spans = [item.payload for item in items] (span,) = ( - span for span in spans if span["attributes"].get("sentry.op") == OP.GEN_AI_CHAT + span + for span in spans + if span["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == OP.GEN_AI_CHAT ) assert ( - json.loads(span["attributes"][SPANDATA.GEN_AI_TOOL_DEFINITIONS]) + json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_TOOL_DEFINITIONS]) == expected_tool_definitions ) @@ -656,15 +670,19 @@ def test_converse_input_attributes( sentry_sdk.flush() spans = [item.payload for item in items] (span,) = ( - span for span in spans if span["attributes"].get("sentry.op") == OP.GEN_AI_CHAT + span + for span in spans + if span["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == OP.GEN_AI_CHAT ) - assert json.loads(span["attributes"][SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS]) == [ + assert json.loads( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS] + ) == [ {"type": "text", "content": "You are a helpful assistant."}, {"type": "text", "content": "Be concise and clear."}, ] assert ( - json.loads(span["attributes"][SPANDATA.GEN_AI_INPUT_MESSAGES]) + json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_INPUT_MESSAGES]) == expected_input_messages ) @@ -713,11 +731,13 @@ def test_converse_output_attributes( sentry_sdk.flush() spans = [item.payload for item in items] (span,) = ( - span for span in spans if span["attributes"].get("sentry.op") == OP.GEN_AI_CHAT + span + for span in spans + if span["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == OP.GEN_AI_CHAT ) assert ( - json.loads(span["attributes"][SPANDATA.GEN_AI_OUTPUT_MESSAGES]) + json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES]) == expected_output_messages ) @@ -759,31 +779,40 @@ def test_converse_stream( sentry_sdk.flush() spans = [item.payload for item in items] (span,) = ( - span for span in spans if span["attributes"].get("sentry.op") == OP.GEN_AI_CHAT + span + for span in spans + if span["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == OP.GEN_AI_CHAT ) - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "aws.bedrock" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is True + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "aws.bedrock" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is True assert ( - span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "anthropic.claude-3-sonnet-20240229-v1:0" ) - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MAX_TOKENS] == 1024 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_TEMPERATURE] == 0.7 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_TOP_P] == 0.9 - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_FINISH_REASONS] == ["end_turn"] - assert SPANDATA.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK in span["attributes"] + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MAX_TOKENS] == 1024 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_TEMPERATURE] == 0.7 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_TOP_P] == 0.9 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_FINISH_REASONS] == [ + "end_turn" + ] + assert ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK in span["attributes"] - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 20 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 30 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 30 if BOTOCORE_VERSION < Version("1.37.24"): - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 return - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 110 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS] == 80 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 110 + assert ( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS] == 80 + ) + assert ( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS] + == 20 + ) def test_converse_stream_error_during_iteration( @@ -820,7 +849,9 @@ def test_converse_stream_error_during_iteration( sentry_sdk.flush() spans = [item.payload for item in items] (span,) = ( - span for span in spans if span["attributes"].get("sentry.op") == OP.GEN_AI_CHAT + span + for span in spans + if span["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == OP.GEN_AI_CHAT ) assert span["status"] == "error" @@ -892,11 +923,13 @@ def test_converse_stream_tool_definitions( sentry_sdk.flush() spans = [item.payload for item in items] (span,) = ( - span for span in spans if span["attributes"].get("sentry.op") == OP.GEN_AI_CHAT + span + for span in spans + if span["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == OP.GEN_AI_CHAT ) assert ( - json.loads(span["attributes"][SPANDATA.GEN_AI_TOOL_DEFINITIONS]) + json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_TOOL_DEFINITIONS]) == expected_tool_definitions ) @@ -941,15 +974,19 @@ def test_converse_stream_input_attributes( sentry_sdk.flush() spans = [item.payload for item in items] (span,) = ( - span for span in spans if span["attributes"].get("sentry.op") == OP.GEN_AI_CHAT + span + for span in spans + if span["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == OP.GEN_AI_CHAT ) - assert json.loads(span["attributes"][SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS]) == [ + assert json.loads( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS] + ) == [ {"type": "text", "content": "You are a helpful assistant."}, {"type": "text", "content": "Be concise and clear."}, ] assert ( - json.loads(span["attributes"][SPANDATA.GEN_AI_INPUT_MESSAGES]) + json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_INPUT_MESSAGES]) == expected_input_messages ) @@ -1153,9 +1190,11 @@ def test_converse_stream_output_attributes( sentry_sdk.flush() spans = [item.payload for item in items] (span,) = ( - span for span in spans if span["attributes"].get("sentry.op") == OP.GEN_AI_CHAT + span + for span in spans + if span["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == OP.GEN_AI_CHAT ) - assert json.loads(span["attributes"][SPANDATA.GEN_AI_OUTPUT_MESSAGES]) == [ + assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES]) == [ {"role": "assistant", "parts": expected_parts} ] diff --git a/tests/integrations/boto3/test_client.py b/tests/integrations/boto3/test_client.py index 576e88c36c..4f401c7fa5 100644 --- a/tests/integrations/boto3/test_client.py +++ b/tests/integrations/boto3/test_client.py @@ -9,6 +9,7 @@ from botocore.exceptions import ClientError, EndpointConnectionError from botocore.response import StreamingBody from botocore.stub import Stubber +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk from sentry_sdk import capture_message @@ -143,14 +144,15 @@ def record_client_span(request, **kwargs): for span in spans if span["name"] == "S3.GetObject" and ( - span.get("attributes", {}).get(SPANDATA.SENTRY_ORIGIN) == ORIGIN - and span.get("attributes", {}).get(SPANDATA.SENTRY_OP) == OP.OBJECT_GET + span.get("attributes", {}).get(ATTRIBUTE_NAMES.SENTRY_ORIGIN) == ORIGIN + and span.get("attributes", {}).get(ATTRIBUTE_NAMES.SENTRY_OP) + == OP.OBJECT_GET ) ] http_spans = [ span for span in spans - if span.get("attributes", {}).get(SPANDATA.SENTRY_ORIGIN) + if span.get("attributes", {}).get(ATTRIBUTE_NAMES.SENTRY_ORIGIN) == "auto.http.stdlib.httplib" ] stream_spans = [ @@ -158,7 +160,8 @@ def record_client_span(request, **kwargs): for span in spans if span["name"] == "S3.GetObject" and ( - span.get("attributes", {}).get(SPANDATA.SENTRY_OP) == OP.HTTP_CLIENT_STREAM + span.get("attributes", {}).get(ATTRIBUTE_NAMES.SENTRY_OP) + == OP.HTTP_CLIENT_STREAM ) ] assert len(client_spans) == 1 @@ -225,9 +228,9 @@ def test_omit_url_data_if_parsing_fails(capture_items, client_factory): parse_url.assert_called() (span,) = spans_by_op[OP.OBJECT_HEAD] attributes = span.get("attributes", {}) - assert SPANDATA.URL_FULL not in attributes - assert SPANDATA.URL_FRAGMENT not in attributes - assert SPANDATA.URL_QUERY not in attributes + assert ATTRIBUTE_NAMES.URL_FULL not in attributes + assert ATTRIBUTE_NAMES.URL_FRAGMENT not in attributes + assert ATTRIBUTE_NAMES.URL_QUERY not in attributes BUCKET_URL = "https://bucket.s3.eu-north-1.amazonaws.com/" @@ -298,16 +301,17 @@ def test_url_query_data_collection( (span,) = [ item.payload for item in items - if item.payload.get("attributes", {}).get(SPANDATA.SENTRY_OP) == OP.OBJECT_LIST + if item.payload.get("attributes", {}).get(ATTRIBUTE_NAMES.SENTRY_OP) + == OP.OBJECT_LIST ] attributes = span.get("attributes", {}) if expected_query == "": - assert SPANDATA.URL_QUERY not in attributes - assert attributes[SPANDATA.URL_FULL] == BUCKET_URL + assert ATTRIBUTE_NAMES.URL_QUERY not in attributes + assert attributes[ATTRIBUTE_NAMES.URL_FULL] == BUCKET_URL else: - assert attributes[SPANDATA.URL_QUERY] == expected_query - assert attributes[SPANDATA.URL_FULL] == BUCKET_URL + "?" + expected_query + assert attributes[ATTRIBUTE_NAMES.URL_QUERY] == expected_query + assert attributes[ATTRIBUTE_NAMES.URL_FULL] == BUCKET_URL + "?" + expected_query @pytest.mark.parametrize( @@ -379,7 +383,7 @@ def _assert_one_failed_span(spans): assert len(spans) == 1 assert spans[0]["status"] == "error" attributes = spans[0].get("attributes", {}) - assert attributes[SPANDATA.ERROR_TYPE] + assert attributes[ATTRIBUTE_NAMES.ERROR_TYPE] assert spans[0].get("end_timestamp") is not None @@ -492,20 +496,20 @@ def test_client_call_has_common_attributes( assert span["name"] == span_name assert span.get("end_timestamp") is not None - assert attributes[SPANDATA.SENTRY_OP] == expected_op - assert attributes[SPANDATA.SENTRY_ORIGIN] == ORIGIN - assert attributes[SPANDATA.SENTRY_KIND] == "client" - assert attributes[SPANDATA.CLOUD_PROVIDER] == CLOUD_PROVIDER - assert attributes[SPANDATA.RPC_SYSTEM_NAME] == AWS_RPC_SYSTEM_NAME - assert attributes[SPANDATA.RPC_SERVICE] == rpc_service - assert attributes[SPANDATA.RPC_METHOD] == rpc_method - assert attributes[SPANDATA.CLOUD_REGION] == "eu-north-1" - assert attributes[SPANDATA.SERVER_ADDRESS] == server_address - assert attributes[SPANDATA.SERVER_PORT] == server_port - assert attributes[SPANDATA.HTTP_STATUS_CODE] == 200 - assert attributes[SPANDATA.AWS_REQUEST_ID] == "request-id" - assert SPANDATA.HTTP_REQUEST_RESEND_COUNT not in attributes - assert SPANDATA.ERROR_TYPE not in attributes + assert attributes[ATTRIBUTE_NAMES.SENTRY_OP] == expected_op + assert attributes[ATTRIBUTE_NAMES.SENTRY_ORIGIN] == ORIGIN + assert attributes[ATTRIBUTE_NAMES.SENTRY_KIND] == "client" + assert attributes[ATTRIBUTE_NAMES.CLOUD_PROVIDER] == CLOUD_PROVIDER + assert attributes[ATTRIBUTE_NAMES.RPC_SYSTEM_NAME] == AWS_RPC_SYSTEM_NAME + assert attributes[ATTRIBUTE_NAMES.RPC_SERVICE] == rpc_service + assert attributes[ATTRIBUTE_NAMES.RPC_METHOD] == rpc_method + assert attributes[ATTRIBUTE_NAMES.CLOUD_REGION] == "eu-north-1" + assert attributes[ATTRIBUTE_NAMES.SERVER_ADDRESS] == server_address + assert attributes[ATTRIBUTE_NAMES.SERVER_PORT] == server_port + assert attributes[ATTRIBUTE_NAMES.HTTP_RESPONSE_STATUS_CODE] == 200 + assert attributes[ATTRIBUTE_NAMES.AWS_REQUEST_ID] == "request-id" + assert ATTRIBUTE_NAMES.HTTP_REQUEST_RESEND_COUNT not in attributes + assert ATTRIBUTE_NAMES.ERROR_TYPE not in attributes def test_client_call_attributes_are_available_at_span_creation( @@ -519,12 +523,12 @@ def test_client_call_attributes_are_available_at_span_creation( ignore_spans=[ { "attributes": { - SPANDATA.CLOUD_PROVIDER: CLOUD_PROVIDER, - SPANDATA.RPC_METHOD: "HeadObject", - SPANDATA.RPC_SERVICE: "S3", - SPANDATA.RPC_SYSTEM_NAME: AWS_RPC_SYSTEM_NAME, - SPANDATA.SERVER_ADDRESS: "s3.eu-north-1.amazonaws.com", - SPANDATA.SERVER_PORT: 443, + ATTRIBUTE_NAMES.CLOUD_PROVIDER: CLOUD_PROVIDER, + ATTRIBUTE_NAMES.RPC_METHOD: "HeadObject", + ATTRIBUTE_NAMES.RPC_SERVICE: "S3", + ATTRIBUTE_NAMES.RPC_SYSTEM_NAME: AWS_RPC_SYSTEM_NAME, + ATTRIBUTE_NAMES.SERVER_ADDRESS: "s3.eu-north-1.amazonaws.com", + ATTRIBUTE_NAMES.SERVER_PORT: 443, } } ], @@ -537,14 +541,15 @@ def test_client_call_attributes_are_available_at_span_creation( response = client.head_object(Bucket="bucket", Key="foo") assert response["ResponseMetadata"]["HTTPStatusCode"] == 200 assert sentry_sdk.get_current_span() is parent - assert SPANDATA.RPC_METHOD not in parent.get_attributes() - assert SPANDATA.HTTP_REQUEST_METHOD not in parent.get_attributes() + assert ATTRIBUTE_NAMES.RPC_METHOD not in parent.get_attributes() + assert ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD not in parent.get_attributes() sentry_sdk.flush() client_spans = [ item.payload for item in items - if item.payload.get("attributes", {}).get(SPANDATA.SENTRY_ORIGIN) == ORIGIN + if item.payload.get("attributes", {}).get(ATTRIBUTE_NAMES.SENTRY_ORIGIN) + == ORIGIN ] assert client_spans == [] @@ -566,10 +571,10 @@ def test_client_call_has_response_header_attributes( spans = spans_by_op[OP.OBJECT_HEAD] assert len(spans) == 1 attributes = spans[0].get("attributes", {}) - assert attributes[SPANDATA.HTTP_STATUS_CODE] == 200 - assert attributes[SPANDATA.AWS_REQUEST_ID] == "request-id" - assert attributes[SPANDATA.AWS_EXTENDED_REQUEST_ID] == "extended-request-id" - assert SPANDATA.HTTP_REQUEST_RESEND_COUNT not in attributes + assert attributes[ATTRIBUTE_NAMES.HTTP_RESPONSE_STATUS_CODE] == 200 + assert attributes[ATTRIBUTE_NAMES.AWS_REQUEST_ID] == "request-id" + assert attributes[ATTRIBUTE_NAMES.AWS_EXTENDED_REQUEST_ID] == "extended-request-id" + assert ATTRIBUTE_NAMES.HTTP_REQUEST_RESEND_COUNT not in attributes def test_retry_attempts_share_one_client_span( @@ -589,7 +594,7 @@ def test_retry_attempts_share_one_client_span( assert len(set(request_span_ids)) == 1 assert len(client_spans) == 1 attributes = client_spans[0].get("attributes", {}) - assert attributes[SPANDATA.HTTP_REQUEST_RESEND_COUNT] == attempt_count - 1 + assert attributes[ATTRIBUTE_NAMES.HTTP_REQUEST_RESEND_COUNT] == attempt_count - 1 def test_retries_exhausted_has_one_failed_client_span( @@ -611,8 +616,8 @@ def attempt_failed_head_object_call(): assert len(set(request_span_ids)) == 1 _assert_one_failed_span(client_spans) attributes = client_spans[0].get("attributes", {}) - assert attributes[SPANDATA.HTTP_STATUS_CODE] == 500 - assert attributes[SPANDATA.HTTP_REQUEST_RESEND_COUNT] == 1 + assert attributes[ATTRIBUTE_NAMES.HTTP_RESPONSE_STATUS_CODE] == 500 + assert attributes[ATTRIBUTE_NAMES.HTTP_REQUEST_RESEND_COUNT] == 1 @pytest.mark.parametrize( @@ -657,7 +662,7 @@ def invoke_failing_client_method(): else "ValueError" ) attributes = client_spans[0].get("attributes", {}) - assert attributes[SPANDATA.ERROR_TYPE] == expected_error_type + assert attributes[ATTRIBUTE_NAMES.ERROR_TYPE] == expected_error_type @pytest.mark.tests_internal_exceptions @@ -771,11 +776,11 @@ def invoke_client_method_and_read_body(): assert len(stream_spans) == 1 client_attributes = client_spans[0].get("attributes", {}) stream_attributes = stream_spans[0].get("attributes", {}) - assert client_attributes[SPANDATA.AWS_REQUEST_ID] == "request-id" - assert client_attributes[SPANDATA.HTTP_STATUS_CODE] == 200 - assert SPANDATA.HTTP_REQUEST_RESEND_COUNT not in client_attributes - assert SPANDATA.AWS_REQUEST_ID not in stream_attributes - assert SPANDATA.HTTP_STATUS_CODE not in stream_attributes + assert client_attributes[ATTRIBUTE_NAMES.AWS_REQUEST_ID] == "request-id" + assert client_attributes[ATTRIBUTE_NAMES.HTTP_RESPONSE_STATUS_CODE] == 200 + assert ATTRIBUTE_NAMES.HTTP_REQUEST_RESEND_COUNT not in client_attributes + assert ATTRIBUTE_NAMES.AWS_REQUEST_ID not in stream_attributes + assert ATTRIBUTE_NAMES.HTTP_RESPONSE_STATUS_CODE not in stream_attributes def test_streaming_body_read_failure_finishes_stream_span( @@ -820,4 +825,4 @@ def invoke_client_method_and_read_body(): _assert_one_failed_span(client_spans) _assert_one_failed_span(stream_spans) attributes = stream_spans[0].get("attributes", {}) - assert attributes[SPANDATA.ERROR_TYPE] == "OSError" + assert attributes[ATTRIBUTE_NAMES.ERROR_TYPE] == "OSError" diff --git a/tests/integrations/boto3/test_dynamodb.py b/tests/integrations/boto3/test_dynamodb.py index 5399e035ae..457bf96856 100644 --- a/tests/integrations/boto3/test_dynamodb.py +++ b/tests/integrations/boto3/test_dynamodb.py @@ -1,7 +1,8 @@ import pytest from botocore.stub import Stubber +from sentry_conventions.attributes import ATTRIBUTE_NAMES -from sentry_sdk.consts import OP, SPANDATA +from sentry_sdk.consts import OP from tests.integrations.boto3.helpers import capture_spans_by_op from tests.integrations.boto3.helpers import client_factory as client_factory @@ -39,8 +40,8 @@ def test_request_attributes( (span,) = spans[OP.RPC] attributes = span["attributes"] if expected_resource_id is None: - assert SPANDATA.CLOUD_ACCOUNT_ID not in attributes - assert SPANDATA.CLOUD_RESOURCE_ID not in attributes + assert ATTRIBUTE_NAMES.CLOUD_ACCOUNT_ID not in attributes + assert ATTRIBUTE_NAMES.CLOUD_RESOURCE_ID not in attributes else: - assert attributes[SPANDATA.CLOUD_ACCOUNT_ID] == "123456789012" - assert attributes[SPANDATA.CLOUD_RESOURCE_ID] == expected_resource_id + assert attributes[ATTRIBUTE_NAMES.CLOUD_ACCOUNT_ID] == "123456789012" + assert attributes[ATTRIBUTE_NAMES.CLOUD_RESOURCE_ID] == expected_resource_id diff --git a/tests/integrations/boto3/test_kinesis.py b/tests/integrations/boto3/test_kinesis.py index 109346bd93..05d45d695b 100644 --- a/tests/integrations/boto3/test_kinesis.py +++ b/tests/integrations/boto3/test_kinesis.py @@ -1,7 +1,8 @@ import pytest from botocore.stub import Stubber +from sentry_conventions.attributes import ATTRIBUTE_NAMES -from sentry_sdk.consts import OP, SPANDATA +from sentry_sdk.consts import OP from tests.integrations.boto3.helpers import ( capture_spans_by_op, require_botocore_model_fields, @@ -33,7 +34,7 @@ def _stubbed_span(client_factory, capture_items, method, params, response): "put_record", {"StreamName": "orders", "Data": b"{}", "PartitionKey": "customer-1"}, {"ShardId": "shardId-000000000000", "SequenceNumber": "1"}, - {SPANDATA.AWS_KINESIS_STREAM_NAME: "orders"}, + {ATTRIBUTE_NAMES.AWS_KINESIS_STREAM_NAME: "orders"}, id="stream-name", ), pytest.param( @@ -41,8 +42,8 @@ def _stubbed_span(client_factory, capture_items, method, params, response): {"StreamARN": STREAM_ARN}, {"Consumers": []}, { - SPANDATA.AWS_KINESIS_STREAM_NAME: "orders", - SPANDATA.CLOUD_RESOURCE_ID: STREAM_ARN, + ATTRIBUTE_NAMES.AWS_KINESIS_STREAM_NAME: "orders", + ATTRIBUTE_NAMES.CLOUD_RESOURCE_ID: STREAM_ARN, }, id="stream-arn", ), @@ -51,8 +52,8 @@ def _stubbed_span(client_factory, capture_items, method, params, response): {"ConsumerARN": CONSUMER_ARN}, {}, { - SPANDATA.AWS_KINESIS_STREAM_NAME: "orders", - SPANDATA.CLOUD_RESOURCE_ID: CONSUMER_ARN, + ATTRIBUTE_NAMES.AWS_KINESIS_STREAM_NAME: "orders", + ATTRIBUTE_NAMES.CLOUD_RESOURCE_ID: CONSUMER_ARN, }, id="consumer-arn", ), @@ -61,8 +62,8 @@ def _stubbed_span(client_factory, capture_items, method, params, response): {"ResourceARN": CONSUMER_ARN}, {"Tags": []}, { - SPANDATA.AWS_KINESIS_STREAM_NAME: "orders", - SPANDATA.CLOUD_RESOURCE_ID: CONSUMER_ARN, + ATTRIBUTE_NAMES.AWS_KINESIS_STREAM_NAME: "orders", + ATTRIBUTE_NAMES.CLOUD_RESOURCE_ID: CONSUMER_ARN, }, id="resource-arn", ), @@ -76,7 +77,8 @@ def test_stream_attributes( assert { key: value for key, value in span["attributes"].items() - if key in (SPANDATA.AWS_KINESIS_STREAM_NAME, SPANDATA.CLOUD_RESOURCE_ID) + if key + in (ATTRIBUTE_NAMES.AWS_KINESIS_STREAM_NAME, ATTRIBUTE_NAMES.CLOUD_RESOURCE_ID) } == expected @@ -117,4 +119,4 @@ def test_put_records_outcome( span = _stubbed_span(client_factory, capture_items, "put_records", params, response) assert span["status"] == ("ok" if expected_error_type is None else "error") - assert span["attributes"].get(SPANDATA.ERROR_TYPE) == expected_error_type + assert span["attributes"].get(ATTRIBUTE_NAMES.ERROR_TYPE) == expected_error_type diff --git a/tests/integrations/boto3/test_lambda.py b/tests/integrations/boto3/test_lambda.py index 94d8697723..3df558cbc9 100644 --- a/tests/integrations/boto3/test_lambda.py +++ b/tests/integrations/boto3/test_lambda.py @@ -7,9 +7,10 @@ import pytest from botocore.response import StreamingBody from botocore.stub import Stubber +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk -from sentry_sdk.consts import OP, SPANDATA +from sentry_sdk.consts import OP from sentry_sdk.integrations.boto3 import Boto3Integration from tests.integrations.boto3.helpers import ( capture_spans_by_op, @@ -62,10 +63,10 @@ def test_invoke_attributes( ) attributes = span["attributes"] - assert attributes[SPANDATA.FAAS_INVOKED_NAME] == "billing" - assert attributes[SPANDATA.FAAS_INVOKED_REGION] == region - assert attributes.get(SPANDATA.CLOUD_RESOURCE_ID) == resource_id - assert SPANDATA.AWS_LAMBDA_TARGET_FUNCTION_NAME not in attributes + assert attributes[ATTRIBUTE_NAMES.FAAS_INVOKED_NAME] == "billing" + assert attributes[ATTRIBUTE_NAMES.FAAS_INVOKED_REGION] == region + assert attributes.get(ATTRIBUTE_NAMES.CLOUD_RESOURCE_ID) == resource_id + assert ATTRIBUTE_NAMES.AWS_LAMBDA_TARGET_FUNCTION_NAME not in attributes def test_function_attributes(client_factory, capture_items): @@ -75,9 +76,9 @@ def test_function_attributes(client_factory, capture_items): ) attributes = span["attributes"] - assert attributes[SPANDATA.AWS_LAMBDA_TARGET_FUNCTION_NAME] == "billing" - assert attributes[SPANDATA.CLOUD_RESOURCE_ID] == arn - assert SPANDATA.FAAS_INVOKED_NAME not in attributes + assert attributes[ATTRIBUTE_NAMES.AWS_LAMBDA_TARGET_FUNCTION_NAME] == "billing" + assert attributes[ATTRIBUTE_NAMES.CLOUD_RESOURCE_ID] == arn + assert ATTRIBUTE_NAMES.FAAS_INVOKED_NAME not in attributes @pytest.mark.parametrize( @@ -106,7 +107,7 @@ def test_event_source_mapping_attributes( span, _ = _stubbed_span(client_factory, capture_items, method, params, response) assert ( - span["attributes"][SPANDATA.AWS_LAMBDA_RESOURCE_MAPPING_ID] + span["attributes"][ATTRIBUTE_NAMES.AWS_LAMBDA_RESOURCE_MAPPING_ID] == "587b4f6c-1d0e-4d48-8a3f-2c6f9a1e0b7d" ) @@ -125,7 +126,7 @@ def test_invoke_function_error(client_factory, capture_items): ) assert span["status"] == "error" - assert span["attributes"][SPANDATA.ERROR_TYPE] == "lambda_function_error" + assert span["attributes"][ATTRIBUTE_NAMES.ERROR_TYPE] == "lambda_function_error" def test_invoke_propagates_trace(client_factory, capture_items): diff --git a/tests/integrations/boto3/test_s3.py b/tests/integrations/boto3/test_s3.py index de70f3a4af..362dae5277 100644 --- a/tests/integrations/boto3/test_s3.py +++ b/tests/integrations/boto3/test_s3.py @@ -4,8 +4,8 @@ import pytest from botocore.stub import Stubber +from sentry_conventions.attributes import ATTRIBUTE_NAMES -from sentry_sdk.consts import SPANDATA from sentry_sdk.integrations.boto3.consts import AWS_RPC_SYSTEM_NAME, ORIGIN from tests.integrations.boto3.helpers import ( capture_spans_by_op, @@ -31,15 +31,15 @@ def _stubbed_span(client, capture_items, method, params, response): assert span["name"] == "S3.%s" % operation assert span.get("end_timestamp") is not None attributes = span.get("attributes", {}) - assert attributes[SPANDATA.SENTRY_ORIGIN] == ORIGIN - assert attributes[SPANDATA.SENTRY_KIND] == "client" - assert attributes[SPANDATA.CLOUD_PROVIDER] == "aws" - assert attributes[SPANDATA.RPC_SYSTEM_NAME] == AWS_RPC_SYSTEM_NAME - assert attributes[SPANDATA.RPC_SERVICE] == "S3" - assert attributes[SPANDATA.RPC_METHOD] == operation - assert attributes[SPANDATA.CLOUD_REGION] == "eu-north-1" - assert attributes[SPANDATA.SERVER_ADDRESS] == "s3.eu-north-1.amazonaws.com" - assert attributes[SPANDATA.SERVER_PORT] == 443 + assert attributes[ATTRIBUTE_NAMES.SENTRY_ORIGIN] == ORIGIN + assert attributes[ATTRIBUTE_NAMES.SENTRY_KIND] == "client" + assert attributes[ATTRIBUTE_NAMES.CLOUD_PROVIDER] == "aws" + assert attributes[ATTRIBUTE_NAMES.RPC_SYSTEM_NAME] == AWS_RPC_SYSTEM_NAME + assert attributes[ATTRIBUTE_NAMES.RPC_SERVICE] == "S3" + assert attributes[ATTRIBUTE_NAMES.RPC_METHOD] == operation + assert attributes[ATTRIBUTE_NAMES.CLOUD_REGION] == "eu-north-1" + assert attributes[ATTRIBUTE_NAMES.SERVER_ADDRESS] == "s3.eu-north-1.amazonaws.com" + assert attributes[ATTRIBUTE_NAMES.SERVER_PORT] == 443 return span @@ -57,16 +57,16 @@ def test_request_attributes(s3_client, capture_items): span = _stubbed_span(s3_client, capture_items, "upload_part", params, {}) attributes = span.get("attributes", {}) - assert attributes[SPANDATA.CLOUD_ACCOUNT_ID] == "123456789012" - assert attributes[SPANDATA.CLOUD_RESOURCE_ID] == bucket_arn - assert attributes[SPANDATA.AWS_S3_BUCKET] == bucket_arn - assert attributes[SPANDATA.AWS_S3_KEY] == "file.txt" - assert attributes[SPANDATA.AWS_S3_UPLOAD_ID] == "upload-id" - assert attributes[SPANDATA.AWS_S3_PART_NUMBER] == 1 - assert SPANDATA.AWS_S3_COPY_SOURCE not in attributes - assert SPANDATA.AWS_S3_DELETE not in attributes - assert SPANDATA.FILE_SIZE not in attributes - assert SPANDATA.HTTP_BODY_SIZE not in attributes + assert attributes[ATTRIBUTE_NAMES.CLOUD_ACCOUNT_ID] == "123456789012" + assert attributes[ATTRIBUTE_NAMES.CLOUD_RESOURCE_ID] == bucket_arn + assert attributes[ATTRIBUTE_NAMES.AWS_S3_BUCKET] == bucket_arn + assert attributes[ATTRIBUTE_NAMES.AWS_S3_KEY] == "file.txt" + assert attributes[ATTRIBUTE_NAMES.AWS_S3_UPLOAD_ID] == "upload-id" + assert attributes[ATTRIBUTE_NAMES.AWS_S3_PART_NUMBER] == 1 + assert ATTRIBUTE_NAMES.AWS_S3_COPY_SOURCE not in attributes + assert ATTRIBUTE_NAMES.AWS_S3_DELETE not in attributes + assert ATTRIBUTE_NAMES.FILE_SIZE not in attributes + assert ATTRIBUTE_NAMES.HTTP_RESPONSE_BODY_SIZE not in attributes assert params == original assert "private-content" not in json.dumps(span) @@ -98,14 +98,17 @@ def test_copy_source(s3_client, capture_items, method, copy_source, expected): source = deepcopy(copy_source) params = {"Bucket": "bucket", "Key": "file.txt", "CopySource": source} expected_attributes = { - SPANDATA.AWS_S3_BUCKET: "bucket", - SPANDATA.AWS_S3_KEY: "file.txt", - SPANDATA.AWS_S3_COPY_SOURCE: expected, + ATTRIBUTE_NAMES.AWS_S3_BUCKET: "bucket", + ATTRIBUTE_NAMES.AWS_S3_KEY: "file.txt", + ATTRIBUTE_NAMES.AWS_S3_COPY_SOURCE: expected, } if method == "upload_part_copy": params.update(UploadId="upload-id", PartNumber=1) expected_attributes.update( - {SPANDATA.AWS_S3_UPLOAD_ID: "upload-id", SPANDATA.AWS_S3_PART_NUMBER: 1} + { + ATTRIBUTE_NAMES.AWS_S3_UPLOAD_ID: "upload-id", + ATTRIBUTE_NAMES.AWS_S3_PART_NUMBER: 1, + } ) span = _stubbed_span(s3_client, capture_items, method, params, {}) @@ -114,11 +117,11 @@ def test_copy_source(s3_client, capture_items, method, copy_source, expected): for key, value in expected_attributes.items(): assert attributes[key] == value for key in ( - SPANDATA.AWS_S3_UPLOAD_ID, - SPANDATA.AWS_S3_PART_NUMBER, - SPANDATA.AWS_S3_DELETE, - SPANDATA.FILE_SIZE, - SPANDATA.HTTP_BODY_SIZE, + ATTRIBUTE_NAMES.AWS_S3_UPLOAD_ID, + ATTRIBUTE_NAMES.AWS_S3_PART_NUMBER, + ATTRIBUTE_NAMES.AWS_S3_DELETE, + ATTRIBUTE_NAMES.FILE_SIZE, + ATTRIBUTE_NAMES.HTTP_RESPONSE_BODY_SIZE, ): if key not in expected_attributes: assert key not in attributes @@ -172,14 +175,14 @@ def test_delete_serialization( span = _stubbed_span(s3_client, capture_items, "delete_objects", params, {}) attributes = span.get("attributes", {}) - assert attributes[SPANDATA.AWS_S3_BUCKET] == "bucket" - assert attributes[SPANDATA.AWS_S3_DELETE] == expected_serialized_delete - assert SPANDATA.AWS_S3_KEY not in attributes - assert SPANDATA.AWS_S3_UPLOAD_ID not in attributes - assert SPANDATA.AWS_S3_COPY_SOURCE not in attributes - assert SPANDATA.AWS_S3_PART_NUMBER not in attributes - assert SPANDATA.FILE_SIZE not in attributes - assert SPANDATA.HTTP_BODY_SIZE not in attributes + assert attributes[ATTRIBUTE_NAMES.AWS_S3_BUCKET] == "bucket" + assert attributes[ATTRIBUTE_NAMES.AWS_S3_DELETE] == expected_serialized_delete + assert ATTRIBUTE_NAMES.AWS_S3_KEY not in attributes + assert ATTRIBUTE_NAMES.AWS_S3_UPLOAD_ID not in attributes + assert ATTRIBUTE_NAMES.AWS_S3_COPY_SOURCE not in attributes + assert ATTRIBUTE_NAMES.AWS_S3_PART_NUMBER not in attributes + assert ATTRIBUTE_NAMES.FILE_SIZE not in attributes + assert ATTRIBUTE_NAMES.HTTP_RESPONSE_BODY_SIZE not in attributes assert params == original assert params["Delete"] is caller_delete assert caller_delete["Objects"] is caller_objects @@ -192,14 +195,14 @@ def test_delete_serialization( "head_object", {}, {"ContentLength": 1024}, - {SPANDATA.FILE_SIZE: 1024}, + {ATTRIBUTE_NAMES.FILE_SIZE: 1024}, id="head-whole-object", ), pytest.param( "head_object", {}, {"ContentLength": 0}, - {SPANDATA.FILE_SIZE: 0}, + {ATTRIBUTE_NAMES.FILE_SIZE: 0}, id="head-empty-object", ), pytest.param( @@ -216,21 +219,21 @@ def test_delete_serialization( "get_object", {"Range": "bytes=0-3"}, {"ContentLength": 4}, - {SPANDATA.HTTP_BODY_SIZE: 4}, + {ATTRIBUTE_NAMES.HTTP_RESPONSE_BODY_SIZE: 4}, id="get-range", ), pytest.param( "get_object_attributes", {"ObjectAttributes": ["ObjectSize"]}, {"ObjectSize": 1024}, - {SPANDATA.FILE_SIZE: 1024}, + {ATTRIBUTE_NAMES.FILE_SIZE: 1024}, id="object-attributes-size", ), pytest.param( "put_object", {"Body": b"data", "WriteOffsetBytes": 1020}, {"Size": 1024}, - {SPANDATA.FILE_SIZE: 1024}, + {ATTRIBUTE_NAMES.FILE_SIZE: 1024}, id="put-append-size", ), pytest.param("put_object", {"Body": b"data"}, {}, {}, id="put-missing-size"), @@ -238,7 +241,10 @@ def test_delete_serialization( "complete_multipart_upload", {"UploadId": "upload-id", "MpuObjectSize": 1024}, {}, - {SPANDATA.FILE_SIZE: 1024, SPANDATA.AWS_S3_UPLOAD_ID: "upload-id"}, + { + ATTRIBUTE_NAMES.FILE_SIZE: 1024, + ATTRIBUTE_NAMES.AWS_S3_UPLOAD_ID: "upload-id", + }, id="multipart-request-size", ), ], @@ -262,19 +268,19 @@ def test_size_attributes( attributes = span.get("attributes", {}) expected_attributes = { - SPANDATA.AWS_S3_BUCKET: "bucket", - SPANDATA.AWS_S3_KEY: "file.txt", + ATTRIBUTE_NAMES.AWS_S3_BUCKET: "bucket", + ATTRIBUTE_NAMES.AWS_S3_KEY: "file.txt", **expected, } for key, value in expected_attributes.items(): assert attributes[key] == value for key in ( - SPANDATA.AWS_S3_UPLOAD_ID, - SPANDATA.AWS_S3_COPY_SOURCE, - SPANDATA.AWS_S3_PART_NUMBER, - SPANDATA.AWS_S3_DELETE, - SPANDATA.FILE_SIZE, - SPANDATA.HTTP_BODY_SIZE, + ATTRIBUTE_NAMES.AWS_S3_UPLOAD_ID, + ATTRIBUTE_NAMES.AWS_S3_COPY_SOURCE, + ATTRIBUTE_NAMES.AWS_S3_PART_NUMBER, + ATTRIBUTE_NAMES.AWS_S3_DELETE, + ATTRIBUTE_NAMES.FILE_SIZE, + ATTRIBUTE_NAMES.HTTP_RESPONSE_BODY_SIZE, ): if key not in expected_attributes: assert key not in attributes diff --git a/tests/integrations/boto3/test_secretsmanager.py b/tests/integrations/boto3/test_secretsmanager.py index 8204012b5d..e403190cc8 100644 --- a/tests/integrations/boto3/test_secretsmanager.py +++ b/tests/integrations/boto3/test_secretsmanager.py @@ -1,8 +1,9 @@ import pytest from botocore.exceptions import ClientError from botocore.stub import Stubber +from sentry_conventions.attributes import ATTRIBUTE_NAMES -from sentry_sdk.consts import OP, SPANDATA +from sentry_sdk.consts import OP from tests.integrations.boto3.helpers import capture_spans_by_op from tests.integrations.boto3.helpers import client_factory as client_factory @@ -71,8 +72,8 @@ def test_secret_attributes( operation = secretsmanager_client.meta.method_to_api_mapping[method] assert span["name"] == "SecretsManager." + operation attributes = span["attributes"] - assert attributes[SPANDATA.AWS_SECRETSMANAGER_SECRET_ARN] == expected_arn - assert attributes[SPANDATA.AWS_SECRETSMANAGER_SECRET_NAME] == expected_name + assert attributes[ATTRIBUTE_NAMES.AWS_SECRETSMANAGER_SECRET_ARN] == expected_arn + assert attributes[ATTRIBUTE_NAMES.AWS_SECRETSMANAGER_SECRET_NAME] == expected_name # secret value is never recorded. assert "hunter2" not in str(span) @@ -97,9 +98,9 @@ def call(): (span,) = spans[OP.RPC] attributes = span["attributes"] assert span["status"] == "error" - assert attributes[SPANDATA.ERROR_TYPE] == "ResourceNotFoundException" - assert attributes[SPANDATA.CLOUD_ACCOUNT_ID] == "123456789012" - assert attributes[SPANDATA.CLOUD_RESOURCE_ID] == SECRET_ARN + assert attributes[ATTRIBUTE_NAMES.ERROR_TYPE] == "ResourceNotFoundException" + assert attributes[ATTRIBUTE_NAMES.CLOUD_ACCOUNT_ID] == "123456789012" + assert attributes[ATTRIBUTE_NAMES.CLOUD_RESOURCE_ID] == SECRET_ARN # both from the request's `SecretId`. - assert attributes[SPANDATA.AWS_SECRETSMANAGER_SECRET_ARN] == SECRET_ARN - assert attributes[SPANDATA.AWS_SECRETSMANAGER_SECRET_NAME] == "db-password" + assert attributes[ATTRIBUTE_NAMES.AWS_SECRETSMANAGER_SECRET_ARN] == SECRET_ARN + assert attributes[ATTRIBUTE_NAMES.AWS_SECRETSMANAGER_SECRET_NAME] == "db-password" diff --git a/tests/integrations/boto3/test_sns.py b/tests/integrations/boto3/test_sns.py index bf1eccf426..753e239053 100644 --- a/tests/integrations/boto3/test_sns.py +++ b/tests/integrations/boto3/test_sns.py @@ -1,8 +1,9 @@ import pytest from botocore.exceptions import ClientError, ParamValidationError from botocore.stub import Stubber +from sentry_conventions.attributes import ATTRIBUTE_NAMES -from sentry_sdk.consts import OP, SPANDATA +from sentry_sdk.consts import OP from sentry_sdk.integrations.boto3 import Boto3Integration from tests.integrations.boto3.helpers import ( capture_spans_by_op, @@ -59,12 +60,12 @@ def test_request_attributes( (span,) = spans[OP.RPC] attributes = span["attributes"] - assert attributes[SPANDATA.MESSAGING_DESTINATION_NAME] == "alerts" - assert attributes.get(SPANDATA.AWS_SNS_TOPIC_ARN) == topic_arn - assert attributes.get(SPANDATA.CLOUD_RESOURCE_ID) == topic_arn + assert attributes[ATTRIBUTE_NAMES.MESSAGING_DESTINATION_NAME] == "alerts" + assert attributes.get(ATTRIBUTE_NAMES.AWS_SNS_TOPIC_ARN) == topic_arn + assert attributes.get(ATTRIBUTE_NAMES.CLOUD_RESOURCE_ID) == topic_arn if topic_arn is not None: - assert attributes[SPANDATA.CLOUD_ACCOUNT_ID] == "123456789012" - assert SPANDATA.MESSAGING_SYSTEM not in attributes + assert attributes[ATTRIBUTE_NAMES.CLOUD_ACCOUNT_ID] == "123456789012" + assert ATTRIBUTE_NAMES.MESSAGING_SYSTEM not in attributes @pytest.mark.parametrize( @@ -216,13 +217,15 @@ def test_publish( assert span["name"] == f"send {destination}" assert span["status"] == ("ok" if error_type is None else "error") attributes = span["attributes"] - assert attributes[SPANDATA.SENTRY_KIND] == "producer" - assert attributes[SPANDATA.MESSAGING_OPERATION_TYPE] == "send" - assert attributes[SPANDATA.MESSAGING_OPERATION_NAME] == "publish" - assert attributes.get(SPANDATA.AWS_SNS_TOPIC_ARN) == topic_arn - assert attributes.get(SPANDATA.MESSAGING_BATCH_MESSAGE_COUNT) == message_count - assert attributes.get(SPANDATA.MESSAGING_MESSAGE_ID) == message_id - assert attributes.get(SPANDATA.ERROR_TYPE) == error_type + assert attributes[ATTRIBUTE_NAMES.SENTRY_KIND] == "producer" + assert attributes[ATTRIBUTE_NAMES.MESSAGING_OPERATION_TYPE] == "send" + assert attributes[ATTRIBUTE_NAMES.MESSAGING_OPERATION_NAME] == "publish" + assert attributes.get(ATTRIBUTE_NAMES.AWS_SNS_TOPIC_ARN) == topic_arn + assert ( + attributes.get(ATTRIBUTE_NAMES.MESSAGING_BATCH_MESSAGE_COUNT) == message_count + ) + assert attributes.get(ATTRIBUTE_NAMES.MESSAGING_MESSAGE_ID) == message_id + assert attributes.get(ATTRIBUTE_NAMES.ERROR_TYPE) == error_type def test_publish_without_destination(client_factory, capture_items): @@ -241,7 +244,7 @@ def call(): (span,) = spans[OP.QUEUE_PUBLISH] assert span["name"] == "send sns.eu-north-1.amazonaws.com:443" - assert SPANDATA.MESSAGING_DESTINATION_NAME not in span["attributes"] + assert ATTRIBUTE_NAMES.MESSAGING_DESTINATION_NAME not in span["attributes"] @pytest.mark.tests_internal_exceptions @@ -277,9 +280,12 @@ def call(): (span,) = spans[OP.QUEUE_PUBLISH] assert span["status"] == "error" attributes = span["attributes"] - assert attributes[SPANDATA.MESSAGING_SYSTEM] == "aws.sns" - assert SPANDATA.MESSAGING_BATCH_MESSAGE_COUNT not in attributes - assert attributes[SPANDATA.ERROR_TYPE] == "botocore.exceptions.ParamValidationError" + assert attributes[ATTRIBUTE_NAMES.MESSAGING_SYSTEM] == "aws.sns" + assert ATTRIBUTE_NAMES.MESSAGING_BATCH_MESSAGE_COUNT not in attributes + assert ( + attributes[ATTRIBUTE_NAMES.ERROR_TYPE] + == "botocore.exceptions.ParamValidationError" + ) @pytest.mark.parametrize( @@ -352,7 +358,7 @@ def test_resource_id_data_collection( ((span,),) = spans.values() attributes = span["attributes"] - assert attributes[SPANDATA.CLOUD_RESOURCE_ID] == ( + assert attributes[ATTRIBUTE_NAMES.CLOUD_RESOURCE_ID] == ( arn if expect_user_info else filtered_arn ) - assert attributes[SPANDATA.CLOUD_ACCOUNT_ID] == "123456789012" + assert attributes[ATTRIBUTE_NAMES.CLOUD_ACCOUNT_ID] == "123456789012" diff --git a/tests/integrations/boto3/test_sqs.py b/tests/integrations/boto3/test_sqs.py index d0a4ef30fa..a78a61260a 100644 --- a/tests/integrations/boto3/test_sqs.py +++ b/tests/integrations/boto3/test_sqs.py @@ -4,8 +4,9 @@ import pytest from botocore.exceptions import ClientError from botocore.stub import Stubber +from sentry_conventions.attributes import ATTRIBUTE_NAMES -from sentry_sdk.consts import OP, SPANDATA +from sentry_sdk.consts import OP from tests.integrations.boto3.helpers import ( capture_spans_by_op, require_botocore_model_fields, @@ -80,11 +81,11 @@ def test_request_attributes( (span,) = spans[OP.RPC] attributes = span["attributes"] - assert attributes[SPANDATA.MESSAGING_DESTINATION_NAME] == "orders" - assert attributes.get(SPANDATA.AWS_SQS_QUEUE_URL) == queue_url - assert attributes.get(SPANDATA.CLOUD_ACCOUNT_ID) == account_id - assert attributes.get(SPANDATA.CLOUD_RESOURCE_ID) == resource_id - assert SPANDATA.MESSAGING_SYSTEM not in attributes + assert attributes[ATTRIBUTE_NAMES.MESSAGING_DESTINATION_NAME] == "orders" + assert attributes.get(ATTRIBUTE_NAMES.AWS_SQS_QUEUE_URL) == queue_url + assert attributes.get(ATTRIBUTE_NAMES.CLOUD_ACCOUNT_ID) == account_id + assert attributes.get(ATTRIBUTE_NAMES.CLOUD_RESOURCE_ID) == resource_id + assert ATTRIBUTE_NAMES.MESSAGING_SYSTEM not in attributes @pytest.mark.parametrize( @@ -192,12 +193,14 @@ def test_send_message( assert span["name"] == "send orders" assert span["status"] == ("ok" if error_type is None else "error") attributes = span["attributes"] - assert attributes[SPANDATA.SENTRY_KIND] == "producer" - assert attributes[SPANDATA.MESSAGING_OPERATION_TYPE] == "send" - assert attributes[SPANDATA.MESSAGING_OPERATION_NAME] == "send" - assert attributes.get(SPANDATA.MESSAGING_BATCH_MESSAGE_COUNT) == message_count - assert attributes.get(SPANDATA.MESSAGING_MESSAGE_ID) == message_id - assert attributes.get(SPANDATA.ERROR_TYPE) == error_type + assert attributes[ATTRIBUTE_NAMES.SENTRY_KIND] == "producer" + assert attributes[ATTRIBUTE_NAMES.MESSAGING_OPERATION_TYPE] == "send" + assert attributes[ATTRIBUTE_NAMES.MESSAGING_OPERATION_NAME] == "send" + assert ( + attributes.get(ATTRIBUTE_NAMES.MESSAGING_BATCH_MESSAGE_COUNT) == message_count + ) + assert attributes.get(ATTRIBUTE_NAMES.MESSAGING_MESSAGE_ID) == message_id + assert attributes.get(ATTRIBUTE_NAMES.ERROR_TYPE) == error_type @pytest.mark.parametrize( @@ -247,11 +250,11 @@ def test_receive_message( (span,) = spans[OP.QUEUE_RECEIVE] assert span["name"] == "receive orders" attributes = span["attributes"] - assert attributes[SPANDATA.SENTRY_KIND] == "client" - assert attributes[SPANDATA.MESSAGING_OPERATION_TYPE] == "receive" - assert attributes[SPANDATA.MESSAGING_OPERATION_NAME] == "receive" - assert attributes[SPANDATA.MESSAGING_BATCH_MESSAGE_COUNT] == message_count - assert attributes.get(SPANDATA.MESSAGING_MESSAGE_ID) == message_id + assert attributes[ATTRIBUTE_NAMES.SENTRY_KIND] == "client" + assert attributes[ATTRIBUTE_NAMES.MESSAGING_OPERATION_TYPE] == "receive" + assert attributes[ATTRIBUTE_NAMES.MESSAGING_OPERATION_NAME] == "receive" + assert attributes[ATTRIBUTE_NAMES.MESSAGING_BATCH_MESSAGE_COUNT] == message_count + assert attributes.get(ATTRIBUTE_NAMES.MESSAGING_MESSAGE_ID) == message_id assert "receipt-handle" not in str(span) @@ -274,7 +277,7 @@ def call(): spans = capture_spans_by_op(call, capture_items) (span,) = spans[OP.QUEUE_RECEIVE] - assert SPANDATA.MESSAGING_BATCH_MESSAGE_COUNT not in span["attributes"] + assert ATTRIBUTE_NAMES.MESSAGING_BATCH_MESSAGE_COUNT not in span["attributes"] @pytest.mark.parametrize( @@ -319,10 +322,12 @@ def test_delete_message( (span,) = spans[OP.QUEUE_SETTLE] assert span["name"] == "settle orders" attributes = span["attributes"] - assert attributes[SPANDATA.SENTRY_KIND] == "client" - assert attributes[SPANDATA.MESSAGING_OPERATION_TYPE] == "settle" - assert attributes[SPANDATA.MESSAGING_OPERATION_NAME] == "settle" - assert attributes.get(SPANDATA.MESSAGING_BATCH_MESSAGE_COUNT) == message_count + assert attributes[ATTRIBUTE_NAMES.SENTRY_KIND] == "client" + assert attributes[ATTRIBUTE_NAMES.MESSAGING_OPERATION_TYPE] == "settle" + assert attributes[ATTRIBUTE_NAMES.MESSAGING_OPERATION_NAME] == "settle" + assert ( + attributes.get(ATTRIBUTE_NAMES.MESSAGING_BATCH_MESSAGE_COUNT) == message_count + ) assert "receipt-handle" not in str(span) @@ -421,7 +426,7 @@ def test_receive_message_links(client_factory, capture_items): (span,) = spans[OP.QUEUE_RECEIVE] link_type = { - SPANDATA.SENTRY_LINK_TYPE: {"value": "message_origin", "type": "string"} + ATTRIBUTE_NAMES.SENTRY_LINK_TYPE: {"value": "message_origin", "type": "string"} } assert span["links"] == [ # messages 1 and 2 share a context, so one link. diff --git a/tests/integrations/boto3/test_stepfunctions.py b/tests/integrations/boto3/test_stepfunctions.py index f2ebc81eb7..13c673ce02 100644 --- a/tests/integrations/boto3/test_stepfunctions.py +++ b/tests/integrations/boto3/test_stepfunctions.py @@ -2,8 +2,9 @@ import pytest from botocore.stub import Stubber +from sentry_conventions.attributes import ATTRIBUTE_NAMES -from sentry_sdk.consts import OP, SPANDATA +from sentry_sdk.consts import OP from tests.integrations.boto3.helpers import ( capture_spans_by_op, require_botocore_model_fields, @@ -41,10 +42,10 @@ def _stubbed_span(client_factory, capture_items, method, params, response): {"stateMachineArn": ALIAS_ARN, "name": "order-1234"}, {"executionArn": EXECUTION_ARN, "startDate": DATE}, { - SPANDATA.CLOUD_RESOURCE_ID: ALIAS_ARN, - SPANDATA.AWS_STEP_FUNCTIONS_STATE_MACHINE_ARN: ALIAS_ARN, - SPANDATA.AWS_STEP_FUNCTIONS_STATE_MACHINE_NAME: "orders", - SPANDATA.AWS_STEP_FUNCTIONS_EXECUTION_ARN: EXECUTION_ARN, + ATTRIBUTE_NAMES.CLOUD_RESOURCE_ID: ALIAS_ARN, + ATTRIBUTE_NAMES.AWS_STEP_FUNCTIONS_STATE_MACHINE_ARN: ALIAS_ARN, + ATTRIBUTE_NAMES.AWS_STEP_FUNCTIONS_STATE_MACHINE_NAME: "orders", + ATTRIBUTE_NAMES.AWS_STEP_FUNCTIONS_EXECUTION_ARN: EXECUTION_ARN, }, id="alias", ), @@ -53,9 +54,9 @@ def _stubbed_span(client_factory, capture_items, method, params, response): {"executionArn": EXECUTION_ARN}, {"stopDate": DATE}, { - SPANDATA.CLOUD_RESOURCE_ID: EXECUTION_ARN, - SPANDATA.AWS_STEP_FUNCTIONS_EXECUTION_ARN: EXECUTION_ARN, - SPANDATA.AWS_STEP_FUNCTIONS_STATE_MACHINE_NAME: "orders", + ATTRIBUTE_NAMES.CLOUD_RESOURCE_ID: EXECUTION_ARN, + ATTRIBUTE_NAMES.AWS_STEP_FUNCTIONS_EXECUTION_ARN: EXECUTION_ARN, + ATTRIBUTE_NAMES.AWS_STEP_FUNCTIONS_STATE_MACHINE_NAME: "orders", }, id="execution", ), @@ -64,8 +65,8 @@ def _stubbed_span(client_factory, capture_items, method, params, response): {"mapRunArn": MAP_RUN_ARN}, {"executions": []}, { - SPANDATA.CLOUD_RESOURCE_ID: MAP_RUN_ARN, - SPANDATA.AWS_STEP_FUNCTIONS_STATE_MACHINE_NAME: "orders", + ATTRIBUTE_NAMES.CLOUD_RESOURCE_ID: MAP_RUN_ARN, + ATTRIBUTE_NAMES.AWS_STEP_FUNCTIONS_STATE_MACHINE_NAME: "orders", }, id="map-run", ), @@ -78,8 +79,8 @@ def _stubbed_span(client_factory, capture_items, method, params, response): }, {"stateMachineArn": STATE_MACHINE_ARN, "creationDate": DATE}, { - SPANDATA.AWS_STEP_FUNCTIONS_STATE_MACHINE_ARN: STATE_MACHINE_ARN, - SPANDATA.AWS_STEP_FUNCTIONS_STATE_MACHINE_NAME: "orders", + ATTRIBUTE_NAMES.AWS_STEP_FUNCTIONS_STATE_MACHINE_ARN: STATE_MACHINE_ARN, + ATTRIBUTE_NAMES.AWS_STEP_FUNCTIONS_STATE_MACHINE_NAME: "orders", }, id="create-state-machine", ), @@ -88,8 +89,8 @@ def _stubbed_span(client_factory, capture_items, method, params, response): {"name": "approve"}, {"activityArn": ACTIVITY_ARN, "creationDate": DATE}, { - SPANDATA.AWS_STEP_FUNCTIONS_ACTIVITY_ARN: ACTIVITY_ARN, - SPANDATA.AWS_STEP_FUNCTIONS_ACTIVITY_NAME: "approve", + ATTRIBUTE_NAMES.AWS_STEP_FUNCTIONS_ACTIVITY_ARN: ACTIVITY_ARN, + ATTRIBUTE_NAMES.AWS_STEP_FUNCTIONS_ACTIVITY_NAME: "approve", }, id="create-activity", ), @@ -98,8 +99,8 @@ def _stubbed_span(client_factory, capture_items, method, params, response): {"resourceArn": ACTIVITY_ARN, "tags": [{"key": "team", "value": "a"}]}, {}, { - SPANDATA.CLOUD_RESOURCE_ID: ACTIVITY_ARN, - SPANDATA.AWS_STEP_FUNCTIONS_ACTIVITY_NAME: "approve", + ATTRIBUTE_NAMES.CLOUD_RESOURCE_ID: ACTIVITY_ARN, + ATTRIBUTE_NAMES.AWS_STEP_FUNCTIONS_ACTIVITY_NAME: "approve", }, id="tag-activity", ), @@ -114,7 +115,8 @@ def test_resource_attributes( assert { key: value for key, value in span["attributes"].items() - if key.startswith("aws.step_functions.") or key == SPANDATA.CLOUD_RESOURCE_ID + if key.startswith("aws.step_functions.") + or key == ATTRIBUTE_NAMES.CLOUD_RESOURCE_ID } == expected @@ -147,7 +149,7 @@ def test_execution_outcome( span = _stubbed_span(client_factory, capture_items, method, params, response) assert span["status"] == ("ok" if expected_error_type is None else "error") - assert span["attributes"].get(SPANDATA.ERROR_TYPE) == expected_error_type + assert span["attributes"].get(ATTRIBUTE_NAMES.ERROR_TYPE) == expected_error_type # the execution's input, error and cause are never recorded. assert "4242" not in str(span) assert "States.TaskFailed" not in str(span) diff --git a/tests/integrations/chalice/test_chalice.py b/tests/integrations/chalice/test_chalice.py index 2bf5ccc1ca..0cc66be725 100644 --- a/tests/integrations/chalice/test_chalice.py +++ b/tests/integrations/chalice/test_chalice.py @@ -4,6 +4,7 @@ from chalice import BadRequestError, Chalice from chalice.local import LambdaContext, LocalGateway from pytest_chalice.handlers import RequestHandler +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk from sentry_sdk import capture_message @@ -189,9 +190,9 @@ def test_existing_span(sentry_init, capture_items, app): name="lambda_segment", parent_span=None, attributes={ - "sentry.origin": "auto.function.aws_lambda", - "sentry.op": "function.aws", - "faas.name": "api_handler", + ATTRIBUTE_NAMES.SENTRY_ORIGIN: "auto.function.aws_lambda", + ATTRIBUTE_NAMES.SENTRY_OP: "function.aws", + ATTRIBUTE_NAMES.FAAS_NAME: "api_handler", }, ): response = client.get("/message") @@ -204,9 +205,9 @@ def test_existing_span(sentry_init, capture_items, app): span = segment_spans[0] attrs = span["attributes"] - assert attrs["sentry.origin"] == "auto.function.chalice" - assert attrs["sentry.op"] == "function.aws" - assert attrs["faas.name"] == "api_handler" + assert attrs[ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.function.chalice" + assert attrs[ATTRIBUTE_NAMES.SENTRY_OP] == "function.aws" + assert attrs[ATTRIBUTE_NAMES.FAAS_NAME] == "api_handler" assert span["status"] == "ok" @@ -221,7 +222,7 @@ def test_existing_span_error( with sentry_sdk.start_span( name="lambda_segment", parent_span=None, - attributes={"sentry.origin": "auto.function.aws_lambda"}, + attributes={ATTRIBUTE_NAMES.SENTRY_ORIGIN: "auto.function.aws_lambda"}, ): response = client.get("/boom") assert response.status_code == 500 @@ -235,5 +236,8 @@ def test_existing_span_error( s.payload for s in items if s.type == "span" and s.payload.get("is_segment") ] assert len(segment_spans) == 1 - assert segment_spans[0]["attributes"]["sentry.origin"] == "auto.function.chalice" + assert ( + segment_spans[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] + == "auto.function.chalice" + ) assert segment_spans[0]["status"] == "error" diff --git a/tests/integrations/gcp/test_gcp.py b/tests/integrations/gcp/test_gcp.py index d7fc229236..2d854b00a1 100644 --- a/tests/integrations/gcp/test_gcp.py +++ b/tests/integrations/gcp/test_gcp.py @@ -12,6 +12,7 @@ from textwrap import dedent import pytest +from sentry_conventions.attributes import ATTRIBUTE_NAMES from sentry_sdk.traces import SpanStatus @@ -466,7 +467,10 @@ def cloud_function(functionhandler, event): ) assert len(span_items) == 1 - assert span_items[0]["attributes"]["sentry.origin"] == "auto.function.gcp" + assert ( + span_items[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] + == "auto.function.gcp" + ) def test_performance_no_error(run_cloud_function): @@ -493,14 +497,26 @@ def cloud_function(functionhandler, event): assert segment_span["is_segment"] is True assert segment_span["name"] == "Google Cloud function" - assert segment_span["attributes"]["sentry.op"] == "function.gcp" - assert segment_span["attributes"]["sentry.origin"] == "auto.function.gcp" - assert segment_span["attributes"]["sentry.segment.name.source"] == "component" - assert segment_span["attributes"]["cloud.provider"] == "gcp" - assert segment_span["attributes"]["faas.name"] == "Google Cloud function" - assert segment_span["attributes"]["gcp.project.id"] == "serverless_project" - assert segment_span["attributes"]["faas.identity"] == "func_ID" - assert segment_span["attributes"]["faas.entry_point"] == "cloud_function" + assert segment_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "function.gcp" + assert ( + segment_span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.function.gcp" + ) + assert ( + segment_span["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE] + == "component" + ) + assert segment_span["attributes"][ATTRIBUTE_NAMES.CLOUD_PROVIDER] == "gcp" + assert ( + segment_span["attributes"][ATTRIBUTE_NAMES.FAAS_NAME] == "Google Cloud function" + ) + assert ( + segment_span["attributes"][ATTRIBUTE_NAMES.GCP_PROJECT_ID] + == "serverless_project" + ) + assert segment_span["attributes"][ATTRIBUTE_NAMES.FAAS_IDENTITY] == "func_ID" + assert ( + segment_span["attributes"][ATTRIBUTE_NAMES.FAAS_ENTRY_POINT] == "cloud_function" + ) def test_performance_error(run_cloud_function): @@ -533,14 +549,26 @@ def cloud_function(functionhandler, event): segment_span = span_items[0] assert segment_span["is_segment"] is True assert segment_span["name"] == "Google Cloud function" - assert segment_span["attributes"]["sentry.op"] == "function.gcp" - assert segment_span["attributes"]["sentry.origin"] == "auto.function.gcp" - assert segment_span["attributes"]["sentry.segment.name.source"] == "component" - assert segment_span["attributes"]["cloud.provider"] == "gcp" - assert segment_span["attributes"]["faas.name"] == "Google Cloud function" - assert segment_span["attributes"]["gcp.project.id"] == "serverless_project" - assert segment_span["attributes"]["faas.identity"] == "func_ID" - assert segment_span["attributes"]["faas.entry_point"] == "cloud_function" + assert segment_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "function.gcp" + assert ( + segment_span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.function.gcp" + ) + assert ( + segment_span["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE] + == "component" + ) + assert segment_span["attributes"][ATTRIBUTE_NAMES.CLOUD_PROVIDER] == "gcp" + assert ( + segment_span["attributes"][ATTRIBUTE_NAMES.FAAS_NAME] == "Google Cloud function" + ) + assert ( + segment_span["attributes"][ATTRIBUTE_NAMES.GCP_PROJECT_ID] + == "serverless_project" + ) + assert segment_span["attributes"][ATTRIBUTE_NAMES.FAAS_IDENTITY] == "func_ID" + assert ( + segment_span["attributes"][ATTRIBUTE_NAMES.FAAS_ENTRY_POINT] == "cloud_function" + ) assert segment_span["status"] == SpanStatus.ERROR @@ -582,14 +610,26 @@ def cloud_function(functionhandler, event): assert segment_span["is_segment"] is True assert segment_span["trace_id"] == trace_id assert segment_span["name"] == "Google Cloud function" - assert segment_span["attributes"]["sentry.op"] == "function.gcp" - assert segment_span["attributes"]["sentry.origin"] == "auto.function.gcp" - assert segment_span["attributes"]["sentry.segment.name.source"] == "component" - assert segment_span["attributes"]["cloud.provider"] == "gcp" - assert segment_span["attributes"]["faas.name"] == "Google Cloud function" - assert segment_span["attributes"]["gcp.project.id"] == "serverless_project" - assert segment_span["attributes"]["faas.identity"] == "func_ID" - assert segment_span["attributes"]["faas.entry_point"] == "cloud_function" + assert segment_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "function.gcp" + assert ( + segment_span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.function.gcp" + ) + assert ( + segment_span["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE] + == "component" + ) + assert segment_span["attributes"][ATTRIBUTE_NAMES.CLOUD_PROVIDER] == "gcp" + assert ( + segment_span["attributes"][ATTRIBUTE_NAMES.FAAS_NAME] == "Google Cloud function" + ) + assert ( + segment_span["attributes"][ATTRIBUTE_NAMES.GCP_PROJECT_ID] + == "serverless_project" + ) + assert segment_span["attributes"][ATTRIBUTE_NAMES.FAAS_IDENTITY] == "func_ID" + assert ( + segment_span["attributes"][ATTRIBUTE_NAMES.FAAS_ENTRY_POINT] == "cloud_function" + ) def test_request_attributes(run_cloud_function): @@ -623,14 +663,14 @@ def cloud_function(functionhandler, event): segment_span = span_items[0] attrs = segment_span["attributes"] - assert attrs["http.request.method"] == "POST" - assert attrs["url.query"] == "foo=bar" + assert attrs[ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD] == "POST" + assert attrs[ATTRIBUTE_NAMES.URL_QUERY] == "foo=bar" assert attrs["http.request.header.content-type"] == ["application/json"] assert attrs["http.request.header.accept"] == ["text/html"] - assert attrs["faas.name"] == "Google Cloud function" - assert attrs["gcp.project.id"] == "serverless_project" - assert attrs["faas.identity"] == "func_ID" - assert attrs["faas.entry_point"] == "cloud_function" + assert attrs[ATTRIBUTE_NAMES.FAAS_NAME] == "Google Cloud function" + assert attrs[ATTRIBUTE_NAMES.GCP_PROJECT_ID] == "serverless_project" + assert attrs[ATTRIBUTE_NAMES.FAAS_IDENTITY] == "func_ID" + assert attrs[ATTRIBUTE_NAMES.FAAS_ENTRY_POINT] == "cloud_function" # Each case is (data_collection, expected_span). ``data_collection`` of None @@ -710,9 +750,9 @@ def cloud_function(functionhandler, event): assert len(span_items) == 1 attrs = span_items[0]["attributes"] if expected_span is None: - assert "url.query" not in attrs + assert ATTRIBUTE_NAMES.URL_QUERY not in attrs else: - assert attrs["url.query"] == expected_span + assert attrs[ATTRIBUTE_NAMES.URL_QUERY] == expected_span # Each case is (data_collection, expected_data, expected_meta).