1313import pytest
1414
1515import sentry_sdk
16- import sentry_sdk .client
16+ from sentry_sdk .transport import Transport
17+ from sentry_sdk .envelope import Envelope
18+
19+
20+ class TransportWithOptions (Transport ):
21+ """Transport does not pass in the options so we need this here"""
22+
23+ def __init__ (self , options = None ):
24+ Transport .__init__ (self , options )
25+
26+ def capture_envelope (self , _ : Envelope ) -> None :
27+ """No-op capture_envelope for tests"""
28+ pass
1729
1830
1931def test_dsc_head_of_trace (sentry_init , capture_envelopes ):
@@ -22,10 +34,11 @@ def test_dsc_head_of_trace(sentry_init, capture_envelopes):
2234 and sends a transaction event to Sentry.
2335 """
2436 sentry_init (
25- dsn = "https://mysecret@bla .ingest.sentry.io/12312012" ,
37+ dsn = "https://mysecret@o1234 .ingest.sentry.io/12312012" ,
26382739 environment = "canary" ,
2840 traces_sample_rate = 1.0 ,
41+ transport = TransportWithOptions ,
2942 )
3043 envelopes = capture_envelopes ()
3144
@@ -45,6 +58,10 @@ def test_dsc_head_of_trace(sentry_init, capture_envelopes):
4558 assert type (envelope_trace_header ["public_key" ]) == str
4659 assert envelope_trace_header ["public_key" ] == "mysecret"
4760
61+ assert "org_id" in envelope_trace_header
62+ assert type (envelope_trace_header ["org_id" ]) == str
63+ assert envelope_trace_header ["org_id" ] == "1234"
64+
4865 assert "sample_rate" in envelope_trace_header
4966 assert type (envelope_trace_header ["sample_rate" ]) == str
5067 assert envelope_trace_header ["sample_rate" ] == "1.0"
@@ -66,16 +83,46 @@ def test_dsc_head_of_trace(sentry_init, capture_envelopes):
6683 assert envelope_trace_header ["transaction" ] == "foo"
6784
6885
86+ def test_dsc_head_of_trace_uses_custom_org_id (sentry_init , capture_envelopes ):
87+ """
88+ Our service is the head of the trace (it starts a new trace)
89+ and sends a transaction event to Sentry.
90+ """
91+ sentry_init (
92+ dsn = "https://[email protected] /12312012" ,
93+ org_id = "9999" ,
94+ 95+ environment = "canary" ,
96+ traces_sample_rate = 1.0 ,
97+ transport = TransportWithOptions ,
98+ )
99+ envelopes = capture_envelopes ()
100+
101+ # We start a new transaction
102+ with sentry_sdk .start_transaction (name = "foo" ):
103+ pass
104+
105+ assert len (envelopes ) == 1
106+
107+ transaction_envelope = envelopes [0 ]
108+ envelope_trace_header = transaction_envelope .headers ["trace" ]
109+
110+ assert "org_id" in envelope_trace_header
111+ assert type (envelope_trace_header ["org_id" ]) == str
112+ assert envelope_trace_header ["org_id" ] == "9999"
113+
114+
69115def test_dsc_continuation_of_trace (sentry_init , capture_envelopes ):
70116 """
71117 Another service calls our service and passes tracing information to us.
72118 Our service is continuing the trace and sends a transaction event to Sentry.
73119 """
74120 sentry_init (
75- dsn = "https://mysecret@bla .ingest.sentry.io/12312012" ,
121+ dsn = "https://mysecret@o1234 .ingest.sentry.io/12312012" ,
7612277123 environment = "canary" ,
78124 traces_sample_rate = 1.0 ,
125+ transport = TransportWithOptions ,
79126 )
80127 envelopes = capture_envelopes ()
81128
@@ -149,10 +196,11 @@ def my_traces_sampler(sampling_context):
149196 return 0.25
150197
151198 sentry_init (
152- dsn = "https://mysecret@bla .ingest.sentry.io/12312012" ,
199+ dsn = "https://mysecret@o1234 .ingest.sentry.io/12312012" ,
153200154201 environment = "canary" ,
155202 traces_sampler = my_traces_sampler ,
203+ transport = TransportWithOptions ,
156204 )
157205 envelopes = capture_envelopes ()
158206
@@ -219,9 +267,10 @@ def test_dsc_issue(sentry_init, capture_envelopes):
219267 Our service is a standalone service that does not have tracing enabled. Just uses Sentry for error reporting.
220268 """
221269 sentry_init (
222- dsn = "https://mysecret@bla .ingest.sentry.io/12312012" ,
270+ dsn = "https://mysecret@o1234 .ingest.sentry.io/12312012" ,
223271224272 environment = "canary" ,
273+ transport = TransportWithOptions ,
225274 )
226275 envelopes = capture_envelopes ()
227276
@@ -244,6 +293,10 @@ def test_dsc_issue(sentry_init, capture_envelopes):
244293 assert type (envelope_trace_header ["public_key" ]) == str
245294 assert envelope_trace_header ["public_key" ] == "mysecret"
246295
296+ assert "org_id" in envelope_trace_header
297+ assert type (envelope_trace_header ["org_id" ]) == str
298+ assert envelope_trace_header ["org_id" ] == "1234"
299+
247300 assert "sample_rate" not in envelope_trace_header
248301
249302 assert "sampled" not in envelope_trace_header
@@ -265,10 +318,11 @@ def test_dsc_issue_with_tracing(sentry_init, capture_envelopes):
265318 Envelopes containing errors also have the same DSC than the transaction envelopes.
266319 """
267320 sentry_init (
268- dsn = "https://mysecret@bla .ingest.sentry.io/12312012" ,
321+ dsn = "https://mysecret@o1234 .ingest.sentry.io/12312012" ,
269322270323 environment = "canary" ,
271324 traces_sample_rate = 1.0 ,
325+ transport = TransportWithOptions ,
272326 )
273327 envelopes = capture_envelopes ()
274328
@@ -294,6 +348,10 @@ def test_dsc_issue_with_tracing(sentry_init, capture_envelopes):
294348 assert type (envelope_trace_header ["public_key" ]) == str
295349 assert envelope_trace_header ["public_key" ] == "mysecret"
296350
351+ assert "org_id" in envelope_trace_header
352+ assert type (envelope_trace_header ["org_id" ]) == str
353+ assert envelope_trace_header ["org_id" ] == "1234"
354+
297355 assert "sample_rate" in envelope_trace_header
298356 assert envelope_trace_header ["sample_rate" ] == "1.0"
299357 assert type (envelope_trace_header ["sample_rate" ]) == str
@@ -332,10 +390,11 @@ def test_dsc_issue_twp(sentry_init, capture_envelopes, traces_sample_rate):
332390 (This test would be service B in this scenario)
333391 """
334392 sentry_init (
335- dsn = "https://mysecret@bla .ingest.sentry.io/12312012" ,
393+ dsn = "https://mysecret@o1234 .ingest.sentry.io/12312012" ,
336394337395 environment = "canary" ,
338396 traces_sample_rate = traces_sample_rate ,
397+ transport = TransportWithOptions ,
339398 )
340399 envelopes = capture_envelopes ()
341400
0 commit comments