Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions dev-packages/bun-integration-tests/node-suites/excludes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,6 @@ const NOT_TRIAGED = [
'suites/tracing/tracer-start-active-span-error/test.ts',
];

// Bun garbage-collects a diagnostics channel that no code references, and its subscribers with it.
// `graphql` 17 publishes its own tracing channels, so the integration only subscribes to them, and
// no spans arrive.
const CHANNEL_GARBAGE_COLLECTED = ['suites/tracing/graphql-tracing-channel/**'];

export const NODE_SUITES_EXCLUDE = [
'**/node_modules/**',
...NODE_ONLY,
Expand All @@ -158,5 +153,4 @@ export const NODE_SUITES_EXCLUDE = [
...NO_OUTGOING_HTTP_INSTRUMENTATION,
...NO_AUTO_INSTRUMENTATION,
...NOT_TRIAGED,
...CHANNEL_GARBAGE_COLLECTED,
];
25 changes: 25 additions & 0 deletions packages/bun/test/integrations/diagnosticsChannelGc.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { channel } from 'node:diagnostics_channel';
import { describe, expect, test } from 'bun:test';
import { init } from '../../src';

const EXPRESS_HANDLE_START = 'tracing:orchestrion:express:handle:start';

function nextTask(): Promise<void> {
return new Promise(resolve => setTimeout(resolve, 0));
}

describe('channel-based integrations', () => {
// Bun garbage-collects a diagnostics channel that no code references, together with its
// subscribers. A GC in the same task does not collect it, so the check runs in a later task.
test('stay subscribed after a garbage collection', async () => {
init({ dsn: 'https://username@domain/123', tracesSampleRate: 1 });
await nextTask();

expect(channel(EXPRESS_HANDLE_START).hasSubscribers).toBe(true);

Bun.gc(true);
await nextTask();

expect(channel(EXPRESS_HANDLE_START).hasSubscribers).toBe(true);
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { subscribe } from 'node:diagnostics_channel';
import { context, trace } from '@opentelemetry/api';
import type { ClientRequest, IncomingMessage } from 'node:http';
import type { Span } from '@sentry/core';
Expand All @@ -10,6 +9,7 @@ import {
HTTP_ON_CLIENT_REQUEST,
patchHttpModuleClient,
} from '@sentry/core/server';
import { subscribeDiagnosticsChannel } from '@sentry/server-utils';
import { NODE_VERSION } from '../../nodeVersion';
import { errorMonitor } from 'node:events';
import * as http from 'node:http';
Expand Down Expand Up @@ -125,7 +125,7 @@ export function instrumentHttpOutgoingRequests(

function instrumentHttpOutgoingRequestsViaChannel(options: HttpInstrumentationOptions): void {
const { [HTTP_ON_CLIENT_REQUEST]: onHttpClientRequestCreated } = getHttpClientSubscriptions(options);
subscribe(HTTP_ON_CLIENT_REQUEST, onHttpClientRequestCreated);
subscribeDiagnosticsChannel(HTTP_ON_CLIENT_REQUEST, onHttpClientRequestCreated);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { subscribe } from 'node:diagnostics_channel';
import type { RequestOptions } from 'node:http';
import { context, createContextKey, propagation } from '@opentelemetry/api';
import type { Integration, IntegrationFn } from '@sentry/core';
import { addNonEnumerableProperty, debug, getClient } from '@sentry/core';
import type { HttpIncomingMessage, HttpServerResponse } from '@sentry/core/server';
import { getHttpServerSubscriptions, HTTP_ON_SERVER_REQUEST, recordRequestSession } from '@sentry/core/server';
import { subscribeDiagnosticsChannel } from '@sentry/server-utils';
import type { RequestEventData } from '@sentry/core';
import { DEBUG_BUILD } from '../../debug-build';

Expand Down Expand Up @@ -143,7 +143,7 @@ const _httpServerIntegration = ((options: HttpServerIntegrationOptions = {}) =>
name: INTEGRATION_NAME,
setupOnce() {
const { [HTTP_ON_SERVER_REQUEST]: onHttpServerRequestStart } = getHttpServerSubscriptions(_options);
subscribe(HTTP_ON_SERVER_REQUEST, onHttpServerRequestStart);
subscribeDiagnosticsChannel(HTTP_ON_SERVER_REQUEST, onHttpServerRequestStart);
},
afterAllSetup(client) {
if (DEBUG_BUILD && client.getIntegrationByName('Http')) {
Expand Down
1 change: 1 addition & 0 deletions packages/server-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export type { InstrumentationConfig } from './orchestrion/apmTypes';
// `orchestrion/bundler/moduleInjectedTransform.ts`); it is a plain runtime
// helper with no orchestrion build-time dependency.
export { orchestrionModuleInjected } from './utils/moduleInjected';
export { subscribe as subscribeDiagnosticsChannel } from './utils/diagnosticsChannel';
export { eveConversationHook, eveIntegration } from './eve';
export { getInstrumentedModuleNames } from './orchestrion/config';
export {
Expand Down
2 changes: 1 addition & 1 deletion packages/server-utils/src/integrations/amqplib.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable max-lines */
import * as diagnosticsChannel from 'node:diagnostics_channel';
import * as diagnosticsChannel from '../utils/diagnosticsChannel';
import type { IntegrationFn, Span, SpanAttributes } from '@sentry/core';
import {
continueTrace,
Expand Down
2 changes: 1 addition & 1 deletion packages/server-utils/src/integrations/anthropic.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GEN_AI_REQUEST_MODEL } from '@sentry/conventions/attributes';
import * as diagnosticsChannel from 'node:diagnostics_channel';
import * as diagnosticsChannel from '../utils/diagnosticsChannel';
import type { IntegrationFn, Span, SpanAttributeValue } from '@sentry/core';
import {
_INTERNAL_shouldSkipAiProviderWrapping,
Expand Down
2 changes: 1 addition & 1 deletion packages/server-utils/src/integrations/aws-sdk/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as diagnosticsChannel from 'node:diagnostics_channel';
import * as diagnosticsChannel from '../../utils/diagnosticsChannel';
import type { IntegrationFn, Span } from '@sentry/core';
import { defineIntegration, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, startInactiveSpan } from '@sentry/core';
import {
Expand Down
2 changes: 1 addition & 1 deletion packages/server-utils/src/integrations/dataloader.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as diagnosticsChannel from 'node:diagnostics_channel';
import * as diagnosticsChannel from '../utils/diagnosticsChannel';
import {
CACHE_KEY,
CACHE_OPERATION,
Expand Down
2 changes: 1 addition & 1 deletion packages/server-utils/src/integrations/express/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as diagnosticsChannel from 'node:diagnostics_channel';
import * as diagnosticsChannel from '../../utils/diagnosticsChannel';
import type { IntegrationFn } from '@sentry/core';
import { defineIntegration } from '@sentry/core';
import { expressModuleNames } from '../../orchestrion/config/express';
Expand Down
2 changes: 1 addition & 1 deletion packages/server-utils/src/integrations/fastify/errors.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { FastifyIntegration, FastifyReply, FastifyRequest } from './types';
import * as diagnosticsChannel from 'node:diagnostics_channel';
import * as diagnosticsChannel from '../../utils/diagnosticsChannel';
import { addNonEnumerableProperty, captureException, getClient } from '@sentry/core';
import { defaultShouldHandleError, INTEGRATION_NAME } from './utils';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
/* eslint-disable @typescript-eslint/no-this-alias */
/* eslint-disable max-lines */

import * as diagnosticsChannel from 'node:diagnostics_channel';
import * as diagnosticsChannel from '../../utils/diagnosticsChannel';
import {
HTTP_REQUEST_METHOD,
HTTP_RESPONSE_STATUS_CODE,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as diagnosticsChannel from 'node:diagnostics_channel';
import * as diagnosticsChannel from '../../utils/diagnosticsChannel';
import { CHANNELS } from '../../orchestrion/channels';
import { bindTracingChannelToSpan, safeChannelCallback } from '../../tracing-channel';
import type { FirestoreReference } from './firestore-types';
Expand Down
2 changes: 1 addition & 1 deletion packages/server-utils/src/integrations/generic-pool.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as diagnosticsChannel from 'node:diagnostics_channel';
import * as diagnosticsChannel from '../utils/diagnosticsChannel';
import { SENTRY_OP } from '@sentry/conventions/attributes';
import { DB } from '@sentry/conventions/op';
import type { IntegrationFn } from '@sentry/core';
Expand Down
2 changes: 1 addition & 1 deletion packages/server-utils/src/integrations/google-genai.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GEN_AI_REQUEST_MODEL, SENTRY_OP, SENTRY_ORIGIN } from '@sentry/conventions/attributes';
import * as diagnosticsChannel from 'node:diagnostics_channel';
import * as diagnosticsChannel from '../utils/diagnosticsChannel';
import type { IntegrationFn, Span } from '@sentry/core';
import {
_INTERNAL_shouldSkipAiProviderWrapping,
Expand Down
2 changes: 1 addition & 1 deletion packages/server-utils/src/integrations/graphql/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as diagnosticsChannel from 'node:diagnostics_channel';
import * as diagnosticsChannel from '../../utils/diagnosticsChannel';
import type { IntegrationFn } from '@sentry/core';
import { defineIntegration, waitForTracingChannelBinding } from '@sentry/core';
import { subscribeGraphqlDiagnosticChannels, type GraphQLOptions } from './graphql-dc-subscriber';
Expand Down
2 changes: 1 addition & 1 deletion packages/server-utils/src/integrations/hapi/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as diagnosticsChannel from 'node:diagnostics_channel';
import * as diagnosticsChannel from '../../utils/diagnosticsChannel';
import type { IntegrationFn } from '@sentry/core';
import { defineIntegration } from '@sentry/core';
import { CHANNELS } from '../../orchestrion/channels';
Expand Down
2 changes: 1 addition & 1 deletion packages/server-utils/src/integrations/kafkajs/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as diagnosticsChannel from 'node:diagnostics_channel';
import * as diagnosticsChannel from '../../utils/diagnosticsChannel';
import type { TracingChannelSubscribers } from 'node:diagnostics_channel';
import type { IntegrationFn, Span } from '@sentry/core';
import { defineIntegration } from '@sentry/core';
Expand Down
2 changes: 1 addition & 1 deletion packages/server-utils/src/integrations/knex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// emit them deliberately to preserve parity with what `@opentelemetry/instrumentation-knex` produced.
/* oxlint-disable typescript/no-deprecated */

import * as diagnosticsChannel from 'node:diagnostics_channel';
import * as diagnosticsChannel from '../utils/diagnosticsChannel';
import type { IntegrationFn, Span, SpanAttributes } from '@sentry/core';
import {
debug,
Expand Down
2 changes: 1 addition & 1 deletion packages/server-utils/src/integrations/koa/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as diagnosticsChannel from 'node:diagnostics_channel';
import * as diagnosticsChannel from '../../utils/diagnosticsChannel';
import type { IntegrationFn } from '@sentry/core';
import {
addNonEnumerableProperty,
Expand Down
2 changes: 1 addition & 1 deletion packages/server-utils/src/integrations/langchain.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as diagnosticsChannel from 'node:diagnostics_channel';
import * as diagnosticsChannel from '../utils/diagnosticsChannel';
import type { IntegrationFn, Span } from '@sentry/core';
import { _INTERNAL_skipAiProviderWrapping, defineIntegration, startInactiveSpan } from '@sentry/core';
import { ANTHROPIC_AI_INTEGRATION_NAME } from '../ai/anthropic-ai/constants';
Expand Down
2 changes: 1 addition & 1 deletion packages/server-utils/src/integrations/langgraph.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as diagnosticsChannel from 'node:diagnostics_channel';
import * as diagnosticsChannel from '../utils/diagnosticsChannel';
import type { IntegrationFn } from '@sentry/core';
import { debug, defineIntegration } from '@sentry/core';
import { resolveAIRecordingOptions } from '../ai/core/utils';
Expand Down
2 changes: 1 addition & 1 deletion packages/server-utils/src/integrations/lru-memoizer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as diagnosticsChannel from 'node:diagnostics_channel';
import * as diagnosticsChannel from '../utils/diagnosticsChannel';
import type { IntegrationFn } from '@sentry/core';
import { defineIntegration } from '@sentry/core';
import { CHANNELS } from '../orchestrion/channels';
Expand Down
2 changes: 1 addition & 1 deletion packages/server-utils/src/integrations/mastra.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as diagnosticsChannel from 'node:diagnostics_channel';
import * as diagnosticsChannel from '../utils/diagnosticsChannel';
import { createRequire } from 'node:module';
import { join } from 'node:path';
import { fileURLToPath } from 'node:url';
Expand Down
2 changes: 1 addition & 1 deletion packages/server-utils/src/integrations/mistral.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as diagnosticsChannel from 'node:diagnostics_channel';
import * as diagnosticsChannel from '../utils/diagnosticsChannel';
import type { IntegrationFn, Span, SpanAttributeValue } from '@sentry/core';
import {
_INTERNAL_shouldSkipAiProviderWrapping,
Expand Down
2 changes: 1 addition & 1 deletion packages/server-utils/src/integrations/mongodb/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as diagnosticsChannel from 'node:diagnostics_channel';
import * as diagnosticsChannel from '../../utils/diagnosticsChannel';
import type { IntegrationFn } from '@sentry/core';
import { defineIntegration } from '@sentry/core';
import type { MongodbNamespace, MongoV3Topology } from './mongodb-span';
Expand Down
2 changes: 1 addition & 1 deletion packages/server-utils/src/integrations/mongoose/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as diagnosticsChannel from 'node:diagnostics_channel';
import * as diagnosticsChannel from '../../utils/diagnosticsChannel';
import type { IntegrationFn, Span } from '@sentry/core';
import { defineIntegration, getActiveSpan, waitForTracingChannelBinding } from '@sentry/core';
import { subscribeMongooseDiagnosticChannels } from './mongoose-dc-subscriber';
Expand Down
2 changes: 1 addition & 1 deletion packages/server-utils/src/integrations/mysql.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as diagnosticsChannel from 'node:diagnostics_channel';
import * as diagnosticsChannel from '../utils/diagnosticsChannel';
import {
DB_NAMESPACE,
DB_QUERY_SUMMARY,
Expand Down
2 changes: 1 addition & 1 deletion packages/server-utils/src/integrations/mysql2/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as diagnosticsChannel from 'node:diagnostics_channel';
import * as diagnosticsChannel from '../../utils/diagnosticsChannel';
import type { IntegrationFn, SpanAttributes } from '@sentry/core';
import {
defineIntegration,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as diagnosticsChannel from 'node:diagnostics_channel';
import * as diagnosticsChannel from '../utils/diagnosticsChannel';
import type { Integration, IntegrationFn, Span, SpanAttributeValue } from '@sentry/core';
import {
_INTERNAL_shouldSkipAiProviderWrapping,
Expand Down
2 changes: 1 addition & 1 deletion packages/server-utils/src/integrations/openai.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as diagnosticsChannel from 'node:diagnostics_channel';
import * as diagnosticsChannel from '../utils/diagnosticsChannel';
import type { IntegrationFn, Span, SpanAttributeValue } from '@sentry/core';
import {
_INTERNAL_shouldSkipAiProviderWrapping,
Expand Down
2 changes: 1 addition & 1 deletion packages/server-utils/src/integrations/postgres-js.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as diagnosticsChannel from 'node:diagnostics_channel';
import * as diagnosticsChannel from '../utils/diagnosticsChannel';
import {
DB_OPERATION_NAME,
DB_QUERY_SUMMARY,
Expand Down
2 changes: 1 addition & 1 deletion packages/server-utils/src/integrations/postgres.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as diagnosticsChannel from 'node:diagnostics_channel';
import * as diagnosticsChannel from '../utils/diagnosticsChannel';
import {
DB_NAMESPACE,
DB_QUERY_SUMMARY,
Expand Down
2 changes: 1 addition & 1 deletion packages/server-utils/src/integrations/redis/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as diagnosticsChannel from 'node:diagnostics_channel';
import * as diagnosticsChannel from '../../utils/diagnosticsChannel';
import {
DB_OPERATION_BATCH_SIZE,
DB_OPERATION_NAME,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as diagnosticsChannel from 'node:diagnostics_channel';
import * as diagnosticsChannel from '../../utils/diagnosticsChannel';
import {
DB_OPERATION_NAME,
DB_QUERY_TEXT,
Expand Down
2 changes: 1 addition & 1 deletion packages/server-utils/src/integrations/tedious.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/* oxlint-disable typescript/no-deprecated */

import { EventEmitter } from 'node:events';
import * as diagnosticsChannel from 'node:diagnostics_channel';
import * as diagnosticsChannel from '../utils/diagnosticsChannel';
import type { IntegrationFn, SpanAttributes } from '@sentry/core';
import {
defineIntegration,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defineIntegration, waitForTracingChannelBinding, type IntegrationFn } from '@sentry/core';
import type { GenAiOptions } from '../../ai/core/utils';
import { subscribeVercelAiTracingChannel } from './vercel-ai-dc-subscriber';
import * as dc from 'node:diagnostics_channel';
import * as dc from '../../utils/diagnosticsChannel';
import { invokeOrchestrionInstrumentation } from '../../orchestrion/instrumentation';
import { vercelAiModuleNames } from '../../orchestrion/config/vercel-ai';
import { subscribeVercelAiOrchestrionChannels } from './vercel-ai-orchestrion-subscriber';
Expand Down
2 changes: 1 addition & 1 deletion packages/server-utils/src/orchestrion/instrumentation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as diagnosticsChannel from 'node:diagnostics_channel';
import * as diagnosticsChannel from '../utils/diagnosticsChannel';
import type { Client } from '@sentry/core';
import { addNonEnumerableProperty, debug, waitForTracingChannelBinding } from '@sentry/core';
import { DEBUG_BUILD } from '../debug-build';
Expand Down
44 changes: 44 additions & 0 deletions packages/server-utils/src/utils/diagnosticsChannel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import * as nodeDiagnosticsChannel from 'node:diagnostics_channel';

// Bun garbage-collects a diagnostics channel that no code references, together with its
// subscribers, so a subscription made at `init()` can stop receiving messages after the next GC.
// Node and Deno keep a channel alive while it has subscribers. These wrappers of the
// `node:diagnostics_channel` functions keep a reference to every channel they return, so the SDK's
// subscriptions stay active on Bun too.

const channelsByName = new Map<string | symbol, nodeDiagnosticsChannel.Channel>();
const tracingChannelsByName = new Map<string, nodeDiagnosticsChannel.TracingChannel>();

/** `diagnostics_channel.channel()`, with the returned channel kept referenced. */
export const channel: typeof nodeDiagnosticsChannel.channel = name => {
let result = channelsByName.get(name);
if (!result) {
result = nodeDiagnosticsChannel.channel(name);
channelsByName.set(name, result);
}
return result;
};

/** `diagnostics_channel.subscribe()`, on a channel kept referenced by {@link channel}. */
export const subscribe: typeof nodeDiagnosticsChannel.subscribe = (name, onMessage) => {
channel(name).subscribe(onMessage);
};

/**
* `diagnostics_channel.tracingChannel()`, with the returned tracing channel (and so its five
* channels) kept referenced when it is created by name. `undefined` where the runtime has no
* `tracingChannel` (Node < 18.19), like the original export.
*/
export const tracingChannel: typeof nodeDiagnosticsChannel.tracingChannel = nodeDiagnosticsChannel.tracingChannel
? (((nameOrChannels: Parameters<typeof nodeDiagnosticsChannel.tracingChannel>[0]) => {
if (typeof nameOrChannels !== 'string') {
return nodeDiagnosticsChannel.tracingChannel(nameOrChannels);
}
let result = tracingChannelsByName.get(nameOrChannels);
if (!result) {
result = nodeDiagnosticsChannel.tracingChannel(nameOrChannels);
tracingChannelsByName.set(nameOrChannels, result);
}
return result;
}) as typeof nodeDiagnosticsChannel.tracingChannel)
: (undefined as unknown as typeof nodeDiagnosticsChannel.tracingChannel);
Loading
Loading