| 79 | } |
| 80 | |
| 81 | function collectEventSearchableText(event) { |
| 82 | const MAX_DEPTH = 4; |
| 83 | const MAX_CHUNKS = 160; |
| 84 | const chunks = []; |
| 85 | const seen = new Set(); |
| 86 | |
| 87 | function pushChunk(value) { |
| 88 | if (value === null || value === undefined) return; |
| 89 | if (chunks.length >= MAX_CHUNKS) return; |
| 90 | chunks.push(String(value)); |
| 91 | } |
| 92 | |
| 93 | function walk(value, depth = 0) { |
| 94 | if (value === null || value === undefined) return; |
| 95 | if (chunks.length >= MAX_CHUNKS) return; |
| 96 | if (depth > MAX_DEPTH) return; |
| 97 | |
| 98 | const valueType = typeof value; |
| 99 | if (valueType === 'string' || valueType === 'number' || valueType === 'boolean') { |
| 100 | pushChunk(value); |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | if (valueType !== 'object') return; |
| 105 | if (seen.has(value)) return; |
| 106 | seen.add(value); |
| 107 | |
| 108 | if (Array.isArray(value)) { |
| 109 | value.forEach(item => walk(item, depth + 1)); |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | Object.keys(value).forEach((key) => { |
| 114 | if (chunks.length >= MAX_CHUNKS) return; |
| 115 | // 优先采集错误链路核心字段,避免遍历过深影响性能 |
| 116 | if ([ |
| 117 | 'message', |
| 118 | 'value', |
| 119 | 'type', |
| 120 | 'reason', |
| 121 | 'originalException', |
| 122 | 'mechanism', |
| 123 | 'exception', |
| 124 | 'extra', |
| 125 | 'breadcrumbs', |
| 126 | 'data', |
| 127 | 'filename', |
| 128 | 'function', |
| 129 | ].includes(key)) { |
| 130 | walk(value[key], depth + 1); |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | if (depth < 2) { |
| 135 | walk(value[key], depth + 1); |
| 136 | } |
| 137 | }); |
| 138 | } |