@syncular/observability-sentry 0.0.1-89

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.
package/src/browser.ts ADDED
@@ -0,0 +1,215 @@
1
+ import * as Sentry from '@sentry/react';
2
+ import {
3
+ configureSyncTelemetry,
4
+ type SyncMetricOptions,
5
+ type SyncTelemetry,
6
+ type SyncTelemetryAttributeValue,
7
+ } from '@syncular/core';
8
+ import { createSentrySyncTelemetry } from './shared';
9
+
10
+ export type BrowserSentryInitOptions = Parameters<typeof Sentry.init>[0];
11
+ export type BrowserSentryCaptureMessageLevel = Parameters<
12
+ typeof Sentry.captureMessage
13
+ >[1];
14
+
15
+ interface BrowserSentryCaptureMessageOptions {
16
+ level?: BrowserSentryCaptureMessageLevel;
17
+ tags?: Record<string, string>;
18
+ }
19
+
20
+ type BrowserSentryLogLevel =
21
+ | 'trace'
22
+ | 'debug'
23
+ | 'info'
24
+ | 'warn'
25
+ | 'error'
26
+ | 'fatal';
27
+
28
+ interface BrowserSentryLogOptions {
29
+ level?: BrowserSentryLogLevel;
30
+ attributes?: Record<string, SyncTelemetryAttributeValue>;
31
+ }
32
+
33
+ function resolveBrowserLogMethod(
34
+ level: BrowserSentryLogLevel
35
+ ):
36
+ | ((
37
+ message: string,
38
+ attributes?: Record<string, SyncTelemetryAttributeValue>
39
+ ) => void)
40
+ | null {
41
+ switch (level) {
42
+ case 'trace':
43
+ return Sentry.logger.trace ?? Sentry.logger.debug ?? Sentry.logger.info;
44
+ case 'debug':
45
+ return Sentry.logger.debug ?? Sentry.logger.info;
46
+ case 'info':
47
+ return Sentry.logger.info;
48
+ case 'warn':
49
+ return Sentry.logger.warn ?? Sentry.logger.info;
50
+ case 'error':
51
+ return Sentry.logger.error ?? Sentry.logger.warn ?? Sentry.logger.info;
52
+ case 'fatal':
53
+ return (
54
+ Sentry.logger.fatal ??
55
+ Sentry.logger.error ??
56
+ Sentry.logger.warn ??
57
+ Sentry.logger.info
58
+ );
59
+ default:
60
+ return Sentry.logger.info;
61
+ }
62
+ }
63
+
64
+ function toCountMetricOptions(
65
+ options?: SyncMetricOptions
66
+ ): Parameters<typeof Sentry.metrics.count>[2] | undefined {
67
+ if (!options?.attributes) return undefined;
68
+ return { attributes: options.attributes };
69
+ }
70
+
71
+ function toValueMetricOptions(
72
+ options?: SyncMetricOptions
73
+ ): Parameters<typeof Sentry.metrics.gauge>[2] | undefined {
74
+ if (!options) return undefined;
75
+ const hasAttributes = Boolean(options.attributes);
76
+ const hasUnit = Boolean(options.unit);
77
+ if (!hasAttributes && !hasUnit) return undefined;
78
+ return {
79
+ attributes: options.attributes,
80
+ unit: options.unit,
81
+ };
82
+ }
83
+
84
+ /**
85
+ * Create a Syncular telemetry backend wired to `@sentry/react`.
86
+ */
87
+ export function createBrowserSentryTelemetry(): SyncTelemetry {
88
+ return createSentrySyncTelemetry({
89
+ logger: Sentry.logger,
90
+ startSpan(options, callback) {
91
+ return Sentry.startSpan(options, (span) =>
92
+ callback({
93
+ setAttribute(name, value) {
94
+ span.setAttribute(name, value);
95
+ },
96
+ setAttributes(attributes) {
97
+ span.setAttributes(attributes);
98
+ },
99
+ setStatus(status) {
100
+ span.setStatus({
101
+ code: status === 'ok' ? 1 : 2,
102
+ });
103
+ },
104
+ })
105
+ );
106
+ },
107
+ metrics: {
108
+ count(name, value, options) {
109
+ const metricOptions = toCountMetricOptions(options);
110
+ if (metricOptions) {
111
+ Sentry.metrics.count(name, value, metricOptions);
112
+ return;
113
+ }
114
+ Sentry.metrics.count(name, value);
115
+ },
116
+ gauge(name, value, options) {
117
+ const metricOptions = toValueMetricOptions(options);
118
+ if (metricOptions) {
119
+ Sentry.metrics.gauge(name, value, metricOptions);
120
+ return;
121
+ }
122
+ Sentry.metrics.gauge(name, value);
123
+ },
124
+ distribution(name, value, options) {
125
+ const metricOptions = toValueMetricOptions(options);
126
+ if (metricOptions) {
127
+ Sentry.metrics.distribution(name, value, metricOptions);
128
+ return;
129
+ }
130
+ Sentry.metrics.distribution(name, value);
131
+ },
132
+ },
133
+ captureException(error) {
134
+ Sentry.captureException(error);
135
+ },
136
+ });
137
+ }
138
+
139
+ /**
140
+ * Configure Syncular core telemetry to use the browser Sentry adapter.
141
+ */
142
+ export function configureBrowserSentryTelemetry(): SyncTelemetry {
143
+ const telemetry = createBrowserSentryTelemetry();
144
+ configureSyncTelemetry(telemetry);
145
+ return telemetry;
146
+ }
147
+
148
+ /**
149
+ * Initialize browser Sentry and configure Syncular telemetry.
150
+ */
151
+ export function initAndConfigureBrowserSentry(
152
+ options: BrowserSentryInitOptions
153
+ ): SyncTelemetry {
154
+ const configuredOptions = ensureBrowserTracingIntegration(options);
155
+ Sentry.init(configuredOptions);
156
+ return configureBrowserSentryTelemetry();
157
+ }
158
+
159
+ function ensureBrowserTracingIntegration(
160
+ options: BrowserSentryInitOptions
161
+ ): BrowserSentryInitOptions {
162
+ const integrations = options.integrations;
163
+ if (typeof integrations === 'function') return options;
164
+
165
+ const configuredIntegrations = integrations ?? [];
166
+ const hasBrowserTracing = configuredIntegrations.some(
167
+ (integration) => integration.name === 'BrowserTracing'
168
+ );
169
+ if (hasBrowserTracing) return options;
170
+
171
+ return {
172
+ ...options,
173
+ integrations: [
174
+ Sentry.browserTracingIntegration(),
175
+ ...configuredIntegrations,
176
+ ],
177
+ };
178
+ }
179
+
180
+ /**
181
+ * Capture a browser message in Sentry with optional tags.
182
+ */
183
+ export function captureBrowserSentryMessage(
184
+ message: string,
185
+ options?: BrowserSentryCaptureMessageOptions
186
+ ): void {
187
+ if (!options?.tags || Object.keys(options.tags).length === 0) {
188
+ Sentry.captureMessage(message, options?.level);
189
+ return;
190
+ }
191
+
192
+ Sentry.withScope((scope) => {
193
+ for (const [name, value] of Object.entries(options.tags ?? {})) {
194
+ scope.setTag(name, value);
195
+ }
196
+ Sentry.captureMessage(message, options?.level);
197
+ });
198
+ }
199
+
200
+ /**
201
+ * Emit a browser Sentry log entry.
202
+ */
203
+ export function logBrowserSentryMessage(
204
+ message: string,
205
+ options?: BrowserSentryLogOptions
206
+ ): void {
207
+ const level = options?.level ?? 'info';
208
+ const logMethod = resolveBrowserLogMethod(level);
209
+ if (!logMethod) return;
210
+ if (!options?.attributes || Object.keys(options.attributes).length === 0) {
211
+ logMethod(message);
212
+ return;
213
+ }
214
+ logMethod(message, options.attributes);
215
+ }
@@ -0,0 +1,187 @@
1
+ import * as Sentry from '@sentry/cloudflare';
2
+ import {
3
+ configureSyncTelemetry,
4
+ type SyncMetricOptions,
5
+ type SyncTelemetry,
6
+ type SyncTelemetryAttributeValue,
7
+ } from '@syncular/core';
8
+ import { createSentrySyncTelemetry } from './shared';
9
+
10
+ function toCountMetricOptions(
11
+ options?: SyncMetricOptions
12
+ ): Parameters<typeof Sentry.metrics.count>[2] | undefined {
13
+ if (!options?.attributes) return undefined;
14
+ return { attributes: options.attributes };
15
+ }
16
+
17
+ function toValueMetricOptions(
18
+ options?: SyncMetricOptions
19
+ ): Parameters<typeof Sentry.metrics.gauge>[2] | undefined {
20
+ if (!options) return undefined;
21
+ const hasAttributes = Boolean(options.attributes);
22
+ const hasUnit = Boolean(options.unit);
23
+ if (!hasAttributes && !hasUnit) return undefined;
24
+ return {
25
+ attributes: options.attributes,
26
+ unit: options.unit,
27
+ };
28
+ }
29
+
30
+ export type CloudflareSentryCaptureMessageLevel = Parameters<
31
+ typeof Sentry.captureMessage
32
+ >[1];
33
+
34
+ interface CloudflareSentryCaptureMessageOptions {
35
+ level?: CloudflareSentryCaptureMessageLevel;
36
+ tags?: Record<string, string>;
37
+ }
38
+
39
+ type CloudflareSentryLogLevel =
40
+ | 'trace'
41
+ | 'debug'
42
+ | 'info'
43
+ | 'warn'
44
+ | 'error'
45
+ | 'fatal';
46
+
47
+ interface CloudflareSentryLogOptions {
48
+ level?: CloudflareSentryLogLevel;
49
+ attributes?: Record<string, SyncTelemetryAttributeValue>;
50
+ }
51
+
52
+ function resolveCloudflareLogMethod(
53
+ level: CloudflareSentryLogLevel
54
+ ):
55
+ | ((
56
+ message: string,
57
+ attributes?: Record<string, SyncTelemetryAttributeValue>
58
+ ) => void)
59
+ | null {
60
+ switch (level) {
61
+ case 'trace':
62
+ return Sentry.logger.trace ?? Sentry.logger.debug ?? Sentry.logger.info;
63
+ case 'debug':
64
+ return Sentry.logger.debug ?? Sentry.logger.info;
65
+ case 'info':
66
+ return Sentry.logger.info;
67
+ case 'warn':
68
+ return Sentry.logger.warn ?? Sentry.logger.info;
69
+ case 'error':
70
+ return Sentry.logger.error ?? Sentry.logger.warn ?? Sentry.logger.info;
71
+ case 'fatal':
72
+ return (
73
+ Sentry.logger.fatal ??
74
+ Sentry.logger.error ??
75
+ Sentry.logger.warn ??
76
+ Sentry.logger.info
77
+ );
78
+ default:
79
+ return Sentry.logger.info;
80
+ }
81
+ }
82
+
83
+ /**
84
+ * Re-export Cloudflare Sentry worker wrapper.
85
+ */
86
+ export const withCloudflareSentry = Sentry.withSentry;
87
+
88
+ /**
89
+ * Create a Syncular telemetry backend wired to `@sentry/cloudflare`.
90
+ */
91
+ export function createCloudflareSentryTelemetry(): SyncTelemetry {
92
+ return createSentrySyncTelemetry({
93
+ logger: Sentry.logger,
94
+ startSpan(options, callback) {
95
+ return Sentry.startSpan(options, (span) =>
96
+ callback({
97
+ setAttribute(name, value) {
98
+ span.setAttribute(name, value);
99
+ },
100
+ setAttributes(attributes) {
101
+ span.setAttributes(attributes);
102
+ },
103
+ setStatus(status) {
104
+ span.setStatus({
105
+ code: status === 'ok' ? 1 : 2,
106
+ });
107
+ },
108
+ })
109
+ );
110
+ },
111
+ metrics: {
112
+ count(name, value, options) {
113
+ const metricOptions = toCountMetricOptions(options);
114
+ if (metricOptions) {
115
+ Sentry.metrics.count(name, value, metricOptions);
116
+ return;
117
+ }
118
+ Sentry.metrics.count(name, value);
119
+ },
120
+ gauge(name, value, options) {
121
+ const metricOptions = toValueMetricOptions(options);
122
+ if (metricOptions) {
123
+ Sentry.metrics.gauge(name, value, metricOptions);
124
+ return;
125
+ }
126
+ Sentry.metrics.gauge(name, value);
127
+ },
128
+ distribution(name, value, options) {
129
+ const metricOptions = toValueMetricOptions(options);
130
+ if (metricOptions) {
131
+ Sentry.metrics.distribution(name, value, metricOptions);
132
+ return;
133
+ }
134
+ Sentry.metrics.distribution(name, value);
135
+ },
136
+ },
137
+ captureException(error) {
138
+ Sentry.captureException(error);
139
+ },
140
+ });
141
+ }
142
+
143
+ /**
144
+ * Configure Syncular core telemetry to use the Cloudflare Sentry adapter.
145
+ */
146
+ export function configureCloudflareSentryTelemetry(): SyncTelemetry {
147
+ const telemetry = createCloudflareSentryTelemetry();
148
+ configureSyncTelemetry(telemetry);
149
+ return telemetry;
150
+ }
151
+
152
+ /**
153
+ * Capture a worker message in Sentry with optional tags.
154
+ */
155
+ export function captureCloudflareSentryMessage(
156
+ message: string,
157
+ options?: CloudflareSentryCaptureMessageOptions
158
+ ): void {
159
+ if (!options?.tags || Object.keys(options.tags).length === 0) {
160
+ Sentry.captureMessage(message, options?.level);
161
+ return;
162
+ }
163
+
164
+ Sentry.withScope((scope) => {
165
+ for (const [name, value] of Object.entries(options.tags ?? {})) {
166
+ scope.setTag(name, value);
167
+ }
168
+ Sentry.captureMessage(message, options?.level);
169
+ });
170
+ }
171
+
172
+ /**
173
+ * Emit a Cloudflare Sentry log entry.
174
+ */
175
+ export function logCloudflareSentryMessage(
176
+ message: string,
177
+ options?: CloudflareSentryLogOptions
178
+ ): void {
179
+ const level = options?.level ?? 'info';
180
+ const logMethod = resolveCloudflareLogMethod(level);
181
+ if (!logMethod) return;
182
+ if (!options?.attributes || Object.keys(options.attributes).length === 0) {
183
+ logMethod(message);
184
+ return;
185
+ }
186
+ logMethod(message, options.attributes);
187
+ }
package/src/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from './browser';
2
+ export * from './cloudflare';
3
+ export * from './shared';
@@ -0,0 +1,145 @@
1
+ import { describe, expect, test } from 'bun:test';
2
+ import type { SyncSpanOptions } from '@syncular/core';
3
+ import {
4
+ createSentrySyncTelemetry,
5
+ type SentryTelemetryAdapter,
6
+ } from './shared';
7
+
8
+ describe('createSentrySyncTelemetry', () => {
9
+ test('routes logs, spans, metrics, and exceptions', () => {
10
+ const logs: Array<{
11
+ level: string;
12
+ message: string;
13
+ attributes?: Record<string, unknown>;
14
+ }> = [];
15
+ const spans: SyncSpanOptions[] = [];
16
+ const metricCalls: Array<{ type: string; name: string; value: number }> =
17
+ [];
18
+ const exceptions: unknown[] = [];
19
+
20
+ const adapter: SentryTelemetryAdapter = {
21
+ logger: {
22
+ info(message, attributes) {
23
+ logs.push({ level: 'info', message, attributes });
24
+ },
25
+ error(message, attributes) {
26
+ logs.push({ level: 'error', message, attributes });
27
+ },
28
+ },
29
+ startSpan(options, callback) {
30
+ spans.push(options);
31
+ return callback({
32
+ setAttribute() {},
33
+ setAttributes() {},
34
+ setStatus() {},
35
+ });
36
+ },
37
+ metrics: {
38
+ count(name, value) {
39
+ metricCalls.push({ type: 'count', name, value });
40
+ },
41
+ gauge(name, value) {
42
+ metricCalls.push({ type: 'gauge', name, value });
43
+ },
44
+ distribution(name, value) {
45
+ metricCalls.push({ type: 'distribution', name, value });
46
+ },
47
+ },
48
+ captureException(error) {
49
+ exceptions.push(error);
50
+ },
51
+ };
52
+
53
+ const telemetry = createSentrySyncTelemetry(adapter);
54
+
55
+ telemetry.log({ event: 'sync.ok', rowCount: 2 });
56
+ telemetry.log({ event: 'sync.fail', error: 'boom' });
57
+
58
+ const spanValue = telemetry.tracer.startSpan(
59
+ {
60
+ name: 'sync.span',
61
+ op: 'sync',
62
+ },
63
+ (span) => {
64
+ span.setAttribute('transport', 'ws');
65
+ span.setStatus('ok');
66
+ return 123;
67
+ }
68
+ );
69
+
70
+ telemetry.metrics.count('sync.count');
71
+ telemetry.metrics.gauge('sync.gauge', 7);
72
+ telemetry.metrics.distribution('sync.dist', 42);
73
+ telemetry.captureException(new Error('crash'), { requestId: 'r1' });
74
+
75
+ expect(spanValue).toBe(123);
76
+ expect(logs[0]).toEqual({
77
+ level: 'info',
78
+ message: 'sync.ok',
79
+ attributes: { rowCount: 2 },
80
+ });
81
+ expect(logs[1]).toEqual({
82
+ level: 'error',
83
+ message: 'sync.fail',
84
+ attributes: { error: 'boom' },
85
+ });
86
+ expect(spans).toEqual([
87
+ {
88
+ name: 'sync.span',
89
+ op: 'sync',
90
+ },
91
+ ]);
92
+ expect(metricCalls).toEqual([
93
+ { type: 'count', name: 'sync.count', value: 1 },
94
+ { type: 'gauge', name: 'sync.gauge', value: 7 },
95
+ { type: 'distribution', name: 'sync.dist', value: 42 },
96
+ ]);
97
+ expect(exceptions).toHaveLength(1);
98
+ expect(logs.at(-1)).toEqual({
99
+ level: 'error',
100
+ message: 'sync.exception.context',
101
+ attributes: { requestId: 'r1' },
102
+ });
103
+ });
104
+
105
+ test('sanitizes non-primitive log attributes', () => {
106
+ const logs: Array<{
107
+ level: string;
108
+ message: string;
109
+ attributes?: Record<string, unknown>;
110
+ }> = [];
111
+
112
+ const telemetry = createSentrySyncTelemetry({
113
+ logger: {
114
+ info(message, attributes) {
115
+ logs.push({ level: 'info', message, attributes });
116
+ },
117
+ },
118
+ });
119
+
120
+ telemetry.log({
121
+ event: 'sync.attributes',
122
+ id: 'abc',
123
+ nested: { ok: true },
124
+ values: [1, 2, 3],
125
+ enabled: true,
126
+ count: 3,
127
+ ignored: undefined,
128
+ nonFinite: Number.POSITIVE_INFINITY,
129
+ });
130
+
131
+ expect(logs).toEqual([
132
+ {
133
+ level: 'info',
134
+ message: 'sync.attributes',
135
+ attributes: {
136
+ id: 'abc',
137
+ nested: '{"ok":true}',
138
+ values: '[1,2,3]',
139
+ enabled: true,
140
+ count: 3,
141
+ },
142
+ },
143
+ ]);
144
+ });
145
+ });