diff --git a/MIGRATION_GUIDE.md b/MIGRATION_GUIDE.md index 558188ae1b..1c0fa76fb0 100644 --- a/MIGRATION_GUIDE.md +++ b/MIGRATION_GUIDE.md @@ -105,6 +105,19 @@ Looking to upgrade from Sentry SDK 2.x to 3.x? Here's a comprehensive list of wh | `aws_event["headers"]["Host"]` | `server.address` | | `aws_context["function_name"]` | `faas.name` | +- If you're using the GCP integration, the `sampling_context` argument of `traces_sampler` doesn't contain the `gcp_env` and `gcp_event` keys anymore. Instead, the following, if available, is accessible: + + | Old sampling context key | New sampling context key | + | --------------------------------- | -------------------------- | + | `gcp_env["function_name"]` | `faas.name` | + | `gcp_env["function_region"]` | `faas.region` | + | `gcp_env["function_project"]` | `gcp.function.project` | + | `gcp_env["function_identity"]` | `gcp.function.identity` | + | `gcp_env["function_entry_point"]` | `gcp.function.entry_point` | + | `gcp_event.method` | `http.request.method` | + | `gcp_event.query_string` | `url.query` | + + ### Removed - Spans no longer have a `description`. Use `name` instead. diff --git a/sentry_sdk/integrations/aws_lambda.py b/sentry_sdk/integrations/aws_lambda.py index 656d71ec8e..177d73a638 100644 --- a/sentry_sdk/integrations/aws_lambda.py +++ b/sentry_sdk/integrations/aws_lambda.py @@ -468,7 +468,9 @@ def _event_from_error_json(error_json): def _prepopulate_attributes(aws_event, aws_context): - attributes = {} + attributes = { + "cloud.provider": "aws", + } for prop, attr in EVENT_TO_ATTRIBUTES.items(): if aws_event.get(prop) is not None: diff --git a/sentry_sdk/integrations/gcp.py b/sentry_sdk/integrations/gcp.py index 6ca52397d8..2f17464f70 100644 --- a/sentry_sdk/integrations/gcp.py +++ b/sentry_sdk/integrations/gcp.py @@ -84,22 +84,12 @@ def sentry_func(functionhandler, gcp_event, *args, **kwargs): headers = gcp_event.headers with sentry_sdk.continue_trace(headers): - sampling_context = { - "gcp_env": { - "function_name": environ.get("FUNCTION_NAME"), - "function_entry_point": environ.get("ENTRY_POINT"), - "function_identity": environ.get("FUNCTION_IDENTITY"), - "function_region": environ.get("FUNCTION_REGION"), - "function_project": environ.get("GCP_PROJECT"), - }, - "gcp_event": gcp_event, - } with sentry_sdk.start_transaction( op=OP.FUNCTION_GCP, name=environ.get("FUNCTION_NAME", ""), source=TRANSACTION_SOURCE_COMPONENT, origin=GcpIntegration.origin, - custom_sampling_context=sampling_context, + attributes=_prepopulate_attributes(gcp_event), ): try: return func(functionhandler, gcp_event, *args, **kwargs) @@ -229,3 +219,33 @@ def _get_google_cloud_logs_url(final_time): ) return url + + +ENV_TO_ATTRIBUTE = { + "FUNCTION_NAME": "faas.name", + "ENTRY_POINT": "gcp.function.entry_point", + "FUNCTION_IDENTITY": "gcp.function.identity", + "FUNCTION_REGION": "faas.region", + "GCP_PROJECT": "gcp.function.project", +} + +EVENT_TO_ATTRIBUTE = { + "method": "http.request.method", + "query_string": "url.query", +} + + +def _prepopulate_attributes(gcp_event): + attributes = { + "cloud.provider": "gcp", + } + + for key, attr in ENV_TO_ATTRIBUTE.items(): + if environ.get(key): + attributes[attr] = environ[key] + + for key, attr in EVENT_TO_ATTRIBUTE.items(): + if getattr(gcp_event, key, None): + attributes[attr] = getattr(gcp_event, key) + + return attributes diff --git a/tests/integrations/gcp/test_gcp.py b/tests/integrations/gcp/test_gcp.py index 22d104c817..f33c1b35d7 100644 --- a/tests/integrations/gcp/test_gcp.py +++ b/tests/integrations/gcp/test_gcp.py @@ -304,16 +304,12 @@ def cloud_function(functionhandler, event): try: traces_sampler.assert_any_call( DictionaryContaining({ - "gcp_env": DictionaryContaining({ - "function_name": "chase_into_tree", - "function_region": "dogpark", - "function_project": "SquirrelChasing", - }), - "gcp_event": { - "type": "chase", - "chasers": ["Maisey", "Charlie"], - "num_squirrels": 2, - }, + "faas.name": "chase_into_tree", + "faas.region": "dogpark", + "gcp.function.identity": "func_ID", + "gcp.function.entry_point": "cloud_function", + "gcp.function.project": "SquirrelChasing", + "cloud.provider": "gcp", }) ) except AssertionError: