|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const { inspect } = require('util') |
| 4 | + |
| 5 | +// Events with the same `info` are only logged once because: |
| 6 | +// - it makes logs clearer |
| 7 | +// - it prevents creating too much CPU load or too many microtasks |
| 8 | +// - it prevents creating too many logs, which can be expensive if logs are |
| 9 | +// hosted remotely |
| 10 | +// - it prevents infinite recursions if |
| 11 | +// `opts.log|getLevel|getMessage|skipEvent()` triggers itself an event |
| 12 | +// (while still reporting that event once) |
| 13 | +const isRepeated = function({ info, previousEvents }) { |
| 14 | + const fingerprint = getFingerprint({ info }) |
| 15 | + |
| 16 | + const isRepeatedEvent = previousEvents.has(fingerprint) |
| 17 | + |
| 18 | + if (!isRepeatedEvent) { |
| 19 | + previousEvents.add(fingerprint) |
| 20 | + } |
| 21 | + |
| 22 | + return isRepeatedEvent |
| 23 | +} |
| 24 | + |
| 25 | +// Serialize `info` into a short fingerprint |
| 26 | +const getFingerprint = function({ info }) { |
| 27 | + const entries = INFO_PROPS.map(propName => serializeEntry({ info, propName })) |
| 28 | + const infoA = Object.assign({}, ...entries) |
| 29 | + |
| 30 | + const fingerprint = JSON.stringify(infoA) |
| 31 | + |
| 32 | + // We truncate fingerprints to prevent consuming too much memory in case some |
| 33 | + // `info` properties are huge. |
| 34 | + // This introduces higher risk of false positives (see comment below). |
| 35 | + // We do not hash as it would be too CPU-intensive if the value is huge. |
| 36 | + const fingerprintA = fingerprint.slice(0, FINGERPRINT_MAX_LENGTH) |
| 37 | + return fingerprintA |
| 38 | +} |
| 39 | + |
| 40 | +// We do not serialize `info.eventName` since this is already `eventName-wise` |
| 41 | +// Key order matters since fingerprint might be truncated: we serialize short |
| 42 | +// and non-dynamic values first. |
| 43 | +const INFO_PROPS = [ |
| 44 | + 'secondPromiseState', |
| 45 | + 'promiseState', |
| 46 | + 'error', |
| 47 | + 'secondPromiseValue', |
| 48 | + 'promiseValue', |
| 49 | +] |
| 50 | + |
| 51 | +const FINGERPRINT_MAX_LENGTH = 1e4 |
| 52 | + |
| 53 | +const serializeEntry = function({ info, propName }) { |
| 54 | + const value = info[propName] |
| 55 | + |
| 56 | + if (value === undefined) { |
| 57 | + return |
| 58 | + } |
| 59 | + |
| 60 | + const valueA = serializeValue({ value }) |
| 61 | + return { [propName]: valueA } |
| 62 | +} |
| 63 | + |
| 64 | +const serializeValue = function({ value }) { |
| 65 | + if (value instanceof Error) { |
| 66 | + return serializeError({ error: value }) |
| 67 | + } |
| 68 | + |
| 69 | + // We use `util.inspect()` instead of `JSON.stringify()` to support more |
| 70 | + // types and circular references. |
| 71 | + // `sorted` prevents the same event using different keys order from having |
| 72 | + // a different fingerprint. It is only `Node.js 10` but is backward-compatible |
| 73 | + // Big arrays, objects or buffers will be truncated, which makes this call |
| 74 | + // less CPU-intensive and the result value smaller in memory. However it |
| 75 | + // introduces higher risk of false positives (event being flagged as repeated |
| 76 | + // even though it's different). Process errors should be exceptional, so this |
| 77 | + // is ok. |
| 78 | + return inspect(value, { sorted: true }) |
| 79 | +} |
| 80 | + |
| 81 | +// We do not serialize `error.message` as it may contain dynamic values like |
| 82 | +// timestamps. This means errors are only `error.name` + `error.stack`, which |
| 83 | +// should be a good fingerprint. |
| 84 | +// Also we only keep first 10 callsites in case of infinitely recursive stack. |
| 85 | +const serializeError = function({ error: { name, stack } }) { |
| 86 | + const stackA = filterErrorStack({ stack }) |
| 87 | + return `${name}\n${stackA}` |
| 88 | +} |
| 89 | + |
| 90 | +const filterErrorStack = function({ stack }) { |
| 91 | + return stack |
| 92 | + .split('\n') |
| 93 | + .filter(line => STACK_TRACE_LINE_REGEXP.test(line)) |
| 94 | + .slice(0, STACK_TRACE_MAX_LENGTH) |
| 95 | + .join('\n') |
| 96 | +} |
| 97 | + |
| 98 | +const STACK_TRACE_LINE_REGEXP = /^\s+at /u |
| 99 | +const STACK_TRACE_MAX_LENGTH = 10 |
| 100 | + |
| 101 | +module.exports = { |
| 102 | + isRepeated, |
| 103 | +} |
0 commit comments