Skip to content

Commit 7298b08

Browse files
committed
chore(gcp): Remove transaction-based tracing
1 parent 7a758b9 commit 7298b08

2 files changed

Lines changed: 34 additions & 209 deletions

File tree

‎sentry_sdk/integrations/gcp.py‎

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,13 @@
66
from typing import TYPE_CHECKING
77

88
import sentry_sdk
9-
from sentry_sdk.api import continue_trace
109
from sentry_sdk.consts import OP
1110
from sentry_sdk.data_collection import _apply_data_collection_filtering_to_query_string
1211
from sentry_sdk.integrations import Integration
1312
from sentry_sdk.integrations._wsgi_common import _filter_headers
1413
from sentry_sdk.integrations.cloud_resource_context import CLOUD_PROVIDER
1514
from sentry_sdk.scope import Scope, should_send_default_pii
1615
from sentry_sdk.traces import SegmentNameSource
17-
from sentry_sdk.tracing import TransactionSource
18-
from sentry_sdk.tracing_utils import has_span_streaming_enabled
1916
from sentry_sdk.utils import (
2017
AnnotatedValue,
2118
TimeoutThread,
@@ -140,36 +137,22 @@ def sentry_func(
140137
if environ.get("ENTRY_POINT"):
141138
additional_attributes["faas.entry_point"] = environ.get("ENTRY_POINT")
142139

143-
if has_span_streaming_enabled(client.options):
144-
sentry_sdk.traces.continue_trace(headers)
145-
Scope.set_custom_sampling_context(sampling_context)
146-
span_ctx = sentry_sdk.traces.start_span(
147-
name=function_name,
148-
parent_span=None,
149-
attributes={
150-
"sentry.op": OP.FUNCTION_GCP,
151-
"sentry.origin": GcpIntegration.origin,
152-
"sentry.segment.name.source": SegmentNameSource.COMPONENT,
153-
"cloud.provider": CLOUD_PROVIDER.GCP,
154-
"faas.name": function_name,
155-
**header_attributes,
156-
**additional_attributes,
157-
},
158-
)
159-
else:
160-
transaction = continue_trace(
161-
headers,
162-
op=OP.FUNCTION_GCP,
163-
name=environ.get("FUNCTION_NAME", ""),
164-
source=TransactionSource.COMPONENT,
165-
origin=GcpIntegration.origin,
166-
)
167-
168-
span_ctx = sentry_sdk.start_transaction(
169-
transaction, custom_sampling_context=sampling_context
170-
)
171-
172-
with span_ctx:
140+
sentry_sdk.traces.continue_trace(headers)
141+
Scope.set_custom_sampling_context(sampling_context)
142+
143+
with sentry_sdk.traces.start_span(
144+
name=function_name,
145+
parent_span=None,
146+
attributes={
147+
"sentry.op": OP.FUNCTION_GCP,
148+
"sentry.origin": GcpIntegration.origin,
149+
"sentry.segment.name.source": SegmentNameSource.COMPONENT,
150+
"cloud.provider": CLOUD_PROVIDER.GCP,
151+
"faas.name": function_name,
152+
**header_attributes,
153+
**additional_attributes,
154+
},
155+
):
173156
try:
174157
return func(functionhandler, gcp_event, *args, **kwargs)
175158
except Exception:

‎tests/integrations/gcp/test_gcp.py‎

Lines changed: 18 additions & 176 deletions
Original file line numberDiff line numberDiff line change
@@ -238,65 +238,6 @@ def cloud_function(functionhandler, event):
238238
assert envelope_items[0]["tags"]["cloud_function"] == "true"
239239

240240

241-
def test_performance_no_error(run_cloud_function):
242-
envelope_items, _, _ = run_cloud_function(
243-
dedent(
244-
"""
245-
functionhandler = None
246-
event = {}
247-
def cloud_function(functionhandler, event):
248-
return "test_string"
249-
"""
250-
)
251-
+ FUNCTIONS_PRELUDE
252-
+ dedent(
253-
"""
254-
init_sdk(traces_sample_rate=1.0)
255-
gcp_functions.worker_v1.FunctionHandler.invoke_user_function(functionhandler, event)
256-
"""
257-
)
258-
)
259-
260-
assert envelope_items[0]["type"] == "transaction"
261-
assert envelope_items[0]["contexts"]["trace"]["op"] == "function.gcp"
262-
assert envelope_items[0]["transaction"].startswith("Google Cloud function")
263-
assert envelope_items[0]["transaction_info"] == {"source": "component"}
264-
assert envelope_items[0]["transaction"] in envelope_items[0]["request"]["url"]
265-
266-
267-
def test_performance_error(run_cloud_function):
268-
envelope_items, _, _ = run_cloud_function(
269-
dedent(
270-
"""
271-
functionhandler = None
272-
event = {}
273-
def cloud_function(functionhandler, event):
274-
raise Exception("something went wrong")
275-
"""
276-
)
277-
+ FUNCTIONS_PRELUDE
278-
+ dedent(
279-
"""
280-
init_sdk(traces_sample_rate=1.0)
281-
gcp_functions.worker_v1.FunctionHandler.invoke_user_function(functionhandler, event)
282-
"""
283-
)
284-
)
285-
286-
assert envelope_items[0]["level"] == "error"
287-
(exception,) = envelope_items[0]["exception"]["values"]
288-
289-
assert exception["type"] == "Exception"
290-
assert exception["value"] == "something went wrong"
291-
assert exception["mechanism"]["type"] == "gcp"
292-
assert not exception["mechanism"]["handled"]
293-
294-
assert envelope_items[1]["type"] == "transaction"
295-
assert envelope_items[1]["contexts"]["trace"]["op"] == "function.gcp"
296-
assert envelope_items[1]["transaction"].startswith("Google Cloud function")
297-
assert envelope_items[1]["transaction"] in envelope_items[0]["request"]["url"]
298-
299-
300241
def test_traces_sampler_gets_correct_values_in_sampling_context(
301242
run_cloud_function,
302243
DictionaryContaining, # noqa:N803
@@ -367,6 +308,7 @@ def _safe_is_equal(x, y):
367308
368309
init_sdk(
369310
traces_sampler=traces_sampler,
311+
trace_lifecycle="stream",
370312
)
371313
372314
gcp_functions.worker_v1.FunctionHandler.invoke_user_function(functionhandler, event)
@@ -379,9 +321,9 @@ def _safe_is_equal(x, y):
379321

380322
def test_error_has_new_trace_context_performance_enabled(run_cloud_function):
381323
"""
382-
Check if an 'trace' context is added to errros and transactions when performance monitoring is enabled.
324+
Check if a 'trace' context is added to errors and spans when performance monitoring is enabled.
383325
"""
384-
envelope_items, _, _ = run_cloud_function(
326+
envelope_items, _, span_items = run_cloud_function(
385327
dedent(
386328
"""
387329
functionhandler = None
@@ -395,26 +337,26 @@ def cloud_function(functionhandler, event):
395337
+ FUNCTIONS_PRELUDE
396338
+ dedent(
397339
"""
398-
init_sdk(traces_sample_rate=1.0)
340+
init_sdk(traces_sample_rate=1.0, trace_lifecycle="stream")
399341
gcp_functions.worker_v1.FunctionHandler.invoke_user_function(functionhandler, event)
400342
"""
401343
)
402344
)
403-
(msg_event, error_event, transaction_event) = envelope_items
345+
(msg_event, error_event) = envelope_items
404346

405347
assert "trace" in msg_event["contexts"]
406348
assert "trace_id" in msg_event["contexts"]["trace"]
407349

408350
assert "trace" in error_event["contexts"]
409351
assert "trace_id" in error_event["contexts"]["trace"]
410352

411-
assert "trace" in transaction_event["contexts"]
412-
assert "trace_id" in transaction_event["contexts"]["trace"]
353+
assert len(span_items) == 1
354+
segment_span = span_items[0]
413355

414356
assert (
415357
msg_event["contexts"]["trace"]["trace_id"]
416358
== error_event["contexts"]["trace"]["trace_id"]
417-
== transaction_event["contexts"]["trace"]["trace_id"]
359+
== segment_span["trace_id"]
418360
)
419361

420362

@@ -456,59 +398,6 @@ def cloud_function(functionhandler, event):
456398
)
457399

458400

459-
def test_error_has_existing_trace_context_performance_enabled(run_cloud_function):
460-
"""
461-
Check if an 'trace' context is added to errros and transactions
462-
from the incoming 'sentry-trace' header when performance monitoring is enabled.
463-
"""
464-
trace_id = "471a43a4192642f0b136d5159a501701"
465-
parent_span_id = "6e8f22c393e68f19"
466-
parent_sampled = 1
467-
sentry_trace_header = "{}-{}-{}".format(trace_id, parent_span_id, parent_sampled)
468-
469-
envelope_items, _, _ = run_cloud_function(
470-
dedent(
471-
"""
472-
functionhandler = None
473-
474-
from collections import namedtuple
475-
GCPEvent = namedtuple("GCPEvent", ["headers"])
476-
event = GCPEvent(headers={"sentry-trace": "%s"})
477-
478-
def cloud_function(functionhandler, event):
479-
sentry_sdk.capture_message("hi")
480-
x = 3/0
481-
return "3"
482-
"""
483-
% sentry_trace_header
484-
)
485-
+ FUNCTIONS_PRELUDE
486-
+ dedent(
487-
"""
488-
init_sdk(traces_sample_rate=1.0)
489-
gcp_functions.worker_v1.FunctionHandler.invoke_user_function(functionhandler, event)
490-
"""
491-
)
492-
)
493-
(msg_event, error_event, transaction_event) = envelope_items
494-
495-
assert "trace" in msg_event["contexts"]
496-
assert "trace_id" in msg_event["contexts"]["trace"]
497-
498-
assert "trace" in error_event["contexts"]
499-
assert "trace_id" in error_event["contexts"]["trace"]
500-
501-
assert "trace" in transaction_event["contexts"]
502-
assert "trace_id" in transaction_event["contexts"]["trace"]
503-
504-
assert (
505-
msg_event["contexts"]["trace"]["trace_id"]
506-
== error_event["contexts"]["trace"]["trace_id"]
507-
== transaction_event["contexts"]["trace"]["trace_id"]
508-
== "471a43a4192642f0b136d5159a501701"
509-
)
510-
511-
512401
def test_error_has_existing_trace_context_performance_disabled(run_cloud_function):
513402
"""
514403
Check if an 'trace' context is added to errros and transactions
@@ -559,7 +448,7 @@ def cloud_function(functionhandler, event):
559448

560449

561450
def test_span_origin(run_cloud_function):
562-
events, _, _ = run_cloud_function(
451+
_, _, span_items = run_cloud_function(
563452
dedent(
564453
"""
565454
functionhandler = None
@@ -571,18 +460,17 @@ def cloud_function(functionhandler, event):
571460
+ FUNCTIONS_PRELUDE
572461
+ dedent(
573462
"""
574-
init_sdk(traces_sample_rate=1.0)
463+
init_sdk(traces_sample_rate=1.0, trace_lifecycle="stream")
575464
gcp_functions.worker_v1.FunctionHandler.invoke_user_function(functionhandler, event)
576465
"""
577466
)
578467
)
579468

580-
(event,) = events
581-
582-
assert event["contexts"]["trace"]["origin"] == "auto.function.gcp"
469+
assert len(span_items) == 1
470+
assert span_items[0]["attributes"]["sentry.origin"] == "auto.function.gcp"
583471

584472

585-
def test_span_streaming_no_error(run_cloud_function):
473+
def test_performance_no_error(run_cloud_function):
586474
_, _, span_items = run_cloud_function(
587475
dedent(
588476
"""
@@ -616,7 +504,7 @@ def cloud_function(functionhandler, event):
616504
assert segment_span["attributes"]["faas.entry_point"] == "cloud_function"
617505

618506

619-
def test_span_streaming_error(run_cloud_function):
507+
def test_performance_error(run_cloud_function):
620508
envelope_items, _, span_items = run_cloud_function(
621509
dedent(
622510
"""
@@ -657,7 +545,7 @@ def cloud_function(functionhandler, event):
657545
assert segment_span["status"] == SpanStatus.ERROR
658546

659547

660-
def test_span_streaming_existing_trace_context(run_cloud_function):
548+
def test_existing_trace_context(run_cloud_function):
661549
trace_id = "471a43a4192642f0b136d5159a501701"
662550
parent_span_id = "6e8f22c393e68f19"
663551
parent_sampled = 1
@@ -705,7 +593,7 @@ def cloud_function(functionhandler, event):
705593
assert segment_span["attributes"]["faas.entry_point"] == "cloud_function"
706594

707595

708-
def test_span_streaming_request_attributes(run_cloud_function):
596+
def test_request_attributes(run_cloud_function):
709597
_, _, span_items = run_cloud_function(
710598
dedent(
711599
"""
@@ -746,7 +634,7 @@ def cloud_function(functionhandler, event):
746634
assert attrs["faas.entry_point"] == "cloud_function"
747635

748636

749-
def test_span_streaming_no_query_string_without_pii(run_cloud_function):
637+
def test_no_query_string_without_pii(run_cloud_function):
750638
_, _, span_items = run_cloud_function(
751639
dedent(
752640
"""
@@ -863,53 +751,7 @@ def _build_init_kwargs(send_default_pii, data_collection):
863751
"send_default_pii, data_collection, expected_event, expected_span",
864752
_QUERY_STRING_DATA_COLLECTION_CASES,
865753
)
866-
def test_query_string_data_collection_event_processor(
867-
run_cloud_function,
868-
send_default_pii,
869-
data_collection,
870-
expected_event,
871-
expected_span,
872-
):
873-
init_kwargs = _build_init_kwargs(send_default_pii, data_collection)
874-
envelope_items, _, _ = run_cloud_function(
875-
dedent(
876-
"""
877-
functionhandler = None
878-
879-
from collections import namedtuple
880-
GCPEvent = namedtuple("GCPEvent", ["headers", "method", "query_string"])
881-
event = GCPEvent(
882-
headers={},
883-
method="GET",
884-
query_string=b"toy=tennisball&color=red&auth=secret",
885-
)
886-
887-
def cloud_function(functionhandler, event):
888-
raise Exception("something went wrong")
889-
"""
890-
)
891-
+ FUNCTIONS_PRELUDE
892-
+ dedent(
893-
"""
894-
init_sdk(%s)
895-
gcp_functions.worker_v1.FunctionHandler.invoke_user_function(functionhandler, event)
896-
"""
897-
% init_kwargs
898-
)
899-
)
900-
901-
request = envelope_items[0]["request"]
902-
if expected_event is None:
903-
assert "query_string" not in request
904-
else:
905-
assert request["query_string"] == expected_event
906-
907-
908-
@pytest.mark.parametrize(
909-
"send_default_pii, data_collection, expected_event, expected_span",
910-
_QUERY_STRING_DATA_COLLECTION_CASES,
911-
)
912-
def test_query_string_data_collection_span_streaming(
754+
def test_query_string_data_collection(
913755
run_cloud_function,
914756
send_default_pii,
915757
data_collection,

0 commit comments

Comments
 (0)