Skip to content

Commit 5beea99

Browse files
authored
Use span attrs in GCP sampling context (#3818)
1 parent 7c2d770 commit 5beea99

File tree

4 files changed

+53
-22
lines changed

4 files changed

+53
-22
lines changed

MIGRATION_GUIDE.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,19 @@ Looking to upgrade from Sentry SDK 2.x to 3.x? Here's a comprehensive list of wh
105105
| `aws_event["headers"]["Host"]` | `server.address` |
106106
| `aws_context["function_name"]` | `faas.name` |
107107

108+
- 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:
109+
110+
| Old sampling context key | New sampling context key |
111+
| --------------------------------- | -------------------------- |
112+
| `gcp_env["function_name"]` | `faas.name` |
113+
| `gcp_env["function_region"]` | `faas.region` |
114+
| `gcp_env["function_project"]` | `gcp.function.project` |
115+
| `gcp_env["function_identity"]` | `gcp.function.identity` |
116+
| `gcp_env["function_entry_point"]` | `gcp.function.entry_point` |
117+
| `gcp_event.method` | `http.request.method` |
118+
| `gcp_event.query_string` | `url.query` |
119+
120+
108121
### Removed
109122

110123
- Spans no longer have a `description`. Use `name` instead.

sentry_sdk/integrations/aws_lambda.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,9 @@ def _event_from_error_json(error_json):
468468

469469

470470
def _prepopulate_attributes(aws_event, aws_context):
471-
attributes = {}
471+
attributes = {
472+
"cloud.provider": "aws",
473+
}
472474

473475
for prop, attr in EVENT_TO_ATTRIBUTES.items():
474476
if aws_event.get(prop) is not None:

sentry_sdk/integrations/gcp.py

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,22 +84,12 @@ def sentry_func(functionhandler, gcp_event, *args, **kwargs):
8484
headers = gcp_event.headers
8585

8686
with sentry_sdk.continue_trace(headers):
87-
sampling_context = {
88-
"gcp_env": {
89-
"function_name": environ.get("FUNCTION_NAME"),
90-
"function_entry_point": environ.get("ENTRY_POINT"),
91-
"function_identity": environ.get("FUNCTION_IDENTITY"),
92-
"function_region": environ.get("FUNCTION_REGION"),
93-
"function_project": environ.get("GCP_PROJECT"),
94-
},
95-
"gcp_event": gcp_event,
96-
}
9787
with sentry_sdk.start_transaction(
9888
op=OP.FUNCTION_GCP,
9989
name=environ.get("FUNCTION_NAME", ""),
10090
source=TRANSACTION_SOURCE_COMPONENT,
10191
origin=GcpIntegration.origin,
102-
custom_sampling_context=sampling_context,
92+
attributes=_prepopulate_attributes(gcp_event),
10393
):
10494
try:
10595
return func(functionhandler, gcp_event, *args, **kwargs)
@@ -229,3 +219,33 @@ def _get_google_cloud_logs_url(final_time):
229219
)
230220

231221
return url
222+
223+
224+
ENV_TO_ATTRIBUTE = {
225+
"FUNCTION_NAME": "faas.name",
226+
"ENTRY_POINT": "gcp.function.entry_point",
227+
"FUNCTION_IDENTITY": "gcp.function.identity",
228+
"FUNCTION_REGION": "faas.region",
229+
"GCP_PROJECT": "gcp.function.project",
230+
}
231+
232+
EVENT_TO_ATTRIBUTE = {
233+
"method": "http.request.method",
234+
"query_string": "url.query",
235+
}
236+
237+
238+
def _prepopulate_attributes(gcp_event):
239+
attributes = {
240+
"cloud.provider": "gcp",
241+
}
242+
243+
for key, attr in ENV_TO_ATTRIBUTE.items():
244+
if environ.get(key):
245+
attributes[attr] = environ[key]
246+
247+
for key, attr in EVENT_TO_ATTRIBUTE.items():
248+
if getattr(gcp_event, key, None):
249+
attributes[attr] = getattr(gcp_event, key)
250+
251+
return attributes

tests/integrations/gcp/test_gcp.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -304,16 +304,12 @@ def cloud_function(functionhandler, event):
304304
try:
305305
traces_sampler.assert_any_call(
306306
DictionaryContaining({
307-
"gcp_env": DictionaryContaining({
308-
"function_name": "chase_into_tree",
309-
"function_region": "dogpark",
310-
"function_project": "SquirrelChasing",
311-
}),
312-
"gcp_event": {
313-
"type": "chase",
314-
"chasers": ["Maisey", "Charlie"],
315-
"num_squirrels": 2,
316-
},
307+
"faas.name": "chase_into_tree",
308+
"faas.region": "dogpark",
309+
"gcp.function.identity": "func_ID",
310+
"gcp.function.entry_point": "cloud_function",
311+
"gcp.function.project": "SquirrelChasing",
312+
"cloud.provider": "gcp",
317313
})
318314
)
319315
except AssertionError:

0 commit comments

Comments
 (0)