Skip to content

Commit 8ec64f0

Browse files
committed
feat(tracing-channel): Adopt bindTracingChannelToSpan in Nitro and redis
Migrate the Nitro HTTP/storage channels and the node/deno redis diagnostics_channel subscribers off the `@sentry/opentelemetry` `tracingChannel` helper onto `bindTracingChannelToSpan` from `@sentry/server-utils`, using the `auto` lifecycle. - Add `beforeSpanEnd(span, data)` to enrich the span on the canonical terminal event (sync `end` / async `asyncEnd`) right before it ends. - Add `captureError` (default `true`) to gate exception capture; the span error status is always set. Redis opts out (`captureError: false`) since command failures are captured at the boundary that owns them. - Unify the error mechanism to `auto.diagnostic_channels.bind_span`. - Create channel spans with `startInactiveSpan`; the binding activates the span as the async context, so parenting is preserved. - node and deno redis now pass the native `node:diagnostics_channel` `tracingChannel` directly; deno's bespoke factory is removed. BREAKING: remove the `@sentry/opentelemetry/tracing-channel` subpath export (shipped in 10.58.0); use `bindTracingChannelToSpan` instead.
1 parent 0db3b5a commit 8ec64f0

13 files changed

Lines changed: 405 additions & 935 deletions

File tree

‎dev-packages/e2e-tests/test-applications/nitro-3/tests/errors.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ test('Sends an error event to Sentry', async ({ request }) => {
1717
expect(errorEvent.exception?.values?.[0]?.mechanism).toEqual(
1818
expect.objectContaining({
1919
handled: false,
20-
type: 'auto.http.nitro.onTraceError',
20+
type: 'auto.diagnostic_channels.bind_span',
2121
}),
2222
);
2323
});

‎packages/deno/src/integrations/redis.ts‎

Lines changed: 5 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,9 @@
22
// lacking `tracingChannel` (added in Deno 1.44.3).
33
// On older runtimes the integration becomes a no-op.
44
import * as dc from 'node:diagnostics_channel';
5-
import type {
6-
RedisDiagnosticChannelResponseHook,
7-
RedisTracingChannel,
8-
RedisTracingChannelFactory,
9-
RedisTracingChannelSubscribers,
10-
} from '@sentry/server-utils';
5+
import type { RedisDiagnosticChannelResponseHook, RedisTracingChannelFactory } from '@sentry/server-utils';
116
import { subscribeRedisDiagnosticChannels } from '@sentry/server-utils';
12-
import type { Integration, IntegrationFn, Span } from '@sentry/core';
7+
import type { Integration, IntegrationFn } from '@sentry/core';
138
import { defineIntegration } from '@sentry/core';
149
import { setAsyncLocalStorageAsyncContextStrategy } from '../async';
1510

@@ -23,49 +18,17 @@ export interface DenoRedisIntegrationOptions {
2318
responseHook?: RedisDiagnosticChannelResponseHook;
2419
}
2520

26-
/**
27-
* Portable tracing-channel factory: wraps `node:diagnostics_channel.tracingChannel`
28-
* and stamps `data._sentrySpan` from `transformStart` in the `start` subscriber.
29-
*
30-
* Unlike `@sentry/opentelemetry/tracing-channel`, this does not call `bindStore`
31-
*/
32-
type DataWithSpan<T> = T & { _sentrySpan?: Span };
33-
type SubscriberFn<T> = (data: DataWithSpan<T>) => void;
34-
35-
const portableTracingChannel: RedisTracingChannelFactory = <T extends object>(
36-
name: string,
37-
transformStart: (data: T) => Span,
38-
): RedisTracingChannel<T> => {
39-
const channel = dc.tracingChannel<DataWithSpan<T>>(name);
40-
return {
41-
subscribe(subs: Partial<RedisTracingChannelSubscribers<T>>): void {
42-
const userStart = subs.start as SubscriberFn<T> | undefined;
43-
const composed: Record<string, SubscriberFn<T>> = {
44-
start(data) {
45-
data._sentrySpan = transformStart(data);
46-
userStart?.(data);
47-
},
48-
};
49-
for (const event of ['asyncStart', 'asyncEnd', 'end', 'error'] as const) {
50-
const fn = subs[event] as SubscriberFn<T> | undefined;
51-
if (fn) composed[event] = fn;
52-
}
53-
// Native subscribe is typed for the full subscriber set, but only the
54-
// handlers actually present are invoked at runtime.
55-
channel.subscribe(composed as unknown as Parameters<typeof channel.subscribe>[0]);
56-
},
57-
};
58-
};
59-
6021
const _denoRedisIntegration = ((options: DenoRedisIntegrationOptions = {}) => {
6122
return {
6223
name: INTEGRATION_NAME,
6324
setupOnce() {
6425
if (!dc.tracingChannel) {
6526
return;
6627
}
28+
// The span is bound into Deno's AsyncLocalStorage context via the async-context
29+
// strategy's `getTracingChannelBinding`, so the native channel can be passed directly.
6730
setAsyncLocalStorageAsyncContextStrategy();
68-
subscribeRedisDiagnosticChannels(portableTracingChannel, options.responseHook);
31+
subscribeRedisDiagnosticChannels(dc.tracingChannel as RedisTracingChannelFactory, options.responseHook);
6932
},
7033
};
7134
}) satisfies IntegrationFn;

