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
Original file line numberDiff line numberDiff line change
Expand Up@@ -29,6 +29,11 @@ async function run() {

await pgClient('User').insert({ name: 'bob', email: 'bob@domain.com' });
await pgClient('User').select('*');

// Trigger a failing query to capture the error span (table does not exist).
await pgClient('DoesNotExist')
.select('*')
.catch(() => {});
} finally {
await pgClient.destroy();
}
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -57,6 +57,22 @@ describe('knex auto instrumentation', () => {
description: 'select * from "User"',
origin: 'auto.db.otel.knex',
}),

expect.objectContaining({
data: expect.objectContaining({
'knex.version': KNEX_VERSION,
'db.operation': 'select',
'db.sql.table': 'DoesNotExist',
'db.system': 'postgresql',
'db.name': 'tests',
'db.statement': 'select * from "DoesNotExist"',
'sentry.origin': 'auto.db.otel.knex',
'sentry.op': 'db',
}),
status: 'internal_error',
Comment thread
logaretm marked this conversation as resolved.
description: 'select * from "DoesNotExist"',
origin: 'auto.db.otel.knex',
}),
]),
};

Expand Down
27 changes: 4 additions & 23 deletions packages/node/src/integrations/tracing/knex/index.ts
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,17 @@
import { KnexInstrumentation } from './vendored/instrumentation';
import type { IntegrationFn } from '@sentry/core';
import { defineIntegration, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, spanToJSON } from '@sentry/core';
import { generateInstrumentOnce, instrumentWhenWrapped } from '@sentry/node-core';
import { defineIntegration } from '@sentry/core';
import { generateInstrumentOnce } from '@sentry/node-core';

const INTEGRATION_NAME = 'Knex';

export const instrumentKnex = generateInstrumentOnce(
INTEGRATION_NAME,
() => new KnexInstrumentation({ requireParentSpan: true }),
);
export const instrumentKnex = generateInstrumentOnce(INTEGRATION_NAME, () => new KnexInstrumentation());

const _knexIntegration = (() => {
let instrumentationWrappedCallback: undefined | ((callback: () => void) => void);

return {
name: INTEGRATION_NAME,
setupOnce() {
const instrumentation = instrumentKnex();
instrumentationWrappedCallback = instrumentWhenWrapped(instrumentation);
},

setup(client) {
instrumentationWrappedCallback?.(() =>
client.on('spanStart', span => {
const { data } = spanToJSON(span);
// knex.version is always set in the span data
// https://github.com/open-telemetry/opentelemetry-js-contrib/blob/0309caeafc44ac9cb13a3345b790b01b76d0497d/plugins/node/opentelemetry-instrumentation-knex/src/instrumentation.ts#L138
if ('knex.version' in data) {
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, 'auto.db.otel.knex');
}
}),
);
instrumentKnex();
},
};
}) satisfies IntegrationFn;
Expand Down

This file was deleted.

Original file line numberDiff line numberDiff line change
Expand Up@@ -6,31 +6,23 @@
* - Vendored from: https://github.com/open-telemetry/opentelemetry-js-contrib/tree/15ef7506553f631ea4181391e0c5725a56f0d082/packages/instrumentation-knex
* - Upstream version: @opentelemetry/instrumentation-knex@0.62.0
* - Minor TypeScript strictness adjustments for this repository's compiler settings
* - Refactored to use Sentry's span APIs instead of OpenTelemetry tracing APIs
*/
/* eslint-disable */

import * as api from '@opentelemetry/api';
import { SDK_VERSION } from '@sentry/core';
import * as constants from './constants';
/* oxlint-disable typescript/no-deprecated */

