Skip to content

Commit b8c8fa1

Browse files
committed
feat: Add enabled option to all db integrations
1 parent 918644d commit b8c8fa1

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

packages/tracing/src/integrations/mongo.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ interface MongoOptions {
8686
operations?: Operation[];
8787
describeOperations?: boolean | Operation[];
8888
useMongoose?: boolean;
89+
enabled?: boolean;
8990
}
9091

9192
/** Tracing integration for mongo package */
@@ -103,6 +104,7 @@ export class Mongo implements Integration {
103104
private _operations: Operation[];
104105
private _describeOperations?: boolean | Operation[];
105106
private _useMongoose: boolean;
107+
private _enabled: boolean;
106108

107109
/**
108110
* @inheritDoc
@@ -113,6 +115,7 @@ export class Mongo implements Integration {
113115
: ((OPERATIONS as unknown) as Operation[]);
114116
this._describeOperations = 'describeOperations' in options ? options.describeOperations : true;
115117
this._useMongoose = !!options.useMongoose;
118+
this._enabled = options === false ? false : true ;
116119
}
117120

118121
/**
@@ -144,9 +147,14 @@ export class Mongo implements Integration {
144147
if (!(operation in collection.prototype)) return;
145148

146149
const getSpanContext = this._getSpanContextFromOperationArguments.bind(this);
150+
const enabled = this._enabled;
147151

148152
fill(collection.prototype, operation, function(orig: () => void | Promise<unknown>) {
149-
return function(this: unknown, ...args: unknown[]) {
153+
return function (this: unknown, ...args: unknown[]) {
154+
if (!enabled) {
155+
// If the integration is not enabled, do not send spans
156+
return;
157+
}
150158
const lastArg = args[args.length - 1];
151159
const scope = getCurrentHub().getScope();
152160
const parentSpan = scope?.getSpan();

packages/tracing/src/integrations/mysql.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ interface MysqlConnection {
66
createQuery: () => void;
77
}
88

9+
interface MysqlOptions {
10+
enabled?: boolean;
11+
}
12+
913
/** Tracing integration for node-mysql package */
1014
export class Mysql implements Integration {
1115
/**
@@ -18,6 +22,15 @@ export class Mysql implements Integration {
1822
*/
1923
public name: string = Mysql.id;
2024

25+
private _enabled: boolean;
26+
27+
/**
28+
* @inheritDoc
29+
*/
30+
public constructor(options: MysqlOptions = {}) {
31+
this._enabled = options === false ? false : true ;
32+
}
33+
2134
/**
2235
* @inheritDoc
2336
*/
@@ -29,12 +42,18 @@ export class Mysql implements Integration {
2942
return;
3043
}
3144

45+
const enabled = this._enabled;
46+
3247
// The original function will have one of these signatures:
3348
// function (callback) => void
3449
// function (options, callback) => void
3550
// function (options, values, callback) => void
3651
fill(pkg, 'createQuery', function(orig: () => void) {
37-
return function(this: unknown, options: unknown, values: unknown, callback: unknown) {
52+
return function (this: unknown, options: unknown, values: unknown, callback: unknown) {
53+
if (!enabled) {
54+
// If the integration is not enabled, do not send spans
55+
return;
56+
}
3857
const scope = getCurrentHub().getScope();
3958
const parentSpan = scope?.getSpan();
4059
const span = parentSpan?.startChild({

packages/tracing/src/integrations/postgres.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ interface PgClient {
88
};
99
}
1010

11+
interface PostgresOptions {
12+
enabled?: boolean;
13+
}
14+
1115
/** Tracing integration for node-postgres package */
1216
export class Postgres implements Integration {
1317
/**
@@ -20,6 +24,15 @@ export class Postgres implements Integration {
2024
*/
2125
public name: string = Postgres.id;
2226

27+
private _enabled: boolean;
28+
29+
/**
30+
* @inheritDoc
31+
*/
32+
public constructor(options: PostgresOptions = {}) {
33+
this._enabled = options === false ? false : true ;
34+
}
35+
2336
/**
2437
* @inheritDoc
2538
*/
@@ -31,14 +44,20 @@ export class Postgres implements Integration {
3144
return;
3245
}
3346

47+
const enabled = this._enabled;
48+
3449
/**
3550
* function (query, callback) => void
3651
* function (query, params, callback) => void
3752
* function (query) => Promise
3853
* function (query, params) => Promise
3954
*/
40-
fill(pkg.Client.prototype, 'query', function(orig: () => void | Promise<unknown>) {
41-
return function(this: unknown, config: unknown, values: unknown, callback: unknown) {
55+
fill(pkg.Client.prototype, 'query', function (orig: () => void | Promise<unknown>) {
56+
return function (this: unknown, config: unknown, values: unknown, callback: unknown) {
57+
if (!enabled) {
58+
// If the integration is not enabled, do not send spans
59+
return;
60+
}
4261
const scope = getCurrentHub().getScope();
4362
const parentSpan = scope?.getSpan();
4463
const span = parentSpan?.startChild({

0 commit comments

Comments
 (0)