From 55740224292eaa8317fc0bd50335bbbabdee431d Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Fri, 22 Nov 2024 14:49:19 +0100 Subject: [PATCH 1/4] . --- sentry_sdk/integrations/gcp.py | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/sentry_sdk/integrations/gcp.py b/sentry_sdk/integrations/gcp.py index 6ca52397d8..6da70a630e 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,22 @@ def _get_google_cloud_logs_url(final_time): ) return url + + +ENV_TO_ATTRIBUTE = { + "FUNCTION_NAME": "", + "ENTRY_POINT": "", + "FUNCTION_IDENTITY": "", + "FUNCTION_REGION": "", + "GCP_PROJECT": "", +} + + +def _prepopulate_attributes(gcp_event): + attributes = {} + + for key, attr in ENV_TO_ATTRIBUTE.items(): + if environ.get(key): + attributes[attr] = environ[key] + + return attributes From bbd14a2d02ee4bd5f0859328e80e6d5766eef85e Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Fri, 22 Nov 2024 15:09:58 +0100 Subject: [PATCH 2/4] . --- sentry_sdk/integrations/aws_lambda.py | 4 +++- sentry_sdk/integrations/gcp.py | 19 ++++++++++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) 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 6da70a630e..e91cd4b2b7 100644 --- a/sentry_sdk/integrations/gcp.py +++ b/sentry_sdk/integrations/gcp.py @@ -222,19 +222,32 @@ def _get_google_cloud_logs_url(final_time): ENV_TO_ATTRIBUTE = { - "FUNCTION_NAME": "", + "FUNCTION_NAME": "faas.name", + # XXX map these to something "ENTRY_POINT": "", "FUNCTION_IDENTITY": "", - "FUNCTION_REGION": "", + "FUNCTION_REGION": "faas.invoked_region", "GCP_PROJECT": "", } +EVENT_TO_ATTRIBUTE = { + # XXX more attrs + "method": "http.request.method", + "query_string": "url.query", +} + def _prepopulate_attributes(gcp_event): - attributes = {} + 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 From 0c4bfbbb53d5f574ec395965e445cf19de026809 Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Thu, 28 Nov 2024 14:08:43 +0100 Subject: [PATCH 3/4] , --- sentry_sdk/integrations/gcp.py | 10 ++++------ tests/integrations/gcp/test_gcp.py | 16 ++++++---------- 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/sentry_sdk/integrations/gcp.py b/sentry_sdk/integrations/gcp.py index e91cd4b2b7..bccfbd67d9 100644 --- a/sentry_sdk/integrations/gcp.py +++ b/sentry_sdk/integrations/gcp.py @@ -223,15 +223,13 @@ def _get_google_cloud_logs_url(final_time): ENV_TO_ATTRIBUTE = { "FUNCTION_NAME": "faas.name", - # XXX map these to something - "ENTRY_POINT": "", - "FUNCTION_IDENTITY": "", - "FUNCTION_REGION": "faas.invoked_region", - "GCP_PROJECT": "", + "ENTRY_POINT": "gcp.entry_point", + "FUNCTION_IDENTITY": "gcp.function_identity", + "FUNCTION_REGION": "faas.region", + "GCP_PROJECT": "gcp.project", } EVENT_TO_ATTRIBUTE = { - # XXX more attrs "method": "http.request.method", "query_string": "url.query", } diff --git a/tests/integrations/gcp/test_gcp.py b/tests/integrations/gcp/test_gcp.py index 22d104c817..34d3fc530a 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.entry_point": "cloud_function", + "gcp.project": "SquirrelChasing", + "cloud.provider": "gcp", }) ) except AssertionError: From e9da4ca702f6bb1427d46a02f89ae4c1149d8809 Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Thu, 28 Nov 2024 14:36:14 +0100 Subject: [PATCH 4/4] . --- MIGRATION_GUIDE.md | 13 +++++++++++++ sentry_sdk/integrations/gcp.py | 6 +++--- tests/integrations/gcp/test_gcp.py | 6 +++--- 3 files changed, 19 insertions(+), 6 deletions(-) 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/gcp.py b/sentry_sdk/integrations/gcp.py index bccfbd67d9..2f17464f70 100644 --- a/sentry_sdk/integrations/gcp.py +++ b/sentry_sdk/integrations/gcp.py @@ -223,10 +223,10 @@ def _get_google_cloud_logs_url(final_time): ENV_TO_ATTRIBUTE = { "FUNCTION_NAME": "faas.name", - "ENTRY_POINT": "gcp.entry_point", - "FUNCTION_IDENTITY": "gcp.function_identity", + "ENTRY_POINT": "gcp.function.entry_point", + "FUNCTION_IDENTITY": "gcp.function.identity", "FUNCTION_REGION": "faas.region", - "GCP_PROJECT": "gcp.project", + "GCP_PROJECT": "gcp.function.project", } EVENT_TO_ATTRIBUTE = { diff --git a/tests/integrations/gcp/test_gcp.py b/tests/integrations/gcp/test_gcp.py index 34d3fc530a..f33c1b35d7 100644 --- a/tests/integrations/gcp/test_gcp.py +++ b/tests/integrations/gcp/test_gcp.py @@ -306,9 +306,9 @@ def cloud_function(functionhandler, event): DictionaryContaining({ "faas.name": "chase_into_tree", "faas.region": "dogpark", - "gcp.function_identity": "func_ID", - "gcp.entry_point": "cloud_function", - "gcp.project": "SquirrelChasing", + "gcp.function.identity": "func_ID", + "gcp.function.entry_point": "cloud_function", + "gcp.function.project": "SquirrelChasing", "cloud.provider": "gcp", }) )