@open-mercato/shared 0.11.1-develop.8298.1.5323e2aa15 → 0.11.1-develop.8317.1.dde3d38b64

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.
@@ -23,6 +23,18 @@ function registerTelemetryRuntime(runtime) {
23
23
  function getTelemetryRuntime() {
24
24
  return store().active;
25
25
  }
26
+ function registerTelemetryErrorReporter(reporter) {
27
+ store().errorReporter = reporter;
28
+ return () => {
29
+ const current = store();
30
+ if (current.errorReporter === reporter) current.errorReporter = void 0;
31
+ };
32
+ }
33
+ function reportTelemetryError(...args) {
34
+ const runtime = getTelemetryRuntime();
35
+ if (runtime) runtime.reportError(...args);
36
+ else store().errorReporter?.(...args);
37
+ }
26
38
  function recordTelemetryMetric(point) {
27
39
  const runtime = getTelemetryRuntime();
28
40
  if (!runtime?.recordMetric) return false;
@@ -88,8 +100,10 @@ export {
88
100
  getTelemetryRuntime,
89
101
  isTelemetryBackendEnabled,
90
102
  recordTelemetryMetric,
103
+ registerTelemetryErrorReporter,
91
104
  registerTelemetryMetricCollector,
92
105
  registerTelemetryRuntime,
106
+ reportTelemetryError,
93
107
  resetTelemetryMetricCollectors,
94
108
  resetTelemetryRuntime,
95
109
  withTelemetrySpan
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../src/lib/telemetry/runtime.ts"],
4
- "sourcesContent": ["export type TelemetryTraceCarrier = Record<string, string>\n\nexport type TelemetrySpanAttributes = Record<string, string | number | boolean | undefined>\n\nexport type TelemetryMetricLabels = Record<string, string | number | boolean | undefined>\n\nexport type TelemetrySpanKind = 'internal' | 'server' | 'client' | 'producer' | 'consumer'\n\nexport type TelemetryMetricPoint = {\n kind: 'counter' | 'histogram' | 'gauge'\n name: string\n value: number\n labels?: TelemetryMetricLabels\n unit?: string\n}\n\nexport type TelemetryMetricCollector = () => void\n\n/** The subset of the telemetry package's `Span` that bridge consumers need. */\nexport type TelemetrySpan = {\n setAttributes(attributes: TelemetrySpanAttributes): void\n /**\n * Rename an in-flight span whose identity is only known once it has run.\n * Optional so a bootstrap predating it still satisfies the contract \u2014 call it\n * as `span.updateName?.(\u2026)`.\n */\n updateName?(name: string): void\n}\n\nexport type TelemetrySpanOptions = {\n kind?: TelemetrySpanKind\n attributes?: TelemetrySpanAttributes\n /** Start a new trace so the sampler decides for this span alone. */\n root?: boolean\n /** Causal links to other traces, as W3C carriers. */\n links?: TelemetryTraceCarrier[]\n}\n\nexport type TelemetryRuntime = {\n /**\n * True only when the active SDK may safely use the process-global W3C\n * propagator for cross-boundary extraction.\n */\n canUseGlobalTracePropagation(): boolean\n captureTraceContext(): TelemetryTraceCarrier\n continueTrace<T>(\n carrier: TelemetryTraceCarrier | undefined,\n name: string,\n fn: () => T,\n options?: { kind?: 'internal' | 'server' | 'client' | 'producer' | 'consumer' },\n ): T\n /**\n * Optional so an older bootstrap that predates span support still satisfies\n * the contract; consumers go through `withTelemetrySpan` and degrade to\n * running `fn` untraced.\n */\n withSpan?<T>(name: string, fn: (span: TelemetrySpan) => T, options?: TelemetrySpanOptions): T\n recordMetric?(point: TelemetryMetricPoint): void\n recordHistogram?(\n name: string,\n value: number,\n labels?: TelemetryMetricLabels,\n unit?: string,\n ): void\n recordHttpDuration(method: string, route: string, status: number, startedAt: number): void\n reportError(\n error: unknown,\n context?: {\n module?: string\n /**\n * Stable, enumerated fingerprint (`module.reason`) the backend groups on.\n * Optional so a bootstrap predating it still satisfies the contract \u2014 an\n * older bridge simply ignores the field.\n */\n code?: string\n attributes?: Record<string, string | number | boolean | undefined>\n },\n ): void\n shutdown(): Promise<void>\n}\n\nconst GLOBAL_KEY = Symbol.for('@open-mercato/shared.telemetryRuntime')\nconst ENABLED_BACKENDS = new Set(['console', 'signoz', 'newrelic', 'otlp'])\n\ntype TelemetryRuntimeStore = {\n active?: TelemetryRuntime\n metricCollectors?: Set<TelemetryMetricCollector>\n}\n\nfunction store(): TelemetryRuntimeStore {\n const globalStore = globalThis as unknown as Record<symbol, TelemetryRuntimeStore | undefined>\n let current = globalStore[GLOBAL_KEY]\n if (!current) {\n current = {}\n globalStore[GLOBAL_KEY] = current\n }\n return current\n}\n\n/**\n * This check is intentionally owned by shared code so hosts can decide whether\n * to dynamically import the telemetry package without evaluating that package.\n */\nexport function isTelemetryBackendEnabled(raw?: string): boolean {\n const value = raw ?? (\n typeof process === 'undefined'\n ? undefined\n : process.env.TELEMETRY_BACKEND\n )\n return ENABLED_BACKENDS.has((value ?? '').trim().toLowerCase())\n}\n\nexport function registerTelemetryRuntime(runtime: TelemetryRuntime): () => void {\n store().active = runtime\n return () => {\n const current = store()\n if (current.active === runtime) current.active = undefined\n }\n}\n\nexport function getTelemetryRuntime(): TelemetryRuntime | undefined {\n return store().active\n}\n\nexport function recordTelemetryMetric(point: TelemetryMetricPoint): boolean {\n const runtime = getTelemetryRuntime()\n if (!runtime?.recordMetric) return false\n runtime.recordMetric(point)\n return true\n}\n\nexport function registerTelemetryMetricCollector(collector: TelemetryMetricCollector): () => void {\n const current = store()\n current.metricCollectors ??= new Set()\n current.metricCollectors.add(collector)\n return () => {\n current.metricCollectors?.delete(collector)\n }\n}\n\nexport function collectTelemetryMetrics(onError?: (error: Error) => void): void {\n const collectors = Array.from(store().metricCollectors ?? [])\n for (const collector of collectors) {\n try {\n collector()\n } catch (error) {\n const failure = error instanceof Error\n ? error\n : new Error('[internal] Telemetry metric collector failed with a non-Error value')\n try {\n getTelemetryRuntime()?.reportError(failure, {\n module: 'telemetry',\n code: 'telemetry.metric_collector_failed',\n })\n } catch {}\n try {\n onError?.(failure)\n } catch (callbackError) {\n try {\n getTelemetryRuntime()?.reportError(callbackError, {\n module: 'telemetry',\n code: 'telemetry.metric_collector_callback_failed',\n })\n } catch {}\n }\n }\n }\n}\n\n/** Test-only: clear the process-wide telemetry bridge. */\nexport function resetTelemetryRuntime(): void {\n store().active = undefined\n}\n\n/** Test-only: clear process-wide metric collectors registered by shared owners. */\nexport function resetTelemetryMetricCollectors(): void {\n store().metricCollectors?.clear()\n}\n\nconst NOOP_SPAN: TelemetrySpan = { setAttributes() {} }\n\n/**\n * Run `fn` inside a span, from a package that must not depend on\n * `@open-mercato/telemetry`. With telemetry off this is `fn` plus one global\n * lookup \u2014 no span object is allocated and the OTEL SDK is never reached.\n *\n * Pass `root: true` for the unit of work a long-lived job should be sampled and\n * rendered by (a batch, a page) so the job is not one trace under one sampling\n * decision, and `links` to keep the causal chain back to what triggered it.\n */\nexport function withTelemetrySpan<T>(\n name: string,\n fn: (span: TelemetrySpan) => T,\n options?: TelemetrySpanOptions,\n): T {\n const runtime = getTelemetryRuntime()\n if (!runtime?.withSpan) return fn(NOOP_SPAN)\n return runtime.withSpan(name, fn, options)\n}\n\n/**\n * The active trace as a carrier, for use as a `links` entry. `undefined` when\n * telemetry is off or nothing is active, which `withTelemetrySpan` treats as\n * \"no link\" rather than an invalid one.\n */\nexport function captureTelemetryTrace(): TelemetryTraceCarrier | undefined {\n const carrier = getTelemetryRuntime()?.captureTraceContext()\n return carrier && Object.keys(carrier).length > 0 ? carrier : undefined\n}\n"],
5
- "mappings": "AAiFA,MAAM,aAAa,uBAAO,IAAI,uCAAuC;AACrE,MAAM,mBAAmB,oBAAI,IAAI,CAAC,WAAW,UAAU,YAAY,MAAM,CAAC;AAO1E,SAAS,QAA+B;AACtC,QAAM,cAAc;AACpB,MAAI,UAAU,YAAY,UAAU;AACpC,MAAI,CAAC,SAAS;AACZ,cAAU,CAAC;AACX,gBAAY,UAAU,IAAI;AAAA,EAC5B;AACA,SAAO;AACT;AAMO,SAAS,0BAA0B,KAAuB;AAC/D,QAAM,QAAQ,QACZ,OAAO,YAAY,cACf,SACA,QAAQ,IAAI;AAElB,SAAO,iBAAiB,KAAK,SAAS,IAAI,KAAK,EAAE,YAAY,CAAC;AAChE;AAEO,SAAS,yBAAyB,SAAuC;AAC9E,QAAM,EAAE,SAAS;AACjB,SAAO,MAAM;AACX,UAAM,UAAU,MAAM;AACtB,QAA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SAAU,QAAO,GAAG,SAAS;AAC3C,SAAO,QAAQ,SAAS,MAAM,IAAI,OAAO;AAC3C;AAOO,SAAS,wBAA2D;AACzE,QAAM,UAAU,oBAAoB,GAAG,oBAAoB;AAC3D,SAAO,WAAW,OAAO,KAAK,OAAO,EAAE,SAAS,IAAI,UAAU;AAChE;",
4
+ "sourcesContent": ["export type TelemetryTraceCarrier = Record<string, string>\n\nexport type TelemetrySpanAttributes = Record<string, string | number | boolean | undefined>\n\nexport type TelemetryMetricLabels = Record<string, string | number | boolean | undefined>\n\nexport type TelemetrySpanKind = 'internal' | 'server' | 'client' | 'producer' | 'consumer'\n\nexport type TelemetryMetricPoint = {\n kind: 'counter' | 'histogram' | 'gauge'\n name: string\n value: number\n labels?: TelemetryMetricLabels\n unit?: string\n}\n\nexport type TelemetryMetricCollector = () => void\n\n/** The subset of the telemetry package's `Span` that bridge consumers need. */\nexport type TelemetrySpan = {\n setAttributes(attributes: TelemetrySpanAttributes): void\n /**\n * Rename an in-flight span whose identity is only known once it has run.\n * Optional so a bootstrap predating it still satisfies the contract \u2014 call it\n * as `span.updateName?.(\u2026)`.\n */\n updateName?(name: string): void\n}\n\nexport type TelemetrySpanOptions = {\n kind?: TelemetrySpanKind\n attributes?: TelemetrySpanAttributes\n /** Start a new trace so the sampler decides for this span alone. */\n root?: boolean\n /** Causal links to other traces, as W3C carriers. */\n links?: TelemetryTraceCarrier[]\n}\n\nexport type TelemetryRuntime = {\n /**\n * True only when the active SDK may safely use the process-global W3C\n * propagator for cross-boundary extraction.\n */\n canUseGlobalTracePropagation(): boolean\n captureTraceContext(): TelemetryTraceCarrier\n continueTrace<T>(\n carrier: TelemetryTraceCarrier | undefined,\n name: string,\n fn: () => T,\n options?: { kind?: 'internal' | 'server' | 'client' | 'producer' | 'consumer' },\n ): T\n /**\n * Optional so an older bootstrap that predates span support still satisfies\n * the contract; consumers go through `withTelemetrySpan` and degrade to\n * running `fn` untraced.\n */\n withSpan?<T>(name: string, fn: (span: TelemetrySpan) => T, options?: TelemetrySpanOptions): T\n recordMetric?(point: TelemetryMetricPoint): void\n recordHistogram?(\n name: string,\n value: number,\n labels?: TelemetryMetricLabels,\n unit?: string,\n ): void\n recordHttpDuration(method: string, route: string, status: number, startedAt: number): void\n reportError(\n error: unknown,\n context?: {\n module?: string\n /**\n * Stable, enumerated fingerprint (`module.reason`) the backend groups on.\n * Optional so a bootstrap predating it still satisfies the contract \u2014 an\n * older bridge simply ignores the field.\n */\n code?: string\n attributes?: Record<string, string | number | boolean | undefined>\n },\n ): void\n shutdown(): Promise<void>\n}\n\nconst GLOBAL_KEY = Symbol.for('@open-mercato/shared.telemetryRuntime')\nconst ENABLED_BACKENDS = new Set(['console', 'signoz', 'newrelic', 'otlp'])\n\ntype TelemetryRuntimeStore = {\n active?: TelemetryRuntime\n errorReporter?: TelemetryRuntime['reportError']\n metricCollectors?: Set<TelemetryMetricCollector>\n}\n\nfunction store(): TelemetryRuntimeStore {\n const globalStore = globalThis as unknown as Record<symbol, TelemetryRuntimeStore | undefined>\n let current = globalStore[GLOBAL_KEY]\n if (!current) {\n current = {}\n globalStore[GLOBAL_KEY] = current\n }\n return current\n}\n\n/**\n * This check is intentionally owned by shared code so hosts can decide whether\n * to dynamically import the telemetry package without evaluating that package.\n */\nexport function isTelemetryBackendEnabled(raw?: string): boolean {\n const value = raw ?? (\n typeof process === 'undefined'\n ? undefined\n : process.env.TELEMETRY_BACKEND\n )\n return ENABLED_BACKENDS.has((value ?? '').trim().toLowerCase())\n}\n\nexport function registerTelemetryRuntime(runtime: TelemetryRuntime): () => void {\n store().active = runtime\n return () => {\n const current = store()\n if (current.active === runtime) current.active = undefined\n }\n}\n\nexport function getTelemetryRuntime(): TelemetryRuntime | undefined {\n return store().active\n}\n\nexport function registerTelemetryErrorReporter(reporter: TelemetryRuntime['reportError']): () => void {\n store().errorReporter = reporter\n return () => {\n const current = store()\n if (current.errorReporter === reporter) current.errorReporter = undefined\n }\n}\n\nexport function reportTelemetryError(...args: Parameters<TelemetryRuntime['reportError']>): void {\n const runtime = getTelemetryRuntime()\n if (runtime) runtime.reportError(...args)\n else store().errorReporter?.(...args)\n}\n\nexport function recordTelemetryMetric(point: TelemetryMetricPoint): boolean {\n const runtime = getTelemetryRuntime()\n if (!runtime?.recordMetric) return false\n runtime.recordMetric(point)\n return true\n}\n\nexport function registerTelemetryMetricCollector(collector: TelemetryMetricCollector): () => void {\n const current = store()\n current.metricCollectors ??= new Set()\n current.metricCollectors.add(collector)\n return () => {\n current.metricCollectors?.delete(collector)\n }\n}\n\nexport function collectTelemetryMetrics(onError?: (error: Error) => void): void {\n const collectors = Array.from(store().metricCollectors ?? [])\n for (const collector of collectors) {\n try {\n collector()\n } catch (error) {\n const failure = error instanceof Error\n ? error\n : new Error('[internal] Telemetry metric collector failed with a non-Error value')\n try {\n getTelemetryRuntime()?.reportError(failure, {\n module: 'telemetry',\n code: 'telemetry.metric_collector_failed',\n })\n } catch {}\n try {\n onError?.(failure)\n } catch (callbackError) {\n try {\n getTelemetryRuntime()?.reportError(callbackError, {\n module: 'telemetry',\n code: 'telemetry.metric_collector_callback_failed',\n })\n } catch {}\n }\n }\n }\n}\n\n/** Test-only: clear the process-wide telemetry bridge. */\nexport function resetTelemetryRuntime(): void {\n store().active = undefined\n}\n\n/** Test-only: clear process-wide metric collectors registered by shared owners. */\nexport function resetTelemetryMetricCollectors(): void {\n store().metricCollectors?.clear()\n}\n\nconst NOOP_SPAN: TelemetrySpan = { setAttributes() {} }\n\n/**\n * Run `fn` inside a span, from a package that must not depend on\n * `@open-mercato/telemetry`. With telemetry off this is `fn` plus one global\n * lookup \u2014 no span object is allocated and the OTEL SDK is never reached.\n *\n * Pass `root: true` for the unit of work a long-lived job should be sampled and\n * rendered by (a batch, a page) so the job is not one trace under one sampling\n * decision, and `links` to keep the causal chain back to what triggered it.\n */\nexport function withTelemetrySpan<T>(\n name: string,\n fn: (span: TelemetrySpan) => T,\n options?: TelemetrySpanOptions,\n): T {\n const runtime = getTelemetryRuntime()\n if (!runtime?.withSpan) return fn(NOOP_SPAN)\n return runtime.withSpan(name, fn, options)\n}\n\n/**\n * The active trace as a carrier, for use as a `links` entry. `undefined` when\n * telemetry is off or nothing is active, which `withTelemetrySpan` treats as\n * \"no link\" rather than an invalid one.\n */\nexport function captureTelemetryTrace(): TelemetryTraceCarrier | undefined {\n const carrier = getTelemetryRuntime()?.captureTraceContext()\n return carrier && Object.keys(carrier).length > 0 ? carrier : undefined\n}\n"],
5
+ "mappings": "AAiFA,MAAM,aAAa,uBAAO,IAAI,uCAAuC;AACrE,MAAM,mBAAmB,oBAAI,IAAI,CAAC,WAAW,UAAU,YAAY,MAAM,CAAC;AAQ1E,SAAS,QAA+B;AACtC,QAAM,cAAc;AACpB,MAAI,UAAU,YAAY,UAAU;AACpC,MAAI,CAAC,SAAS;AACZ,cAAU,CAAC;AACX,gBAAY,UAAU,IAAI;AAAA,EAC5B;AACA,SAAO;AACT;AAMO,SAAS,0BAA0B,KAAuB;AAC/D,QAAM,QAAQ,QACZ,OAAO,YAAY,cACf,SACA,QAAQ,IAAI;AAElB,SAAO,iBAAiB,KAAK,SAAS,IAAI,KAAK,EAAE,YAAY,CAAC;AAChE;AAEO,SAAS,yBAAyB,SAAuC;AAC9E,QAAM,EAAE,SAAS;AACjB,SAAO,MAAM;AACX,UAAM,UAAU,MAAM;AACtB,QAAI,QAAQ,WAAW,QAAS,SAAQ,SAAS;AAAA,EACnD;AACF;AAEO,SAAS,sBAAoD;AAClE,SAAO,MAAM,EAAE;AACjB;AAEO,SAAS,+BAA+B,UAAuD;AACpG,QAAM,EAAE,gBAAgB;AACxB,SAAO,MAAM;AACX,UAAM,UAAU,MAAM;AACtB,QAAI,QAAQ,kBAAkB,SAAU,SAAQ,gBAAgB;AAAA,EAClE;AACF;AAEO,SAAS,wBAAwB,MAAyD;AAC/F,QAAM,UAAU,oBAAoB;AACpC,MAAI,QAAS,SAAQ,YAAY,GAAG,IAAI;AAAA,MACnC,OAAM,EAAE,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AAOO,SAAS,wBAA2D;AACzE,QAAM,UAAU,oBAAoB,GAAG,oBAAoB;AAC3D,SAAO,WAAW,OAAO,KAAK,OAAO,EAAE,SAAS,IAAI,UAAU;AAChE;",
6
6
  "names": []
7
7
  }
@@ -1,4 +1,4 @@
1
- const APP_VERSION = "0.11.1-develop.8298.1.5323e2aa15";
1
+ const APP_VERSION = "0.11.1-develop.8317.1.dde3d38b64";
2
2
  const appVersion = APP_VERSION;
3
3
  export {
4
4
  APP_VERSION,
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/lib/version.ts"],
4
- "sourcesContent": ["// Build-time generated version\nexport const APP_VERSION = '0.11.1-develop.8298.1.5323e2aa15';\nexport const appVersion = APP_VERSION;\n"],
4
+ "sourcesContent": ["// Build-time generated version\nexport const APP_VERSION = '0.11.1-develop.8317.1.dde3d38b64';\nexport const appVersion = APP_VERSION;\n"],
5
5
  "mappings": "AACO,MAAM,cAAc;AACpB,MAAM,aAAa;",
6
6
  "names": []
7
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-mercato/shared",
3
- "version": "0.11.1-develop.8298.1.5323e2aa15",
3
+ "version": "0.11.1-develop.8317.1.dde3d38b64",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -130,7 +130,7 @@
130
130
  "@mikro-orm/core": "^7.1.14",
131
131
  "@mikro-orm/decorators": "^7.1.14",
132
132
  "@mikro-orm/postgresql": "^7.1.14",
133
- "@open-mercato/cache": "0.11.1-develop.8298.1.5323e2aa15",
133
+ "@open-mercato/cache": "0.11.1-develop.8317.1.dde3d38b64",
134
134
  "@types/big.js": "^7.0.0",
135
135
  "@types/html-to-text": "^9.0.4",
136
136
  "@types/sanitize-html": "^2.16.1",
@@ -6,6 +6,8 @@ import {
6
6
  recordTelemetryMetric,
7
7
  registerTelemetryMetricCollector,
8
8
  registerTelemetryRuntime,
9
+ registerTelemetryErrorReporter,
10
+ reportTelemetryError,
9
11
  resetTelemetryMetricCollectors,
10
12
  resetTelemetryRuntime,
11
13
  withTelemetrySpan,
@@ -17,6 +19,44 @@ import {
17
19
 
18
20
  type SpanCall = { name: string; options?: TelemetrySpanOptions }
19
21
 
22
+ describe('reportTelemetryError', () => {
23
+ afterEach(() => resetTelemetryRuntime())
24
+
25
+ it('prefers the complete runtime over the browser reporter without double reporting', () => {
26
+ const browserReporter = jest.fn()
27
+ const runtimeReporter = jest.fn()
28
+ const dispose = registerTelemetryErrorReporter(browserReporter)
29
+ registerTelemetryRuntime(createRuntime({ reportError: runtimeReporter }))
30
+ const error = new Error('[internal] lookup failed')
31
+ try {
32
+ reportTelemetryError(error, { module: 'ui', code: 'ui.address_types_load_failed' })
33
+ expect(runtimeReporter).toHaveBeenCalledWith(error, { module: 'ui', code: 'ui.address_types_load_failed' })
34
+ expect(browserReporter).not.toHaveBeenCalled()
35
+ } finally {
36
+ dispose()
37
+ }
38
+ })
39
+
40
+ it('keeps the current browser reporter when a previous registration is disposed', () => {
41
+ const previous = jest.fn()
42
+ const current = jest.fn()
43
+ const disposePrevious = registerTelemetryErrorReporter(previous)
44
+ const disposeCurrent = registerTelemetryErrorReporter(current)
45
+ const error = new Error('[internal] lookup failed')
46
+ try {
47
+ disposePrevious()
48
+ reportTelemetryError(error)
49
+ expect(previous).not.toHaveBeenCalled()
50
+ expect(current).toHaveBeenCalledWith(error)
51
+ disposeCurrent()
52
+ reportTelemetryError(error)
53
+ expect(current).toHaveBeenCalledTimes(1)
54
+ } finally {
55
+ disposeCurrent()
56
+ }
57
+ })
58
+ })
59
+
20
60
  function createRuntime(overrides: Partial<TelemetryRuntime> = {}): TelemetryRuntime {
21
61
  return {
22
62
  canUseGlobalTracePropagation: () => true,
@@ -84,6 +84,7 @@ const ENABLED_BACKENDS = new Set(['console', 'signoz', 'newrelic', 'otlp'])
84
84
 
85
85
  type TelemetryRuntimeStore = {
86
86
  active?: TelemetryRuntime
87
+ errorReporter?: TelemetryRuntime['reportError']
87
88
  metricCollectors?: Set<TelemetryMetricCollector>
88
89
  }
89
90
 
@@ -122,6 +123,20 @@ export function getTelemetryRuntime(): TelemetryRuntime | undefined {
122
123
  return store().active
123
124
  }
124
125
 
126
+ export function registerTelemetryErrorReporter(reporter: TelemetryRuntime['reportError']): () => void {
127
+ store().errorReporter = reporter
128
+ return () => {
129
+ const current = store()
130
+ if (current.errorReporter === reporter) current.errorReporter = undefined
131
+ }
132
+ }
133
+
134
+ export function reportTelemetryError(...args: Parameters<TelemetryRuntime['reportError']>): void {
135
+ const runtime = getTelemetryRuntime()
136
+ if (runtime) runtime.reportError(...args)
137
+ else store().errorReporter?.(...args)
138
+ }
139
+
125
140
  export function recordTelemetryMetric(point: TelemetryMetricPoint): boolean {
126
141
  const runtime = getTelemetryRuntime()
127
142
  if (!runtime?.recordMetric) return false