Skip to content

Commit 107bff8

Browse files
committed
chore(instrumentation-knex): Simplify exception parsing
1 parent 6dd3912 commit 107bff8

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

plugins/node/opentelemetry-instrumentation-knex/src/instrumentation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,8 @@ export class KnexInstrumentation extends InstrumentationBase<KnexInstrumentation
185185
const formatter = utils.getFormatter(this);
186186
const fullQuery = formatter(query.sql, query.bindings || []);
187187
const message = err.message.replace(fullQuery + ' - ', '');
188-
const clonedError = utils.cloneErrorWithNewMessage(err, message);
189-
span.recordException(clonedError);
188+
const exc = utils.otelExceptionFromKnexError(err, message);
189+
span.recordException(exc);
190190
span.setStatus({ code: api.SpanStatusCode.ERROR, message });
191191
span.end();
192192
throw err;

plugins/node/opentelemetry-instrumentation-knex/src/utils.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,14 @@
1414
* limitations under the License.
1515
*/
1616

17+
import { Exception } from '@opentelemetry/api';
1718
import {
1819
DBSYSTEMVALUES_SQLITE,
1920
DBSYSTEMVALUES_POSTGRESQL,
2021
} from '@opentelemetry/semantic-conventions';
2122

22-
type Exception = {
23-
new (message: string): Exception;
24-
constructor: Exception;
25-
errno?: number;
23+
type KnexError = Error & {
2624
code?: string;
27-
stack?: string;
2825
};
2926

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

46-
export const cloneErrorWithNewMessage = (err: Exception, message: string) => {
47-
if (err && err instanceof Error) {
48-
const clonedError = Object.assign({}, err);
49-
clonedError.message = message;
50-
clonedError.code = err.code;
51-
clonedError.stack = err.stack;
52-
clonedError.errno = err.errno;
53-
return clonedError;
43+
export function otelExceptionFromKnexError(
44+
err: KnexError,
45+
message: string
46+
): Exception {
47+
if (!(err && err instanceof Error)) {
48+
return err;
5449
}
55-
return err;
56-
};
50+
51+
return {
52+
message,
53+
code: err.code,
54+
stack: err.stack,
55+
name: err.name,
56+
};
57+
}
5758

5859
const systemMap = new Map([
5960
['sqlite3', DBSYSTEMVALUES_SQLITE],
6061
['pg', DBSYSTEMVALUES_POSTGRESQL],
6162
]);
63+
6264
export const mapSystem = (knexSystem: string) => {
6365
return systemMap.get(knexSystem) || knexSystem;
6466
};

0 commit comments

Comments
 (0)