-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
transactions_processor.py
369 lines (318 loc) · 13.8 KB
/
transactions_processor.py
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import logging
import numbers
import uuid
from datetime import datetime
from typing import Any, Dict, Mapping, MutableMapping, Optional, Tuple
from sentry_relay.consts import SPAN_STATUS_NAME_TO_CODE
from snuba import environment, settings
from snuba.consumers.types import KafkaMessageMetadata
from snuba.datasets.events_format import (
EventTooOld,
enforce_retention,
extract_base,
extract_extra_contexts,
extract_extra_tags,
extract_http,
extract_nested,
extract_user,
)
from snuba.processor import (
InsertBatch,
MessageProcessor,
ProcessedMessage,
_as_dict_safe,
_ensure_valid_date,
_ensure_valid_ip,
_unicodify,
)
from snuba.state import get_config
from snuba.utils.metrics.wrapper import MetricsWrapper
logger = logging.getLogger(__name__)
metrics = MetricsWrapper(environment.metrics, "transactions.processor")
UNKNOWN_SPAN_STATUS = 2
EventDict = Dict[str, Any]
SpanDict = Dict[str, Any]
RetentionDays = int
class TransactionsMessageProcessor(MessageProcessor):
PROMOTED_TAGS = {
"environment",
"sentry:release",
"sentry:user",
"sentry:dist",
}
def __extract_timestamp(self, field: int) -> Tuple[datetime, int]:
# We are purposely using a naive datetime here to work with the rest of the codebase.
# We can be confident that clients are only sending UTC dates.
timestamp = _ensure_valid_date(datetime.utcfromtimestamp(field))
if timestamp is None:
timestamp = datetime.utcnow()
milliseconds = int(timestamp.microsecond / 1000)
return (timestamp, milliseconds)
def _structure_and_validate_message(
self, message: Tuple[int, str, Dict[str, Any]]
) -> Optional[Tuple[EventDict, RetentionDays]]:
if not (isinstance(message, (list, tuple)) and len(message) >= 2):
return None
version = message[0]
if version not in (0, 1, 2):
return None
type_, event = message[1:3]
if type_ != "insert":
return None
data = event["data"]
event_type = data.get("type")
if event_type != "transaction":
return None
if not data.get("contexts", {}).get("trace"):
return None
try:
# We are purposely using a naive datetime here to work with the
# rest of the codebase. We can be confident that clients are only
# sending UTC dates.
retention_days = enforce_retention(
event, datetime.utcfromtimestamp(data["timestamp"])
)
except EventTooOld:
return None
return event, retention_days
def _process_base_event_values(
self, processed: MutableMapping[str, Any], event_dict: EventDict
) -> MutableMapping[str, Any]:
extract_base(processed, event_dict)
transaction_ctx = event_dict["data"]["contexts"]["trace"]
trace_id = transaction_ctx["trace_id"]
processed["event_id"] = str(uuid.UUID(processed["event_id"]))
processed["trace_id"] = str(uuid.UUID(trace_id))
processed["span_id"] = int(transaction_ctx["span_id"], 16)
processed["transaction_op"] = _unicodify(transaction_ctx.get("op") or "")
processed["transaction_name"] = _unicodify(
event_dict["data"].get("transaction") or ""
)
processed["start_ts"], processed["start_ms"] = self.__extract_timestamp(
event_dict["data"]["start_timestamp"],
)
status = transaction_ctx.get("status", None)
if status:
int_status = SPAN_STATUS_NAME_TO_CODE.get(status, UNKNOWN_SPAN_STATUS)
else:
int_status = UNKNOWN_SPAN_STATUS
processed["transaction_status"] = int_status
if event_dict["data"]["timestamp"] - event_dict["data"]["start_timestamp"] < 0:
# Seems we have some negative durations in the DB
metrics.increment("negative_duration")
processed["finish_ts"], processed["finish_ms"] = self.__extract_timestamp(
event_dict["data"]["timestamp"],
)
duration_secs = (processed["finish_ts"] - processed["start_ts"]).total_seconds()
processed["duration"] = max(int(duration_secs * 1000), 0)
processed["platform"] = _unicodify(event_dict["platform"])
return processed
def _process_tags(
self, processed: MutableMapping[str, Any], event_dict: EventDict,
) -> None:
tags: Mapping[str, Any] = _as_dict_safe(event_dict["data"].get("tags", None))
processed["tags.key"], processed["tags.value"] = extract_extra_tags(tags)
promoted_tags = {col: tags[col] for col in self.PROMOTED_TAGS if col in tags}
processed["release"] = promoted_tags.get(
"sentry:release", event_dict.get("release"),
)
processed["environment"] = promoted_tags.get("environment")
processed["user"] = promoted_tags.get("sentry:user", "")
processed["dist"] = _unicodify(
promoted_tags.get("sentry:dist", event_dict["data"].get("dist")),
)
def _process_measurements(
self, processed: MutableMapping[str, Any], event_dict: EventDict,
) -> None:
measurements = event_dict["data"].get("measurements")
if measurements is not None:
try:
(
processed["measurements.key"],
processed["measurements.value"],
) = extract_nested(
measurements,
lambda value: float(value["value"])
if (
value is not None
and isinstance(value.get("value"), numbers.Number)
)
else None,
)
except Exception:
# Not failing the event in this case just yet, because we are still
# developing this feature.
logger.error(
"Invalid measurements field.",
extra={"measurements": measurements},
exc_info=True,
)
def _process_breakdown(
self, processed: MutableMapping[str, Any], event_dict: EventDict,
) -> None:
breakdowns = event_dict["data"].get("breakdowns")
if breakdowns is not None:
span_op_breakdowns = breakdowns.get("span_ops")
if span_op_breakdowns is not None:
try:
(
processed["span_op_breakdowns.key"],
processed["span_op_breakdowns.value"],
) = extract_nested(
span_op_breakdowns,
lambda value: float(value["value"])
if (
value is not None
and isinstance(value.get("value"), numbers.Number)
)
else None,
)
except Exception:
# Not failing the event in this case just yet, because we are still
# developing this feature.
logger.error(
"Invalid breakdowns.span_ops field.",
extra={"span_op_breakdowns": span_op_breakdowns},
exc_info=True,
)
def _process_contexts_and_user(
self, processed: MutableMapping[str, Any], event_dict: EventDict,
) -> None:
contexts: MutableMapping[str, Any] = _as_dict_safe(
event_dict["data"].get("contexts", None)
)
user_dict = (
event_dict["data"].get(
"user", event_dict["data"].get("sentry.interfaces.User", None)
)
or {}
)
geo = user_dict.get("geo", None) or {}
if "geo" not in contexts and isinstance(geo, dict):
contexts["geo"] = geo
skipped_contexts = settings.TRANSACT_SKIP_CONTEXT_STORE.get(
processed["project_id"], set()
)
for context in skipped_contexts:
if context in contexts:
del contexts[context]
transaction_ctx = contexts.get("trace", {})
# We store trace_id and span_id as promoted columns and on the query level
# we make sure that all queries on contexts[trace.trace_id/span_id] use those promoted
# columns instead. So we don't need to store them in the contexts array as well
transaction_ctx.pop("trace_id", None)
transaction_ctx.pop("span_id", None)
processed["contexts.key"], processed["contexts.value"] = extract_extra_contexts(
contexts
)
user_data: MutableMapping[str, Any] = {}
extract_user(user_data, user_dict)
processed["user_name"] = user_data["username"]
processed["user_id"] = user_data["user_id"]
processed["user_email"] = user_data["email"]
ip_address = _ensure_valid_ip(user_data["ip_address"])
if ip_address:
if ip_address.version == 4:
processed["ip_address_v4"] = str(ip_address)
elif ip_address.version == 6:
processed["ip_address_v6"] = str(ip_address)
def _process_request_data(
self, processed: MutableMapping[str, Any], event_dict: EventDict,
) -> None:
request = (
event_dict["data"].get(
"request", event_dict["data"].get("sentry.interfaces.Http", None)
)
or {}
)
http_data: MutableMapping[str, Any] = {}
extract_http(http_data, request)
processed["http_method"] = http_data["http_method"]
processed["http_referer"] = http_data["http_referer"]
def _process_sdk_data(
self, processed: MutableMapping[str, Any], event_dict: EventDict,
) -> None:
sdk = event_dict["data"].get("sdk", None) or {}
processed["sdk_name"] = _unicodify(sdk.get("name") or "")
processed["sdk_version"] = _unicodify(sdk.get("version") or "")
if processed["sdk_name"] == "":
metrics.increment("missing_sdk_name")
if processed["sdk_version"] == "":
metrics.increment("missing_sdk_version")
def _process_span(self, span_dict: SpanDict) -> Optional[Tuple[str, int, float]]:
op = span_dict.get("op")
group = span_dict.get("hash")
exclusive_time = span_dict.get("exclusive_time")
if op is None or group is None or exclusive_time is None:
return None
return op, int(group, 16), exclusive_time
def _process_spans(
self, processed: MutableMapping[str, Any], event_dict: EventDict,
) -> None:
data = event_dict["data"]
trace_context = data["contexts"]["trace"]
try:
max_spans_per_transaction = get_config("max_spans_per_transaction", 2000)
assert isinstance(max_spans_per_transaction, (int, float))
except Exception:
metrics.increment("bad_config.max_spans_per_transaction")
max_spans_per_transaction = 2000
num_processed = 0
processed_spans = []
processed_root_span = self._process_span(trace_context)
if processed_root_span is not None:
num_processed += 1
processed_spans.append(processed_root_span)
for span in data.get("spans", []):
# The number of spans should not exceed 1000 as enforced by SDKs.
# As a safety precaution, enforce a hard limit on the number of
# spans we actually store .
if num_processed >= max_spans_per_transaction:
metrics.increment("too_many_spans")
break
processed_span = self._process_span(span)
if processed_span is not None:
num_processed += 1
processed_spans.append(processed_span)
processed["spans.op"] = []
processed["spans.group"] = []
processed["spans.exclusive_time"] = []
processed["spans.exclusive_time_32"] = []
for op, group, exclusive_time in sorted(processed_spans):
processed["spans.op"].append(op)
processed["spans.group"].append(group)
processed["spans.exclusive_time"].append(0)
processed["spans.exclusive_time_32"].append(exclusive_time)
# The hash and exclusive_time is being stored in the spans columns
# so there is no need to store it again in the context array.
trace_context.pop("hash", None)
trace_context.pop("exclusive_time", None)
def process_message(
self, message: Tuple[int, str, Dict[Any, Any]], metadata: KafkaMessageMetadata
) -> Optional[ProcessedMessage]:
event_dict, retention_days = self._structure_and_validate_message(message) or (
None,
None,
)
if not event_dict:
return None
processed: MutableMapping[str, Any] = {
"deleted": 0,
"retention_days": retention_days,
}
# The following helper functions should be able to be applied in any order.
# At time of writing, there are no reads of the values in the `processed`
# dictionary to inform values in other functions.
# Ideally we keep continue that rule
self._process_base_event_values(processed, event_dict)
self._process_tags(processed, event_dict)
self._process_measurements(processed, event_dict)
self._process_breakdown(processed, event_dict)
self._process_spans(processed, event_dict)
self._process_request_data(processed, event_dict)
self._process_sdk_data(processed, event_dict)
processed["partition"] = metadata.partition
processed["offset"] = metadata.offset
# the following operation modifies the event_dict and is therefore *not* order-independent
self._process_contexts_and_user(processed, event_dict)
return InsertBatch([processed], None)