Skip to content
Merged
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
1 change: 1 addition & 0 deletions packages/astro/src/index.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export {
pinoIntegration,
postgresIntegration,
postgresJsIntegration,
neonIntegration,
prismaIntegration,
openTelemetryIntegration,
getOtlpTracesEndpoint,
Expand Down
1 change: 1 addition & 0 deletions packages/aws-serverless/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ export {
pinoIntegration,
postgresIntegration,
postgresJsIntegration,
neonIntegration,
processSessionIntegration,
prismaIntegration,
openTelemetryIntegration,
Expand Down
1 change: 1 addition & 0 deletions packages/bun/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ export {
tediousIntegration,
postgresIntegration,
postgresJsIntegration,
neonIntegration,
prismaIntegration,
openTelemetryIntegration,
getOtlpTracesEndpoint,
Expand Down
1 change: 1 addition & 0 deletions packages/deno/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ export {
typesafeIntegration,
postgresIntegration,
postgresJsIntegration,
neonIntegration,
tediousIntegration,
eveConversationHook,
eveIntegration,
Expand Down
1 change: 1 addition & 0 deletions packages/elysia/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ export {
tediousIntegration,
postgresIntegration,
postgresJsIntegration,
neonIntegration,
prismaIntegration,
processSessionIntegration,
hapiIntegration,
Expand Down
1 change: 1 addition & 0 deletions packages/google-cloud-serverless/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ export {
pinoIntegration,
postgresIntegration,
postgresJsIntegration,
neonIntegration,
prismaIntegration,
openTelemetryIntegration,
getOtlpTracesEndpoint,
Expand Down
1 change: 1 addition & 0 deletions packages/node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export {
typesafeIntegration,
postgresIntegration,
postgresJsIntegration,
neonIntegration,
redisIntegration,
tediousIntegration,
vercelAIIntegration,
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 @@ -68,6 +68,7 @@ export { mysql2Integration } from './integrations/mysql2';
export { openAIIntegration } from './integrations/openai';
export { postgresIntegration } from './integrations/postgres';
export { postgresJsIntegration } from './integrations/postgres-js';
export { neonIntegration } from './integrations/neon';
export { tediousIntegration } from './integrations/tedious';
export { vercelAIIntegration } from './integrations/vercel-ai';
export { expressIntegration } from './integrations/express';
Expand Down
2 changes: 2 additions & 0 deletions packages/server-utils/src/integrations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { groqIntegration } from './groq';
import { togetherAIIntegration } from './together-ai';
import { typesafeIntegration } from './typesafe';
import { postgresJsIntegration } from './postgres-js';
import { neonIntegration } from './neon';
import { firebaseIntegration } from './firebase';
import { expressIntegration } from './express';
import { fastifyIntegration } from './fastify';
Expand Down Expand Up @@ -70,6 +71,7 @@ export function getTracingIntegrations(): Integration[] {
togetherAIIntegration(),
typesafeIntegration(),
postgresJsIntegration(),
neonIntegration(),
firebaseIntegration(),
mcpServerIntegration(),
];
Expand Down
158 changes: 158 additions & 0 deletions packages/server-utils/src/integrations/neon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
import * as diagnosticsChannel from '../utils/diagnosticsChannel';
import {
DB_OPERATION_BATCH_SIZE,
DB_QUERY_SUMMARY,
DB_QUERY_TEXT,
DB_SYSTEM_NAME,
SENTRY_KIND,
SENTRY_OP,
SENTRY_ORIGIN,
} from '@sentry/conventions/attributes';
import { DB } from '@sentry/conventions/op';
import type { IntegrationFn, Span } from '@sentry/core';
import { defineIntegration, getActiveSpan, startInactiveSpan } from '@sentry/core';
import { sanitizeSqlQueryWithSummary } from '../utils/sql';
import { CHANNELS } from '../orchestrion/channels';
import { neonModuleNames } from '../orchestrion/config/neon';
import { invokeOrchestrionInstrumentation } from '../orchestrion/instrumentation';
import { bindTracingChannelToSpan } from '../tracing-channel';
import { bindPgQueryChannel, getConnectionAttributes } from './postgres';

const INTEGRATION_NAME = 'Neon' as const;
const ORIGIN = 'auto.db.neon';
const DB_SYSTEM_POSTGRESQL = 'postgresql';
const DEFAULT_PORT = 5432;
const NOOP = (): void => {};

// `sql.query(text, params)` builds the first shape, a tagged-template call the second.
type NeonQueryData = { query: string; params?: unknown[] } | NeonSqlTemplate;

interface NeonSqlTemplate {
strings: readonly string[];
values: unknown[];
toParameterizedQuery?: () => { query: string };
}

interface NeonHttpQueryContext {
// `[queryData, opts]` for a single query, `[queryData[], opts[]]` for a `transaction()` batch.
arguments: unknown[];
}

interface NeonResolveConnectionContext {
result?: { resolvedURL?: URL };
}

// The HTTP query spans this integration started, so the connection-resolver subscriber only
// enriches those and never a user's own active span.
const httpQuerySpans = new WeakSet<Span>();

const _neonIntegration = (() => {
return {
name: INTEGRATION_NAME,
setup(client) {
invokeOrchestrionInstrumentation(client, neonModuleNames, instrumentNeon, []);
},
};
}) satisfies IntegrationFn;

function instrumentNeon(): void {
// The WebSocket driver is pg's `Client`, bundled into the package, so its query channel gets
// the pg span shape.
bindPgQueryChannel(CHANNELS.NEON_QUERY, { origin: ORIGIN });

bindTracingChannelToSpan(
diagnosticsChannel.tracingChannel<NeonHttpQueryContext>(CHANNELS.NEON_HTTP_QUERY),
data => {
const span = startInactiveSpan(httpQuerySpanOptions(data.arguments));
httpQuerySpans.add(span);
return span;
},
{ requiresParentSpan: true },
);

// The connection string is resolved inside the HTTP executor, so the query span is active
// when this settles. The executor's own context never sees the string.
diagnosticsChannel.tracingChannel<NeonResolveConnectionContext>(CHANNELS.NEON_RESOLVE_CONNECTION).subscribe({
start: NOOP,
end: NOOP,
asyncStart: NOOP,
error: NOOP,
asyncEnd(data) {
const span = getActiveSpan();
const url = data.result?.resolvedURL;
if (!span || !httpQuerySpans.has(span) || !url) {
return;
}
span.setAttributes(
getConnectionAttributes({
host: url.hostname,
port: Number(url.port) || DEFAULT_PORT,
database: url.pathname.slice(1),
user: url.username || undefined,
}),
);
},
});
}

function httpQuerySpanOptions(args: unknown[]): Parameters<typeof startInactiveSpan>[0] {
const queryData = args[0] as NeonQueryData | NeonQueryData[] | undefined;
const batchSize = Array.isArray(queryData) ? queryData.length : undefined;
// A batch gets one span whose text and summary list every statement, in order.
const statements = (Array.isArray(queryData) ? queryData : [queryData])
Comment thread
chargome marked this conversation as resolved.
.map(getQueryText)
.filter((text): text is string => !!text)
.map(text => sanitizeSqlQueryWithSummary(text));
const queryText = joinDefined(statements.map(s => s.queryText));
const querySummary = joinDefined(statements.map(s => s.querySummary));
const name = querySummary || DB_SYSTEM_POSTGRESQL;

return {
name,
attributes: {
[SENTRY_OP]: DB,
[SENTRY_ORIGIN]: ORIGIN,
[SENTRY_KIND]: 'client',
[DB_SYSTEM_NAME]: DB_SYSTEM_POSTGRESQL,
[DB_QUERY_TEXT]: queryText || undefined,
[DB_QUERY_SUMMARY]: querySummary,
[DB_OPERATION_BATCH_SIZE]: batchSize,
},
};
}

function joinDefined(parts: (string | undefined)[]): string | undefined {
const defined = parts.filter((part): part is string => !!part);
return defined.length ? defined.join('; ') : undefined;
}

function getQueryText(data: NeonQueryData | undefined): string | undefined {
if (!data || typeof data !== 'object') {
return undefined;
}
if ('query' in data && typeof data.query === 'string') {
return data.query;
}
if (!('strings' in data) || !Array.isArray(data.strings)) {
return undefined;
}
if (typeof data.toParameterizedQuery === 'function') {
try {
// Neon's own rendering also inlines `sql.unsafe()` fragments and nested templates.
return data.toParameterizedQuery().query;
} catch {
// fall through to the plain placeholder rendering
}
}
return data.strings.reduce((text, part, i) => `${text}$${i}${part}`);
}

/**
* Diagnostics-channel-based `@neondatabase/serverless` integration.
*
* Subscribes to the channels Sentry's code transform injects into the package's bundled pg
* `Client.prototype.query` (WebSocket driver) and into the HTTP executor behind `neon()`, so both
* drivers emit `db` spans with the same shape as the `pg` integration. Requires the Sentry runtime
* hook or bundler plugin to be active.
*/
export const neonIntegration = defineIntegration(_neonIntegration);
Comment thread
cursor[bot] marked this conversation as resolved.
2 changes: 2 additions & 0 deletions packages/server-utils/src/orchestrion/channels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { mongodbChannels } from './config/mongodb';
import { mongooseChannels } from './config/mongoose';
import { mysql2Channels } from './config/mysql2';
import { mysqlChannels } from './config/mysql';
import { neonChannels } from './config/neon';
import { nestjsChannels } from './config/nestjs';
import { openaiChannels } from './config/openai';
import { pgChannels } from './config/pg';
Expand Down Expand Up @@ -80,6 +81,7 @@ export const CHANNELS = {
...mongooseChannels,
...mysql2Channels,
...mysqlChannels,
...neonChannels,
...nestjsChannels,
...openaiChannels,
...pgChannels,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
export const CHANNEL_INTEGRATION_DEFINITIONS = [
{ exportName: 'postgresIntegration', modules: ['pg', 'pg-pool'] },
{ exportName: 'postgresJsIntegration', modules: ['postgres'] },
{ exportName: 'neonIntegration', modules: ['@neondatabase/serverless'] },
{ exportName: 'prismaIntegration', modules: ['@prisma/orm-family-sql'] },
{ exportName: 'mysqlIntegration', modules: ['mysql'] },
{ exportName: 'mysql2Integration', modules: ['mysql2'] },
Expand Down
2 changes: 2 additions & 0 deletions packages/server-utils/src/orchestrion/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { mongodbConfig } from './mongodb';
import { mongooseConfig } from './mongoose';
import { mysql2Config } from './mysql2';
import { mysqlConfig } from './mysql';
import { neonConfig } from './neon';
import { nestjsConfig } from './nestjs';
import { openaiConfig } from './openai';
import { pgConfig } from './pg';
Expand Down Expand Up @@ -86,6 +87,7 @@ export const SENTRY_INSTRUMENTATIONS: InstrumentationConfig[] = [
...mongooseConfig,
...mysql2Config,
...mysqlConfig,
...neonConfig,
...nestjsConfig,
...openaiConfig,
...pgConfig,
Expand Down
Loading
Loading