@multiplayer-app/session-recorder-common 1.3.32 → 1.3.34

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  import { context, trace, SpanStatusCode } from '@opentelemetry/api';
2
- import { ATTR_EXCEPTION_MESSAGE, ATTR_EXCEPTION_STACKTRACE, ATTR_EXCEPTION_TYPE, } from '@opentelemetry/semantic-conventions';
2
+ import { ATTR_EXCEPTION_MESSAGE, ATTR_EXCEPTION_STACKTRACE, ATTR_EXCEPTION_TYPE } from '@opentelemetry/semantic-conventions';
3
3
  import { getResourceAttributes } from './set-resource-attributes';
4
4
  /**
5
5
  * @description Add error to current span
@@ -7,7 +7,7 @@ import { getResourceAttributes } from './set-resource-attributes';
7
7
  * @returns {void}
8
8
  */
9
9
  export const captureException = (error, errorInfo) => {
10
- if (!error) {
10
+ if (!error || !shouldCaptureException(error)) {
11
11
  return;
12
12
  }
13
13
  const activeContext = context.active();
@@ -15,7 +15,7 @@ export const captureException = (error, errorInfo) => {
15
15
  let isNewSpan = false;
16
16
  if (!span || !span.isRecording()) {
17
17
  span = trace.getTracer('exception').startSpan(error.name || 'Error', {
18
- attributes: Object.assign({ [ATTR_EXCEPTION_MESSAGE]: error.message, [ATTR_EXCEPTION_STACKTRACE]: error.stack, [ATTR_EXCEPTION_TYPE]: error.name }, getResourceAttributes()),
18
+ attributes: Object.assign({ [ATTR_EXCEPTION_MESSAGE]: error.message, [ATTR_EXCEPTION_STACKTRACE]: error.stack, [ATTR_EXCEPTION_TYPE]: error.name }, getResourceAttributes())
19
19
  });
20
20
  trace.setSpan(activeContext, span);
21
21
  isNewSpan = true;
@@ -24,7 +24,7 @@ export const captureException = (error, errorInfo) => {
24
24
  span.setAttributes({
25
25
  [ATTR_EXCEPTION_MESSAGE]: error.message,
26
26
  [ATTR_EXCEPTION_STACKTRACE]: error.stack,
27
- [ATTR_EXCEPTION_TYPE]: error.name,
27
+ [ATTR_EXCEPTION_TYPE]: error.name
28
28
  });
29
29
  }
30
30
  if (errorInfo) {
@@ -35,10 +35,44 @@ export const captureException = (error, errorInfo) => {
35
35
  span.recordException(error);
36
36
  span.setStatus({
37
37
  code: SpanStatusCode.ERROR,
38
- message: error.message,
38
+ message: error.message
39
39
  });
40
40
  if (isNewSpan) {
41
41
  span.end();
42
42
  }
43
43
  };
44
+ /**
45
+ * Best-effort deduplication of exceptions that fire multiple times
46
+ * (e.g. framework handler + global handlers) within a short time window.
47
+ */
48
+ const exceptionDedupeWindowMs = 2000;
49
+ const recentExceptionFingerprints = new Map();
50
+ export const shouldCaptureException = (error, _errorInfo) => {
51
+ if (!error)
52
+ return false;
53
+ const now = Date.now();
54
+ // Build a fingerprint that is stable enough across repeated emissions
55
+ // but not so broad that different errors collapse into one.
56
+ const keyParts = [];
57
+ keyParts.push(error.name || 'Error');
58
+ keyParts.push(error.message || '');
59
+ // First stack line tends to include file/line where it originated.
60
+ if (typeof error.stack === 'string') {
61
+ const firstFrame = error.stack.split('\n')[1] || '';
62
+ keyParts.push(firstFrame.trim());
63
+ }
64
+ const fingerprint = keyParts.join('|').slice(0, 500);
65
+ const lastSeen = recentExceptionFingerprints.get(fingerprint);
66
+ if (lastSeen && now - lastSeen < exceptionDedupeWindowMs) {
67
+ return false;
68
+ }
69
+ recentExceptionFingerprints.set(fingerprint, now);
70
+ // Cheap cleanup of old entries to avoid unbounded growth.
71
+ for (const [key, ts] of recentExceptionFingerprints) {
72
+ if (now - ts > exceptionDedupeWindowMs * 5) {
73
+ recentExceptionFingerprints.delete(key);
74
+ }
75
+ }
76
+ return true;
77
+ };
44
78
  //# sourceMappingURL=capture-exception.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"capture-exception.js","sourceRoot":"","sources":["../../../src/sdk/capture-exception.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAA;AACnE,OAAO,EACL,sBAAsB,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G,EAAE,KAAK,CAAC,EAAE,EAAE;YACjD,IAAI,CAAC,YAAY,CAAC,cAAc,GAAG,EAAE,EAAE,KAAK,CAAC,CAAA;QAC/C,CAAC,CAAC,CAAA;IACJ,CAAC;IAED,IAAI,CAAC,eAAe,CAAC,KAAK,CAAC,CAAA;IAC3B,IAAI,CAAC,SAAS,CAAC;QACb,IAAI,EAAE,cAAc,CAAC,KAAK;QAC1B,OAAO,EAAE,KAAK,CAAC,OAAO;KACvB,CAAC,CAAA;IAEF,IAAI,SAAS,EAAE,CAAC;QACd,IAAI,CAAC,GAAG,EAAE,CAAA;IACZ,CAAC;AACH,CAAC,CAAA","sourcesContent":["import { context, trace, SpanStatusCode } from '@opentelemetry/api'\nimport {\n ATTR_EXCEPTION_MESSAGE,\n ATTR_EXCEPTION_STACKTRACE,\n ATTR_EXCEPTION_TYPE,\n} from '@opentelemetry/semantic-conventions'\nimport { getResourceAttributes } from './set-resource-attributes'\n\n/**\n * @description Add error to current span\n * @param {Error} error\n * @returns {void}\n */\nexport const captureException = (\n error: Error,\n errorInfo?: Record<string, any>,\n) => {\n if (!error) {\n return\n }\n\n const activeContext = context.active()\n\n let span = trace.getSpan(activeContext)\n let isNewSpan = false\n\n if (!span || !span.isRecording()) {\n span = trace.getTracer('exception').startSpan(\n error.name || 'Error',\n {\n attributes: {\n [ATTR_EXCEPTION_MESSAGE]: error.message,\n [ATTR_EXCEPTION_STACKTRACE]: error.stack,\n [ATTR_EXCEPTION_TYPE]: error.name,\n ...getResourceAttributes(),\n },\n },\n )\n trace.setSpan(activeContext, span)\n isNewSpan = true\n } else {\n span.setAttributes({\n [ATTR_EXCEPTION_MESSAGE]: error.message,\n [ATTR_EXCEPTION_STACKTRACE]: error.stack,\n [ATTR_EXCEPTION_TYPE]: error.name,\n })\n }\n\n if (errorInfo) {\n Object.entries(errorInfo).forEach(([key, value]) => {\n span.setAttribute(`error_info.${key}`, value)\n })\n }\n\n span.recordException(error)\n span.setStatus({\n code: SpanStatusCode.ERROR,\n message: error.message,\n })\n\n if (isNewSpan) {\n span.end()\n }\n}\n"]}
1
+ {"version":3,"file":"capture-exception.js","sourceRoot":"","sources":["../../../src/sdk/capture-exception.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAA;AACnE,OAAO,EACL,sBAAsB,EACtB,yBAAyB,EACzB,mBAAmB,EACpB,MAAM,qCAAqC,CAAA;AAC5C,OAAO,EAAE,qBAAqB,EAAE,MAAM,2BAA2B,CAAA;AAEjE;;;;GAIG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,KAAY,EAAE,SAA+B,EAAE,EAAE;IAChF,IAAI,CAAC,KAAK,IAAI,CAAC,sBAAsB,CAAC,KAAK,CAAC,EAAE,CAAC;QAC7C,OAAM;IACR,CAAC;IAED,MAAM,aAAa,GAAG,OAAO,CAAC,MAAM,EAAE,CAAA;IAEtC,IAAI,IAAI,GAAG,KAAK,CAAC,OAAO,CAAC,aAAa,CAAC,CAAA;IACvC,IAAI,SAAS,GAAG,KAAK,CAAA;IAErB,IAAI,CAAC,IAAI,IAAI,CAAC,IAAI,CAAC,WAAW,EAAE,EAAE,CAAC;QACjC,IAAI,GAAG,KAAK,CAAC,SAAS,CAAC,WAAW,CAAC,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,IAAI,OAAO,EAAE;YACnE,UAAU,kBACR,CAAC,sBAAsB,CAAC,EAAE,KAAK,CAAC,OAAO,EACvC,CAAC,yBAAyB,CAAC,EAAE,KAAK,CAAC,KAAK,EACxC,CAAC,mBAAmB,CAAC,EAAE,KAAK,CAAC,IAAI,IAC9B,qBAAqB,EAAE,CAC3B;SACF,CAAC,CAAA;QACF,KAAK,CAAC,OAAO,CAAC,aAAa,EAAE,IAAI,CAAC,CAAA;QAClC,SAAS,GAAG,IAAI,CAAA;IAClB,CAAC;SAAM,CAAC;QACN,IAAI,CAAC,aAAa,CAAC;YACjB,C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KAAK,QAAQ,EAAE,CAAC;QACpC,MAAM,UAAU,GAAG,KAAK,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAA;QACnD,QAAQ,CAAC,IAAI,CAAC,UAAU,CAAC,IAAI,EAAE,CAAC,CAAA;IAClC,CAAC;IAED,MAAM,WAAW,GAAG,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,GAAG,CAAC,CAAA;IAEpD,MAAM,QAAQ,GAAG,2BAA2B,CAAC,GAAG,CAAC,WAAW,CAAC,CAAA;IAC7D,IAAI,QAAQ,IAAI,GAAG,GAAG,QAAQ,GAAG,uBAAuB,EAAE,CAAC;QACzD,OAAO,KAAK,CAAA;IACd,CAAC;IAED,2BAA2B,CAAC,GAAG,CAAC,WAAW,EAAE,GAAG,CAAC,CAAA;IAEjD,0DAA0D;IAC1D,KAAK,MAAM,CAAC,GAAG,EAAE,EAAE,CAAC,IAAI,2BAA2B,EAAE,CAAC;QACpD,IAAI,GAAG,GAAG,EAAE,GAAG,uBAAuB,GAAG,CAAC,EAAE,CAAC;YAC3C,2BAA2B,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;QACzC,CAAC;IACH,CAAC;IAED,OAAO,IAAI,CAAA;AACb,CAAC,CAAA","sourcesContent":["import { context, trace, SpanStatusCode } from '@opentelemetry/api'\nimport {\n ATTR_EXCEPTION_MESSAGE,\n ATTR_EXCEPTION_STACKTRACE,\n ATTR_EXCEPTION_TYPE\n} from '@opentelemetry/semantic-conventions'\nimport { getResourceAttributes } from './set-resource-attributes'\n\n/**\n * @description Add error to current span\n * @param {Error} error\n * @returns {void}\n */\nexport const captureException = (error: Error, errorInfo?: Record<string, any>) => {\n if (!error || !shouldCaptureException(error)) {\n return\n }\n\n const activeContext = context.active()\n\n let span = trace.getSpan(activeContext)\n let isNewSpan = false\n\n if (!span || !span.isRecording()) {\n span = trace.getTracer('exception').startSpan(error.name || 'Error', {\n attributes: {\n [ATTR_EXCEPTION_MESSAGE]: error.message,\n [ATTR_EXCEPTION_STACKTRACE]: error.stack,\n [ATTR_EXCEPTION_TYPE]: error.name,\n ...getResourceAttributes()\n }\n })\n trace.setSpan(activeContext, span)\n isNewSpan = true\n } else {\n span.setAttributes({\n [ATTR_EXCEPTION_MESSAGE]: error.message,\n [ATTR_EXCEPTION_STACKTRACE]: error.stack,\n [ATTR_EXCEPTION_TYPE]: error.name\n })\n }\n\n if (errorInfo) {\n Object.entries(errorInfo).forEach(([key, value]) => {\n span.setAttribute(`error_info.${key}`, value)\n })\n }\n\n span.recordException(error)\n span.setStatus({\n code: SpanStatusCode.ERROR,\n message: error.message\n })\n\n if (isNewSpan) {\n span.end()\n }\n}\n\n/**\n * Best-effort deduplication of exceptions that fire multiple times\n * (e.g. framework handler + global handlers) within a short time window.\n */\n\nconst exceptionDedupeWindowMs = 2000\nconst recentExceptionFingerprints = new Map<string, number>()\n\nexport const shouldCaptureException = (error: Error, _errorInfo?: Record<string, any>): boolean => {\n if (!error) return false\n\n const now = Date.now()\n\n // Build a fingerprint that is stable enough across repeated emissions\n // but not so broad that different errors collapse into one.\n const keyParts: string[] = []\n keyParts.push(error.name || 'Error')\n keyParts.push(error.message || '')\n\n // First stack line tends to include file/line where it originated.\n if (typeof error.stack === 'string') {\n const firstFrame = error.stack.split('\\n')[1] || ''\n keyParts.push(firstFrame.trim())\n }\n\n const fingerprint = keyParts.join('|').slice(0, 500)\n\n const lastSeen = recentExceptionFingerprints.get(fingerprint)\n if (lastSeen && now - lastSeen < exceptionDedupeWindowMs) {\n return false\n }\n\n recentExceptionFingerprints.set(fingerprint, now)\n\n // Cheap cleanup of old entries to avoid unbounded growth.\n for (const [key, ts] of recentExceptionFingerprints) {\n if (now - ts > exceptionDedupeWindowMs * 5) {\n recentExceptionFingerprints.delete(key)\n }\n }\n\n return true\n}\n"]}