‎packages/nitro/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"@sentry/bundler-plugin-core": "^5.3.0",
3939
"@sentry/core": "10.58.0",
4040
"@sentry/node": "10.58.0",
41-
"@sentry/opentelemetry": "10.58.0"
41+
"@sentry/server-utils": "10.58.0"
4242
},
4343
"devDependencies": {
4444
"nitro": "^3.0.260415-beta",

‎packages/nitro/src/runtime/hooks/captureStorageEvents.ts‎

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1+
import { tracingChannel } from 'node:diagnostics_channel';
12
import {
2-
captureException,
33
flushIfServerless,
44
GLOBAL_OBJ,
55
SEMANTIC_ATTRIBUTE_CACHE_HIT,
66
SEMANTIC_ATTRIBUTE_CACHE_KEY,
77
SEMANTIC_ATTRIBUTE_SENTRY_OP,
88
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
9-
SPAN_STATUS_ERROR,
109
SPAN_STATUS_OK,
11-
startSpanManual,
10+
startInactiveSpan,
1211
} from '@sentry/core';
13-
import { tracingChannel, type TracingChannelContextWithSpan } from '@sentry/opentelemetry/tracing-channel';
12+
import { bindTracingChannelToSpan } from '@sentry/server-utils';
1413
import type { TraceContext } from 'unstorage/tracing';
1514

1615
const ORIGIN = 'auto.cache.nitro';
@@ -57,11 +56,12 @@ function setupStorageTracingChannel(operation: TracedOperation): void {
5756
const keys = (data: TraceContext): string[] => data.keys ?? [];
5857
const mountBase = (data: TraceContext): string => (data.base ?? '').replace(/:$/, '');
5958

60-
const channel = tracingChannel<TraceContext>(`unstorage.${operation}`, data => {
61-
const cacheKeys = keys(data);
59+
bindTracingChannelToSpan(
60+
tracingChannel<TraceContext>(`unstorage.${operation}`),
61+
data => {
62+
const cacheKeys = keys(data);
6263

63-
return startSpanManual(
64-
{
64+
return startInactiveSpan({
6565
name: cacheKeys.join(', ') || operation,
6666
attributes: {
6767
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: `cache.${normalizeMethodName(operation)}`,
@@ -71,34 +71,24 @@ function setupStorageTracingChannel(operation: TracedOperation): void {
7171
'db.collection.name': mountBase(data),
7272
'db.system.name': data.driver?.name ?? 'unknown',
7373
},
74-
},
75-
span => span,
76-
);
77-
});
78-
79-
channel.subscribe({
80-
asyncEnd(data: TracingChannelContextWithSpan<TraceContext & { result?: unknown }>) {
81-
if (data._sentrySpan && CACHE_HIT_OPERATIONS.has(operation)) {
82-
const hit = operation === 'hasItem' ? Boolean(data.result) : isCacheHit(data.keys?.[0], data.result);
83-
data._sentrySpan.setAttribute(SEMANTIC_ATTRIBUTE_CACHE_HIT, hit);
84-
}
85-
86-
data._sentrySpan?.setStatus({ code: SPAN_STATUS_OK });
87-
data._sentrySpan?.end();
88-
89-
void flushIfServerless();
90-
},
91-
error(data: TracingChannelContextWithSpan<TraceContext & { error?: unknown }>) {
92-
captureException(data.error, {
93-
mechanism: { handled: false, type: ORIGIN },
9474
});
95-
96-
data._sentrySpan?.setStatus({ code: SPAN_STATUS_ERROR, message: 'internal_error' });
97-
data._sentrySpan?.end();
98-
99-
void flushIfServerless();
10075
},
101-
});
76+
{
77+
beforeSpanEnd(span, data) {
78+
// Auto-capture + error status is handled by the binding; only enrich the success path.
79+
if (!('error' in data)) {
80+
const result = (data as { result?: unknown }).result;
81+
if (CACHE_HIT_OPERATIONS.has(operation)) {
82+
const hit = operation === 'hasItem' ? Boolean(result) : isCacheHit(data.keys?.[0], result);
83+
span.setAttribute(SEMANTIC_ATTRIBUTE_CACHE_HIT, hit);
84+
}
85+
span.setStatus({ code: SPAN_STATUS_OK });
86+
}
87+
88+
void flushIfServerless();
89+
},
90+
},
91+
);
10292
}
10393

10494
function normalizeMethodName(methodName: string): string {

0 commit comments

Comments
 (0)