|
| 1 | +import pytest |
1 | 2 | from sentry_sdk import start_span
|
2 | 3 |
|
3 | 4 |
|
@@ -36,3 +37,118 @@ def test_span_origin_custom(sentry_init, capture_events):
|
36 | 37 |
|
37 | 38 | assert second_transaction["contexts"]["trace"]["origin"] == "ho.ho2.ho3"
|
38 | 39 | assert second_transaction["spans"][0]["origin"] == "baz.baz2.baz3"
|
| 40 | + |
| 41 | + |
| 42 | +@pytest.mark.parametrize("excluded_origins", [None, [], "noop"]) |
| 43 | +def test_exclude_span_origins_empty(sentry_init, capture_events, excluded_origins): |
| 44 | + if excluded_origins in (None, []): |
| 45 | + sentry_init(traces_sample_rate=1.0, exclude_span_origins=excluded_origins) |
| 46 | + elif excluded_origins == "noop": |
| 47 | + sentry_init( |
| 48 | + traces_sample_rate=1.0, |
| 49 | + # default is None |
| 50 | + ) |
| 51 | + |
| 52 | + events = capture_events() |
| 53 | + |
| 54 | + with start_span(name="span1"): |
| 55 | + pass |
| 56 | + with start_span(name="span2", origin="auto.http.requests"): |
| 57 | + pass |
| 58 | + with start_span(name="span3", origin="auto.db.postgres"): |
| 59 | + pass |
| 60 | + |
| 61 | + assert len(events) == 3 |
| 62 | + |
| 63 | + |
| 64 | +@pytest.mark.parametrize( |
| 65 | + "excluded_origins,origins,expected_allowed_origins", |
| 66 | + [ |
| 67 | + # Regexes |
| 68 | + ( |
| 69 | + [r"auto\.http\..*", r"auto\.db\..*"], |
| 70 | + [ |
| 71 | + "auto.http.requests", |
| 72 | + "auto.db.sqlite", |
| 73 | + "manual", |
| 74 | + ], |
| 75 | + ["manual"], |
| 76 | + ), |
| 77 | + # Substring matching |
| 78 | + ( |
| 79 | + ["http"], |
| 80 | + [ |
| 81 | + "auto.http.requests", |
| 82 | + "http.client", |
| 83 | + "my.http.integration", |
| 84 | + "manual", |
| 85 | + "auto.db.postgres", |
| 86 | + ], |
| 87 | + ["manual", "auto.db.postgres"], |
| 88 | + ), |
| 89 | + # Mix and match |
| 90 | + ( |
| 91 | + ["manual", r"auto\.http\..*", "db"], |
| 92 | + [ |
| 93 | + "manual", |
| 94 | + "auto.http.requests", |
| 95 | + "auto.db.postgres", |
| 96 | + "auto.grpc.server", |
| 97 | + ], |
| 98 | + ["auto.grpc.server"], |
| 99 | + ), |
| 100 | + ], |
| 101 | +) |
| 102 | +def test_exclude_span_origins_patterns( |
| 103 | + sentry_init, |
| 104 | + capture_events, |
| 105 | + excluded_origins, |
| 106 | + origins, |
| 107 | + expected_allowed_origins, |
| 108 | +): |
| 109 | + sentry_init( |
| 110 | + traces_sample_rate=1.0, |
| 111 | + exclude_span_origins=excluded_origins, |
| 112 | + ) |
| 113 | + |
| 114 | + events = capture_events() |
| 115 | + |
| 116 | + for origin in origins: |
| 117 | + with start_span(name="span", origin=origin): |
| 118 | + pass |
| 119 | + |
| 120 | + assert len(events) == len(expected_allowed_origins) |
| 121 | + |
| 122 | + if len(expected_allowed_origins) > 0: |
| 123 | + captured_origins = {event["contexts"]["trace"]["origin"] for event in events} |
| 124 | + assert captured_origins == set(expected_allowed_origins) |
| 125 | + |
| 126 | + |
| 127 | +def test_exclude_span_origins_with_child_spans(sentry_init, capture_events): |
| 128 | + sentry_init(traces_sample_rate=1.0, exclude_span_origins=[r"auto\.http\..*"]) |
| 129 | + events = capture_events() |
| 130 | + |
| 131 | + with start_span(name="parent", origin="manual"): |
| 132 | + with start_span(name="http-child", origin="auto.http.requests"): |
| 133 | + pass |
| 134 | + with start_span(name="db-child", origin="auto.db.postgres"): |
| 135 | + pass |
| 136 | + |
| 137 | + assert len(events) == 1 |
| 138 | + assert events[0]["contexts"]["trace"]["origin"] == "manual" |
| 139 | + assert len(events[0]["spans"]) == 1 |
| 140 | + assert events[0]["spans"][0]["origin"] == "auto.db.postgres" |
| 141 | + |
| 142 | + |
| 143 | +def test_exclude_span_origins_parent_with_child_spans(sentry_init, capture_events): |
| 144 | + sentry_init(traces_sample_rate=1.0, exclude_span_origins=[r"auto\.http\..*"]) |
| 145 | + events = capture_events() |
| 146 | + |
| 147 | + with start_span(name="parent", origin="auto.http.requests"): |
| 148 | + with start_span( |
| 149 | + name="db-child", origin="auto.db.postgres", only_if_parent=True |
| 150 | + ): |
| 151 | + # Note: without only_if_parent, the child span would be promoted to a transaction |
| 152 | + pass |
| 153 | + |
| 154 | + assert len(events) == 0 |
0 commit comments