|
| 1 | +/* eslint-disable no-console */ |
| 2 | + |
| 3 | +const fs = require('fs'); |
| 4 | +const path = require('path'); |
| 5 | + |
| 6 | +if (process.argv.length < 4) { |
| 7 | + throw new Error('Please provide an input and output file path as an argument.'); |
| 8 | +} |
| 9 | + |
| 10 | +const resolvedInputPath = path.resolve(process.argv[2]); |
| 11 | +const resolvedOutputPath = path.resolve(process.argv[3]); |
| 12 | + |
| 13 | +const fileContents = fs.readFileSync(resolvedInputPath, 'utf8'); |
| 14 | + |
| 15 | +const transactionNodes = []; |
| 16 | + |
| 17 | +fileContents.split('\n').forEach(serializedEnvelope => { |
| 18 | + let envelope; |
| 19 | + try { |
| 20 | + envelope = JSON.parse(serializedEnvelope); |
| 21 | + } catch (e) { |
| 22 | + return; |
| 23 | + // noop |
| 24 | + } |
| 25 | + |
| 26 | + const envelopeItems = envelope[1]; |
| 27 | + |
| 28 | + envelopeItems.forEach(([envelopeItemHeader, transaction]) => { |
| 29 | + if (envelopeItemHeader.type === 'transaction') { |
| 30 | + const rootNode = { |
| 31 | + runtime: transaction.contexts.runtime?.name, |
| 32 | + op: transaction.contexts.trace.op, |
| 33 | + name: transaction.transaction, |
| 34 | + children: [], |
| 35 | + }; |
| 36 | + |
| 37 | + const spanMap = new Map(); |
| 38 | + spanMap.set(transaction.contexts.trace.span_id, rootNode); |
| 39 | + |
| 40 | + transaction.spans.forEach(span => { |
| 41 | + const node = { |
| 42 | + op: span.data['sentry.op'], |
| 43 | + name: span.description, |
| 44 | + parent_span_id: span.parent_span_id, |
| 45 | + children: [], |
| 46 | + }; |
| 47 | + spanMap.set(span.span_id, node); |
| 48 | + }); |
| 49 | + |
| 50 | + transaction.spans.forEach(span => { |
| 51 | + const node = spanMap.get(span.span_id); |
| 52 | + if (node && node.parent_span_id) { |
| 53 | + const parentNode = spanMap.get(node.parent_span_id); |
| 54 | + parentNode.children.push(node); |
| 55 | + } |
| 56 | + }); |
| 57 | + |
| 58 | + transactionNodes.push(rootNode); |
| 59 | + } |
| 60 | + }); |
| 61 | +}); |
| 62 | + |
| 63 | +const output = transactionNodes |
| 64 | + .sort((a, b) => { |
| 65 | + const aSerialized = serializeNode(a); |
| 66 | + const bSerialized = serializeNode(b); |
| 67 | + if (aSerialized < bSerialized) { |
| 68 | + return -1; |
| 69 | + } else if (aSerialized > bSerialized) { |
| 70 | + return 1; |
| 71 | + } else { |
| 72 | + return 0; |
| 73 | + } |
| 74 | + }) |
| 75 | + .map(node => buildDeterministicStringFromNode(node)) |
| 76 | + .join('\n\n-----------------------\n\n'); |
| 77 | + |
| 78 | +fs.writeFileSync(resolvedOutputPath, output, 'utf-8'); |
| 79 | + |
| 80 | +// ------- utility fns ---------- |
| 81 | + |
| 82 | +function buildDeterministicStringFromNode(node, depth = 0) { |
| 83 | + const mainParts = []; |
| 84 | + if (node.runtime) { |
| 85 | + mainParts.push(`(${node.runtime})`); |
| 86 | + } |
| 87 | + mainParts.push(`${node.op ?? 'default'} -`); |
| 88 | + mainParts.push(node.name); |
| 89 | + const main = mainParts.join(' '); |
| 90 | + const children = node.children |
| 91 | + .sort((a, b) => { |
| 92 | + const aSerialized = serializeNode(a); |
| 93 | + const bSerialized = serializeNode(b); |
| 94 | + if (aSerialized < bSerialized) { |
| 95 | + return -1; |
| 96 | + } else if (aSerialized > bSerialized) { |
| 97 | + return 1; |
| 98 | + } else { |
| 99 | + return 0; |
| 100 | + } |
| 101 | + }) |
| 102 | + .map(child => '\n' + buildDeterministicStringFromNode(child, depth + 1)) |
| 103 | + .join(''); |
| 104 | + return `${main}${children}`.split('\n').join('\n '); |
| 105 | +} |
| 106 | + |
| 107 | +function serializeNode(node) { |
| 108 | + return [node.op, node.name, node.runtime].join('---'); |
| 109 | +} |
0 commit comments