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
|
from sentry_sdk.envelope import Envelope
from sentry_sdk.session import Session
from sentry_sdk import capture_event
import sentry_sdk.client
def generate_transaction_item():
return {
"event_id": "15210411201320122115110420122013",
"type": "transaction",
"transaction": "/interactions/other-dogs/new-dog",
"start_timestamp": 1353568872.11122131,
"timestamp": 1356942672.09040815,
"contexts": {
"trace": {
"trace_id": "12312012123120121231201212312012",
"span_id": "0415201309082013",
"parent_span_id": None,
"description": "<OrganizationContext>",
"op": "greeting.sniff",
"dynamic_sampling_context": {
"trace_id": "12312012123120121231201212312012",
"sample_rate": "1.0",
"environment": "dogpark",
"release": "off.leash.park",
"public_key": "dogsarebadatkeepingsecrets",
"transaction": "/interactions/other-dogs/new-dog",
},
}
},
"spans": [
{
"description": "<OrganizationContext>",
"op": "greeting.sniff",
"parent_span_id": None,
"span_id": "0415201309082013",
"start_timestamp": 1353568872.11122131,
"timestamp": 1356942672.09040815,
"trace_id": "12312012123120121231201212312012",
}
],
}
def test_add_and_get_basic_event():
envelope = Envelope()
expected = {"message": "Hello, World!"}
envelope.add_event(expected)
assert envelope.get_event() == {"message": "Hello, World!"}
def test_add_and_get_transaction_event():
envelope = Envelope()
transaction_item = generate_transaction_item()
transaction_item.update({"event_id": "a" * 32})
envelope.add_transaction(transaction_item)
# typically it should not be possible to be able to add a second transaction;
# but we do it anyways
another_transaction_item = generate_transaction_item()
envelope.add_transaction(another_transaction_item)
# should only fetch the first inserted transaction event
assert envelope.get_transaction_event() == transaction_item
def test_add_and_get_session():
envelope = Envelope()
expected = Session()
envelope.add_session(expected)
for item in envelope:
if item.type == "session":
assert item.payload.json == expected.to_json()
def test_envelope_headers(sentry_init, capture_envelopes, monkeypatch):
monkeypatch.setattr(
sentry_sdk.client,
"format_timestamp",
lambda x: "2012-11-21T12:31:12.415908Z",
)
sentry_init(
dsn="https://dogsarebadatkeepingsecrets@squirrelchasers.ingest.sentry.io/12312012",
traces_sample_rate=1.0,
)
envelopes = capture_envelopes()
capture_event(generate_transaction_item())
assert len(envelopes) == 1
assert envelopes[0].headers == {
"event_id": "15210411201320122115110420122013",
"sent_at": "2012-11-21T12:31:12.415908Z",
"trace": {
"trace_id": "12312012123120121231201212312012",
"sample_rate": "1.0",
"environment": "dogpark",
"release": "off.leash.park",
"public_key": "dogsarebadatkeepingsecrets",
"transaction": "/interactions/other-dogs/new-dog",
},
}
def test_envelope_with_sized_items():
"""
Tests that it successfully parses envelopes with
the item size specified in the header
"""
envelope_raw = (
b'{"event_id":"9ec79c33ec9942ab8353589fcb2e04dc"}\n'
b'{"type":"type1","length":4 }\n1234\n'
b'{"type":"type2","length":4 }\nabcd\n'
b'{"type":"type3","length":0}\n\n'
b'{"type":"type4","length":4 }\nab12\n'
)
envelope_raw_eof_terminated = envelope_raw[:-1]
for envelope in (envelope_raw, envelope_raw_eof_terminated):
actual = Envelope.deserialize(envelope)
items = [item for item in actual]
assert len(items) == 4
assert items[0].type == "type1"
assert items[0].get_bytes() == b"1234"
assert items[1].type == "type2"
assert items[1].get_bytes() == b"abcd"
assert items[2].type == "type3"
assert items[2].get_bytes() == b""
assert items[3].type == "type4"
assert items[3].get_bytes() == b"ab12"
assert actual.headers["event_id"] == "9ec79c33ec9942ab8353589fcb2e04dc"
def test_envelope_with_implicitly_sized_items():
"""
Tests that it successfully parses envelopes with
the item size not specified in the header
"""
envelope_raw = (
b'{"event_id":"9ec79c33ec9942ab8353589fcb2e04dc"}\n'
b'{"type":"type1"}\n1234\n'
b'{"type":"type2"}\nabcd\n'
b'{"type":"type3"}\n\n'
b'{"type":"type4"}\nab12\n'
)
envelope_raw_eof_terminated = envelope_raw[:-1]
for envelope in (envelope_raw, envelope_raw_eof_terminated):
actual = Envelope.deserialize(envelope)
assert actual.headers["event_id"] == "9ec79c33ec9942ab8353589fcb2e04dc"
items = [item for item in actual]
assert len(items) == 4
assert items[0].type == "type1"
assert items[0].get_bytes() == b"1234"
assert items[1].type == "type2"
assert items[1].get_bytes() == b"abcd"
assert items[2].type == "type3"
assert items[2].get_bytes() == b""
assert items[3].type == "type4"
assert items[3].get_bytes() == b"ab12"
def test_envelope_with_two_attachments():
"""
Test that items are correctly parsed in an envelope with to size specified items
"""
two_attachments = (
b'{"event_id":"9ec79c33ec9942ab8353589fcb2e04dc","dsn":"https://e12d836b15bb49d7bbf99e64295d995b:@sentry.io/42"}\n'
+ b'{"type":"attachment","length":10,"content_type":"text/plain","filename":"hello.txt"}\n'
+ b"\xef\xbb\xbfHello\r\n\n"
+ b'{"type":"event","length":41,"content_type":"application/json","filename":"application.log"}\n'
+ b'{"message":"hello world","level":"error"}\n'
)
two_attachments_eof_terminated = two_attachments[
:-1
] # last \n is optional, without it should still be a valid envelope
for envelope_raw in (two_attachments, two_attachments_eof_terminated):
actual = Envelope.deserialize(envelope_raw)
items = [item for item in actual]
assert len(items) == 2
assert items[0].get_bytes() == b"\xef\xbb\xbfHello\r\n"
assert items[1].payload.json == {"message": "hello world", "level": "error"}
def test_envelope_with_empty_attachments():
"""
Test that items are correctly parsed in an envelope with two 0 length items (with size specified in the header
"""
two_empty_attachments = (
b'{"event_id":"9ec79c33ec9942ab8353589fcb2e04dc"}\n'
+ b'{"type":"attachment","length":0}\n\n'
+ b'{"type":"attachment","length":0}\n\n'
)
two_empty_attachments_eof_terminated = two_empty_attachments[
:-1
] # last \n is optional, without it should still be a valid envelope
for envelope_raw in (two_empty_attachments, two_empty_attachments_eof_terminated):
actual = Envelope.deserialize(envelope_raw)
items = [item for item in actual]
assert len(items) == 2
assert items[0].get_bytes() == b""
assert items[1].get_bytes() == b""
def test_envelope_without_headers():
"""
Test that an envelope without headers is parsed successfully
"""
envelope_without_headers = (
b"{}\n" + b'{"type":"session"}\n' + b'{"started": "2020-02-07T14:16:00Z"}'
)
actual = Envelope.deserialize(envelope_without_headers)
items = [item for item in actual]
assert len(items) == 1
assert items[0].payload.get_bytes() == b'{"started": "2020-02-07T14:16:00Z"}'
|