Skip to content

Commit

Permalink
chore(instrumentation-knex): Simplify exception parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
deejay1 committed Jan 16, 2025
1 parent 6dd3912 commit 107bff8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ export class KnexInstrumentation extends InstrumentationBase<KnexInstrumentation
const formatter = utils.getFormatter(this);
const fullQuery = formatter(query.sql, query.bindings || []);
const message = err.message.replace(fullQuery + ' - ', '');
const clonedError = utils.cloneErrorWithNewMessage(err, message);
span.recordException(clonedError);
const exc = utils.otelExceptionFromKnexError(err, message);
span.recordException(exc);
span.setStatus({ code: api.SpanStatusCode.ERROR, message });
span.end();
throw err;
Expand Down
32 changes: 17 additions & 15 deletions plugins/node/opentelemetry-instrumentation-knex/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,14 @@
* limitations under the License.
*/

import { Exception } from '@opentelemetry/api';
import {
DBSYSTEMVALUES_SQLITE,
DBSYSTEMVALUES_POSTGRESQL,
} from '@opentelemetry/semantic-conventions';

type Exception = {
new (message: string): Exception;
constructor: Exception;
errno?: number;
type KnexError = Error & {
code?: string;
stack?: string;
};

export const getFormatter = (runner: any) => {
Expand All @@ -43,22 +40,27 @@ export const getFormatter = (runner: any) => {
return () => '<noop formatter>';
};

export const cloneErrorWithNewMessage = (err: Exception, message: string) => {
if (err && err instanceof Error) {
const clonedError = Object.assign({}, err);
clonedError.message = message;
clonedError.code = err.code;
clonedError.stack = err.stack;
clonedError.errno = err.errno;
return clonedError;
export function otelExceptionFromKnexError(
err: KnexError,
message: string
): Exception {
if (!(err && err instanceof Error)) {
return err;
}
return err;
};

return {
message,
code: err.code,
stack: err.stack,
name: err.name,
};
}

const systemMap = new Map([
['sqlite3', DBSYSTEMVALUES_SQLITE],
['pg', DBSYSTEMVALUES_POSTGRESQL],
]);

export const mapSystem = (knexSystem: string) => {
return systemMap.get(knexSystem) || knexSystem;
};
Expand Down

0 comments on commit 107bff8

Please sign in to comment.