-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
ref(node): Streamline knex instrumentation #21561
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,31 +17,21 @@ | |
| * - 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'; | ||
| 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, | ||
|
|
@@ -53,65 +43,69 @@ | |
| 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) => { | ||
|
|
@@ -123,9 +117,7 @@ | |
| ); | ||
| } | ||
|
|
||
| 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; | ||
|
|
@@ -137,93 +129,62 @@ | |
| 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), | ||
| kind: SpanKind.CLIENT, | ||
| attributes, | ||
| parentSpan, | ||
| onlyIfParent: true, | ||
| }, | ||
|
Comment on lines
+149
to
159
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Passing an ended span as Suggested FixBefore calling Prompt for AI AgentDid we get this right? 👍 / 👎 to inform future reviews. |
||
| 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; | ||
| }), | ||
| ); | ||
| 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); | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.