|
| 1 | +import pytest |
1 | 2 | from dataclasses import asdict, dataclass |
2 | 3 | from typing import Optional, List |
3 | 4 |
|
4 | | -from sentry_sdk.tracing_utils import _should_be_included, Baggage |
5 | | -import pytest |
| 5 | +from sentry_sdk.tracing_utils import ( |
| 6 | + _should_be_included, |
| 7 | + _should_continue_trace, |
| 8 | + Baggage, |
| 9 | +) |
| 10 | +from tests.conftest import TestTransportWithOptions |
6 | 11 |
|
7 | 12 |
|
8 | 13 | def id_function(val): |
@@ -146,3 +151,130 @@ def test_strip_sentry_baggage(header, expected): |
146 | 151 | ) |
147 | 152 | def test_baggage_repr(baggage, expected_repr): |
148 | 153 | assert repr(baggage) == expected_repr |
| 154 | + |
| 155 | + |
| 156 | +@pytest.mark.parametrize( |
| 157 | + ( |
| 158 | + "baggage_header", |
| 159 | + "dsn", |
| 160 | + "explicit_org_id", |
| 161 | + "strict_trace_continuation", |
| 162 | + "should_continue_trace", |
| 163 | + ), |
| 164 | + ( |
| 165 | + # continue cases when strict_trace_continuation=False |
| 166 | + ( |
| 167 | + "sentry-trace_id=771a43a4192642f0b136d5159a501700, sentry-org_id=1234", |
| 168 | + "https://mysecret@o1234.ingest.sentry.io/12312012", |
| 169 | + None, |
| 170 | + False, |
| 171 | + True, |
| 172 | + ), |
| 173 | + ( |
| 174 | + "sentry-trace_id=771a43a4192642f0b136d5159a501700", |
| 175 | + "https://mysecret@o1234.ingest.sentry.io/12312012", |
| 176 | + None, |
| 177 | + False, |
| 178 | + True, |
| 179 | + ), |
| 180 | + ( |
| 181 | + "sentry-trace_id=771a43a4192642f0b136d5159a501700, sentry-org_id=1234", |
| 182 | + None, |
| 183 | + None, |
| 184 | + False, |
| 185 | + True, |
| 186 | + ), |
| 187 | + ( |
| 188 | + "sentry-trace_id=771a43a4192642f0b136d5159a501700, sentry-org_id=1234", |
| 189 | + None, |
| 190 | + "1234", |
| 191 | + False, |
| 192 | + True, |
| 193 | + ), |
| 194 | + ( |
| 195 | + "sentry-trace_id=771a43a4192642f0b136d5159a501700, sentry-org_id=1234", |
| 196 | + "https://mysecret@not_org_id.ingest.sentry.io/12312012", |
| 197 | + None, |
| 198 | + False, |
| 199 | + True, |
| 200 | + ), |
| 201 | + # start new cases when strict_trace_continuation=False |
| 202 | + ( |
| 203 | + "sentry-trace_id=771a43a4192642f0b136d5159a501700, sentry-org_id=1234", |
| 204 | + "https://mysecret@o9999.ingest.sentry.io/12312012", |
| 205 | + None, |
| 206 | + False, |
| 207 | + False, |
| 208 | + ), |
| 209 | + ( |
| 210 | + "sentry-trace_id=771a43a4192642f0b136d5159a501700, sentry-org_id=1234", |
| 211 | + "https://mysecret@o1234.ingest.sentry.io/12312012", |
| 212 | + "9999", |
| 213 | + False, |
| 214 | + False, |
| 215 | + ), |
| 216 | + # continue cases when strict_trace_continuation=True |
| 217 | + ( |
| 218 | + "sentry-trace_id=771a43a4192642f0b136d5159a501700, sentry-org_id=1234", |
| 219 | + "https://mysecret@o1234.ingest.sentry.io/12312012", |
| 220 | + None, |
| 221 | + True, |
| 222 | + True, |
| 223 | + ), |
| 224 | + ("sentry-trace_id=771a43a4192642f0b136d5159a501700", None, None, True, True), |
| 225 | + # start new cases when strict_trace_continuation=True |
| 226 | + ( |
| 227 | + "sentry-trace_id=771a43a4192642f0b136d5159a501700", |
| 228 | + "https://mysecret@o1234.ingest.sentry.io/12312012", |
| 229 | + None, |
| 230 | + True, |
| 231 | + False, |
| 232 | + ), |
| 233 | + ( |
| 234 | + "sentry-trace_id=771a43a4192642f0b136d5159a501700, sentry-org_id=1234", |
| 235 | + None, |
| 236 | + None, |
| 237 | + True, |
| 238 | + False, |
| 239 | + ), |
| 240 | + ( |
| 241 | + "sentry-trace_id=771a43a4192642f0b136d5159a501700, sentry-org_id=1234", |
| 242 | + "https://mysecret@not_org_id.ingest.sentry.io/12312012", |
| 243 | + None, |
| 244 | + True, |
| 245 | + False, |
| 246 | + ), |
| 247 | + ( |
| 248 | + "sentry-trace_id=771a43a4192642f0b136d5159a501700, sentry-org_id=1234", |
| 249 | + "https://mysecret@o9999.ingest.sentry.io/12312012", |
| 250 | + None, |
| 251 | + True, |
| 252 | + False, |
| 253 | + ), |
| 254 | + ( |
| 255 | + "sentry-trace_id=771a43a4192642f0b136d5159a501700, sentry-org_id=1234", |
| 256 | + "https://mysecret@o1234.ingest.sentry.io/12312012", |
| 257 | + "9999", |
| 258 | + True, |
| 259 | + False, |
| 260 | + ), |
| 261 | + ), |
| 262 | +) |
| 263 | +def test_should_continue_trace( |
| 264 | + sentry_init, |
| 265 | + baggage_header, |
| 266 | + dsn, |
| 267 | + explicit_org_id, |
| 268 | + strict_trace_continuation, |
| 269 | + should_continue_trace, |
| 270 | +): |
| 271 | + sentry_init( |
| 272 | + dsn=dsn, |
| 273 | + org_id=explicit_org_id, |
| 274 | + strict_trace_continuation=strict_trace_continuation, |
| 275 | + traces_sample_rate=1.0, |
| 276 | + transport=TestTransportWithOptions, |
| 277 | + ) |
| 278 | + |
| 279 | + baggage = Baggage.from_incoming_header(baggage_header) if baggage_header else None |
| 280 | + assert _should_continue_trace(baggage) == should_continue_trace |
0 commit comments