@@ -360,7 +360,7 @@ def _safe_is_equal(x, y):
360360
361361def test_error_has_new_trace_context_performance_enabled (run_cloud_function ):
362362 """
363- Check if an 'trace' context is added to errros and transactions when performance monitoring is enabled.
363+ Check if an 'trace' context is added to errors and transactions when performance monitoring is enabled.
364364 """
365365 envelope_items , _ = run_cloud_function (
366366 dedent (
@@ -401,7 +401,7 @@ def cloud_function(functionhandler, event):
401401
402402def test_error_has_new_trace_context_performance_disabled (run_cloud_function ):
403403 """
404- Check if an 'trace' context is added to errros and transactions when performance monitoring is disabled.
404+ Check if an 'trace' context is added to errors and transactions when performance monitoring is disabled.
405405 """
406406 envelope_items , _ = run_cloud_function (
407407 dedent (
@@ -439,13 +439,123 @@ def cloud_function(functionhandler, event):
439439
440440def test_error_has_existing_trace_context_performance_enabled (run_cloud_function ):
441441 """
442- Check if an 'trace' context is added to errros and transactions
442+ Check if an 'trace' context is added to errors and transactions
443443 from the incoming 'sentry-trace' header when performance monitoring is enabled.
444444 """
445445 trace_id = "471a43a4192642f0b136d5159a501701"
446446 parent_span_id = "6e8f22c393e68f19"
447447 parent_sampled = 1
448448 sentry_trace_header = "{}-{}-{}" .format (trace_id , parent_span_id , parent_sampled )
449+ w3c_trace_header = "00-971a43a4192642f0b136d5159a501701-6e8f22c393e68f19-00"
450+
451+ # If both sentry-trace and traceparent headers are present, sentry-trace takes precedence.
452+ # See: https://github.com/getsentry/team-sdks/issues/41
453+
454+ envelope_items , _ = run_cloud_function (
455+ dedent (
456+ """
457+ functionhandler = None
458+
459+ from collections import namedtuple
460+ GCPEvent = namedtuple("GCPEvent", ["headers"])
461+ event = GCPEvent(headers={"sentry-trace": "%s", "traceparent": "%s"})
462+
463+ def cloud_function(functionhandler, event):
464+ sentry_sdk.capture_message("hi")
465+ x = 3/0
466+ return "3"
467+ """
468+ % sentry_trace_header ,
469+ w3c_trace_header ,
470+ )
471+ + FUNCTIONS_PRELUDE
472+ + dedent (
473+ """
474+ init_sdk(traces_sample_rate=1.0)
475+ gcp_functions.worker_v1.FunctionHandler.invoke_user_function(functionhandler, event)
476+ """
477+ )
478+ )
479+ (msg_event , error_event , transaction_event ) = envelope_items
480+
481+ assert "trace" in msg_event ["contexts" ]
482+ assert "trace_id" in msg_event ["contexts" ]["trace" ]
483+
484+ assert "trace" in error_event ["contexts" ]
485+ assert "trace_id" in error_event ["contexts" ]["trace" ]
486+
487+ assert "trace" in transaction_event ["contexts" ]
488+ assert "trace_id" in transaction_event ["contexts" ]["trace" ]
489+
490+ assert (
491+ msg_event ["contexts" ]["trace" ]["trace_id" ]
492+ == error_event ["contexts" ]["trace" ]["trace_id" ]
493+ == transaction_event ["contexts" ]["trace" ]["trace_id" ]
494+ == "471a43a4192642f0b136d5159a501701"
495+ )
496+
497+
498+ def test_error_has_existing_w3c_trace_context_performance_disabled (run_cloud_function ):
499+ """
500+ Check if an 'trace' context is added to errors and transactions
501+ from the incoming 'traceparent' header when performance monitoring is disabled.
502+ """
503+ trace_id = "471a43a4192642f0b136d5159a501701"
504+ parent_span_id = "6e8f22c393e68f19"
505+ parent_sampled = "01"
506+ w3c_trace_header = "00-{}-{}-{}" .format (trace_id , parent_span_id , parent_sampled )
507+
508+ # If both sentry-trace and traceparent headers are present, sentry-trace takes precedence.
509+ # See: https://github.com/getsentry/team-sdks/issues/41
510+
511+ envelope_items , _ = run_cloud_function (
512+ dedent (
513+ """
514+ functionhandler = None
515+
516+ from collections import namedtuple
517+ GCPEvent = namedtuple("GCPEvent", ["headers"])
518+ event = GCPEvent(headers={"traceparent": "%s"})
519+
520+ def cloud_function(functionhandler, event):
521+ sentry_sdk.capture_message("hi")
522+ x = 3/0
523+ return "3"
524+ """
525+ % w3c_trace_header
526+ )
527+ + FUNCTIONS_PRELUDE
528+ + dedent (
529+ """
530+ init_sdk(traces_sample_rate=None), # this is the default, just added for clarity
531+ gcp_functions.worker_v1.FunctionHandler.invoke_user_function(functionhandler, event)
532+ """
533+ )
534+ )
535+ (msg_event , error_event ) = envelope_items
536+
537+ assert "trace" in msg_event ["contexts" ]
538+ assert "trace_id" in msg_event ["contexts" ]["trace" ]
539+
540+ assert "trace" in error_event ["contexts" ]
541+ assert "trace_id" in error_event ["contexts" ]["trace" ]
542+
543+ assert (
544+ msg_event ["contexts" ]["trace" ]["trace_id" ]
545+ == error_event ["contexts" ]["trace" ]["trace_id" ]
546+ == "471a43a4192642f0b136d5159a501701"
547+ )
548+
549+
550+ def test_error_has_existing_w3c_trace_context_performance_enabled (run_cloud_function ):
551+ """
552+ Check if an 'trace' context is added to errors and transactions
553+ from the incoming 'traceparent' header when performance monitoring is enabled.
554+ """
555+ trace_id = "471a43a4192642f0b136d5159a501701"
556+ parent_span_id = "6e8f22c393e68f19"
557+ parent_sampled = "01"
558+ w3c_trace_header = "00-{}-{}-{}" .format (trace_id , parent_span_id , parent_sampled )
449559
450560 envelope_items , _ = run_cloud_function (
451561 dedent (
@@ -454,14 +564,14 @@ def test_error_has_existing_trace_context_performance_enabled(run_cloud_function
454564
455565 from collections import namedtuple
456566 GCPEvent = namedtuple("GCPEvent", ["headers"])
457- event = GCPEvent(headers={"sentry-trace ": "%s"})
567+ event = GCPEvent(headers={"traceparent ": "%s"})
458568
459569 def cloud_function(functionhandler, event):
460570 sentry_sdk.capture_message("hi")
461571 x = 3/0
462572 return "3"
463573 """
464- % sentry_trace_header
574+ % w3c_trace_header
465575 )
466576 + FUNCTIONS_PRELUDE
467577 + dedent (
@@ -492,13 +602,13 @@ def cloud_function(functionhandler, event):
492602
493603def test_error_has_existing_trace_context_performance_disabled (run_cloud_function ):
494604 """
495- Check if an 'trace' context is added to errros and transactions
605+ Check if an 'trace' context is added to errors and transactions
496606 from the incoming 'sentry-trace' header when performance monitoring is disabled.
497607 """
498608 trace_id = "471a43a4192642f0b136d5159a501701"
499609 parent_span_id = "6e8f22c393e68f19"
500- parent_sampled = 1
501- sentry_trace_header = "{}-{}-{}" .format (trace_id , parent_span_id , parent_sampled )
610+ parent_sampled = "01"
611+ w3c_trace_header = "00- {}-{}-{}" .format (trace_id , parent_span_id , parent_sampled )
502612
503613 envelope_items , _ = run_cloud_function (
504614 dedent (
@@ -507,14 +617,14 @@ def test_error_has_existing_trace_context_performance_disabled(run_cloud_functio
507617
508618 from collections import namedtuple
509619 GCPEvent = namedtuple("GCPEvent", ["headers"])
510- event = GCPEvent(headers={"sentry-trace ": "%s"})
620+ event = GCPEvent(headers={"traceparent ": "%s"})
511621
512622 def cloud_function(functionhandler, event):
513623 sentry_sdk.capture_message("hi")
514624 x = 3/0
515625 return "3"
516626 """
517- % sentry_trace_header
627+ % w3c_trace_header
518628 )
519629 + FUNCTIONS_PRELUDE
520630 + dedent (
0 commit comments