1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
|
"""
This tests test for the correctness of the dynamic sampling context (DSC) in the trace header of envelopes.
The DSC is defined here:
https://develop.sentry.dev/sdk/telemetry/traces/dynamic-sampling-context/#dsc-specification
The DSC is propagated between service using a header called "baggage".
This is not tested in this file.
"""
import pytest
import sentry_sdk
import sentry_sdk.client
def test_dsc_head_of_trace(sentry_init, capture_envelopes):
"""
Our service is the head of the trace (it starts a new trace)
and sends a transaction event to Sentry.
"""
sentry_init(
dsn="https://mysecret@bla.ingest.sentry.io/12312012",
release="myapp@0.0.1",
environment="canary",
traces_sample_rate=1.0,
)
envelopes = capture_envelopes()
# We start a new transaction
with sentry_sdk.start_transaction(name="foo"):
pass
assert len(envelopes) == 1
transaction_envelope = envelopes[0]
envelope_trace_header = transaction_envelope.headers["trace"]
assert "trace_id" in envelope_trace_header
assert type(envelope_trace_header["trace_id"]) == str
assert "public_key" in envelope_trace_header
assert type(envelope_trace_header["public_key"]) == str
assert envelope_trace_header["public_key"] == "mysecret"
assert "sample_rate" in envelope_trace_header
assert type(envelope_trace_header["sample_rate"]) == str
assert envelope_trace_header["sample_rate"] == "1.0"
assert "sampled" in envelope_trace_header
assert type(envelope_trace_header["sampled"]) == str
assert envelope_trace_header["sampled"] == "true"
assert "release" in envelope_trace_header
assert type(envelope_trace_header["release"]) == str
assert envelope_trace_header["release"] == "myapp@0.0.1"
assert "environment" in envelope_trace_header
assert type(envelope_trace_header["environment"]) == str
assert envelope_trace_header["environment"] == "canary"
assert "transaction" in envelope_trace_header
assert type(envelope_trace_header["transaction"]) == str
assert envelope_trace_header["transaction"] == "foo"
def test_dsc_continuation_of_trace(sentry_init, capture_envelopes):
"""
Another service calls our service and passes tracing information to us.
Our service is continuing the trace and sends a transaction event to Sentry.
"""
sentry_init(
dsn="https://mysecret@bla.ingest.sentry.io/12312012",
release="myapp@0.0.1",
environment="canary",
traces_sample_rate=1.0,
)
envelopes = capture_envelopes()
# This is what the upstream service sends us
sentry_trace = "771a43a4192642f0b136d5159a501700-1234567890abcdef-1"
baggage = (
"other-vendor-value-1=foo;bar;baz, "
"sentry-trace_id=771a43a4192642f0b136d5159a501700, "
"sentry-public_key=frontendpublickey, "
"sentry-sample_rate=0.01337, "
"sentry-sampled=true, "
"sentry-release=myfrontend@1.2.3, "
"sentry-environment=bird, "
"sentry-transaction=bar, "
"other-vendor-value-2=foo;bar;"
)
incoming_http_headers = {
"HTTP_SENTRY_TRACE": sentry_trace,
"HTTP_BAGGAGE": baggage,
}
# We continue the incoming trace and start a new transaction
transaction = sentry_sdk.continue_trace(incoming_http_headers)
with sentry_sdk.start_transaction(transaction, name="foo"):
pass
assert len(envelopes) == 1
transaction_envelope = envelopes[0]
envelope_trace_header = transaction_envelope.headers["trace"]
assert "trace_id" in envelope_trace_header
assert type(envelope_trace_header["trace_id"]) == str
assert envelope_trace_header["trace_id"] == "771a43a4192642f0b136d5159a501700"
assert "public_key" in envelope_trace_header
assert type(envelope_trace_header["public_key"]) == str
assert envelope_trace_header["public_key"] == "frontendpublickey"
assert "sample_rate" in envelope_trace_header
assert type(envelope_trace_header["sample_rate"]) == str
assert envelope_trace_header["sample_rate"] == "0.01337"
assert "sampled" in envelope_trace_header
assert type(envelope_trace_header["sampled"]) == str
assert envelope_trace_header["sampled"] == "true"
assert "release" in envelope_trace_header
assert type(envelope_trace_header["release"]) == str
assert envelope_trace_header["release"] == "myfrontend@1.2.3"
assert "environment" in envelope_trace_header
assert type(envelope_trace_header["environment"]) == str
assert envelope_trace_header["environment"] == "bird"
assert "transaction" in envelope_trace_header
assert type(envelope_trace_header["transaction"]) == str
assert envelope_trace_header["transaction"] == "bar"
def test_dsc_issue(sentry_init, capture_envelopes):
"""
Our service is a standalone service that does not have tracing enabled. Just uses Sentry for error reporting.
"""
sentry_init(
dsn="https://mysecret@bla.ingest.sentry.io/12312012",
release="myapp@0.0.1",
environment="canary",
)
envelopes = capture_envelopes()
# No transaction is started, just an error is captured
try:
1 / 0
except ZeroDivisionError as exp:
sentry_sdk.capture_exception(exp)
assert len(envelopes) == 1
error_envelope = envelopes[0]
envelope_trace_header = error_envelope.headers["trace"]
assert "trace_id" in envelope_trace_header
assert type(envelope_trace_header["trace_id"]) == str
assert "public_key" in envelope_trace_header
assert type(envelope_trace_header["public_key"]) == str
assert envelope_trace_header["public_key"] == "mysecret"
assert "sample_rate" not in envelope_trace_header
assert "sampled" not in envelope_trace_header
assert "release" in envelope_trace_header
assert type(envelope_trace_header["release"]) == str
assert envelope_trace_header["release"] == "myapp@0.0.1"
assert "environment" in envelope_trace_header
assert type(envelope_trace_header["environment"]) == str
assert envelope_trace_header["environment"] == "canary"
assert "transaction" not in envelope_trace_header
def test_dsc_issue_with_tracing(sentry_init, capture_envelopes):
"""
Our service has tracing enabled and an error occurs in an transaction.
Envelopes containing errors also have the same DSC than the transaction envelopes.
"""
sentry_init(
dsn="https://mysecret@bla.ingest.sentry.io/12312012",
release="myapp@0.0.1",
environment="canary",
traces_sample_rate=1.0,
)
envelopes = capture_envelopes()
# We start a new transaction and an error occurs
with sentry_sdk.start_transaction(name="foo"):
try:
1 / 0
except ZeroDivisionError as exp:
sentry_sdk.capture_exception(exp)
assert len(envelopes) == 2
error_envelope, transaction_envelope = envelopes
assert error_envelope.headers["trace"] == transaction_envelope.headers["trace"]
envelope_trace_header = error_envelope.headers["trace"]
assert "trace_id" in envelope_trace_header
assert type(envelope_trace_header["trace_id"]) == str
assert "public_key" in envelope_trace_header
assert type(envelope_trace_header["public_key"]) == str
assert envelope_trace_header["public_key"] == "mysecret"
assert "sample_rate" in envelope_trace_header
assert envelope_trace_header["sample_rate"] == "1.0"
assert type(envelope_trace_header["sample_rate"]) == str
assert "sampled" in envelope_trace_header
assert type(envelope_trace_header["sampled"]) == str
assert envelope_trace_header["sampled"] == "true"
assert "release" in envelope_trace_header
assert type(envelope_trace_header["release"]) == str
assert envelope_trace_header["release"] == "myapp@0.0.1"
assert "environment" in envelope_trace_header
assert type(envelope_trace_header["environment"]) == str
assert envelope_trace_header["environment"] == "canary"
assert "transaction" in envelope_trace_header
assert type(envelope_trace_header["transaction"]) == str
assert envelope_trace_header["transaction"] == "foo"
@pytest.mark.parametrize(
"traces_sample_rate",
[
0, # no traces will be started, but if incoming traces will be continued (by our instrumentations, not happening in this test)
None, # no tracing at all. This service will never create transactions.
],
)
def test_dsc_issue_twp(sentry_init, capture_envelopes, traces_sample_rate):
"""
Our service does not have tracing enabled, but we receive tracing information from an upstream service.
Error envelopes still contain a DCS. This is called "tracing without performance" or TWP for short.
This way if I have three services A, B, and C, and A and C have tracing enabled, but B does not,
we still can see the full trace in Sentry, and associate errors send by service B to Sentry.
(This test would be service B in this scenario)
"""
sentry_init(
dsn="https://mysecret@bla.ingest.sentry.io/12312012",
release="myapp@0.0.1",
environment="canary",
traces_sample_rate=traces_sample_rate,
)
envelopes = capture_envelopes()
# This is what the upstream service sends us
sentry_trace = "771a43a4192642f0b136d5159a501700-1234567890abcdef-1"
baggage = (
"other-vendor-value-1=foo;bar;baz, "
"sentry-trace_id=771a43a4192642f0b136d5159a501700, "
"sentry-public_key=frontendpublickey, "
"sentry-sample_rate=0.01337, "
"sentry-sampled=true, "
"sentry-release=myfrontend@1.2.3, "
"sentry-environment=bird, "
"sentry-transaction=bar, "
"other-vendor-value-2=foo;bar;"
)
incoming_http_headers = {
"HTTP_SENTRY_TRACE": sentry_trace,
"HTTP_BAGGAGE": baggage,
}
# We continue the trace (meaning: saving the incoming trace information on the scope)
# but in this test, we do not start a transaction.
sentry_sdk.continue_trace(incoming_http_headers)
# No transaction is started, just an error is captured
try:
1 / 0
except ZeroDivisionError as exp:
sentry_sdk.capture_exception(exp)
assert len(envelopes) == 1
error_envelope = envelopes[0]
envelope_trace_header = error_envelope.headers["trace"]
assert "trace_id" in envelope_trace_header
assert type(envelope_trace_header["trace_id"]) == str
assert envelope_trace_header["trace_id"] == "771a43a4192642f0b136d5159a501700"
assert "public_key" in envelope_trace_header
assert type(envelope_trace_header["public_key"]) == str
assert envelope_trace_header["public_key"] == "frontendpublickey"
assert "sample_rate" in envelope_trace_header
assert type(envelope_trace_header["sample_rate"]) == str
assert envelope_trace_header["sample_rate"] == "0.01337"
assert "sampled" in envelope_trace_header
assert type(envelope_trace_header["sampled"]) == str
assert envelope_trace_header["sampled"] == "true"
assert "release" in envelope_trace_header
assert type(envelope_trace_header["release"]) == str
assert envelope_trace_header["release"] == "myfrontend@1.2.3"
assert "environment" in envelope_trace_header
assert type(envelope_trace_header["environment"]) == str
assert envelope_trace_header["environment"] == "bird"
assert "transaction" in envelope_trace_header
assert type(envelope_trace_header["transaction"]) == str
assert envelope_trace_header["transaction"] == "bar"
|