Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use span attrs in GCP sampling context #3818

Merged
merged 6 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions MIGRATION_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion sentry_sdk/integrations/aws_lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
42 changes: 31 additions & 11 deletions sentry_sdk/integrations/gcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
16 changes: 6 additions & 10 deletions tests/integrations/gcp/test_gcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading