|
| 1 | +import pytest |
| 2 | + |
| 3 | +from opentelemetry import trace |
| 4 | + |
| 5 | +import sentry_sdk |
| 6 | + |
| 7 | + |
| 8 | +tracer = trace.get_tracer(__name__) |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture(autouse=True) |
| 12 | +def sentry_init_potel(sentry_init): |
| 13 | + sentry_init( |
| 14 | + traces_sample_rate=1.0, |
| 15 | + _experiments={"otel_powered_performance": True}, |
| 16 | + ) |
| 17 | + |
| 18 | + |
| 19 | +def test_root_span_transaction_payload_started_with_otel_only(capture_envelopes): |
| 20 | + envelopes = capture_envelopes() |
| 21 | + |
| 22 | + with tracer.start_as_current_span("request"): |
| 23 | + pass |
| 24 | + |
| 25 | + (envelope,) = envelopes |
| 26 | + # TODO-neel-potel DSC header |
| 27 | + (item,) = envelope.items |
| 28 | + payload = item.payload.json |
| 29 | + |
| 30 | + assert payload["type"] == "transaction" |
| 31 | + assert payload["transaction"] == "request" |
| 32 | + assert payload["transaction_info"] == {"source": "custom"} |
| 33 | + assert payload["timestamp"] is not None |
| 34 | + assert payload["start_timestamp"] is not None |
| 35 | + |
| 36 | + contexts = payload["contexts"] |
| 37 | + assert "runtime" in contexts |
| 38 | + assert "otel" in contexts |
| 39 | + assert "resource" in contexts["otel"] |
| 40 | + |
| 41 | + trace_context = contexts["trace"] |
| 42 | + assert "trace_id" in trace_context |
| 43 | + assert "span_id" in trace_context |
| 44 | + assert trace_context["origin"] == "auto.otel" |
| 45 | + assert trace_context["op"] == "request" |
| 46 | + assert trace_context["status"] == "ok" |
| 47 | + |
| 48 | + assert payload["spans"] == [] |
| 49 | + |
| 50 | + |
| 51 | +def test_child_span_payload_started_with_otel_only(capture_envelopes): |
| 52 | + envelopes = capture_envelopes() |
| 53 | + |
| 54 | + with tracer.start_as_current_span("request"): |
| 55 | + with tracer.start_as_current_span("db"): |
| 56 | + pass |
| 57 | + |
| 58 | + (envelope,) = envelopes |
| 59 | + (item,) = envelope.items |
| 60 | + payload = item.payload.json |
| 61 | + (span,) = payload["spans"] |
| 62 | + |
| 63 | + assert span["op"] == "db" |
| 64 | + assert span["description"] == "db" |
| 65 | + assert span["origin"] == "auto.otel" |
| 66 | + assert span["status"] == "ok" |
| 67 | + assert span["span_id"] is not None |
| 68 | + assert span["trace_id"] == payload["contexts"]["trace"]["trace_id"] |
| 69 | + assert span["parent_span_id"] == payload["contexts"]["trace"]["span_id"] |
| 70 | + assert span["timestamp"] is not None |
| 71 | + assert span["start_timestamp"] is not None |
| 72 | + |
| 73 | + |
| 74 | +def test_children_span_nesting_started_with_otel_only(capture_envelopes): |
| 75 | + envelopes = capture_envelopes() |
| 76 | + |
| 77 | + with tracer.start_as_current_span("request"): |
| 78 | + with tracer.start_as_current_span("db"): |
| 79 | + with tracer.start_as_current_span("redis"): |
| 80 | + pass |
| 81 | + with tracer.start_as_current_span("http"): |
| 82 | + pass |
| 83 | + |
| 84 | + (envelope,) = envelopes |
| 85 | + (item,) = envelope.items |
| 86 | + payload = item.payload.json |
| 87 | + (db_span, http_span, redis_span) = payload["spans"] |
| 88 | + |
| 89 | + assert db_span["op"] == "db" |
| 90 | + assert redis_span["op"] == "redis" |
| 91 | + assert http_span["op"] == "http" |
| 92 | + |
| 93 | + assert db_span["trace_id"] == payload["contexts"]["trace"]["trace_id"] |
| 94 | + assert redis_span["trace_id"] == payload["contexts"]["trace"]["trace_id"] |
| 95 | + assert http_span["trace_id"] == payload["contexts"]["trace"]["trace_id"] |
| 96 | + |
| 97 | + assert db_span["parent_span_id"] == payload["contexts"]["trace"]["span_id"] |
| 98 | + assert http_span["parent_span_id"] == payload["contexts"]["trace"]["span_id"] |
| 99 | + assert redis_span["parent_span_id"] == db_span["span_id"] |
| 100 | + |
| 101 | + |
| 102 | +def test_root_span_transaction_payload_started_with_sentry_only(capture_envelopes): |
| 103 | + envelopes = capture_envelopes() |
| 104 | + |
| 105 | + with sentry_sdk.start_span(description="request"): |
| 106 | + pass |
| 107 | + |
| 108 | + (envelope,) = envelopes |
| 109 | + # TODO-neel-potel DSC header |
| 110 | + (item,) = envelope.items |
| 111 | + payload = item.payload.json |
| 112 | + |
| 113 | + assert payload["type"] == "transaction" |
| 114 | + assert payload["transaction"] == "request" |
| 115 | + assert payload["transaction_info"] == {"source": "custom"} |
| 116 | + assert payload["timestamp"] is not None |
| 117 | + assert payload["start_timestamp"] is not None |
| 118 | + |
| 119 | + contexts = payload["contexts"] |
| 120 | + assert "runtime" in contexts |
| 121 | + assert "otel" in contexts |
| 122 | + assert "resource" in contexts["otel"] |
| 123 | + |
| 124 | + trace_context = contexts["trace"] |
| 125 | + assert "trace_id" in trace_context |
| 126 | + assert "span_id" in trace_context |
| 127 | + assert trace_context["origin"] == "auto.otel" |
| 128 | + assert trace_context["op"] == "request" |
| 129 | + assert trace_context["status"] == "ok" |
| 130 | + |
| 131 | + assert payload["spans"] == [] |
| 132 | + |
| 133 | + |
| 134 | +def test_child_span_payload_started_with_sentry_only(capture_envelopes): |
| 135 | + envelopes = capture_envelopes() |
| 136 | + |
| 137 | + with sentry_sdk.start_span(description="request"): |
| 138 | + with sentry_sdk.start_span(description="db"): |
| 139 | + pass |
| 140 | + |
| 141 | + (envelope,) = envelopes |
| 142 | + (item,) = envelope.items |
| 143 | + payload = item.payload.json |
| 144 | + (span,) = payload["spans"] |
| 145 | + |
| 146 | + assert span["op"] == "db" |
| 147 | + assert span["description"] == "db" |
| 148 | + assert span["origin"] == "auto.otel" |
| 149 | + assert span["status"] == "ok" |
| 150 | + assert span["span_id"] is not None |
| 151 | + assert span["trace_id"] == payload["contexts"]["trace"]["trace_id"] |
| 152 | + assert span["parent_span_id"] == payload["contexts"]["trace"]["span_id"] |
| 153 | + assert span["timestamp"] is not None |
| 154 | + assert span["start_timestamp"] is not None |
| 155 | + |
| 156 | + |
| 157 | +def test_children_span_nesting_started_with_sentry_only(capture_envelopes): |
| 158 | + envelopes = capture_envelopes() |
| 159 | + |
| 160 | + with sentry_sdk.start_span(description="request"): |
| 161 | + with sentry_sdk.start_span(description="db"): |
| 162 | + with sentry_sdk.start_span(description="redis"): |
| 163 | + pass |
| 164 | + with sentry_sdk.start_span(description="http"): |
| 165 | + pass |
| 166 | + |
| 167 | + (envelope,) = envelopes |
| 168 | + (item,) = envelope.items |
| 169 | + payload = item.payload.json |
| 170 | + (db_span, http_span, redis_span) = payload["spans"] |
| 171 | + |
| 172 | + assert db_span["op"] == "db" |
| 173 | + assert redis_span["op"] == "redis" |
| 174 | + assert http_span["op"] == "http" |
| 175 | + |
| 176 | + assert db_span["trace_id"] == payload["contexts"]["trace"]["trace_id"] |
| 177 | + assert redis_span["trace_id"] == payload["contexts"]["trace"]["trace_id"] |
| 178 | + assert http_span["trace_id"] == payload["contexts"]["trace"]["trace_id"] |
| 179 | + |
| 180 | + assert db_span["parent_span_id"] == payload["contexts"]["trace"]["span_id"] |
| 181 | + assert http_span["parent_span_id"] == payload["contexts"]["trace"]["span_id"] |
| 182 | + assert redis_span["parent_span_id"] == db_span["span_id"] |
| 183 | + |
| 184 | + |
| 185 | +def test_children_span_nesting_mixed(capture_envelopes): |
| 186 | + envelopes = capture_envelopes() |
| 187 | + |
| 188 | + with sentry_sdk.start_span(description="request"): |
| 189 | + with tracer.start_as_current_span("db"): |
| 190 | + with sentry_sdk.start_span(description="redis"): |
| 191 | + pass |
| 192 | + with tracer.start_as_current_span("http"): |
| 193 | + pass |
| 194 | + |
| 195 | + (envelope,) = envelopes |
| 196 | + (item,) = envelope.items |
| 197 | + payload = item.payload.json |
| 198 | + (db_span, http_span, redis_span) = payload["spans"] |
| 199 | + |
| 200 | + assert db_span["op"] == "db" |
| 201 | + assert redis_span["op"] == "redis" |
| 202 | + assert http_span["op"] == "http" |
| 203 | + |
| 204 | + assert db_span["trace_id"] == payload["contexts"]["trace"]["trace_id"] |
| 205 | + assert redis_span["trace_id"] == payload["contexts"]["trace"]["trace_id"] |
| 206 | + assert http_span["trace_id"] == payload["contexts"]["trace"]["trace_id"] |
| 207 | + |
| 208 | + assert db_span["parent_span_id"] == payload["contexts"]["trace"]["span_id"] |
| 209 | + assert http_span["parent_span_id"] == payload["contexts"]["trace"]["span_id"] |
| 210 | + assert redis_span["parent_span_id"] == db_span["span_id"] |
0 commit comments