-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfingerprint.rs
More file actions
347 lines (296 loc) · 13.6 KB
/
fingerprint.rs
File metadata and controls
347 lines (296 loc) · 13.6 KB
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
//! Fingerprinting for issue grouping.
//!
//! The thing is, Sentry groups events into issues by fingerprint — and the
//! priority chain matters: SDK-provided fingerprint > exception type+value >
//! log message template > transaction name > random UUID. Each fingerprint
//! is scoped by project_id so projects don't bleed into each other.
use crate::models::ItemType;
use serde_json::Value;
/// FNV-1a 64-bit — fast, deterministic, and good enough for fingerprinting.
pub(crate) fn fnv1a_64(data: &[u8]) -> u64 {
const FNV_OFFSET_BASIS: u64 = 0xcbf29ce484222325;
const FNV_PRIME: u64 = 0x00000100000001B3;
let mut hash = FNV_OFFSET_BASIS;
for &byte in data {
hash ^= byte as u64;
hash = hash.wrapping_mul(FNV_PRIME);
}
hash
}
/// Zero-padded 16-char hex string from a 64-bit hash.
fn format_hash(hash: u64) -> String {
format!("{:016x}", hash)
}
/// Fingerprint from an already-parsed JSON value.
/// Returns `None` for item types that don't produce issues — sessions, client reports, etc.
pub fn compute_fingerprint_from_value(
project_id: u64,
item_type: &ItemType,
json: &Value,
) -> Option<String> {
match item_type {
ItemType::Event | ItemType::Transaction => {}
_ => return None,
}
compute_fingerprint_inner(project_id, json)
}
/// Fingerprint from raw JSON bytes.
/// Returns `None` for non-issue item types. Falls back to a random UUID
/// if the JSON can't be parsed — better to store an ungrouped event than drop it.
pub fn compute_fingerprint(
project_id: u64,
item_type: &ItemType,
payload_json: &[u8],
) -> Option<String> {
match item_type {
ItemType::Event | ItemType::Transaction => {}
_ => return None,
}
let json: Value = match serde_json::from_slice(payload_json) {
Ok(v) => v,
Err(_) => return Some(uuid::Uuid::new_v4().to_string()),
};
compute_fingerprint_inner(project_id, &json)
}
fn compute_fingerprint_inner(project_id: u64, json: &Value) -> Option<String> {
// SDK-provided fingerprint array wins — unless it's just ["{{ default }}"]
if let Some(fp_array) = json.get("fingerprint").and_then(|v| v.as_array()) {
let is_default_only = fp_array.len() == 1 && fp_array[0].as_str() == Some("{{ default }}");
if !is_default_only && !fp_array.is_empty() {
let mut input = Vec::new();
input.extend_from_slice(&project_id.to_be_bytes());
for (i, elem) in fp_array.iter().enumerate() {
if i > 0 {
input.push(0x00);
}
if let Some(s) = elem.as_str() {
input.extend_from_slice(s.as_bytes());
} else {
// Non-string elements get their JSON repr — shouldn't happen often
input.extend_from_slice(elem.to_string().as_bytes());
}
}
return Some(format_hash(fnv1a_64(&input)));
}
}
// Exception type+value, scoped by project — the null bytes prevent collisions
if let Some(exc) = json
.get("exception")
.and_then(|e| e.get("values"))
.and_then(|v| v.as_array())
.and_then(|arr| arr.first())
{
let exc_type = exc.get("type").and_then(|v| v.as_str()).unwrap_or("");
let exc_value = exc.get("value").and_then(|v| v.as_str()).unwrap_or("");
let mut input = Vec::new();
input.extend_from_slice(project_id.to_string().as_bytes());
input.push(0x00);
input.extend_from_slice(exc_type.as_bytes());
input.push(0x00);
input.extend_from_slice(exc_value.as_bytes());
return Some(format_hash(fnv1a_64(&input)));
}
// Message template — logentry.message is the unformatted template, which is
// what we want for grouping. Top-level `message` is the fallback.
let logentry_msg = json
.get("logentry")
.and_then(|l| l.get("message"))
.and_then(|v| v.as_str());
let top_msg = json.get("message").and_then(|v| v.as_str());
if let Some(msg) = logentry_msg.or(top_msg) {
let mut input = Vec::new();
input.extend_from_slice(project_id.to_string().as_bytes());
input.push(0x00);
input.extend_from_slice(msg.as_bytes());
return Some(format_hash(fnv1a_64(&input)));
}
// Transaction name — last structured option before we give up
if let Some(txn) = json.get("transaction").and_then(|v| v.as_str()) {
let mut input = Vec::new();
input.extend_from_slice(project_id.to_string().as_bytes());
input.push(0x00);
input.extend_from_slice(txn.as_bytes());
return Some(format_hash(fnv1a_64(&input)));
}
// Nothing to group on — random UUID, each event becomes its own issue
Some(uuid::Uuid::new_v4().to_string())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn fnv1a_known_value() {
// Empty string should give back the offset basis — it's the known starting point
assert_eq!(fnv1a_64(b""), 0xcbf29ce484222325);
}
#[test]
fn format_hash_zero_padded() {
assert_eq!(format_hash(0), "0000000000000000");
assert_eq!(format_hash(255), "00000000000000ff");
assert_eq!(format_hash(0xdeadbeefcafebabe), "deadbeefcafebabe");
}
#[test]
fn non_event_types_return_none() {
let payload = br#"{"message":"hello"}"#;
assert!(compute_fingerprint(1, &ItemType::Session, payload).is_none());
assert!(compute_fingerprint(1, &ItemType::Sessions, payload).is_none());
assert!(compute_fingerprint(1, &ItemType::ClientReport, payload).is_none());
assert!(compute_fingerprint(1, &ItemType::Attachment, payload).is_none());
assert!(compute_fingerprint(1, &ItemType::CheckIn, payload).is_none());
assert!(compute_fingerprint(1, &ItemType::Profile, payload).is_none());
assert!(compute_fingerprint(1, &ItemType::ReplayEvent, payload).is_none());
assert!(compute_fingerprint(1, &ItemType::ReplayRecording, payload).is_none());
assert!(compute_fingerprint(1, &ItemType::UserReport, payload).is_none());
assert!(compute_fingerprint(1, &ItemType::Unknown, payload).is_none());
}
#[test]
fn event_and_transaction_return_some() {
let payload = br#"{"message":"hello"}"#;
assert!(compute_fingerprint(1, &ItemType::Event, payload).is_some());
assert!(compute_fingerprint(1, &ItemType::Transaction, payload).is_some());
}
#[test]
fn custom_fingerprint_array() {
let payload = br#"{"fingerprint":["my-custom-group","extra"],"message":"hello"}"#;
let fp = compute_fingerprint(1, &ItemType::Event, payload).unwrap();
// 16-char hex from hashing the concatenated fingerprint parts
assert_eq!(fp.len(), 16);
// Same custom fingerprint on a different project — should NOT collide
let payload2 = br#"{"fingerprint":["my-custom-group","extra"],"message":"different"}"#;
let fp2 = compute_fingerprint(999, &ItemType::Event, payload2).unwrap();
assert_ne!(fp, fp2);
// Same custom fingerprint on the same project — should be deterministic
let fp3 = compute_fingerprint(1, &ItemType::Event, payload).unwrap();
assert_eq!(fp, fp3);
}
#[test]
fn default_fingerprint_falls_through() {
// ["{{ default }}"] means "use normal grouping" — it shouldn't override
let payload = br#"{"fingerprint":["{{ default }}"],"message":"hello"}"#;
let fp = compute_fingerprint(1, &ItemType::Event, payload).unwrap();
// Should fall through to message-based fingerprinting
let payload_no_fp = br#"{"message":"hello"}"#;
let fp_no_fp = compute_fingerprint(1, &ItemType::Event, payload_no_fp).unwrap();
assert_eq!(fp, fp_no_fp);
}
#[test]
fn exception_fingerprint() {
let payload =
br#"{"exception":{"values":[{"type":"TypeError","value":"null is not an object"}]}}"#;
let fp = compute_fingerprint(42, &ItemType::Event, payload).unwrap();
assert_eq!(fp.len(), 16);
// Same exception on same project — should be deterministic
let fp2 = compute_fingerprint(42, &ItemType::Event, payload).unwrap();
assert_eq!(fp, fp2);
// Different project — shouldn't collide
let fp3 = compute_fingerprint(43, &ItemType::Event, payload).unwrap();
assert_ne!(fp, fp3);
}
#[test]
fn chained_exceptions_uses_first() {
let payload = br#"{"exception":{"values":[
{"type":"ValueError","value":"bad value"},
{"type":"RuntimeError","value":"runtime issue"}
]}}"#;
let fp = compute_fingerprint(1, &ItemType::Event, payload).unwrap();
// We only look at the first exception in the chain for grouping
let payload_single =
br#"{"exception":{"values":[{"type":"ValueError","value":"bad value"}]}}"#;
let fp_single = compute_fingerprint(1, &ItemType::Event, payload_single).unwrap();
assert_eq!(fp, fp_single);
}
#[test]
fn message_fingerprint() {
let payload = br#"{"message":"something broke"}"#;
let fp = compute_fingerprint(1, &ItemType::Event, payload).unwrap();
assert_eq!(fp.len(), 16);
// Deterministic — same message, same project, same result
let fp2 = compute_fingerprint(1, &ItemType::Event, payload).unwrap();
assert_eq!(fp, fp2);
// Different message — different fingerprint
let payload2 = br#"{"message":"something else broke"}"#;
let fp3 = compute_fingerprint(1, &ItemType::Event, payload2).unwrap();
assert_ne!(fp, fp3);
}
#[test]
fn logentry_template_preferred_over_formatted() {
// logentry.message is the unformatted template — that's what we group on
let payload =
br#"{"logentry":{"message":"User %s logged in","formatted":"User alice logged in"}}"#;
let fp = compute_fingerprint(1, &ItemType::Event, payload).unwrap();
// Different rendered value but same template — should still group together
let payload2 =
br#"{"logentry":{"message":"User %s logged in","formatted":"User bob logged in"}}"#;
let fp2 = compute_fingerprint(1, &ItemType::Event, payload2).unwrap();
assert_eq!(fp, fp2);
}
#[test]
fn logentry_preferred_over_top_level_message() {
let payload = br#"{"logentry":{"message":"template %s"},"message":"rendered value"}"#;
let fp = compute_fingerprint(1, &ItemType::Event, payload).unwrap();
// logentry.message takes priority over top-level message
let payload_logentry_only = br#"{"logentry":{"message":"template %s"}}"#;
let fp2 = compute_fingerprint(1, &ItemType::Event, payload_logentry_only).unwrap();
assert_eq!(fp, fp2);
}
#[test]
fn transaction_fingerprint() {
let payload = br#"{"transaction":"/api/health","type":"transaction"}"#;
let fp = compute_fingerprint(1, &ItemType::Transaction, payload).unwrap();
assert_eq!(fp.len(), 16);
// Deterministic
let fp2 = compute_fingerprint(1, &ItemType::Transaction, payload).unwrap();
assert_eq!(fp, fp2);
}
#[test]
fn fallback_uuid_for_empty_event() {
let payload = br#"{"level":"info"}"#;
let fp = compute_fingerprint(1, &ItemType::Event, payload).unwrap();
// UUID fallback — 36 chars, dashes included
assert_eq!(fp.len(), 36);
assert!(fp.contains('-'));
}
#[test]
fn invalid_json_gives_uuid_fallback() {
let payload = b"not json at all";
let fp = compute_fingerprint(1, &ItemType::Event, payload).unwrap();
assert_eq!(fp.len(), 36);
}
#[test]
fn null_separator_prevents_ambiguity() {
// The null byte separator prevents "TypeError" + "" from colliding with "Type" + "Error"
let payload1 = br#"{"exception":{"values":[{"type":"TypeError","value":""}]}}"#;
let payload2 = br#"{"exception":{"values":[{"type":"Type","value":"Error"}]}}"#;
let fp1 = compute_fingerprint(1, &ItemType::Event, payload1).unwrap();
let fp2 = compute_fingerprint(1, &ItemType::Event, payload2).unwrap();
assert_ne!(fp1, fp2);
}
#[test]
fn exception_takes_priority_over_message() {
let payload =
br#"{"exception":{"values":[{"type":"TypeError","value":"bad"}]},"message":"hello"}"#;
let fp = compute_fingerprint(1, &ItemType::Event, payload).unwrap();
// Exception wins over message in the priority chain
let payload_exc_only = br#"{"exception":{"values":[{"type":"TypeError","value":"bad"}]}}"#;
let fp_exc = compute_fingerprint(1, &ItemType::Event, payload_exc_only).unwrap();
assert_eq!(fp, fp_exc);
}
#[test]
fn custom_fingerprint_takes_priority_over_exception() {
let payload = br#"{"fingerprint":["custom"],"exception":{"values":[{"type":"TypeError","value":"bad"}]}}"#;
let fp = compute_fingerprint(1, &ItemType::Event, payload).unwrap();
// Custom fingerprint trumps everything
let payload_custom_only = br#"{"fingerprint":["custom"]}"#;
let fp_custom = compute_fingerprint(1, &ItemType::Event, payload_custom_only).unwrap();
assert_eq!(fp, fp_custom);
}
#[test]
fn empty_fingerprint_array_falls_through() {
let payload = br#"{"fingerprint":[],"message":"hello"}"#;
let fp = compute_fingerprint(1, &ItemType::Event, payload).unwrap();
// Empty array — should fall through to message
let payload_msg = br#"{"message":"hello"}"#;
let fp_msg = compute_fingerprint(1, &ItemType::Event, payload_msg).unwrap();
assert_eq!(fp, fp_msg);
}
}