Skip to content
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
80 changes: 68 additions & 12 deletions sentry_sdk/integrations/gcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
from sentry_sdk.consts import OP
from sentry_sdk.integrations import Integration
from sentry_sdk.integrations._wsgi_common import _filter_headers
from sentry_sdk.scope import should_send_default_pii
from sentry_sdk.integrations.cloud_resource_context import CLOUD_PROVIDER
from sentry_sdk.scope import Scope, should_send_default_pii
from sentry_sdk.traces import SegmentSource
from sentry_sdk.tracing import TransactionSource
from sentry_sdk.tracing_utils import has_span_streaming_enabled
from sentry_sdk.utils import (
AnnotatedValue,
TimeoutThread,
Expand Down Expand Up @@ -82,16 +85,26 @@ def sentry_func(
timeout_thread.start()

headers = {}
header_attributes: "dict[str, Any]" = {}
if hasattr(gcp_event, "headers"):
headers = gcp_event.headers
for header, header_value in _filter_headers(
headers, use_annotated_value=False
).items():
header_attributes[f"http.request.header.{header.lower()}"] = (
# header_value will always be a string because we set `use_annotated_value` to false above
header_value
)

additional_attributes = {}
if hasattr(gcp_event, "method"):
additional_attributes["http.request.method"] = gcp_event.method

if should_send_default_pii() and hasattr(gcp_event, "query_string"):
additional_attributes["url.query"] = gcp_event.query_string.decode(
"utf-8", errors="replace"
)
Comment thread
ericapisani marked this conversation as resolved.

transaction = continue_trace(
headers,
op=OP.FUNCTION_GCP,
name=environ.get("FUNCTION_NAME", ""),
source=TransactionSource.COMPONENT,
origin=GcpIntegration.origin,
)
sampling_context = {
"gcp_env": {
"function_name": environ.get("FUNCTION_NAME"),
Expand All @@ -102,9 +115,50 @@ def sentry_func(
},
"gcp_event": gcp_event,
}
with sentry_sdk.start_transaction(
transaction, custom_sampling_context=sampling_context
):

function_name = environ.get("FUNCTION_NAME", "<unknown GCP function>")

if environ.get("GCP_PROJECT"):
additional_attributes["gcp.project.id"] = environ.get("GCP_PROJECT")

if environ.get("FUNCTION_IDENTITY"):
additional_attributes["faas.identity"] = environ.get(
"FUNCTION_IDENTITY"
)

if environ.get("ENTRY_POINT"):
additional_attributes["faas.entry_point"] = environ.get("ENTRY_POINT")

if has_span_streaming_enabled(client.options):
sentry_sdk.traces.continue_trace(headers)
Scope.set_custom_sampling_context(sampling_context)
span_ctx = sentry_sdk.traces.start_span(
name=function_name,
parent_span=None,
attributes={
"sentry.op": OP.FUNCTION_GCP,
"sentry.origin": GcpIntegration.origin,
"sentry.span.source": SegmentSource.COMPONENT,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent enum value usage for span attribute

Low Severity

SegmentSource.COMPONENT is passed directly as an attribute value, while the equivalent code in the aiohttp integration uses SegmentSource.ROUTE.value to extract the plain string. While SegmentSource is a str enum so both work today, passing the enum member rather than .value is inconsistent with the established pattern and could behave unexpectedly if serialization logic changes.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit c3bbc45. Configure here.

"cloud.provider": CLOUD_PROVIDER.GCP,
"faas.name": function_name,
**header_attributes,
**additional_attributes,
},
)
else:
transaction = continue_trace(
headers,
op=OP.FUNCTION_GCP,
name=environ.get("FUNCTION_NAME", ""),
source=TransactionSource.COMPONENT,
origin=GcpIntegration.origin,
)

span_ctx = sentry_sdk.start_transaction(
transaction, custom_sampling_context=sampling_context
)

with span_ctx:
try:
return func(functionhandler, gcp_event, *args, **kwargs)
except Exception:
Expand Down Expand Up @@ -181,7 +235,9 @@ def event_processor(event: "Event", hint: "Hint") -> "Optional[Event]":
request["method"] = gcp_event.method

if hasattr(gcp_event, "query_string"):
request["query_string"] = gcp_event.query_string.decode("utf-8")
request["query_string"] = gcp_event.query_string.decode(
"utf-8", errors="replace"
)

if hasattr(gcp_event, "headers"):
request["headers"] = _filter_headers(gcp_event.headers)
Expand Down
Loading
Loading