import { SpanKind } from '@opentelemetry/api';
import type { InstrumentationConfig } from '@opentelemetry/instrumentation';
import { InstrumentationBase, InstrumentationNodeModuleDefinition, isWrapped } from '@opentelemetry/instrumentation';
import type { Span, SpanAttributes } from '@sentry/core';
import {
InstrumentationBase,
InstrumentationNodeModuleDefinition,
isWrapped,
SemconvStability,
semconvStabilityFromStr,
} from '@opentelemetry/instrumentation';
getActiveSpan,
SDK_VERSION,
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
SPAN_STATUS_ERROR,
startSpan,
} from '@sentry/core';
import { InstrumentationNodeModuleFile } from '../../InstrumentationNodeModuleFile';
import * as utils from './utils';
import { KnexInstrumentationConfig } from './types';
import {
ATTR_DB_COLLECTION_NAME,
ATTR_DB_NAMESPACE,
ATTR_DB_OPERATION_NAME,
ATTR_DB_QUERY_TEXT,
ATTR_DB_SYSTEM_NAME,
ATTR_SERVER_ADDRESS,
ATTR_SERVER_PORT,
} from '@opentelemetry/semantic-conventions';
import {
ATTR_DB_NAME,
ATTR_DB_OPERATION,
Expand All@@ -42,65 +34,69 @@ import {
ATTR_NET_PEER_PORT,
ATTR_NET_TRANSPORT,
} from './semconv';
import * as utils from './utils';

const PACKAGE_NAME = '@sentry/instrumentation-knex';

const contextSymbol = Symbol('opentelemetry.instrumentation-knex.context');
const DEFAULT_CONFIG: KnexInstrumentationConfig = {
maxQueryLength: 1022,
requireParentSpan: false,
};

export class KnexInstrumentation extends InstrumentationBase<KnexInstrumentationConfig> {
private _semconvStability: SemconvStability;

constructor(config: KnexInstrumentationConfig = {}) {
super(PACKAGE_NAME, SDK_VERSION, { ...DEFAULT_CONFIG, ...config });

this._semconvStability = semconvStabilityFromStr('database', process.env.OTEL_SEMCONV_STABILITY_OPT_IN);
}

override setConfig(config: KnexInstrumentationConfig = {}) {
super.setConfig({ ...DEFAULT_CONFIG, ...config });
const ORIGIN = 'auto.db.otel.knex';

const MODULE_NAME = 'knex';
const SUPPORTED_VERSIONS = [
// use "lib/execution" for runner.js, "lib" for client.js as basepath, latest tested 0.95.6
'>=0.22.0 <4',
// use "lib" as basepath
'>=0.10.0 <0.18.0',
'>=0.19.0 <0.22.0',
// use "src" as basepath
'>=0.18.0 <0.19.0',
];

// Max length of the query text captured in the `db.statement` attribute; ".." is appended when truncated.
const MAX_QUERY_LENGTH = 1022;

const parentSpanSymbol = Symbol('sentry.instrumentation-knex.parent-span');

export class KnexInstrumentation extends InstrumentationBase<InstrumentationConfig> {
public constructor(config: InstrumentationConfig = {}) {
super(PACKAGE_NAME, SDK_VERSION, config);
}

init() {
const module = new InstrumentationNodeModuleDefinition(constants.MODULE_NAME, constants.SUPPORTED_VERSIONS);
public init(): InstrumentationNodeModuleDefinition {
const module = new InstrumentationNodeModuleDefinition(MODULE_NAME, SUPPORTED_VERSIONS);

module.files.push(
this.getClientNodeModuleFileInstrumentation('src'),
this.getClientNodeModuleFileInstrumentation('lib'),
this.getRunnerNodeModuleFileInstrumentation('src'),
this.getRunnerNodeModuleFileInstrumentation('lib'),
this.getRunnerNodeModuleFileInstrumentation('lib/execution'),
this._getClientNodeModuleFileInstrumentation('src'),
this._getClientNodeModuleFileInstrumentation('lib'),
this._getRunnerNodeModuleFileInstrumentation('src'),
this._getRunnerNodeModuleFileInstrumentation('lib'),
this._getRunnerNodeModuleFileInstrumentation('lib/execution'),
);

return module;
}

private getRunnerNodeModuleFileInstrumentation(basePath: string) {
private _getRunnerNodeModuleFileInstrumentation(basePath: string): InstrumentationNodeModuleFile {
return new InstrumentationNodeModuleFile(
`knex/${basePath}/runner.js`,
constants.SUPPORTED_VERSIONS,
SUPPORTED_VERSIONS,
(Runner: any, moduleVersion?: string) => {
this.ensureWrapped(Runner.prototype, 'query', this.createQueryWrapper(moduleVersion));
this._ensureWrapped(Runner.prototype, 'query', this._createQueryWrapper(moduleVersion));
return Runner;
},
(Runner: any, _moduleVersion?: string) => {
(Runner: any) => {
this._unwrap(Runner.prototype, 'query');
return Runner;
},
);
}

private getClientNodeModuleFileInstrumentation(basePath: string) {
private _getClientNodeModuleFileInstrumentation(basePath: string): InstrumentationNodeModuleFile {
return new InstrumentationNodeModuleFile(
`knex/${basePath}/client.js`,
constants.SUPPORTED_VERSIONS,
SUPPORTED_VERSIONS,
(Client: any) => {
this.ensureWrapped(Client.prototype, 'queryBuilder', this.storeContext.bind(this));
this.ensureWrapped(Client.prototype, 'schemaBuilder', this.storeContext.bind(this));
this.ensureWrapped(Client.prototype, 'raw', this.storeContext.bind(this));
this._ensureWrapped(Client.prototype, 'queryBuilder', this._storeContext.bind(this));
this._ensureWrapped(Client.prototype, 'schemaBuilder', this._storeContext.bind(this));
this._ensureWrapped(Client.prototype, 'raw', this._storeContext.bind(this));
return Client;
},
(Client: any) => {
Expand All@@ -112,9 +108,7 @@ export class KnexInstrumentation extends InstrumentationBase<KnexInstrumentation
);
}

private createQueryWrapper(moduleVersion?: string) {
const instrumentation = this;

private _createQueryWrapper(moduleVersion?: string) {
return function wrapQuery(original: (...args: any[]) => any) {
return function wrapped_logging_method(this: any, query: any) {
const config = this.client.config;
Expand All@@ -126,93 +120,62 @@ export class KnexInstrumentation extends InstrumentationBase<KnexInstrumentation
config?.connection?.filename ||
config?.connection?.database ||
utils.extractDatabaseFromConnectionString(connectionString);
const { maxQueryLength } = instrumentation.getConfig();

const attributes: api.Attributes = {
const attributes: SpanAttributes = {
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: ORIGIN,
'knex.version': moduleVersion,
[ATTR_DB_SYSTEM]: utils.mapSystem(this.client.driverName),
[ATTR_DB_SQL_TABLE]: table,
[ATTR_DB_OPERATION]: operation,
[ATTR_DB_USER]: config?.connection?.user,
[ATTR_DB_NAME]: name,
[ATTR_NET_PEER_NAME]: config?.connection?.host ?? utils.extractHostFromConnectionString(connectionString),
[ATTR_NET_PEER_PORT]: config?.connection?.port ?? utils.extractPortFromConnectionString(connectionString),
[ATTR_NET_TRANSPORT]: config?.connection?.filename === ':memory:' ? 'inproc' : undefined,
[ATTR_DB_STATEMENT]: utils.limitLength(query?.sql, MAX_QUERY_LENGTH),
};
const transport = config?.connection?.filename === ':memory:' ? 'inproc' : undefined;

if (instrumentation._semconvStability & SemconvStability.OLD) {
Object.assign(attributes, {
[ATTR_DB_SYSTEM]: utils.mapSystem(this.client.driverName),
[ATTR_DB_SQL_TABLE]: table,
[ATTR_DB_OPERATION]: operation,
[ATTR_DB_USER]: config?.connection?.user,
[ATTR_DB_NAME]: name,
[ATTR_NET_PEER_NAME]: config?.connection?.host ?? utils.extractHostFromConnectionString(connectionString),
[ATTR_NET_PEER_PORT]: config?.connection?.port ?? utils.extractPortFromConnectionString(connectionString),
[ATTR_NET_TRANSPORT]: transport,
});
}
if (instrumentation._semconvStability & SemconvStability.STABLE) {
Object.assign(attributes, {
[ATTR_DB_SYSTEM_NAME]: utils.mapSystem(this.client.driverName),
[ATTR_DB_COLLECTION_NAME]: table,
[ATTR_DB_OPERATION_NAME]: operation,
[ATTR_DB_NAMESPACE]: name,
[ATTR_SERVER_ADDRESS]: config?.connection?.host ?? utils.extractHostFromConnectionString(connectionString),
[ATTR_SERVER_PORT]: config?.connection?.port ?? utils.extractPortFromConnectionString(connectionString),
});
}
if (maxQueryLength) {
const queryText = utils.limitLength(query?.sql, maxQueryLength);
if (instrumentation._semconvStability & SemconvStability.STABLE) {
attributes[ATTR_DB_QUERY_TEXT] = queryText;
}
if (instrumentation._semconvStability & SemconvStability.OLD) {
attributes[ATTR_DB_STATEMENT] = queryText;
}
}

const parentContext = this.builder[contextSymbol] || api.context.active();
const parentSpan = api.trace.getSpan(parentContext);
const hasActiveParent = parentSpan && api.trace.isSpanContextValid(parentSpan.spanContext());
if (instrumentation._config.requireParentSpan && !hasActiveParent) {
return original.bind(this)(...arguments);
}

const span = instrumentation.tracer.startSpan(
utils.getName(name, operation, table),

// The query builder captures the span active when it was created (see `_storeContext`).
// `onlyIfParent` ensures we only instrument queries that run as part of an existing trace.
const parentSpan: Span | undefined = this.builder[parentSpanSymbol] || getActiveSpan();

const args = arguments;
return startSpan(
{
kind: api.SpanKind.CLIENT,
name: utils.getName(name, operation, table),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Span description no longer SQL

Medium Severity

Knex spans now use utils.getName as the span name, which becomes the exported description. The prior OTEL exporter derived descriptions from db.statement, so UI and tests expecting full SQL (for example select * from "User") no longer match.

Fix in CursorFix in Web

Reviewed by Cursor Bugbot for commit 216196d. Configure here.

kind: SpanKind.CLIENT,
attributes,
parentSpan,
onlyIfParent: true,
},
Comment thread
sentry[bot] marked this conversation as resolved.
parentContext,
span =>
// `Runner.query` returns a real, already-executing Promise, so it is safe to let
// `startSpan` await it and auto-end the span.
original.apply(this, args).catch((err: any) => {
const formatter = utils.getFormatter(this);
const fullQuery = formatter(query.sql, query.bindings || []);
const message = err.message.replace(`${fullQuery} - `, '');
span.setStatus({ code: SPAN_STATUS_ERROR, message });
throw err;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error status not normalized

Medium Severity

Failed queries set the span status message to the trimmed Knex/Postgres error text. OTEL export previously mapped non-canonical error messages to internal_error, but native spans keep the raw message, diverging from tests and prior telemetry.

Fix in CursorFix in Web

Reviewed by Cursor Bugbot for commit 216196d. Configure here.

}),
);
const spanContext = api.trace.setSpan(api.context.active(), span);

return api.context
.with(spanContext, original, this, ...arguments)
.then((result: unknown) => {
span.end();
return result;
})
.catch((err: any) => {
const formatter = utils.getFormatter(this);
const fullQuery = formatter(query.sql, query.bindings || []);
const message = err.message.replace(fullQuery + ' - ', '');
const exc = utils.otelExceptionFromKnexError(err, message);
span.recordException(exc);
span.setStatus({ code: api.SpanStatusCode.ERROR, message });
span.end();
throw err;
});
};
};
}

private storeContext(original: Function) {
private _storeContext(original: (...args: any[]) => any) {
return function wrapped_logging_method(this: any) {
const builder = original.apply(this, arguments);
Object.defineProperty(builder, contextSymbol, {
value: api.context.active(),
// Capture the span that is active when the query builder is created. The query often executes
// in a different async context, so we reuse this span as the parent when the query runs.
Object.defineProperty(builder, parentSpanSymbol, {
value: getActiveSpan(),
});
return builder;
};
}

ensureWrapped(obj: any, methodName: string, wrapper: (original: any) => any) {
private _ensureWrapped(obj: any, methodName: string, wrapper: (original: any) => any): void {
if (isWrapped(obj[methodName])) {
this._unwrap(obj, methodName);
}
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
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
Original file line numberDiff line numberDiff line change
Expand Up@@ -29,6 +29,11 @@ async function run() {

await pgClient('User').insert({ name: 'bob', email: 'bob@domain.com' });
await pgClient('User').select('*');

// Trigger a failing query to capture the error span (table does not exist).
await pgClient('DoesNotExist')
.select('*')
.catch(() => {});
} finally {
await pgClient.destroy();
}
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -57,6 +57,22 @@ describe('knex auto instrumentation', () => {
description: 'select * from "User"',
origin: 'auto.db.otel.knex',
}),

expect.objectContaining({
data: expect.objectContaining({
'knex.version': KNEX_VERSION,
'db.operation': 'select',
'db.sql.table': 'DoesNotExist',
'db.system': 'postgresql',
'db.name': 'tests',
'db.statement': 'select * from "DoesNotExist"',
'sentry.origin': 'auto.db.otel.knex',
'sentry.op': 'db',
}),
status: 'internal_error',
Comment thread
logaretm marked this conversation as resolved.
description: 'select * from "DoesNotExist"',
origin: 'auto.db.otel.knex',
}),
]),
};

Expand Down
27 changes: 4 additions & 23 deletions packages/node/src/integrations/tracing/knex/index.ts
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,17 @@
import { KnexInstrumentation } from './vendored/instrumentation';
import type { IntegrationFn } from '@sentry/core';
import { defineIntegration, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, spanToJSON } from '@sentry/core';
import { generateInstrumentOnce, instrumentWhenWrapped } from '@sentry/node-core';
import { defineIntegration } from '@sentry/core';
import { generateInstrumentOnce } from '@sentry/node-core';

const INTEGRATION_NAME = 'Knex';

export const instrumentKnex = generateInstrumentOnce(
INTEGRATION_NAME,
() => new KnexInstrumentation({ requireParentSpan: true }),
);
export const instrumentKnex = generateInstrumentOnce(INTEGRATION_NAME, () => new KnexInstrumentation());

const _knexIntegration = (() => {
let instrumentationWrappedCallback: undefined | ((callback: () => void) => void);

return {
name: INTEGRATION_NAME,
setupOnce() {
const instrumentation = instrumentKnex();
instrumentationWrappedCallback = instrumentWhenWrapped(instrumentation);
},

setup(client) {
instrumentationWrappedCallback?.(() =>
client.on('spanStart', span => {
const { data } = spanToJSON(span);
// knex.version is always set in the span data
// https://github.com/open-telemetry/opentelemetry-js-contrib/blob/0309caeafc44ac9cb13a3345b790b01b76d0497d/plugins/node/opentelemetry-instrumentation-knex/src/instrumentation.ts#L138
if ('knex.version' in data) {
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, 'auto.db.otel.knex');
}
}),
);
instrumentKnex();
},
};
}) satisfies IntegrationFn;
Expand Down

This file was deleted.

Original file line numberDiff line numberDiff line change
Expand Up@@ -6,31 +6,23 @@
* - Vendored from: https://github.com/open-telemetry/opentelemetry-js-contrib/tree/15ef7506553f631ea4181391e0c5725a56f0d082/packages/instrumentation-knex
* - Upstream version: @opentelemetry/instrumentation-knex@0.62.0
* - Minor TypeScript strictness adjustments for this repository's compiler settings
* - Refactored to use Sentry's span APIs instead of OpenTelemetry tracing APIs
*/
/* eslint-disable */

import * as api from '@opentelemetry/api';
import { SDK_VERSION } from '@sentry/core';
import * as constants from './constants';
/* oxlint-disable typescript/no-deprecated */

import { SpanKind } from '@opentelemetry/api';
import type { InstrumentationConfig } from '@opentelemetry/instrumentation';
import { InstrumentationBase, InstrumentationNodeModuleDefinition, isWrapped } from '@opentelemetry/instrumentation';
import type { Span, SpanAttributes } from '@sentry/core';
import {
InstrumentationBase,
InstrumentationNodeModuleDefinition,
isWrapped,
SemconvStability,
semconvStabilityFromStr,
} from '@opentelemetry/instrumentation';
getActiveSpan,
SDK_VERSION,
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
SPAN_STATUS_ERROR,
startSpan,
} from '@sentry/core';
import { InstrumentationNodeModuleFile } from '../../InstrumentationNodeModuleFile';
import * as utils from './utils';
import { KnexInstrumentationConfig } from './types';
import {
ATTR_DB_COLLECTION_NAME,
ATTR_DB_NAMESPACE,
ATTR_DB_OPERATION_NAME,
ATTR_DB_QUERY_TEXT,
ATTR_DB_SYSTEM_NAME,
ATTR_SERVER_ADDRESS,
ATTR_SERVER_PORT,
} from '@opentelemetry/semantic-conventions';
import {
ATTR_DB_NAME,
ATTR_DB_OPERATION,
Expand All@@ -42,65 +34,69 @@ import {
ATTR_NET_PEER_PORT,
ATTR_NET_TRANSPORT,
} from './semconv';
import * as utils from './utils';

const PACKAGE_NAME = '@sentry/instrumentation-knex';

const contextSymbol = Symbol('opentelemetry.instrumentation-knex.context');
const DEFAULT_CONFIG: KnexInstrumentationConfig = {
maxQueryLength: 1022,
requireParentSpan: false,
};

export class KnexInstrumentation extends InstrumentationBase<KnexInstrumentationConfig> {
private _semconvStability: SemconvStability;

constructor(config: KnexInstrumentationConfig = {}) {
super(PACKAGE_NAME, SDK_VERSION, { ...DEFAULT_CONFIG, ...config });

this._semconvStability = semconvStabilityFromStr('database', process.env.OTEL_SEMCONV_STABILITY_OPT_IN);
}

override setConfig(config: KnexInstrumentationConfig = {}) {
super.setConfig({ ...DEFAULT_CONFIG, ...config });
const ORIGIN = 'auto.db.otel.knex';

const MODULE_NAME = 'knex';
const SUPPORTED_VERSIONS = [
// use "lib/execution" for runner.js, "lib" for client.js as basepath, latest tested 0.95.6
'>=0.22.0 <4',
// use "lib" as basepath
'>=0.10.0 <0.18.0',
'>=0.19.0 <0.22.0',
// use "src" as basepath
'>=0.18.0 <0.19.0',
];

// Max length of the query text captured in the `db.statement` attribute; ".." is appended when truncated.
const MAX_QUERY_LENGTH = 1022;

const parentSpanSymbol = Symbol('sentry.instrumentation-knex.parent-span');

export class KnexInstrumentation extends InstrumentationBase<InstrumentationConfig> {
public constructor(config: InstrumentationConfig = {}) {
super(PACKAGE_NAME, SDK_VERSION, config);
}

init() {
const module = new InstrumentationNodeModuleDefinition(constants.MODULE_NAME, constants.SUPPORTED_VERSIONS);
public init(): InstrumentationNodeModuleDefinition {
const module = new InstrumentationNodeModuleDefinition(MODULE_NAME, SUPPORTED_VERSIONS);

module.files.push(
this.getClientNodeModuleFileInstrumentation('src'),
this.getClientNodeModuleFileInstrumentation('lib'),
this.getRunnerNodeModuleFileInstrumentation('src'),
this.getRunnerNodeModuleFileInstrumentation('lib'),
this.getRunnerNodeModuleFileInstrumentation('lib/execution'),
this._getClientNodeModuleFileInstrumentation('src'),
this._getClientNodeModuleFileInstrumentation('lib'),
this._getRunnerNodeModuleFileInstrumentation('src'),
this._getRunnerNodeModuleFileInstrumentation('lib'),
this._getRunnerNodeModuleFileInstrumentation('lib/execution'),
);

return module;
}

private getRunnerNodeModuleFileInstrumentation(basePath: string) {
private _getRunnerNodeModuleFileInstrumentation(basePath: string): InstrumentationNodeModuleFile {
return new InstrumentationNodeModuleFile(
`knex/${basePath}/runner.js`,
constants.SUPPORTED_VERSIONS,
SUPPORTED_VERSIONS,
(Runner: any, moduleVersion?: string) => {
this.ensureWrapped(Runner.prototype, 'query', this.createQueryWrapper(moduleVersion));
this._ensureWrapped(Runner.prototype, 'query', this._createQueryWrapper(moduleVersion));
return Runner;
},
(Runner: any, _moduleVersion?: string) => {
(Runner: any) => {
this._unwrap(Runner.prototype, 'query');
return Runner;
},
);
}

private getClientNodeModuleFileInstrumentation(basePath: string) {
private _getClientNodeModuleFileInstrumentation(basePath: string): InstrumentationNodeModuleFile {
return new InstrumentationNodeModuleFile(
`knex/${basePath}/client.js`,
constants.SUPPORTED_VERSIONS,
SUPPORTED_VERSIONS,
(Client: any) => {
this.ensureWrapped(Client.prototype, 'queryBuilder', this.storeContext.bind(this));
this.ensureWrapped(Client.prototype, 'schemaBuilder', this.storeContext.bind(this));
this.ensureWrapped(Client.prototype, 'raw', this.storeContext.bind(this));
this._ensureWrapped(Client.prototype, 'queryBuilder', this._storeContext.bind(this));
this._ensureWrapped(Client.prototype, 'schemaBuilder', this._storeContext.bind(this));
this._ensureWrapped(Client.prototype, 'raw', this._storeContext.bind(this));
return Client;
},
(Client: any) => {
Expand All@@ -112,9 +108,7 @@ export class KnexInstrumentation extends InstrumentationBase<KnexInstrumentation
);
}

private createQueryWrapper(moduleVersion?: string) {
const instrumentation = this;

private _createQueryWrapper(moduleVersion?: string) {
return function wrapQuery(original: (...args: any[]) => any) {
return function wrapped_logging_method(this: any, query: any) {
const config = this.client.config;
Expand All@@ -126,93 +120,62 @@ export class KnexInstrumentation extends InstrumentationBase<KnexInstrumentation
config?.connection?.filename ||
config?.connection?.database ||
utils.extractDatabaseFromConnectionString(connectionString);
const { maxQueryLength } = instrumentation.getConfig();

const attributes: api.Attributes = {
const attributes: SpanAttributes = {
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: ORIGIN,
'knex.version': moduleVersion,
[ATTR_DB_SYSTEM]: utils.mapSystem(this.client.driverName),
[ATTR_DB_SQL_TABLE]: table,
[ATTR_DB_OPERATION]: operation,
[ATTR_DB_USER]: config?.connection?.user,
[ATTR_DB_NAME]: name,
[ATTR_NET_PEER_NAME]: config?.connection?.host ?? utils.extractHostFromConnectionString(connectionString),
[ATTR_NET_PEER_PORT]: config?.connection?.port ?? utils.extractPortFromConnectionString(connectionString),
[ATTR_NET_TRANSPORT]: config?.connection?.filename === ':memory:' ? 'inproc' : undefined,
[ATTR_DB_STATEMENT]: utils.limitLength(query?.sql, MAX_QUERY_LENGTH),
};
const transport = config?.connection?.filename === ':memory:' ? 'inproc' : undefined;

if (instrumentation._semconvStability & SemconvStability.OLD) {
Object.assign(attributes, {
[ATTR_DB_SYSTEM]: utils.mapSystem(this.client.driverName),
[ATTR_DB_SQL_TABLE]: table,
[ATTR_DB_OPERATION]: operation,
[ATTR_DB_USER]: config?.connection?.user,
[ATTR_DB_NAME]: name,
[ATTR_NET_PEER_NAME]: config?.connection?.host ?? utils.extractHostFromConnectionString(connectionString),
[ATTR_NET_PEER_PORT]: config?.connection?.port ?? utils.extractPortFromConnectionString(connectionString),
[ATTR_NET_TRANSPORT]: transport,
});
}
if (instrumentation._semconvStability & SemconvStability.STABLE) {
Object.assign(attributes, {
[ATTR_DB_SYSTEM_NAME]: utils.mapSystem(this.client.driverName),
[ATTR_DB_COLLECTION_NAME]: table,
[ATTR_DB_OPERATION_NAME]: operation,
[ATTR_DB_NAMESPACE]: name,
[ATTR_SERVER_ADDRESS]: config?.connection?.host ?? utils.extractHostFromConnectionString(connectionString),
[ATTR_SERVER_PORT]: config?.connection?.port ?? utils.extractPortFromConnectionString(connectionString),
});
}
if (maxQueryLength) {
const queryText = utils.limitLength(query?.sql, maxQueryLength);
if (instrumentation._semconvStability & SemconvStability.STABLE) {
attributes[ATTR_DB_QUERY_TEXT] = queryText;
}
if (instrumentation._semconvStability & SemconvStability.OLD) {
attributes[ATTR_DB_STATEMENT] = queryText;
}
}

const parentContext = this.builder[contextSymbol] || api.context.active();
const parentSpan = api.trace.getSpan(parentContext);
const hasActiveParent = parentSpan && api.trace.isSpanContextValid(parentSpan.spanContext());
if (instrumentation._config.requireParentSpan && !hasActiveParent) {
return original.bind(this)(...arguments);
}

const span = instrumentation.tracer.startSpan(
utils.getName(name, operation, table),

// The query builder captures the span active when it was created (see `_storeContext`).
// `onlyIfParent` ensures we only instrument queries that run as part of an existing trace.
const parentSpan: Span | undefined = this.builder[parentSpanSymbol] || getActiveSpan();

const args = arguments;
return startSpan(
{
kind: api.SpanKind.CLIENT,
name: utils.getName(name, operation, table),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Span description no longer SQL

Medium Severity

Knex spans now use utils.getName as the span name, which becomes the exported description. The prior OTEL exporter derived descriptions from db.statement, so UI and tests expecting full SQL (for example select * from "User") no longer match.

Fix in CursorFix in Web

Reviewed by Cursor Bugbot for commit 216196d. Configure here.

kind: SpanKind.CLIENT,
attributes,
parentSpan,
onlyIfParent: true,
},
Comment thread
sentry[bot] marked this conversation as resolved.
parentContext,
span =>
// `Runner.query` returns a real, already-executing Promise, so it is safe to let
// `startSpan` await it and auto-end the span.
original.apply(this, args).catch((err: any) => {
const formatter = utils.getFormatter(this);
const fullQuery = formatter(query.sql, query.bindings || []);
const message = err.message.replace(`${fullQuery} - `, '');
span.setStatus({ code: SPAN_STATUS_ERROR, message });
throw err;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error status not normalized

Medium Severity

Failed queries set the span status message to the trimmed Knex/Postgres error text. OTEL export previously mapped non-canonical error messages to internal_error, but native spans keep the raw message, diverging from tests and prior telemetry.

Fix in CursorFix in Web

Reviewed by Cursor Bugbot for commit 216196d. Configure here.

}),
);
const spanContext = api.trace.setSpan(api.context.active(), span);

return api.context
.with(spanContext, original, this, ...arguments)
.then((result: unknown) => {
span.end();
return result;
})
.catch((err: any) => {
const formatter = utils.getFormatter(this);
const fullQuery = formatter(query.sql, query.bindings || []);
const message = err.message.replace(fullQuery + ' - ', '');
const exc = utils.otelExceptionFromKnexError(err, message);
span.recordException(exc);
span.setStatus({ code: api.SpanStatusCode.ERROR, message });
span.end();
throw err;
});
};
};
}

private storeContext(original: Function) {
private _storeContext(original: (...args: any[]) => any) {
return function wrapped_logging_method(this: any) {
const builder = original.apply(this, arguments);
Object.defineProperty(builder, contextSymbol, {
value: api.context.active(),
// Capture the span that is active when the query builder is created. The query often executes
// in a different async context, so we reuse this span as the parent when the query runs.
Object.defineProperty(builder, parentSpanSymbol, {
value: getActiveSpan(),
});
return builder;
};
}

ensureWrapped(obj: any, methodName: string, wrapper: (original: any) => any) {
private _ensureWrapped(obj: any, methodName: string, wrapper: (original: any) => any): void {
if (isWrapped(obj[methodName])) {
this._unwrap(obj, methodName);
}
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
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
Original file line numberDiff line numberDiff line change
Expand Up@@ -29,6 +29,11 @@ async function run() {

await pgClient('User').insert({ name: 'bob', email: 'bob@domain.com' });
await pgClient('User').select('*');

// Trigger a failing query to capture the error span (table does not exist).
await pgClient('DoesNotExist')
.select('*')
.catch(() => {});
} finally {
await pgClient.destroy();
}
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -57,6 +57,22 @@ describe('knex auto instrumentation', () => {
description: 'select * from "User"',
origin: 'auto.db.otel.knex',
}),

expect.objectContaining({
data: expect.objectContaining({
'knex.version': KNEX_VERSION,
'db.operation': 'select',
'db.sql.table': 'DoesNotExist',
'db.system': 'postgresql',
'db.name': 'tests',
'db.statement': 'select * from "DoesNotExist"',
'sentry.origin': 'auto.db.otel.knex',
'sentry.op': 'db',
}),
status: 'internal_error',
Comment thread
logaretm marked this conversation as resolved.
description: 'select * from "DoesNotExist"',
origin: 'auto.db.otel.knex',
}),
]),
};

Expand Down
27 changes: 4 additions & 23 deletions packages/node/src/integrations/tracing/knex/index.ts
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,17 @@
import { KnexInstrumentation } from './vendored/instrumentation';
import type { IntegrationFn } from '@sentry/core';
import { defineIntegration, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, spanToJSON } from '@sentry/core';
import { generateInstrumentOnce, instrumentWhenWrapped } from '@sentry/node-core';
import { defineIntegration } from '@sentry/core';
import { generateInstrumentOnce } from '@sentry/node-core';

const INTEGRATION_NAME = 'Knex';

export const instrumentKnex = generateInstrumentOnce(
INTEGRATION_NAME,
() => new KnexInstrumentation({ requireParentSpan: true }),
);
export const instrumentKnex = generateInstrumentOnce(INTEGRATION_NAME, () => new KnexInstrumentation());

const _knexIntegration = (() => {
let instrumentationWrappedCallback: undefined | ((callback: () => void) => void);

return {
name: INTEGRATION_NAME,
setupOnce() {
const instrumentation = instrumentKnex();
instrumentationWrappedCallback = instrumentWhenWrapped(instrumentation);
},

setup(client) {
instrumentationWrappedCallback?.(() =>
client.on('spanStart', span => {
const { data } = spanToJSON(span);
// knex.version is always set in the span data
// https://github.com/open-telemetry/opentelemetry-js-contrib/blob/0309caeafc44ac9cb13a3345b790b01b76d0497d/plugins/node/opentelemetry-instrumentation-knex/src/instrumentation.ts#L138
if ('knex.version' in data) {
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, 'auto.db.otel.knex');
}
}),
);
instrumentKnex();
},
};
}) satisfies IntegrationFn;
Expand Down

This file was deleted.

Original file line numberDiff line numberDiff line change
Expand Up@@ -6,31 +6,23 @@
* - Vendored from: https://github.com/open-telemetry/opentelemetry-js-contrib/tree/15ef7506553f631ea4181391e0c5725a56f0d082/packages/instrumentation-knex
* - Upstream version: @opentelemetry/instrumentation-knex@0.62.0
* - Minor TypeScript strictness adjustments for this repository's compiler settings
* - Refactored to use Sentry's span APIs instead of OpenTelemetry tracing APIs
*/
/* eslint-disable */

import * as api from '@opentelemetry/api';
import { SDK_VERSION } from '@sentry/core';
import * as constants from './constants';
/* oxlint-disable typescript/no-deprecated */

import { SpanKind } from '@opentelemetry/api';
import type { InstrumentationConfig } from '@opentelemetry/instrumentation';
import { InstrumentationBase, InstrumentationNodeModuleDefinition, isWrapped } from '@opentelemetry/instrumentation';
import type { Span, SpanAttributes } from '@sentry/core';
import {
InstrumentationBase,
InstrumentationNodeModuleDefinition,
isWrapped,
SemconvStability,
semconvStabilityFromStr,
} from '@opentelemetry/instrumentation';
getActiveSpan,
SDK_VERSION,
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
SPAN_STATUS_ERROR,
startSpan,
} from '@sentry/core';
import { InstrumentationNodeModuleFile } from '../../InstrumentationNodeModuleFile';
import * as utils from './utils';
import { KnexInstrumentationConfig } from './types';
import {
ATTR_DB_COLLECTION_NAME,
ATTR_DB_NAMESPACE,
ATTR_DB_OPERATION_NAME,
ATTR_DB_QUERY_TEXT,
ATTR_DB_SYSTEM_NAME,
ATTR_SERVER_ADDRESS,
ATTR_SERVER_PORT,
} from '@opentelemetry/semantic-conventions';
import {
ATTR_DB_NAME,
ATTR_DB_OPERATION,
Expand All@@ -42,65 +34,69 @@ import {
ATTR_NET_PEER_PORT,
ATTR_NET_TRANSPORT,
} from './semconv';
import * as utils from './utils';

const PACKAGE_NAME = '@sentry/instrumentation-knex';

const contextSymbol = Symbol('opentelemetry.instrumentation-knex.context');
const DEFAULT_CONFIG: KnexInstrumentationConfig = {
maxQueryLength: 1022,
requireParentSpan: false,
};

export class KnexInstrumentation extends InstrumentationBase<KnexInstrumentationConfig> {
private _semconvStability: SemconvStability;

constructor(config: KnexInstrumentationConfig = {}) {
super(PACKAGE_NAME, SDK_VERSION, { ...DEFAULT_CONFIG, ...config });

this._semconvStability = semconvStabilityFromStr('database', process.env.OTEL_SEMCONV_STABILITY_OPT_IN);
}

override setConfig(config: KnexInstrumentationConfig = {}) {
super.setConfig({ ...DEFAULT_CONFIG, ...config });
const ORIGIN = 'auto.db.otel.knex';

const MODULE_NAME = 'knex';
const SUPPORTED_VERSIONS = [
// use "lib/execution" for runner.js, "lib" for client.js as basepath, latest tested 0.95.6
'>=0.22.0 <4',
// use "lib" as basepath
'>=0.10.0 <0.18.0',
'>=0.19.0 <0.22.0',
// use "src" as basepath
'>=0.18.0 <0.19.0',
];

// Max length of the query text captured in the `db.statement` attribute; ".." is appended when truncated.
const MAX_QUERY_LENGTH = 1022;

const parentSpanSymbol = Symbol('sentry.instrumentation-knex.parent-span');

export class KnexInstrumentation extends InstrumentationBase<InstrumentationConfig> {
public constructor(config: InstrumentationConfig = {}) {
super(PACKAGE_NAME, SDK_VERSION, config);
}

init() {
const module = new InstrumentationNodeModuleDefinition(constants.MODULE_NAME, constants.SUPPORTED_VERSIONS);
public init(): InstrumentationNodeModuleDefinition {
const module = new InstrumentationNodeModuleDefinition(MODULE_NAME, SUPPORTED_VERSIONS);

module.files.push(
this.getClientNodeModuleFileInstrumentation('src'),
this.getClientNodeModuleFileInstrumentation('lib'),
this.getRunnerNodeModuleFileInstrumentation('src'),
this.getRunnerNodeModuleFileInstrumentation('lib'),
this.getRunnerNodeModuleFileInstrumentation('lib/execution'),
this._getClientNodeModuleFileInstrumentation('src'),
this._getClientNodeModuleFileInstrumentation('lib'),
this._getRunnerNodeModuleFileInstrumentation('src'),
this._getRunnerNodeModuleFileInstrumentation('lib'),
this._getRunnerNodeModuleFileInstrumentation('lib/execution'),
);

return module;
}

private getRunnerNodeModuleFileInstrumentation(basePath: string) {
private _getRunnerNodeModuleFileInstrumentation(basePath: string): InstrumentationNodeModuleFile {
return new InstrumentationNodeModuleFile(
`knex/${basePath}/runner.js`,
constants.SUPPORTED_VERSIONS,
SUPPORTED_VERSIONS,
(Runner: any, moduleVersion?: string) => {
this.ensureWrapped(Runner.prototype, 'query', this.createQueryWrapper(moduleVersion));
this._ensureWrapped(Runner.prototype, 'query', this._createQueryWrapper(moduleVersion));
return Runner;
},
(Runner: any, _moduleVersion?: string) => {
(Runner: any) => {
this._unwrap(Runner.prototype, 'query');
return Runner;
},
);
}

private getClientNodeModuleFileInstrumentation(basePath: string) {
private _getClientNodeModuleFileInstrumentation(basePath: string): InstrumentationNodeModuleFile {
return new InstrumentationNodeModuleFile(
`knex/${basePath}/client.js`,
constants.SUPPORTED_VERSIONS,
SUPPORTED_VERSIONS,
(Client: any) => {
this.ensureWrapped(Client.prototype, 'queryBuilder', this.storeContext.bind(this));
this.ensureWrapped(Client.prototype, 'schemaBuilder', this.storeContext.bind(this));
this.ensureWrapped(Client.prototype, 'raw', this.storeContext.bind(this));
this._ensureWrapped(Client.prototype, 'queryBuilder', this._storeContext.bind(this));
this._ensureWrapped(Client.prototype, 'schemaBuilder', this._storeContext.bind(this));
this._ensureWrapped(Client.prototype, 'raw', this._storeContext.bind(this));
return Client;
},
(Client: any) => {
Expand All@@ -112,9 +108,7 @@ export class KnexInstrumentation extends InstrumentationBase<KnexInstrumentation
);
}

private createQueryWrapper(moduleVersion?: string) {
const instrumentation = this;

private _createQueryWrapper(moduleVersion?: string) {
return function wrapQuery(original: (...args: any[]) => any) {
return function wrapped_logging_method(this: any, query: any) {
const config = this.client.config;
Expand All@@ -126,93 +120,62 @@ export class KnexInstrumentation extends InstrumentationBase<KnexInstrumentation
config?.connection?.filename ||
config?.connection?.database ||
utils.extractDatabaseFromConnectionString(connectionString);
const { maxQueryLength } = instrumentation.getConfig();

const attributes: api.Attributes = {
const attributes: SpanAttributes = {
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: ORIGIN,
'knex.version': moduleVersion,
[ATTR_DB_SYSTEM]: utils.mapSystem(this.client.driverName),
[ATTR_DB_SQL_TABLE]: table,
[ATTR_DB_OPERATION]: operation,
[ATTR_DB_USER]: config?.connection?.user,
[ATTR_DB_NAME]: name,
[ATTR_NET_PEER_NAME]: config?.connection?.host ?? utils.extractHostFromConnectionString(connectionString),
[ATTR_NET_PEER_PORT]: config?.connection?.port ?? utils.extractPortFromConnectionString(connectionString),
[ATTR_NET_TRANSPORT]: config?.connection?.filename === ':memory:' ? 'inproc' : undefined,
[ATTR_DB_STATEMENT]: utils.limitLength(query?.sql, MAX_QUERY_LENGTH),
};
const transport = config?.connection?.filename === ':memory:' ? 'inproc' : undefined;

if (instrumentation._semconvStability & SemconvStability.OLD) {
Object.assign(attributes, {
[ATTR_DB_SYSTEM]: utils.mapSystem(this.client.driverName),
[ATTR_DB_SQL_TABLE]: table,
[ATTR_DB_OPERATION]: operation,
[ATTR_DB_USER]: config?.connection?.user,
[ATTR_DB_NAME]: name,
[ATTR_NET_PEER_NAME]: config?.connection?.host ?? utils.extractHostFromConnectionString(connectionString),
[ATTR_NET_PEER_PORT]: config?.connection?.port ?? utils.extractPortFromConnectionString(connectionString),
[ATTR_NET_TRANSPORT]: transport,
});
}
if (instrumentation._semconvStability & SemconvStability.STABLE) {
Object.assign(attributes, {
[ATTR_DB_SYSTEM_NAME]: utils.mapSystem(this.client.driverName),
[ATTR_DB_COLLECTION_NAME]: table,
[ATTR_DB_OPERATION_NAME]: operation,
[ATTR_DB_NAMESPACE]: name,
[ATTR_SERVER_ADDRESS]: config?.connection?.host ?? utils.extractHostFromConnectionString(connectionString),
[ATTR_SERVER_PORT]: config?.connection?.port ?? utils.extractPortFromConnectionString(connectionString),
});
}
if (maxQueryLength) {
const queryText = utils.limitLength(query?.sql, maxQueryLength);
if (instrumentation._semconvStability & SemconvStability.STABLE) {
attributes[ATTR_DB_QUERY_TEXT] = queryText;
}
if (instrumentation._semconvStability & SemconvStability.OLD) {
attributes[ATTR_DB_STATEMENT] = queryText;
}
}

const parentContext = this.builder[contextSymbol] || api.context.active();
const parentSpan = api.trace.getSpan(parentContext);
const hasActiveParent = parentSpan && api.trace.isSpanContextValid(parentSpan.spanContext());
if (instrumentation._config.requireParentSpan && !hasActiveParent) {
return original.bind(this)(...arguments);
}

const span = instrumentation.tracer.startSpan(
utils.getName(name, operation, table),

// The query builder captures the span active when it was created (see `_storeContext`).
// `onlyIfParent` ensures we only instrument queries that run as part of an existing trace.
const parentSpan: Span | undefined = this.builder[parentSpanSymbol] || getActiveSpan();

const args = arguments;
return startSpan(
{
kind: api.SpanKind.CLIENT,
name: utils.getName(name, operation, table),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Span description no longer SQL

Medium Severity

Knex spans now use utils.getName as the span name, which becomes the exported description. The prior OTEL exporter derived descriptions from db.statement, so UI and tests expecting full SQL (for example select * from "User") no longer match.

Fix in CursorFix in Web

Reviewed by Cursor Bugbot for commit 216196d. Configure here.

kind: SpanKind.CLIENT,
attributes,
parentSpan,
onlyIfParent: true,
},
Comment thread
sentry[bot] marked this conversation as resolved.
parentContext,
span =>
// `Runner.query` returns a real, already-executing Promise, so it is safe to let
// `startSpan` await it and auto-end the span.
original.apply(this, args).catch((err: any) => {
const formatter = utils.getFormatter(this);
const fullQuery = formatter(query.sql, query.bindings || []);
const message = err.message.replace(`${fullQuery} - `, '');
span.setStatus({ code: SPAN_STATUS_ERROR, message });
throw err;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error status not normalized

Medium Severity

Failed queries set the span status message to the trimmed Knex/Postgres error text. OTEL export previously mapped non-canonical error messages to internal_error, but native spans keep the raw message, diverging from tests and prior telemetry.

Fix in CursorFix in Web

Reviewed by Cursor Bugbot for commit 216196d. Configure here.

}),
);
const spanContext = api.trace.setSpan(api.context.active(), span);

return api.context
.with(spanContext, original, this, ...arguments)
.then((result: unknown) => {
span.end();
return result;
})
.catch((err: any) => {
const formatter = utils.getFormatter(this);
const fullQuery = formatter(query.sql, query.bindings || []);
const message = err.message.replace(fullQuery + ' - ', '');
const exc = utils.otelExceptionFromKnexError(err, message);
span.recordException(exc);
span.setStatus({ code: api.SpanStatusCode.ERROR, message });
span.end();
throw err;
});
};
};
}

private storeContext(original: Function) {
private _storeContext(original: (...args: any[]) => any) {
return function wrapped_logging_method(this: any) {
const builder = original.apply(this, arguments);
Object.defineProperty(builder, contextSymbol, {
value: api.context.active(),
// Capture the span that is active when the query builder is created. The query often executes
// in a different async context, so we reuse this span as the parent when the query runs.
Object.defineProperty(builder, parentSpanSymbol, {
value: getActiveSpan(),
});
return builder;
};
}

ensureWrapped(obj: any, methodName: string, wrapper: (original: any) => any) {
private _ensureWrapped(obj: any, methodName: string, wrapper: (original: any) => any): void {
if (isWrapped(obj[methodName])) {
this._unwrap(obj, methodName);
}
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
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
Original file line numberDiff line numberDiff line change
Expand Up@@ -29,6 +29,11 @@ async function run() {

await pgClient('User').insert({ name: 'bob', email: 'bob@domain.com' });
await pgClient('User').select('*');

// Trigger a failing query to capture the error span (table does not exist).
await pgClient('DoesNotExist')
.select('*')
.catch(() => {});
} finally {
await pgClient.destroy();
}
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -57,6 +57,22 @@ describe('knex auto instrumentation', () => {
description: 'select * from "User"',
origin: 'auto.db.otel.knex',
}),

expect.objectContaining({
data: expect.objectContaining({
'knex.version': KNEX_VERSION,
'db.operation': 'select',
'db.sql.table': 'DoesNotExist',
'db.system': 'postgresql',
'db.name': 'tests',
'db.statement': 'select * from "DoesNotExist"',
'sentry.origin': 'auto.db.otel.knex',
'sentry.op': 'db',
}),
status: 'internal_error',
Comment thread
logaretm marked this conversation as resolved.
description: 'select * from "DoesNotExist"',
origin: 'auto.db.otel.knex',
}),
]),
};

Expand Down
27 changes: 4 additions & 23 deletions packages/node/src/integrations/tracing/knex/index.ts
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,17 @@
import { KnexInstrumentation } from './vendored/instrumentation';
import type { IntegrationFn } from '@sentry/core';
import { defineIntegration, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, spanToJSON } from '@sentry/core';
import { generateInstrumentOnce, instrumentWhenWrapped } from '@sentry/node-core';
import { defineIntegration } from '@sentry/core';
import { generateInstrumentOnce } from '@sentry/node-core';

const INTEGRATION_NAME = 'Knex';

export const instrumentKnex = generateInstrumentOnce(
INTEGRATION_NAME,
() => new KnexInstrumentation({ requireParentSpan: true }),
);
export const instrumentKnex = generateInstrumentOnce(INTEGRATION_NAME, () => new KnexInstrumentation());

const _knexIntegration = (() => {
let instrumentationWrappedCallback: undefined | ((callback: () => void) => void);

return {
name: INTEGRATION_NAME,
setupOnce() {
const instrumentation = instrumentKnex();
instrumentationWrappedCallback = instrumentWhenWrapped(instrumentation);
},

setup(client) {
instrumentationWrappedCallback?.(() =>
client.on('spanStart', span => {
const { data } = spanToJSON(span);
// knex.version is always set in the span data
// https://github.com/open-telemetry/opentelemetry-js-contrib/blob/0309caeafc44ac9cb13a3345b790b01b76d0497d/plugins/node/opentelemetry-instrumentation-knex/src/instrumentation.ts#L138
if ('knex.version' in data) {
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, 'auto.db.otel.knex');
}
}),
);
instrumentKnex();
},
};
}) satisfies IntegrationFn;
Expand Down

This file was deleted.

Original file line numberDiff line numberDiff line change
Expand Up@@ -6,31 +6,23 @@
* - Vendored from: https://github.com/open-telemetry/opentelemetry-js-contrib/tree/15ef7506553f631ea4181391e0c5725a56f0d082/packages/instrumentation-knex
* - Upstream version: @opentelemetry/instrumentation-knex@0.62.0
* - Minor TypeScript strictness adjustments for this repository's compiler settings
* - Refactored to use Sentry's span APIs instead of OpenTelemetry tracing APIs
*/
/* eslint-disable */

import * as api from '@opentelemetry/api';
import { SDK_VERSION } from '@sentry/core';
import * as constants from './constants';
/* oxlint-disable typescript/no-deprecated */

import { SpanKind } from '@opentelemetry/api';
import type { InstrumentationConfig } from '@opentelemetry/instrumentation';
import { InstrumentationBase, InstrumentationNodeModuleDefinition, isWrapped } from '@opentelemetry/instrumentation';
import type { Span, SpanAttributes } from '@sentry/core';
import {
InstrumentationBase,
InstrumentationNodeModuleDefinition,
isWrapped,
SemconvStability,
semconvStabilityFromStr,
} from '@opentelemetry/instrumentation';
getActiveSpan,
SDK_VERSION,
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
SPAN_STATUS_ERROR,
startSpan,
} from '@sentry/core';
import { InstrumentationNodeModuleFile } from '../../InstrumentationNodeModuleFile';
import * as utils from './utils';
import { KnexInstrumentationConfig } from './types';
import {
ATTR_DB_COLLECTION_NAME,
ATTR_DB_NAMESPACE,
ATTR_DB_OPERATION_NAME,
ATTR_DB_QUERY_TEXT,
ATTR_DB_SYSTEM_NAME,
ATTR_SERVER_ADDRESS,
ATTR_SERVER_PORT,
} from '@opentelemetry/semantic-conventions';
import {
ATTR_DB_NAME,
ATTR_DB_OPERATION,
Expand All@@ -42,65 +34,69 @@ import {
ATTR_NET_PEER_PORT,
ATTR_NET_TRANSPORT,
} from './semconv';
import * as utils from './utils';

const PACKAGE_NAME = '@sentry/instrumentation-knex';

const contextSymbol = Symbol('opentelemetry.instrumentation-knex.context');
const DEFAULT_CONFIG: KnexInstrumentationConfig = {
maxQueryLength: 1022,
requireParentSpan: false,
};

export class KnexInstrumentation extends InstrumentationBase<KnexInstrumentationConfig> {
private _semconvStability: SemconvStability;

constructor(config: KnexInstrumentationConfig = {}) {
super(PACKAGE_NAME, SDK_VERSION, { ...DEFAULT_CONFIG, ...config });

this._semconvStability = semconvStabilityFromStr('database', process.env.OTEL_SEMCONV_STABILITY_OPT_IN);
}

override setConfig(config: KnexInstrumentationConfig = {}) {
super.setConfig({ ...DEFAULT_CONFIG, ...config });
const ORIGIN = 'auto.db.otel.knex';

const MODULE_NAME = 'knex';
const SUPPORTED_VERSIONS = [
// use "lib/execution" for runner.js, "lib" for client.js as basepath, latest tested 0.95.6
'>=0.22.0 <4',
// use "lib" as basepath
'>=0.10.0 <0.18.0',
'>=0.19.0 <0.22.0',
// use "src" as basepath
'>=0.18.0 <0.19.0',
];

// Max length of the query text captured in the `db.statement` attribute; ".." is appended when truncated.
const MAX_QUERY_LENGTH = 1022;

const parentSpanSymbol = Symbol('sentry.instrumentation-knex.parent-span');

export class KnexInstrumentation extends InstrumentationBase<InstrumentationConfig> {
public constructor(config: InstrumentationConfig = {}) {
super(PACKAGE_NAME, SDK_VERSION, config);
}

init() {
const module = new InstrumentationNodeModuleDefinition(constants.MODULE_NAME, constants.SUPPORTED_VERSIONS);
public init(): InstrumentationNodeModuleDefinition {
const module = new InstrumentationNodeModuleDefinition(MODULE_NAME, SUPPORTED_VERSIONS);

module.files.push(
this.getClientNodeModuleFileInstrumentation('src'),
this.getClientNodeModuleFileInstrumentation('lib'),
this.getRunnerNodeModuleFileInstrumentation('src'),
this.getRunnerNodeModuleFileInstrumentation('lib'),
this.getRunnerNodeModuleFileInstrumentation('lib/execution'),
this._getClientNodeModuleFileInstrumentation('src'),
this._getClientNodeModuleFileInstrumentation('lib'),
this._getRunnerNodeModuleFileInstrumentation('src'),
this._getRunnerNodeModuleFileInstrumentation('lib'),
this._getRunnerNodeModuleFileInstrumentation('lib/execution'),
);

return module;
}

private getRunnerNodeModuleFileInstrumentation(basePath: string) {
private _getRunnerNodeModuleFileInstrumentation(basePath: string): InstrumentationNodeModuleFile {
return new InstrumentationNodeModuleFile(
`knex/${basePath}/runner.js`,
constants.SUPPORTED_VERSIONS,
SUPPORTED_VERSIONS,
(Runner: any, moduleVersion?: string) => {
this.ensureWrapped(Runner.prototype, 'query', this.createQueryWrapper(moduleVersion));
this._ensureWrapped(Runner.prototype, 'query', this._createQueryWrapper(moduleVersion));
return Runner;
},
(Runner: any, _moduleVersion?: string) => {
(Runner: any) => {
this._unwrap(Runner.prototype, 'query');
return Runner;
},
);
}

private getClientNodeModuleFileInstrumentation(basePath: string) {
private _getClientNodeModuleFileInstrumentation(basePath: string): InstrumentationNodeModuleFile {
return new InstrumentationNodeModuleFile(
`knex/${basePath}/client.js`,
constants.SUPPORTED_VERSIONS,
SUPPORTED_VERSIONS,
(Client: any) => {
this.ensureWrapped(Client.prototype, 'queryBuilder', this.storeContext.bind(this));
this.ensureWrapped(Client.prototype, 'schemaBuilder', this.storeContext.bind(this));
this.ensureWrapped(Client.prototype, 'raw', this.storeContext.bind(this));
this._ensureWrapped(Client.prototype, 'queryBuilder', this._storeContext.bind(this));
this._ensureWrapped(Client.prototype, 'schemaBuilder', this._storeContext.bind(this));
this._ensureWrapped(Client.prototype, 'raw', this._storeContext.bind(this));
return Client;
},
(Client: any) => {
Expand All@@ -112,9 +108,7 @@ export class KnexInstrumentation extends InstrumentationBase<KnexInstrumentation
);
}

private createQueryWrapper(moduleVersion?: string) {
const instrumentation = this;

private _createQueryWrapper(moduleVersion?: string) {
return function wrapQuery(original: (...args: any[]) => any) {
return function wrapped_logging_method(this: any, query: any) {
const config = this.client.config;
Expand All@@ -126,93 +120,62 @@ export class KnexInstrumentation extends InstrumentationBase<KnexInstrumentation
config?.connection?.filename ||
config?.connection?.database ||
utils.extractDatabaseFromConnectionString(connectionString);
const { maxQueryLength } = instrumentation.getConfig();

const attributes: api.Attributes = {
const attributes: SpanAttributes = {
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: ORIGIN,
'knex.version': moduleVersion,
[ATTR_DB_SYSTEM]: utils.mapSystem(this.client.driverName),
[ATTR_DB_SQL_TABLE]: table,
[ATTR_DB_OPERATION]: operation,
[ATTR_DB_USER]: config?.connection?.user,
[ATTR_DB_NAME]: name,
[ATTR_NET_PEER_NAME]: config?.connection?.host ?? utils.extractHostFromConnectionString(connectionString),
[ATTR_NET_PEER_PORT]: config?.connection?.port ?? utils.extractPortFromConnectionString(connectionString),
[ATTR_NET_TRANSPORT]: config?.connection?.filename === ':memory:' ? 'inproc' : undefined,
[ATTR_DB_STATEMENT]: utils.limitLength(query?.sql, MAX_QUERY_LENGTH),
};
const transport = config?.connection?.filename === ':memory:' ? 'inproc' : undefined;

if (instrumentation._semconvStability & SemconvStability.OLD) {
Object.assign(attributes, {
[ATTR_DB_SYSTEM]: utils.mapSystem(this.client.driverName),
[ATTR_DB_SQL_TABLE]: table,
[ATTR_DB_OPERATION]: operation,
[ATTR_DB_USER]: config?.connection?.user,
[ATTR_DB_NAME]: name,
[ATTR_NET_PEER_NAME]: config?.connection?.host ?? utils.extractHostFromConnectionString(connectionString),
[ATTR_NET_PEER_PORT]: config?.connection?.port ?? utils.extractPortFromConnectionString(connectionString),
[ATTR_NET_TRANSPORT]: transport,
});
}
if (instrumentation._semconvStability & SemconvStability.STABLE) {
Object.assign(attributes, {
[ATTR_DB_SYSTEM_NAME]: utils.mapSystem(this.client.driverName),
[ATTR_DB_COLLECTION_NAME]: table,
[ATTR_DB_OPERATION_NAME]: operation,
[ATTR_DB_NAMESPACE]: name,
[ATTR_SERVER_ADDRESS]: config?.connection?.host ?? utils.extractHostFromConnectionString(connectionString),
[ATTR_SERVER_PORT]: config?.connection?.port ?? utils.extractPortFromConnectionString(connectionString),
});
}
if (maxQueryLength) {
const queryText = utils.limitLength(query?.sql, maxQueryLength);
if (instrumentation._semconvStability & SemconvStability.STABLE) {
attributes[ATTR_DB_QUERY_TEXT] = queryText;
}
if (instrumentation._semconvStability & SemconvStability.OLD) {
attributes[ATTR_DB_STATEMENT] = queryText;
}
}

const parentContext = this.builder[contextSymbol] || api.context.active();
const parentSpan = api.trace.getSpan(parentContext);
const hasActiveParent = parentSpan && api.trace.isSpanContextValid(parentSpan.spanContext());
if (instrumentation._config.requireParentSpan && !hasActiveParent) {
return original.bind(this)(...arguments);
}

const span = instrumentation.tracer.startSpan(
utils.getName(name, operation, table),

// The query builder captures the span active when it was created (see `_storeContext`).
// `onlyIfParent` ensures we only instrument queries that run as part of an existing trace.
const parentSpan: Span | undefined = this.builder[parentSpanSymbol] || getActiveSpan();

const args = arguments;
return startSpan(
{
kind: api.SpanKind.CLIENT,
name: utils.getName(name, operation, table),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Span description no longer SQL

Medium Severity

Knex spans now use utils.getName as the span name, which becomes the exported description. The prior OTEL exporter derived descriptions from db.statement, so UI and tests expecting full SQL (for example select * from "User") no longer match.

Fix in CursorFix in Web

Reviewed by Cursor Bugbot for commit 216196d. Configure here.

kind: SpanKind.CLIENT,
attributes,
parentSpan,
onlyIfParent: true,
},
Comment thread
sentry[bot] marked this conversation as resolved.
parentContext,
span =>
// `Runner.query` returns a real, already-executing Promise, so it is safe to let
// `startSpan` await it and auto-end the span.
original.apply(this, args).catch((err: any) => {
const formatter = utils.getFormatter(this);
const fullQuery = formatter(query.sql, query.bindings || []);
const message = err.message.replace(`${fullQuery} - `, '');
span.setStatus({ code: SPAN_STATUS_ERROR, message });
throw err;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error status not normalized

Medium Severity

Failed queries set the span status message to the trimmed Knex/Postgres error text. OTEL export previously mapped non-canonical error messages to internal_error, but native spans keep the raw message, diverging from tests and prior telemetry.

Fix in CursorFix in Web

Reviewed by Cursor Bugbot for commit 216196d. Configure here.

}),
);
const spanContext = api.trace.setSpan(api.context.active(), span);

return api.context
.with(spanContext, original, this, ...arguments)
.then((result: unknown) => {
span.end();
return result;
})
.catch((err: any) => {
const formatter = utils.getFormatter(this);
const fullQuery = formatter(query.sql, query.bindings || []);
const message = err.message.replace(fullQuery + ' - ', '');
const exc = utils.otelExceptionFromKnexError(err, message);
span.recordException(exc);
span.setStatus({ code: api.SpanStatusCode.ERROR, message });
span.end();
throw err;
});
};
};
}

private storeContext(original: Function) {
private _storeContext(original: (...args: any[]) => any) {
return function wrapped_logging_method(this: any) {
const builder = original.apply(this, arguments);
Object.defineProperty(builder, contextSymbol, {
value: api.context.active(),
// Capture the span that is active when the query builder is created. The query often executes
// in a different async context, so we reuse this span as the parent when the query runs.
Object.defineProperty(builder, parentSpanSymbol, {
value: getActiveSpan(),
});
return builder;
};
}

ensureWrapped(obj: any, methodName: string, wrapper: (original: any) => any) {
private _ensureWrapped(obj: any, methodName: string, wrapper: (original: any) => any): void {
if (isWrapped(obj[methodName])) {
this._unwrap(obj, methodName);
}
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
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
Original file line numberDiff line numberDiff line change
Expand Up@@ -29,6 +29,11 @@ async function run() {

await pgClient('User').insert({ name: 'bob', email: 'bob@domain.com' });
await pgClient('User').select('*');

// Trigger a failing query to capture the error span (table does not exist).
await pgClient('DoesNotExist')
.select('*')
.catch(() => {});
} finally {
await pgClient.destroy();
}
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -57,6 +57,22 @@ describe('knex auto instrumentation', () => {
description: 'select * from "User"',
origin: 'auto.db.otel.knex',
}),

expect.objectContaining({
data: expect.objectContaining({
'knex.version': KNEX_VERSION,
'db.operation': 'select',
'db.sql.table': 'DoesNotExist',
'db.system': 'postgresql',
'db.name': 'tests',
'db.statement': 'select * from "DoesNotExist"',
'sentry.origin': 'auto.db.otel.knex',
'sentry.op': 'db',
}),
status: 'internal_error',
Comment thread
logaretm marked this conversation as resolved.
description: 'select * from "DoesNotExist"',
origin: 'auto.db.otel.knex',
}),
]),
};

Expand Down
27 changes: 4 additions & 23 deletions packages/node/src/integrations/tracing/knex/index.ts
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,17 @@
import { KnexInstrumentation } from './vendored/instrumentation';
import type { IntegrationFn } from '@sentry/core';
import { defineIntegration, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, spanToJSON } from '@sentry/core';
import { generateInstrumentOnce, instrumentWhenWrapped } from '@sentry/node-core';
import { defineIntegration } from '@sentry/core';
import { generateInstrumentOnce } from '@sentry/node-core';

const INTEGRATION_NAME = 'Knex';

export const instrumentKnex = generateInstrumentOnce(
INTEGRATION_NAME,
() => new KnexInstrumentation({ requireParentSpan: true }),
);
export const instrumentKnex = generateInstrumentOnce(INTEGRATION_NAME, () => new KnexInstrumentation());

const _knexIntegration = (() => {
let instrumentationWrappedCallback: undefined | ((callback: () => void) => void);

return {
name: INTEGRATION_NAME,
setupOnce() {
const instrumentation = instrumentKnex();
instrumentationWrappedCallback = instrumentWhenWrapped(instrumentation);
},

setup(client) {
instrumentationWrappedCallback?.(() =>
client.on('spanStart', span => {
const { data } = spanToJSON(span);
// knex.version is always set in the span data
// https://github.com/open-telemetry/opentelemetry-js-contrib/blob/0309caeafc44ac9cb13a3345b790b01b76d0497d/plugins/node/opentelemetry-instrumentation-knex/src/instrumentation.ts#L138
if ('knex.version' in data) {
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, 'auto.db.otel.knex');
}
}),
);
instrumentKnex();
},
};
}) satisfies IntegrationFn;
Expand Down

This file was deleted.

Original file line numberDiff line numberDiff line change
Expand Up@@ -6,31 +6,23 @@
* - Vendored from: https://github.com/open-telemetry/opentelemetry-js-contrib/tree/15ef7506553f631ea4181391e0c5725a56f0d082/packages/instrumentation-knex
* - Upstream version: @opentelemetry/instrumentation-knex@0.62.0
* - Minor TypeScript strictness adjustments for this repository's compiler settings
* - Refactored to use Sentry's span APIs instead of OpenTelemetry tracing APIs
*/
/* eslint-disable */

import * as api from '@opentelemetry/api';
import { SDK_VERSION } from '@sentry/core';
import * as constants from './constants';
/* oxlint-disable typescript/no-deprecated */

import { SpanKind } from '@opentelemetry/api';
import type { InstrumentationConfig } from '@opentelemetry/instrumentation';
import { InstrumentationBase, InstrumentationNodeModuleDefinition, isWrapped } from '@opentelemetry/instrumentation';
import type { Span, SpanAttributes } from '@sentry/core';
import {
InstrumentationBase,
InstrumentationNodeModuleDefinition,
isWrapped,
SemconvStability,
semconvStabilityFromStr,
} from '@opentelemetry/instrumentation';
getActiveSpan,
SDK_VERSION,
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
SPAN_STATUS_ERROR,
startSpan,
} from '@sentry/core';
import { InstrumentationNodeModuleFile } from '../../InstrumentationNodeModuleFile';
import * as utils from './utils';
import { KnexInstrumentationConfig } from './types';
import {
ATTR_DB_COLLECTION_NAME,
ATTR_DB_NAMESPACE,
ATTR_DB_OPERATION_NAME,
ATTR_DB_QUERY_TEXT,
ATTR_DB_SYSTEM_NAME,
ATTR_SERVER_ADDRESS,
ATTR_SERVER_PORT,
} from '@opentelemetry/semantic-conventions';
import {
ATTR_DB_NAME,
ATTR_DB_OPERATION,
Expand All@@ -42,65 +34,69 @@ import {
ATTR_NET_PEER_PORT,
ATTR_NET_TRANSPORT,
} from './semconv';
import * as utils from './utils';

const PACKAGE_NAME = '@sentry/instrumentation-knex';

const contextSymbol = Symbol('opentelemetry.instrumentation-knex.context');
const DEFAULT_CONFIG: KnexInstrumentationConfig = {
maxQueryLength: 1022,
requireParentSpan: false,
};

export class KnexInstrumentation extends InstrumentationBase<KnexInstrumentationConfig> {
private _semconvStability: SemconvStability;

constructor(config: KnexInstrumentationConfig = {}) {
super(PACKAGE_NAME, SDK_VERSION, { ...DEFAULT_CONFIG, ...config });

this._semconvStability = semconvStabilityFromStr('database', process.env.OTEL_SEMCONV_STABILITY_OPT_IN);
}

override setConfig(config: KnexInstrumentationConfig = {}) {
super.setConfig({ ...DEFAULT_CONFIG, ...config });
const ORIGIN = 'auto.db.otel.knex';

const MODULE_NAME = 'knex';
const SUPPORTED_VERSIONS = [
// use "lib/execution" for runner.js, "lib" for client.js as basepath, latest tested 0.95.6
'>=0.22.0 <4',
// use "lib" as basepath
'>=0.10.0 <0.18.0',
'>=0.19.0 <0.22.0',
// use "src" as basepath
'>=0.18.0 <0.19.0',
];

// Max length of the query text captured in the `db.statement` attribute; ".." is appended when truncated.
const MAX_QUERY_LENGTH = 1022;

const parentSpanSymbol = Symbol('sentry.instrumentation-knex.parent-span');

export class KnexInstrumentation extends InstrumentationBase<InstrumentationConfig> {
public constructor(config: InstrumentationConfig = {}) {
super(PACKAGE_NAME, SDK_VERSION, config);
}

init() {
const module = new InstrumentationNodeModuleDefinition(constants.MODULE_NAME, constants.SUPPORTED_VERSIONS);
public init(): InstrumentationNodeModuleDefinition {
const module = new InstrumentationNodeModuleDefinition(MODULE_NAME, SUPPORTED_VERSIONS);

module.files.push(
this.getClientNodeModuleFileInstrumentation('src'),
this.getClientNodeModuleFileInstrumentation('lib'),
this.getRunnerNodeModuleFileInstrumentation('src'),
this.getRunnerNodeModuleFileInstrumentation('lib'),
this.getRunnerNodeModuleFileInstrumentation('lib/execution'),
this._getClientNodeModuleFileInstrumentation('src'),
this._getClientNodeModuleFileInstrumentation('lib'),
this._getRunnerNodeModuleFileInstrumentation('src'),
this._getRunnerNodeModuleFileInstrumentation('lib'),
this._getRunnerNodeModuleFileInstrumentation('lib/execution'),
);

return module;
}

private getRunnerNodeModuleFileInstrumentation(basePath: string) {
private _getRunnerNodeModuleFileInstrumentation(basePath: string): InstrumentationNodeModuleFile {
return new InstrumentationNodeModuleFile(
`knex/${basePath}/runner.js`,
constants.SUPPORTED_VERSIONS,
SUPPORTED_VERSIONS,
(Runner: any, moduleVersion?: string) => {
this.ensureWrapped(Runner.prototype, 'query', this.createQueryWrapper(moduleVersion));
this._ensureWrapped(Runner.prototype, 'query', this._createQueryWrapper(moduleVersion));
return Runner;
},
(Runner: any, _moduleVersion?: string) => {
(Runner: any) => {
this._unwrap(Runner.prototype, 'query');
return Runner;
},
);
}

private getClientNodeModuleFileInstrumentation(basePath: string) {
private _getClientNodeModuleFileInstrumentation(basePath: string): InstrumentationNodeModuleFile {
return new InstrumentationNodeModuleFile(
`knex/${basePath}/client.js`,
constants.SUPPORTED_VERSIONS,
SUPPORTED_VERSIONS,
(Client: any) => {
this.ensureWrapped(Client.prototype, 'queryBuilder', this.storeContext.bind(this));
this.ensureWrapped(Client.prototype, 'schemaBuilder', this.storeContext.bind(this));
this.ensureWrapped(Client.prototype, 'raw', this.storeContext.bind(this));
this._ensureWrapped(Client.prototype, 'queryBuilder', this._storeContext.bind(this));
this._ensureWrapped(Client.prototype, 'schemaBuilder', this._storeContext.bind(this));
this._ensureWrapped(Client.prototype, 'raw', this._storeContext.bind(this));
return Client;
},
(Client: any) => {
Expand All@@ -112,9 +108,7 @@ export class KnexInstrumentation extends InstrumentationBase<KnexInstrumentation
);
}

private createQueryWrapper(moduleVersion?: string) {
const instrumentation = this;

private _createQueryWrapper(moduleVersion?: string) {
return function wrapQuery(original: (...args: any[]) => any) {
return function wrapped_logging_method(this: any, query: any) {
const config = this.client.config;
Expand All@@ -126,93 +120,62 @@ export class KnexInstrumentation extends InstrumentationBase<KnexInstrumentation
config?.connection?.filename ||
config?.connection?.database ||
utils.extractDatabaseFromConnectionString(connectionString);
const { maxQueryLength } = instrumentation.getConfig();

const attributes: api.Attributes = {
const attributes: SpanAttributes = {
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: ORIGIN,
'knex.version': moduleVersion,
[ATTR_DB_SYSTEM]: utils.mapSystem(this.client.driverName),
[ATTR_DB_SQL_TABLE]: table,
[ATTR_DB_OPERATION]: operation,
[ATTR_DB_USER]: config?.connection?.user,
[ATTR_DB_NAME]: name,
[ATTR_NET_PEER_NAME]: config?.connection?.host ?? utils.extractHostFromConnectionString(connectionString),
[ATTR_NET_PEER_PORT]: config?.connection?.port ?? utils.extractPortFromConnectionString(connectionString),
[ATTR_NET_TRANSPORT]: config?.connection?.filename === ':memory:' ? 'inproc' : undefined,
[ATTR_DB_STATEMENT]: utils.limitLength(query?.sql, MAX_QUERY_LENGTH),
};
const transport = config?.connection?.filename === ':memory:' ? 'inproc' : undefined;

if (instrumentation._semconvStability & SemconvStability.OLD) {
Object.assign(attributes, {
[ATTR_DB_SYSTEM]: utils.mapSystem(this.client.driverName),
[ATTR_DB_SQL_TABLE]: table,
[ATTR_DB_OPERATION]: operation,
[ATTR_DB_USER]: config?.connection?.user,
[ATTR_DB_NAME]: name,
[ATTR_NET_PEER_NAME]: config?.connection?.host ?? utils.extractHostFromConnectionString(connectionString),
[ATTR_NET_PEER_PORT]: config?.connection?.port ?? utils.extractPortFromConnectionString(connectionString),
[ATTR_NET_TRANSPORT]: transport,
});
}
if (instrumentation._semconvStability & SemconvStability.STABLE) {
Object.assign(attributes, {
[ATTR_DB_SYSTEM_NAME]: utils.mapSystem(this.client.driverName),
[ATTR_DB_COLLECTION_NAME]: table,
[ATTR_DB_OPERATION_NAME]: operation,
[ATTR_DB_NAMESPACE]: name,
[ATTR_SERVER_ADDRESS]: config?.connection?.host ?? utils.extractHostFromConnectionString(connectionString),
[ATTR_SERVER_PORT]: config?.connection?.port ?? utils.extractPortFromConnectionString(connectionString),
});
}
if (maxQueryLength) {
const queryText = utils.limitLength(query?.sql, maxQueryLength);
if (instrumentation._semconvStability & SemconvStability.STABLE) {
attributes[ATTR_DB_QUERY_TEXT] = queryText;
}
if (instrumentation._semconvStability & SemconvStability.OLD) {
attributes[ATTR_DB_STATEMENT] = queryText;
}
}

const parentContext = this.builder[contextSymbol] || api.context.active();
const parentSpan = api.trace.getSpan(parentContext);
const hasActiveParent = parentSpan && api.trace.isSpanContextValid(parentSpan.spanContext());
if (instrumentation._config.requireParentSpan && !hasActiveParent) {
return original.bind(this)(...arguments);
}

const span = instrumentation.tracer.startSpan(
utils.getName(name, operation, table),

// The query builder captures the span active when it was created (see `_storeContext`).
// `onlyIfParent` ensures we only instrument queries that run as part of an existing trace.
const parentSpan: Span | undefined = this.builder[parentSpanSymbol] || getActiveSpan();

const args = arguments;
return startSpan(
{
kind: api.SpanKind.CLIENT,
name: utils.getName(name, operation, table),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Span description no longer SQL

Medium Severity

Knex spans now use utils.getName as the span name, which becomes the exported description. The prior OTEL exporter derived descriptions from db.statement, so UI and tests expecting full SQL (for example select * from "User") no longer match.

Fix in CursorFix in Web

Reviewed by Cursor Bugbot for commit 216196d. Configure here.

kind: SpanKind.CLIENT,
attributes,
parentSpan,
onlyIfParent: true,
},
Comment thread
sentry[bot] marked this conversation as resolved.
parentContext,
span =>
// `Runner.query` returns a real, already-executing Promise, so it is safe to let
// `startSpan` await it and auto-end the span.
original.apply(this, args).catch((err: any) => {
const formatter = utils.getFormatter(this);
const fullQuery = formatter(query.sql, query.bindings || []);
const message = err.message.replace(`${fullQuery} - `, '');
span.setStatus({ code: SPAN_STATUS_ERROR, message });
throw err;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error status not normalized

Medium Severity

Failed queries set the span status message to the trimmed Knex/Postgres error text. OTEL export previously mapped non-canonical error messages to internal_error, but native spans keep the raw message, diverging from tests and prior telemetry.

Fix in CursorFix in Web

Reviewed by Cursor Bugbot for commit 216196d. Configure here.

}),
);
const spanContext = api.trace.setSpan(api.context.active(), span);

return api.context
.with(spanContext, original, this, ...arguments)
.then((result: unknown) => {
span.end();
return result;
})
.catch((err: any) => {
const formatter = utils.getFormatter(this);
const fullQuery = formatter(query.sql, query.bindings || []);
const message = err.message.replace(fullQuery + ' - ', '');
const exc = utils.otelExceptionFromKnexError(err, message);
span.recordException(exc);
span.setStatus({ code: api.SpanStatusCode.ERROR, message });
span.end();
throw err;
});
};
};
}

private storeContext(original: Function) {
private _storeContext(original: (...args: any[]) => any) {
return function wrapped_logging_method(this: any) {
const builder = original.apply(this, arguments);
Object.defineProperty(builder, contextSymbol, {
value: api.context.active(),
// Capture the span that is active when the query builder is created. The query often executes
// in a different async context, so we reuse this span as the parent when the query runs.
Object.defineProperty(builder, parentSpanSymbol, {
value: getActiveSpan(),
});
return builder;
};
}

ensureWrapped(obj: any, methodName: string, wrapper: (original: any) => any) {
private _ensureWrapped(obj: any, methodName: string, wrapper: (original: any) => any): void {
if (isWrapped(obj[methodName])) {
this._unwrap(obj, methodName);
}
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
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
Original file line numberDiff line numberDiff line change
Expand Up@@ -29,6 +29,11 @@ async function run() {

await pgClient('User').insert({ name: 'bob', email: 'bob@domain.com' });
await pgClient('User').select('*');

// Trigger a failing query to capture the error span (table does not exist).
await pgClient('DoesNotExist')
.select('*')
.catch(() => {});
} finally {
await pgClient.destroy();
}
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -57,6 +57,22 @@ describe('knex auto instrumentation', () => {
description: 'select * from "User"',
origin: 'auto.db.otel.knex',
}),

expect.objectContaining({
data: expect.objectContaining({
'knex.version': KNEX_VERSION,
'db.operation': 'select',
'db.sql.table': 'DoesNotExist',
'db.system': 'postgresql',
'db.name': 'tests',
'db.statement': 'select * from "DoesNotExist"',
'sentry.origin': 'auto.db.otel.knex',
'sentry.op': 'db',
}),
status: 'internal_error',
Comment thread
logaretm marked this conversation as resolved.
description: 'select * from "DoesNotExist"',
origin: 'auto.db.otel.knex',
}),
]),
};

Expand Down
27 changes: 4 additions & 23 deletions packages/node/src/integrations/tracing/knex/index.ts
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,17 @@
import { KnexInstrumentation } from './vendored/instrumentation';
import type { IntegrationFn } from '@sentry/core';
import { defineIntegration, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, spanToJSON } from '@sentry/core';
import { generateInstrumentOnce, instrumentWhenWrapped } from '@sentry/node-core';
import { defineIntegration } from '@sentry/core';
import { generateInstrumentOnce } from '@sentry/node-core';

const INTEGRATION_NAME = 'Knex';

export const instrumentKnex = generateInstrumentOnce(
INTEGRATION_NAME,
() => new KnexInstrumentation({ requireParentSpan: true }),
);
export const instrumentKnex = generateInstrumentOnce(INTEGRATION_NAME, () => new KnexInstrumentation());

const _knexIntegration = (() => {
let instrumentationWrappedCallback: undefined | ((callback: () => void) => void);

return {
name: INTEGRATION_NAME,
setupOnce() {
const instrumentation = instrumentKnex();
instrumentationWrappedCallback = instrumentWhenWrapped(instrumentation);
},

setup(client) {
instrumentationWrappedCallback?.(() =>
client.on('spanStart', span => {
const { data } = spanToJSON(span);
// knex.version is always set in the span data
// https://github.com/open-telemetry/opentelemetry-js-contrib/blob/0309caeafc44ac9cb13a3345b790b01b76d0497d/plugins/node/opentelemetry-instrumentation-knex/src/instrumentation.ts#L138
if ('knex.version' in data) {
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, 'auto.db.otel.knex');
}
}),
);
instrumentKnex();
},
};
}) satisfies IntegrationFn;
Expand Down

This file was deleted.

Original file line numberDiff line numberDiff line change
Expand Up@@ -6,31 +6,23 @@
* - Vendored from: https://github.com/open-telemetry/opentelemetry-js-contrib/tree/15ef7506553f631ea4181391e0c5725a56f0d082/packages/instrumentation-knex
* - Upstream version: @opentelemetry/instrumentation-knex@0.62.0
* - Minor TypeScript strictness adjustments for this repository's compiler settings
* - Refactored to use Sentry's span APIs instead of OpenTelemetry tracing APIs
*/
/* eslint-disable */

import * as api from '@opentelemetry/api';
import { SDK_VERSION } from '@sentry/core';
import * as constants from './constants';
/* oxlint-disable typescript/no-deprecated */

import { SpanKind } from '@opentelemetry/api';
import type { InstrumentationConfig } from '@opentelemetry/instrumentation';
import { InstrumentationBase, InstrumentationNodeModuleDefinition, isWrapped } from '@opentelemetry/instrumentation';
import type { Span, SpanAttributes } from '@sentry/core';
import {
InstrumentationBase,
InstrumentationNodeModuleDefinition,
isWrapped,
SemconvStability,
semconvStabilityFromStr,
} from '@opentelemetry/instrumentation';
getActiveSpan,
SDK_VERSION,
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
SPAN_STATUS_ERROR,
startSpan,
} from '@sentry/core';
import { InstrumentationNodeModuleFile } from '../../InstrumentationNodeModuleFile';
import * as utils from './utils';
import { KnexInstrumentationConfig } from './types';
import {
ATTR_DB_COLLECTION_NAME,
ATTR_DB_NAMESPACE,
ATTR_DB_OPERATION_NAME,
ATTR_DB_QUERY_TEXT,
ATTR_DB_SYSTEM_NAME,
ATTR_SERVER_ADDRESS,
ATTR_SERVER_PORT,
} from '@opentelemetry/semantic-conventions';
import {
ATTR_DB_NAME,
ATTR_DB_OPERATION,
Expand All@@ -42,65 +34,69 @@ import {
ATTR_NET_PEER_PORT,
ATTR_NET_TRANSPORT,
} from './semconv';
import * as utils from './utils';

const PACKAGE_NAME = '@sentry/instrumentation-knex';

const contextSymbol = Symbol('opentelemetry.instrumentation-knex.context');
const DEFAULT_CONFIG: KnexInstrumentationConfig = {
maxQueryLength: 1022,
requireParentSpan: false,
};

export class KnexInstrumentation extends InstrumentationBase<KnexInstrumentationConfig> {
private _semconvStability: SemconvStability;

constructor(config: KnexInstrumentationConfig = {}) {
super(PACKAGE_NAME, SDK_VERSION, { ...DEFAULT_CONFIG, ...config });

this._semconvStability = semconvStabilityFromStr('database', process.env.OTEL_SEMCONV_STABILITY_OPT_IN);
}

override setConfig(config: KnexInstrumentationConfig = {}) {
super.setConfig({ ...DEFAULT_CONFIG, ...config });
const ORIGIN = 'auto.db.otel.knex';

const MODULE_NAME = 'knex';
const SUPPORTED_VERSIONS = [
// use "lib/execution" for runner.js, "lib" for client.js as basepath, latest tested 0.95.6
'>=0.22.0 <4',
// use "lib" as basepath
'>=0.10.0 <0.18.0',
'>=0.19.0 <0.22.0',
// use "src" as basepath
'>=0.18.0 <0.19.0',
];

// Max length of the query text captured in the `db.statement` attribute; ".." is appended when truncated.
const MAX_QUERY_LENGTH = 1022;

const parentSpanSymbol = Symbol('sentry.instrumentation-knex.parent-span');

export class KnexInstrumentation extends InstrumentationBase<InstrumentationConfig> {
public constructor(config: InstrumentationConfig = {}) {
super(PACKAGE_NAME, SDK_VERSION, config);
}

init() {
const module = new InstrumentationNodeModuleDefinition(constants.MODULE_NAME, constants.SUPPORTED_VERSIONS);
public init(): InstrumentationNodeModuleDefinition {
const module = new InstrumentationNodeModuleDefinition(MODULE_NAME, SUPPORTED_VERSIONS);

module.files.push(
this.getClientNodeModuleFileInstrumentation('src'),
this.getClientNodeModuleFileInstrumentation('lib'),
this.getRunnerNodeModuleFileInstrumentation('src'),
this.getRunnerNodeModuleFileInstrumentation('lib'),
this.getRunnerNodeModuleFileInstrumentation('lib/execution'),
this._getClientNodeModuleFileInstrumentation('src'),
this._getClientNodeModuleFileInstrumentation('lib'),
this._getRunnerNodeModuleFileInstrumentation('src'),
this._getRunnerNodeModuleFileInstrumentation('lib'),
this._getRunnerNodeModuleFileInstrumentation('lib/execution'),
);

return module;
}

private getRunnerNodeModuleFileInstrumentation(basePath: string) {
private _getRunnerNodeModuleFileInstrumentation(basePath: string): InstrumentationNodeModuleFile {
return new InstrumentationNodeModuleFile(
`knex/${basePath}/runner.js`,
constants.SUPPORTED_VERSIONS,
SUPPORTED_VERSIONS,
(Runner: any, moduleVersion?: string) => {
this.ensureWrapped(Runner.prototype, 'query', this.createQueryWrapper(moduleVersion));
this._ensureWrapped(Runner.prototype, 'query', this._createQueryWrapper(moduleVersion));
return Runner;
},
(Runner: any, _moduleVersion?: string) => {
(Runner: any) => {
this._unwrap(Runner.prototype, 'query');
return Runner;
},
);
}

private getClientNodeModuleFileInstrumentation(basePath: string) {
private _getClientNodeModuleFileInstrumentation(basePath: string): InstrumentationNodeModuleFile {
return new InstrumentationNodeModuleFile(
`knex/${basePath}/client.js`,
constants.SUPPORTED_VERSIONS,
SUPPORTED_VERSIONS,
(Client: any) => {
this.ensureWrapped(Client.prototype, 'queryBuilder', this.storeContext.bind(this));
this.ensureWrapped(Client.prototype, 'schemaBuilder', this.storeContext.bind(this));
this.ensureWrapped(Client.prototype, 'raw', this.storeContext.bind(this));
this._ensureWrapped(Client.prototype, 'queryBuilder', this._storeContext.bind(this));
this._ensureWrapped(Client.prototype, 'schemaBuilder', this._storeContext.bind(this));
this._ensureWrapped(Client.prototype, 'raw', this._storeContext.bind(this));
return Client;
},
(Client: any) => {
Expand All@@ -112,9 +108,7 @@ export class KnexInstrumentation extends InstrumentationBase<KnexInstrumentation
);
}

private createQueryWrapper(moduleVersion?: string) {
const instrumentation = this;

private _createQueryWrapper(moduleVersion?: string) {
return function wrapQuery(original: (...args: any[]) => any) {
return function wrapped_logging_method(this: any, query: any) {
const config = this.client.config;
Expand All@@ -126,93 +120,62 @@ export class KnexInstrumentation extends InstrumentationBase<KnexInstrumentation
config?.connection?.filename ||
config?.connection?.database ||
utils.extractDatabaseFromConnectionString(connectionString);
const { maxQueryLength } = instrumentation.getConfig();

const attributes: api.Attributes = {
const attributes: SpanAttributes = {
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: ORIGIN,
'knex.version': moduleVersion,
[ATTR_DB_SYSTEM]: utils.mapSystem(this.client.driverName),
[ATTR_DB_SQL_TABLE]: table,
[ATTR_DB_OPERATION]: operation,
[ATTR_DB_USER]: config?.connection?.user,
[ATTR_DB_NAME]: name,
[ATTR_NET_PEER_NAME]: config?.connection?.host ?? utils.extractHostFromConnectionString(connectionString),
[ATTR_NET_PEER_PORT]: config?.connection?.port ?? utils.extractPortFromConnectionString(connectionString),
[ATTR_NET_TRANSPORT]: config?.connection?.filename === ':memory:' ? 'inproc' : undefined,
[ATTR_DB_STATEMENT]: utils.limitLength(query?.sql, MAX_QUERY_LENGTH),
};
const transport = config?.connection?.filename === ':memory:' ? 'inproc' : undefined;

if (instrumentation._semconvStability & SemconvStability.OLD) {
Object.assign(attributes, {
[ATTR_DB_SYSTEM]: utils.mapSystem(this.client.driverName),
[ATTR_DB_SQL_TABLE]: table,
[ATTR_DB_OPERATION]: operation,
[ATTR_DB_USER]: config?.connection?.user,
[ATTR_DB_NAME]: name,
[ATTR_NET_PEER_NAME]: config?.connection?.host ?? utils.extractHostFromConnectionString(connectionString),
[ATTR_NET_PEER_PORT]: config?.connection?.port ?? utils.extractPortFromConnectionString(connectionString),
[ATTR_NET_TRANSPORT]: transport,
});
}
if (instrumentation._semconvStability & SemconvStability.STABLE) {
Object.assign(attributes, {
[ATTR_DB_SYSTEM_NAME]: utils.mapSystem(this.client.driverName),
[ATTR_DB_COLLECTION_NAME]: table,
[ATTR_DB_OPERATION_NAME]: operation,
[ATTR_DB_NAMESPACE]: name,
[ATTR_SERVER_ADDRESS]: config?.connection?.host ?? utils.extractHostFromConnectionString(connectionString),
[ATTR_SERVER_PORT]: config?.connection?.port ?? utils.extractPortFromConnectionString(connectionString),
});
}
if (maxQueryLength) {
const queryText = utils.limitLength(query?.sql, maxQueryLength);
if (instrumentation._semconvStability & SemconvStability.STABLE) {
attributes[ATTR_DB_QUERY_TEXT] = queryText;
}
if (instrumentation._semconvStability & SemconvStability.OLD) {
attributes[ATTR_DB_STATEMENT] = queryText;
}
}

const parentContext = this.builder[contextSymbol] || api.context.active();
const parentSpan = api.trace.getSpan(parentContext);
const hasActiveParent = parentSpan && api.trace.isSpanContextValid(parentSpan.spanContext());
if (instrumentation._config.requireParentSpan && !hasActiveParent) {
return original.bind(this)(...arguments);
}

const span = instrumentation.tracer.startSpan(
utils.getName(name, operation, table),

// The query builder captures the span active when it was created (see `_storeContext`).
// `onlyIfParent` ensures we only instrument queries that run as part of an existing trace.
const parentSpan: Span | undefined = this.builder[parentSpanSymbol] || getActiveSpan();

const args = arguments;
return startSpan(
{
kind: api.SpanKind.CLIENT,
name: utils.getName(name, operation, table),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Span description no longer SQL

Medium Severity

Knex spans now use utils.getName as the span name, which becomes the exported description. The prior OTEL exporter derived descriptions from db.statement, so UI and tests expecting full SQL (for example select * from "User") no longer match.

Fix in CursorFix in Web

Reviewed by Cursor Bugbot for commit 216196d. Configure here.

kind: SpanKind.CLIENT,
attributes,
parentSpan,
onlyIfParent: true,
},
Comment thread
sentry[bot] marked this conversation as resolved.
parentContext,
span =>
// `Runner.query` returns a real, already-executing Promise, so it is safe to let
// `startSpan` await it and auto-end the span.
original.apply(this, args).catch((err: any) => {
const formatter = utils.getFormatter(this);
const fullQuery = formatter(query.sql, query.bindings || []);
const message = err.message.replace(`${fullQuery} - `, '');
span.setStatus({ code: SPAN_STATUS_ERROR, message });
throw err;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error status not normalized

Medium Severity

Failed queries set the span status message to the trimmed Knex/Postgres error text. OTEL export previously mapped non-canonical error messages to internal_error, but native spans keep the raw message, diverging from tests and prior telemetry.

Fix in CursorFix in Web

Reviewed by Cursor Bugbot for commit 216196d. Configure here.

}),
);
const spanContext = api.trace.setSpan(api.context.active(), span);

return api.context
.with(spanContext, original, this, ...arguments)
.then((result: unknown) => {
span.end();
return result;
})
.catch((err: any) => {
const formatter = utils.getFormatter(this);
const fullQuery = formatter(query.sql, query.bindings || []);
const message = err.message.replace(fullQuery + ' - ', '');
const exc = utils.otelExceptionFromKnexError(err, message);
span.recordException(exc);
span.setStatus({ code: api.SpanStatusCode.ERROR, message });
span.end();
throw err;
});
};
};
}

private storeContext(original: Function) {
private _storeContext(original: (...args: any[]) => any) {
return function wrapped_logging_method(this: any) {
const builder = original.apply(this, arguments);
Object.defineProperty(builder, contextSymbol, {
value: api.context.active(),
// Capture the span that is active when the query builder is created. The query often executes
// in a different async context, so we reuse this span as the parent when the query runs.
Object.defineProperty(builder, parentSpanSymbol, {
value: getActiveSpan(),
});
return builder;
};
}

ensureWrapped(obj: any, methodName: string, wrapper: (original: any) => any) {
private _ensureWrapped(obj: any, methodName: string, wrapper: (original: any) => any): void {
if (isWrapped(obj[methodName])) {
this._unwrap(obj, methodName);
}
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
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
Original file line numberDiff line numberDiff line change
Expand Up@@ -29,6 +29,11 @@ async function run() {

await pgClient('User').insert({ name: 'bob', email: 'bob@domain.com' });
await pgClient('User').select('*');

// Trigger a failing query to capture the error span (table does not exist).
await pgClient('DoesNotExist')
.select('*')
.catch(() => {});
} finally {
await pgClient.destroy();
}
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -57,6 +57,22 @@ describe('knex auto instrumentation', () => {
description: 'select * from "User"',
origin: 'auto.db.otel.knex',
}),

expect.objectContaining({
data: expect.objectContaining({
'knex.version': KNEX_VERSION,
'db.operation': 'select',
'db.sql.table': 'DoesNotExist',
'db.system': 'postgresql',
'db.name': 'tests',
'db.statement': 'select * from "DoesNotExist"',
'sentry.origin': 'auto.db.otel.knex',
'sentry.op': 'db',
}),
status: 'internal_error',
Comment thread
logaretm marked this conversation as resolved.
description: 'select * from "DoesNotExist"',
origin: 'auto.db.otel.knex',
}),
]),
};

Expand Down
27 changes: 4 additions & 23 deletions packages/node/src/integrations/tracing/knex/index.ts
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,17 @@
import { KnexInstrumentation } from './vendored/instrumentation';
import type { IntegrationFn } from '@sentry/core';
import { defineIntegration, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, spanToJSON } from '@sentry/core';
import { generateInstrumentOnce, instrumentWhenWrapped } from '@sentry/node-core';
import { defineIntegration } from '@sentry/core';
import { generateInstrumentOnce } from '@sentry/node-core';

const INTEGRATION_NAME = 'Knex';

export const instrumentKnex = generateInstrumentOnce(
INTEGRATION_NAME,
() => new KnexInstrumentation({ requireParentSpan: true }),
);
export const instrumentKnex = generateInstrumentOnce(INTEGRATION_NAME, () => new KnexInstrumentation());

const _knexIntegration = (() => {
let instrumentationWrappedCallback: undefined | ((callback: () => void) => void);

return {
name: INTEGRATION_NAME,
setupOnce() {
const instrumentation = instrumentKnex();
instrumentationWrappedCallback = instrumentWhenWrapped(instrumentation);
},

setup(client) {
instrumentationWrappedCallback?.(() =>
client.on('spanStart', span => {
const { data } = spanToJSON(span);
// knex.version is always set in the span data
// https://github.com/open-telemetry/opentelemetry-js-contrib/blob/0309caeafc44ac9cb13a3345b790b01b76d0497d/plugins/node/opentelemetry-instrumentation-knex/src/instrumentation.ts#L138
if ('knex.version' in data) {
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, 'auto.db.otel.knex');
}
}),
);
instrumentKnex();
},
};
}) satisfies IntegrationFn;
Expand Down

This file was deleted.

Original file line numberDiff line numberDiff line change
Expand Up@@ -6,31 +6,23 @@
* - Vendored from: https://github.com/open-telemetry/opentelemetry-js-contrib/tree/15ef7506553f631ea4181391e0c5725a56f0d082/packages/instrumentation-knex
* - Upstream version: @opentelemetry/instrumentation-knex@0.62.0
* - Minor TypeScript strictness adjustments for this repository's compiler settings
* - Refactored to use Sentry's span APIs instead of OpenTelemetry tracing APIs
*/
/* eslint-disable */

import * as api from '@opentelemetry/api';
import { SDK_VERSION } from '@sentry/core';
import * as constants from './constants';
/* oxlint-disable typescript/no-deprecated */

import { SpanKind } from '@opentelemetry/api';
import type { InstrumentationConfig } from '@opentelemetry/instrumentation';
import { InstrumentationBase, InstrumentationNodeModuleDefinition, isWrapped } from '@opentelemetry/instrumentation';
import type { Span, SpanAttributes } from '@sentry/core';
import {
InstrumentationBase,
InstrumentationNodeModuleDefinition,
isWrapped,
SemconvStability,
semconvStabilityFromStr,
} from '@opentelemetry/instrumentation';
getActiveSpan,
SDK_VERSION,
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
SPAN_STATUS_ERROR,
startSpan,
} from '@sentry/core';
import { InstrumentationNodeModuleFile } from '../../InstrumentationNodeModuleFile';
import * as utils from './utils';
import { KnexInstrumentationConfig } from './types';
import {
ATTR_DB_COLLECTION_NAME,
ATTR_DB_NAMESPACE,
ATTR_DB_OPERATION_NAME,
ATTR_DB_QUERY_TEXT,
ATTR_DB_SYSTEM_NAME,
ATTR_SERVER_ADDRESS,
ATTR_SERVER_PORT,
} from '@opentelemetry/semantic-conventions';
import {
ATTR_DB_NAME,
ATTR_DB_OPERATION,
Expand All@@ -42,65 +34,69 @@ import {
ATTR_NET_PEER_PORT,
ATTR_NET_TRANSPORT,
} from './semconv';
import * as utils from './utils';

const PACKAGE_NAME = '@sentry/instrumentation-knex';

const contextSymbol = Symbol('opentelemetry.instrumentation-knex.context');
const DEFAULT_CONFIG: KnexInstrumentationConfig = {
maxQueryLength: 1022,
requireParentSpan: false,
};

export class KnexInstrumentation extends InstrumentationBase<KnexInstrumentationConfig> {
private _semconvStability: SemconvStability;

constructor(config: KnexInstrumentationConfig = {}) {
super(PACKAGE_NAME, SDK_VERSION, { ...DEFAULT_CONFIG, ...config });

this._semconvStability = semconvStabilityFromStr('database', process.env.OTEL_SEMCONV_STABILITY_OPT_IN);
}

override setConfig(config: KnexInstrumentationConfig = {}) {
super.setConfig({ ...DEFAULT_CONFIG, ...config });
const ORIGIN = 'auto.db.otel.knex';

const MODULE_NAME = 'knex';
const SUPPORTED_VERSIONS = [
// use "lib/execution" for runner.js, "lib" for client.js as basepath, latest tested 0.95.6
'>=0.22.0 <4',
// use "lib" as basepath
'>=0.10.0 <0.18.0',
'>=0.19.0 <0.22.0',
// use "src" as basepath
'>=0.18.0 <0.19.0',
];

// Max length of the query text captured in the `db.statement` attribute; ".." is appended when truncated.
const MAX_QUERY_LENGTH = 1022;

const parentSpanSymbol = Symbol('sentry.instrumentation-knex.parent-span');

export class KnexInstrumentation extends InstrumentationBase<InstrumentationConfig> {
public constructor(config: InstrumentationConfig = {}) {
super(PACKAGE_NAME, SDK_VERSION, config);
}

init() {
const module = new InstrumentationNodeModuleDefinition(constants.MODULE_NAME, constants.SUPPORTED_VERSIONS);
public init(): InstrumentationNodeModuleDefinition {
const module = new InstrumentationNodeModuleDefinition(MODULE_NAME, SUPPORTED_VERSIONS);

module.files.push(
this.getClientNodeModuleFileInstrumentation('src'),
this.getClientNodeModuleFileInstrumentation('lib'),
this.getRunnerNodeModuleFileInstrumentation('src'),
this.getRunnerNodeModuleFileInstrumentation('lib'),
this.getRunnerNodeModuleFileInstrumentation('lib/execution'),
this._getClientNodeModuleFileInstrumentation('src'),
this._getClientNodeModuleFileInstrumentation('lib'),
this._getRunnerNodeModuleFileInstrumentation('src'),
this._getRunnerNodeModuleFileInstrumentation('lib'),
this._getRunnerNodeModuleFileInstrumentation('lib/execution'),
);

return module;
}

private getRunnerNodeModuleFileInstrumentation(basePath: string) {
private _getRunnerNodeModuleFileInstrumentation(basePath: string): InstrumentationNodeModuleFile {
return new InstrumentationNodeModuleFile(
`knex/${basePath}/runner.js`,
constants.SUPPORTED_VERSIONS,
SUPPORTED_VERSIONS,
(Runner: any, moduleVersion?: string) => {
this.ensureWrapped(Runner.prototype, 'query', this.createQueryWrapper(moduleVersion));
this._ensureWrapped(Runner.prototype, 'query', this._createQueryWrapper(moduleVersion));
return Runner;
},
(Runner: any, _moduleVersion?: string) => {
(Runner: any) => {
this._unwrap(Runner.prototype, 'query');
return Runner;
},
);
}

private getClientNodeModuleFileInstrumentation(basePath: string) {
private _getClientNodeModuleFileInstrumentation(basePath: string): InstrumentationNodeModuleFile {
return new InstrumentationNodeModuleFile(
`knex/${basePath}/client.js`,
constants.SUPPORTED_VERSIONS,
SUPPORTED_VERSIONS,
(Client: any) => {
this.ensureWrapped(Client.prototype, 'queryBuilder', this.storeContext.bind(this));
this.ensureWrapped(Client.prototype, 'schemaBuilder', this.storeContext.bind(this));
this.ensureWrapped(Client.prototype, 'raw', this.storeContext.bind(this));
this._ensureWrapped(Client.prototype, 'queryBuilder', this._storeContext.bind(this));
this._ensureWrapped(Client.prototype, 'schemaBuilder', this._storeContext.bind(this));
this._ensureWrapped(Client.prototype, 'raw', this._storeContext.bind(this));
return Client;
},
(Client: any) => {
Expand All@@ -112,9 +108,7 @@ export class KnexInstrumentation extends InstrumentationBase<KnexInstrumentation
);
}

private createQueryWrapper(moduleVersion?: string) {
const instrumentation = this;

private _createQueryWrapper(moduleVersion?: string) {
return function wrapQuery(original: (...args: any[]) => any) {
return function wrapped_logging_method(this: any, query: any) {
const config = this.client.config;
Expand All@@ -126,93 +120,62 @@ export class KnexInstrumentation extends InstrumentationBase<KnexInstrumentation
config?.connection?.filename ||
config?.connection?.database ||
utils.extractDatabaseFromConnectionString(connectionString);
const { maxQueryLength } = instrumentation.getConfig();

const attributes: api.Attributes = {
const attributes: SpanAttributes = {
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: ORIGIN,
'knex.version': moduleVersion,
[ATTR_DB_SYSTEM]: utils.mapSystem(this.client.driverName),
[ATTR_DB_SQL_TABLE]: table,
[ATTR_DB_OPERATION]: operation,
[ATTR_DB_USER]: config?.connection?.user,
[ATTR_DB_NAME]: name,
[ATTR_NET_PEER_NAME]: config?.connection?.host ?? utils.extractHostFromConnectionString(connectionString),
[ATTR_NET_PEER_PORT]: config?.connection?.port ?? utils.extractPortFromConnectionString(connectionString),
[ATTR_NET_TRANSPORT]: config?.connection?.filename === ':memory:' ? 'inproc' : undefined,
[ATTR_DB_STATEMENT]: utils.limitLength(query?.sql, MAX_QUERY_LENGTH),
};
const transport = config?.connection?.filename === ':memory:' ? 'inproc' : undefined;

if (instrumentation._semconvStability & SemconvStability.OLD) {
Object.assign(attributes, {
[ATTR_DB_SYSTEM]: utils.mapSystem(this.client.driverName),
[ATTR_DB_SQL_TABLE]: table,
[ATTR_DB_OPERATION]: operation,
[ATTR_DB_USER]: config?.connection?.user,
[ATTR_DB_NAME]: name,
[ATTR_NET_PEER_NAME]: config?.connection?.host ?? utils.extractHostFromConnectionString(connectionString),
[ATTR_NET_PEER_PORT]: config?.connection?.port ?? utils.extractPortFromConnectionString(connectionString),
[ATTR_NET_TRANSPORT]: transport,
});
}
if (instrumentation._semconvStability & SemconvStability.STABLE) {
Object.assign(attributes, {
[ATTR_DB_SYSTEM_NAME]: utils.mapSystem(this.client.driverName),
[ATTR_DB_COLLECTION_NAME]: table,
[ATTR_DB_OPERATION_NAME]: operation,
[ATTR_DB_NAMESPACE]: name,
[ATTR_SERVER_ADDRESS]: config?.connection?.host ?? utils.extractHostFromConnectionString(connectionString),
[ATTR_SERVER_PORT]: config?.connection?.port ?? utils.extractPortFromConnectionString(connectionString),
});
}
if (maxQueryLength) {
const queryText = utils.limitLength(query?.sql, maxQueryLength);
if (instrumentation._semconvStability & SemconvStability.STABLE) {
attributes[ATTR_DB_QUERY_TEXT] = queryText;
}
if (instrumentation._semconvStability & SemconvStability.OLD) {
attributes[ATTR_DB_STATEMENT] = queryText;
}
}

const parentContext = this.builder[contextSymbol] || api.context.active();
const parentSpan = api.trace.getSpan(parentContext);
const hasActiveParent = parentSpan && api.trace.isSpanContextValid(parentSpan.spanContext());
if (instrumentation._config.requireParentSpan && !hasActiveParent) {
return original.bind(this)(...arguments);
}

const span = instrumentation.tracer.startSpan(
utils.getName(name, operation, table),

// The query builder captures the span active when it was created (see `_storeContext`).
// `onlyIfParent` ensures we only instrument queries that run as part of an existing trace.
const parentSpan: Span | undefined = this.builder[parentSpanSymbol] || getActiveSpan();

const args = arguments;
return startSpan(
{
kind: api.SpanKind.CLIENT,
name: utils.getName(name, operation, table),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Span description no longer SQL

Medium Severity

Knex spans now use utils.getName as the span name, which becomes the exported description. The prior OTEL exporter derived descriptions from db.statement, so UI and tests expecting full SQL (for example select * from "User") no longer match.

Fix in CursorFix in Web

Reviewed by Cursor Bugbot for commit 216196d. Configure here.

kind: SpanKind.CLIENT,
attributes,
parentSpan,
onlyIfParent: true,
},
Comment thread
sentry[bot] marked this conversation as resolved.
parentContext,
span =>
// `Runner.query` returns a real, already-executing Promise, so it is safe to let
// `startSpan` await it and auto-end the span.
original.apply(this, args).catch((err: any) => {
const formatter = utils.getFormatter(this);
const fullQuery = formatter(query.sql, query.bindings || []);
const message = err.message.replace(`${fullQuery} - `, '');
span.setStatus({ code: SPAN_STATUS_ERROR, message });
throw err;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error status not normalized

Medium Severity

Failed queries set the span status message to the trimmed Knex/Postgres error text. OTEL export previously mapped non-canonical error messages to internal_error, but native spans keep the raw message, diverging from tests and prior telemetry.

Fix in CursorFix in Web

Reviewed by Cursor Bugbot for commit 216196d. Configure here.

}),
);
const spanContext = api.trace.setSpan(api.context.active(), span);

return api.context
.with(spanContext, original, this, ...arguments)
.then((result: unknown) => {
span.end();
return result;
})
.catch((err: any) => {
const formatter = utils.getFormatter(this);
const fullQuery = formatter(query.sql, query.bindings || []);
const message = err.message.replace(fullQuery + ' - ', '');
const exc = utils.otelExceptionFromKnexError(err, message);
span.recordException(exc);
span.setStatus({ code: api.SpanStatusCode.ERROR, message });
span.end();
throw err;
});
};
};
}

private storeContext(original: Function) {
private _storeContext(original: (...args: any[]) => any) {
return function wrapped_logging_method(this: any) {
const builder = original.apply(this, arguments);
Object.defineProperty(builder, contextSymbol, {
value: api.context.active(),
// Capture the span that is active when the query builder is created. The query often executes
// in a different async context, so we reuse this span as the parent when the query runs.
Object.defineProperty(builder, parentSpanSymbol, {
value: getActiveSpan(),
});
return builder;
};
}

ensureWrapped(obj: any, methodName: string, wrapper: (original: any) => any) {
private _ensureWrapped(obj: any, methodName: string, wrapper: (original: any) => any): void {
if (isWrapped(obj[methodName])) {
this._unwrap(obj, methodName);
}
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
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
Original file line numberDiff line numberDiff line change
Expand Up@@ -29,6 +29,11 @@ async function run() {

await pgClient('User').insert({ name: 'bob', email: 'bob@domain.com' });
await pgClient('User').select('*');

// Trigger a failing query to capture the error span (table does not exist).
await pgClient('DoesNotExist')
.select('*')
.catch(() => {});
} finally {
await pgClient.destroy();
}
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -57,6 +57,22 @@ describe('knex auto instrumentation', () => {
description: 'select * from "User"',
origin: 'auto.db.otel.knex',
}),

expect.objectContaining({
data: expect.objectContaining({
'knex.version': KNEX_VERSION,
'db.operation': 'select',
'db.sql.table': 'DoesNotExist',
'db.system': 'postgresql',
'db.name': 'tests',
'db.statement': 'select * from "DoesNotExist"',
'sentry.origin': 'auto.db.otel.knex',
'sentry.op': 'db',
}),
status: 'internal_error',
Comment thread
logaretm marked this conversation as resolved.
description: 'select * from "DoesNotExist"',
origin: 'auto.db.otel.knex',
}),
]),
};

Expand Down
27 changes: 4 additions & 23 deletions packages/node/src/integrations/tracing/knex/index.ts
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,17 @@
import { KnexInstrumentation } from './vendored/instrumentation';
import type { IntegrationFn } from '@sentry/core';
import { defineIntegration, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, spanToJSON } from '@sentry/core';
import { generateInstrumentOnce, instrumentWhenWrapped } from '@sentry/node-core';
import { defineIntegration } from '@sentry/core';
import { generateInstrumentOnce } from '@sentry/node-core';

const INTEGRATION_NAME = 'Knex';

export const instrumentKnex = generateInstrumentOnce(
INTEGRATION_NAME,
() => new KnexInstrumentation({ requireParentSpan: true }),
);
export const instrumentKnex = generateInstrumentOnce(INTEGRATION_NAME, () => new KnexInstrumentation());

const _knexIntegration = (() => {
let instrumentationWrappedCallback: undefined | ((callback: () => void) => void);

return {
name: INTEGRATION_NAME,
setupOnce() {
const instrumentation = instrumentKnex();
instrumentationWrappedCallback = instrumentWhenWrapped(instrumentation);
},

setup(client) {
instrumentationWrappedCallback?.(() =>
client.on('spanStart', span => {
const { data } = spanToJSON(span);
// knex.version is always set in the span data
// https://github.com/open-telemetry/opentelemetry-js-contrib/blob/0309caeafc44ac9cb13a3345b790b01b76d0497d/plugins/node/opentelemetry-instrumentation-knex/src/instrumentation.ts#L138
if ('knex.version' in data) {
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, 'auto.db.otel.knex');
}
}),
);
instrumentKnex();
},
};
}) satisfies IntegrationFn;
Expand Down

This file was deleted.

Original file line numberDiff line numberDiff line change
Expand Up@@ -6,31 +6,23 @@
* - Vendored from: https://github.com/open-telemetry/opentelemetry-js-contrib/tree/15ef7506553f631ea4181391e0c5725a56f0d082/packages/instrumentation-knex
* - Upstream version: @opentelemetry/instrumentation-knex@0.62.0
* - Minor TypeScript strictness adjustments for this repository's compiler settings
* - Refactored to use Sentry's span APIs instead of OpenTelemetry tracing APIs
*/
/* eslint-disable */

import * as api from '@opentelemetry/api';
import { SDK_VERSION } from '@sentry/core';
import * as constants from './constants';
/* oxlint-disable typescript/no-deprecated */

import { SpanKind } from '@opentelemetry/api';
import type { InstrumentationConfig } from '@opentelemetry/instrumentation';
import { InstrumentationBase, InstrumentationNodeModuleDefinition, isWrapped } from '@opentelemetry/instrumentation';
import type { Span, SpanAttributes } from '@sentry/core';
import {
InstrumentationBase,
InstrumentationNodeModuleDefinition,
isWrapped,
SemconvStability,
semconvStabilityFromStr,
} from '@opentelemetry/instrumentation';
getActiveSpan,
SDK_VERSION,
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
SPAN_STATUS_ERROR,
startSpan,
} from '@sentry/core';
import { InstrumentationNodeModuleFile } from '../../InstrumentationNodeModuleFile';
import * as utils from './utils';
import { KnexInstrumentationConfig } from './types';
import {
ATTR_DB_COLLECTION_NAME,
ATTR_DB_NAMESPACE,
ATTR_DB_OPERATION_NAME,
ATTR_DB_QUERY_TEXT,
ATTR_DB_SYSTEM_NAME,
ATTR_SERVER_ADDRESS,
ATTR_SERVER_PORT,
} from '@opentelemetry/semantic-conventions';
import {
ATTR_DB_NAME,
ATTR_DB_OPERATION,
Expand All@@ -42,65 +34,69 @@ import {
ATTR_NET_PEER_PORT,
ATTR_NET_TRANSPORT,
} from './semconv';
import * as utils from './utils';

const PACKAGE_NAME = '@sentry/instrumentation-knex';

const contextSymbol = Symbol('opentelemetry.instrumentation-knex.context');
const DEFAULT_CONFIG: KnexInstrumentationConfig = {
maxQueryLength: 1022,
requireParentSpan: false,
};

export class KnexInstrumentation extends InstrumentationBase<KnexInstrumentationConfig> {
private _semconvStability: SemconvStability;

constructor(config: KnexInstrumentationConfig = {}) {
super(PACKAGE_NAME, SDK_VERSION, { ...DEFAULT_CONFIG, ...config });

this._semconvStability = semconvStabilityFromStr('database', process.env.OTEL_SEMCONV_STABILITY_OPT_IN);
}

override setConfig(config: KnexInstrumentationConfig = {}) {
super.setConfig({ ...DEFAULT_CONFIG, ...config });
const ORIGIN = 'auto.db.otel.knex';

const MODULE_NAME = 'knex';
const SUPPORTED_VERSIONS = [
// use "lib/execution" for runner.js, "lib" for client.js as basepath, latest tested 0.95.6
'>=0.22.0 <4',
// use "lib" as basepath
'>=0.10.0 <0.18.0',
'>=0.19.0 <0.22.0',
// use "src" as basepath
'>=0.18.0 <0.19.0',
];

// Max length of the query text captured in the `db.statement` attribute; ".." is appended when truncated.
const MAX_QUERY_LENGTH = 1022;

const parentSpanSymbol = Symbol('sentry.instrumentation-knex.parent-span');

export class KnexInstrumentation extends InstrumentationBase<InstrumentationConfig> {
public constructor(config: InstrumentationConfig = {}) {
super(PACKAGE_NAME, SDK_VERSION, config);
}

init() {
const module = new InstrumentationNodeModuleDefinition(constants.MODULE_NAME, constants.SUPPORTED_VERSIONS);
public init(): InstrumentationNodeModuleDefinition {
const module = new InstrumentationNodeModuleDefinition(MODULE_NAME, SUPPORTED_VERSIONS);

module.files.push(
this.getClientNodeModuleFileInstrumentation('src'),
this.getClientNodeModuleFileInstrumentation('lib'),
this.getRunnerNodeModuleFileInstrumentation('src'),
this.getRunnerNodeModuleFileInstrumentation('lib'),
this.getRunnerNodeModuleFileInstrumentation('lib/execution'),
this._getClientNodeModuleFileInstrumentation('src'),
this._getClientNodeModuleFileInstrumentation('lib'),
this._getRunnerNodeModuleFileInstrumentation('src'),
this._getRunnerNodeModuleFileInstrumentation('lib'),
this._getRunnerNodeModuleFileInstrumentation('lib/execution'),
);

return module;
}

private getRunnerNodeModuleFileInstrumentation(basePath: string) {
private _getRunnerNodeModuleFileInstrumentation(basePath: string): InstrumentationNodeModuleFile {
return new InstrumentationNodeModuleFile(
`knex/${basePath}/runner.js`,
constants.SUPPORTED_VERSIONS,
SUPPORTED_VERSIONS,
(Runner: any, moduleVersion?: string) => {
this.ensureWrapped(Runner.prototype, 'query', this.createQueryWrapper(moduleVersion));
this._ensureWrapped(Runner.prototype, 'query', this._createQueryWrapper(moduleVersion));
return Runner;
},
(Runner: any, _moduleVersion?: string) => {
(Runner: any) => {
this._unwrap(Runner.prototype, 'query');
return Runner;
},
);
}

private getClientNodeModuleFileInstrumentation(basePath: string) {
private _getClientNodeModuleFileInstrumentation(basePath: string): InstrumentationNodeModuleFile {
return new InstrumentationNodeModuleFile(
`knex/${basePath}/client.js`,
constants.SUPPORTED_VERSIONS,
SUPPORTED_VERSIONS,
(Client: any) => {
this.ensureWrapped(Client.prototype, 'queryBuilder', this.storeContext.bind(this));
this.ensureWrapped(Client.prototype, 'schemaBuilder', this.storeContext.bind(this));
this.ensureWrapped(Client.prototype, 'raw', this.storeContext.bind(this));
this._ensureWrapped(Client.prototype, 'queryBuilder', this._storeContext.bind(this));
this._ensureWrapped(Client.prototype, 'schemaBuilder', this._storeContext.bind(this));
this._ensureWrapped(Client.prototype, 'raw', this._storeContext.bind(this));
return Client;
},
(Client: any) => {
Expand All@@ -112,9 +108,7 @@ export class KnexInstrumentation extends InstrumentationBase<KnexInstrumentation
);
}

private createQueryWrapper(moduleVersion?: string) {
const instrumentation = this;

private _createQueryWrapper(moduleVersion?: string) {
return function wrapQuery(original: (...args: any[]) => any) {
return function wrapped_logging_method(this: any, query: any) {
const config = this.client.config;
Expand All@@ -126,93 +120,62 @@ export class KnexInstrumentation extends InstrumentationBase<KnexInstrumentation
config?.connection?.filename ||
config?.connection?.database ||
utils.extractDatabaseFromConnectionString(connectionString);
const { maxQueryLength } = instrumentation.getConfig();

const attributes: api.Attributes = {
const attributes: SpanAttributes = {
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: ORIGIN,
'knex.version': moduleVersion,
[ATTR_DB_SYSTEM]: utils.mapSystem(this.client.driverName),
[ATTR_DB_SQL_TABLE]: table,
[ATTR_DB_OPERATION]: operation,
[ATTR_DB_USER]: config?.connection?.user,
[ATTR_DB_NAME]: name,
[ATTR_NET_PEER_NAME]: config?.connection?.host ?? utils.extractHostFromConnectionString(connectionString),
[ATTR_NET_PEER_PORT]: config?.connection?.port ?? utils.extractPortFromConnectionString(connectionString),
[ATTR_NET_TRANSPORT]: config?.connection?.filename === ':memory:' ? 'inproc' : undefined,
[ATTR_DB_STATEMENT]: utils.limitLength(query?.sql, MAX_QUERY_LENGTH),
};
const transport = config?.connection?.filename === ':memory:' ? 'inproc' : undefined;

if (instrumentation._semconvStability & SemconvStability.OLD) {
Object.assign(attributes, {
[ATTR_DB_SYSTEM]: utils.mapSystem(this.client.driverName),
[ATTR_DB_SQL_TABLE]: table,
[ATTR_DB_OPERATION]: operation,
[ATTR_DB_USER]: config?.connection?.user,
[ATTR_DB_NAME]: name,
[ATTR_NET_PEER_NAME]: config?.connection?.host ?? utils.extractHostFromConnectionString(connectionString),
[ATTR_NET_PEER_PORT]: config?.connection?.port ?? utils.extractPortFromConnectionString(connectionString),
[ATTR_NET_TRANSPORT]: transport,
});
}
if (instrumentation._semconvStability & SemconvStability.STABLE) {
Object.assign(attributes, {
[ATTR_DB_SYSTEM_NAME]: utils.mapSystem(this.client.driverName),
[ATTR_DB_COLLECTION_NAME]: table,
[ATTR_DB_OPERATION_NAME]: operation,
[ATTR_DB_NAMESPACE]: name,
[ATTR_SERVER_ADDRESS]: config?.connection?.host ?? utils.extractHostFromConnectionString(connectionString),
[ATTR_SERVER_PORT]: config?.connection?.port ?? utils.extractPortFromConnectionString(connectionString),
});
}
if (maxQueryLength) {
const queryText = utils.limitLength(query?.sql, maxQueryLength);
if (instrumentation._semconvStability & SemconvStability.STABLE) {
attributes[ATTR_DB_QUERY_TEXT] = queryText;
}
if (instrumentation._semconvStability & SemconvStability.OLD) {
attributes[ATTR_DB_STATEMENT] = queryText;
}
}

const parentContext = this.builder[contextSymbol] || api.context.active();
const parentSpan = api.trace.getSpan(parentContext);
const hasActiveParent = parentSpan && api.trace.isSpanContextValid(parentSpan.spanContext());
if (instrumentation._config.requireParentSpan && !hasActiveParent) {
return original.bind(this)(...arguments);
}

const span = instrumentation.tracer.startSpan(
utils.getName(name, operation, table),

// The query builder captures the span active when it was created (see `_storeContext`).
// `onlyIfParent` ensures we only instrument queries that run as part of an existing trace.
const parentSpan: Span | undefined = this.builder[parentSpanSymbol] || getActiveSpan();

const args = arguments;
return startSpan(
{
kind: api.SpanKind.CLIENT,
name: utils.getName(name, operation, table),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Span description no longer SQL

Medium Severity

Knex spans now use utils.getName as the span name, which becomes the exported description. The prior OTEL exporter derived descriptions from db.statement, so UI and tests expecting full SQL (for example select * from "User") no longer match.

Fix in CursorFix in Web

Reviewed by Cursor Bugbot for commit 216196d. Configure here.

kind: SpanKind.CLIENT,
attributes,
parentSpan,
onlyIfParent: true,
},
Comment thread
sentry[bot] marked this conversation as resolved.
parentContext,
span =>
// `Runner.query` returns a real, already-executing Promise, so it is safe to let
// `startSpan` await it and auto-end the span.
original.apply(this, args).catch((err: any) => {
const formatter = utils.getFormatter(this);
const fullQuery = formatter(query.sql, query.bindings || []);
const message = err.message.replace(`${fullQuery} - `, '');
span.setStatus({ code: SPAN_STATUS_ERROR, message });
throw err;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error status not normalized

Medium Severity

Failed queries set the span status message to the trimmed Knex/Postgres error text. OTEL export previously mapped non-canonical error messages to internal_error, but native spans keep the raw message, diverging from tests and prior telemetry.

Fix in CursorFix in Web

Reviewed by Cursor Bugbot for commit 216196d. Configure here.

}),
);
const spanContext = api.trace.setSpan(api.context.active(), span);

return api.context
.with(spanContext, original, this, ...arguments)
.then((result: unknown) => {
span.end();
return result;
})
.catch((err: any) => {
const formatter = utils.getFormatter(this);
const fullQuery = formatter(query.sql, query.bindings || []);
const message = err.message.replace(fullQuery + ' - ', '');
const exc = utils.otelExceptionFromKnexError(err, message);
span.recordException(exc);
span.setStatus({ code: api.SpanStatusCode.ERROR, message });
span.end();
throw err;
});
};
};
}

private storeContext(original: Function) {
private _storeContext(original: (...args: any[]) => any) {
return function wrapped_logging_method(this: any) {
const builder = original.apply(this, arguments);
Object.defineProperty(builder, contextSymbol, {
value: api.context.active(),
// Capture the span that is active when the query builder is created. The query often executes
// in a different async context, so we reuse this span as the parent when the query runs.
Object.defineProperty(builder, parentSpanSymbol, {
value: getActiveSpan(),
});
return builder;
};
}

ensureWrapped(obj: any, methodName: string, wrapper: (original: any) => any) {
private _ensureWrapped(obj: any, methodName: string, wrapper: (original: any) => any): void {
if (isWrapped(obj[methodName])) {
this._unwrap(obj, methodName);
}
Expand Down
Loading
Loading