Skip to content

Commit

Permalink
Merge pull request open-telemetry#200 from opentelemetrybot/opentelem…
Browse files Browse the repository at this point in the history
…etrybot/merge-main

[AutoMerge][Main] Merging staged change(s) to main
  • Loading branch information
MSNev committed Oct 24, 2023
2 parents 97f33e1 + ceb8c08 commit d8fab86
Show file tree
Hide file tree
Showing 26 changed files with 400 additions and 221 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ export class IORedisInstrumentation extends InstrumentationBase<any> {
new InstrumentationNodeModuleDefinition<any>(
'ioredis',
['>1', '<6'],
(moduleExports, moduleVersion?: string) => {
(module, moduleVersion?: string) => {
const moduleExports =
module[Symbol.toStringTag] === 'Module'
? module.default // ESM
: module; // CommonJS
diag.debug('Applying patch for ioredis');
if (isWrapped(moduleExports.prototype.sendCommand)) {
this._unwrap(moduleExports.prototype, 'sendCommand');
Expand All @@ -67,10 +71,14 @@ export class IORedisInstrumentation extends InstrumentationBase<any> {
'connect',
this._patchConnection()
);
return moduleExports;
return module;
},
moduleExports => {
if (moduleExports === undefined) return;
module => {
if (module === undefined) return;
const moduleExports =
module[Symbol.toStringTag] === 'Module'
? module.default // ESM
: module; // CommonJS
diag.debug('Removing patch for ioredis');
this._unwrap(moduleExports.prototype, 'sendCommand');
this._unwrap(moduleExports.prototype, 'connect');
Expand All @@ -84,17 +92,17 @@ export class IORedisInstrumentation extends InstrumentationBase<any> {
*/
private _patchSendCommand(moduleVersion?: string) {
return (original: Function) => {
return this.traceSendCommand(original, moduleVersion);
return this._traceSendCommand(original, moduleVersion);
};
}

private _patchConnection() {
return (original: Function) => {
return this.traceConnection(original);
return this._traceConnection(original);
};
}

private traceSendCommand = (original: Function, moduleVersion?: string) => {
private _traceSendCommand(original: Function, moduleVersion?: string) {
const instrumentation = this;
return function (this: RedisInterface, cmd?: IORedisCommand) {
if (arguments.length < 1 || typeof cmd !== 'object') {
Expand Down Expand Up @@ -178,9 +186,9 @@ export class IORedisInstrumentation extends InstrumentationBase<any> {
throw error;
}
};
};
}

private traceConnection = (original: Function) => {
private _traceConnection(original: Function) {
const instrumentation = this;
return function (this: RedisInterface) {
const config =
Expand Down Expand Up @@ -213,5 +221,5 @@ export class IORedisInstrumentation extends InstrumentationBase<any> {
throw error;
}
};
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { AsyncResource } from 'async_hooks';

import {
context,
Expand Down Expand Up @@ -42,6 +43,7 @@ import {
MongoInternalTopology,
WireProtocolInternal,
V4Connection,
V4ConnectionPool,
} from './internal-types';
import { V4Connect, V4Session } from './internal-types';
import { VERSION } from './version';
Expand Down Expand Up @@ -76,6 +78,8 @@ export class MongoDBInstrumentation extends InstrumentationBase {
const { v4PatchConnect, v4UnpatchConnect } = this._getV4ConnectPatches();
const { v4PatchConnection, v4UnpatchConnection } =
this._getV4ConnectionPatches();
const { v4PatchConnectionPool, v4UnpatchConnectionPool } =
this._getV4ConnectionPoolPatches();
const { v4PatchSessions, v4UnpatchSessions } = this._getV4SessionsPatches();

return [
Expand Down Expand Up @@ -105,6 +109,12 @@ export class MongoDBInstrumentation extends InstrumentationBase {
v4PatchConnection,
v4UnpatchConnection
),
new InstrumentationNodeModuleFile<V4ConnectionPool>(
'mongodb/lib/cmap/connection_pool.js',
['4.*', '5.*'],
v4PatchConnectionPool,
v4UnpatchConnectionPool
),
new InstrumentationNodeModuleFile<V4Connect>(
'mongodb/lib/cmap/connect.js',
['4.*', '5.*'],
Expand Down Expand Up @@ -268,6 +278,35 @@ export class MongoDBInstrumentation extends InstrumentationBase {
};
}

private _getV4ConnectionPoolPatches<T extends V4ConnectionPool>() {
return {
v4PatchConnectionPool: (moduleExports: any, moduleVersion?: string) => {
diag.debug(`Applying patch for mongodb@${moduleVersion}`);
const poolPrototype = moduleExports.ConnectionPool.prototype;

if (isWrapped(poolPrototype.checkOut)) {
this._unwrap(poolPrototype, 'checkOut');
}

this._wrap(
poolPrototype,
'checkOut',
this._getV4ConnectionPoolCheckOut()
);
return moduleExports;
},
v4UnpatchConnectionPool: (
moduleExports?: any,
moduleVersion?: string
) => {
diag.debug(`Removing internal patch for mongodb@${moduleVersion}`);
if (moduleExports === undefined) return;

this._unwrap(moduleExports.ConnectionPool.prototype, 'checkOut');
},
};
}

private _getV4ConnectPatches<T extends V4Connect>() {
return {
v4PatchConnect: (moduleExports: any, moduleVersion?: string) => {
Expand All @@ -288,6 +327,17 @@ export class MongoDBInstrumentation extends InstrumentationBase {
};
}

// This patch will become unnecessary once
// https://jira.mongodb.org/browse/NODE-5639 is done.
private _getV4ConnectionPoolCheckOut() {
return (original: V4ConnectionPool['checkOut']) => {
return function patchedCheckout(this: unknown, callback: any) {
const patchedCallback = AsyncResource.bind(callback);
return original.call(this, patchedCallback);
};
};
}

private _getV4ConnectCommand() {
const instrumentation = this;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,13 @@ export type V4Connection = {
): void;
};

// https://github.com/mongodb/node-mongodb-native/blob/v4.2.2/src/cmap/connection_pool.ts
export type V4ConnectionPool = {
// Instrumentation just cares about carrying the async context so
// types of callback params are not needed
checkOut: (callback: (error: any, connection: any) => void) => void;
};

// https://github.com/mongodb/node-mongodb-native/blob/v4.2.2/src/cmap/connect.ts
export type V4Connect = {
connect: (options: any, callback: any) => void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,44 @@ describe('MongoDBInstrumentation-Tracing-v4', () => {
});
});
});

it('should create child spans for concurrent cursor operations', done => {
const queries = [{ a: 1 }, { a: 2 }, { a: 3 }];
const tasks = queries.map((query, idx) => {
return new Promise((resolve, reject) => {
process.nextTick(() => {
const span = trace
.getTracer('default')
.startSpan(`findRootSpan ${idx}`);
context.with(trace.setSpan(context.active(), span), () => {
collection
.find(query)
.toArray()
.then(() => {
resolve(span.end());
})
.catch(reject);
});
});
});
});

Promise.all(tasks)
.then(() => {
const spans = getTestSpans();
const roots = spans.filter(s => s.name.startsWith('findRootSpan'));

roots.forEach(root => {
const rootId = root.spanContext().spanId;
const children = spans.filter(s => s.parentSpanId === rootId);
assert.strictEqual(children.length, 1);
});
done();
})
.catch(err => {
done(err);
});
});
});

/** Should intercept command */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,44 @@ describe('MongoDBInstrumentation-Tracing-v5', () => {
});
});
});

it('should create child spans for concurrent cursor operations', done => {
const queries = [{ a: 1 }, { a: 2 }, { a: 3 }];
const tasks = queries.map((query, idx) => {
return new Promise((resolve, reject) => {
process.nextTick(() => {
const span = trace
.getTracer('default')
.startSpan(`findRootSpan ${idx}`);
context.with(trace.setSpan(context.active(), span), () => {
collection
.find(query)
.toArray()
.then(() => {
resolve(span.end());
})
.catch(reject);
});
});
});
});

Promise.all(tasks)
.then(() => {
const spans = getTestSpans();
const roots = spans.filter(s => s.name.startsWith('findRootSpan'));

roots.forEach(root => {
const rootId = root.spanContext().spanId;
const children = spans.filter(s => s.parentSpanId === rootId);
assert.strictEqual(children.length, 1);
});
done();
})
.catch(err => {
done(err);
});
});
});

/** Should intercept command */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ export class PgInstrumentation extends InstrumentationBase {
const modulePG = new InstrumentationNodeModuleDefinition<typeof pgTypes>(
'pg',
['8.*'],
moduleExports => {
(module: any) => {
const moduleExports: typeof pgTypes =
module[Symbol.toStringTag] === 'Module'
? module.default // ESM
: module; // CommonJS
if (isWrapped(moduleExports.Client.prototype.query)) {
this._unwrap(moduleExports.Client.prototype, 'query');
}
Expand All @@ -87,9 +91,13 @@ export class PgInstrumentation extends InstrumentationBase {
this._getClientConnectPatch() as any
);

return moduleExports;
return module;
},
moduleExports => {
(module: any) => {
const moduleExports: typeof pgTypes =
module[Symbol.toStringTag] === 'Module'
? module.default // ESM
: module; // CommonJS
if (isWrapped(moduleExports.Client.prototype.query)) {
this._unwrap(moduleExports.Client.prototype, 'query');
}
Expand Down
2 changes: 1 addition & 1 deletion auto-merge/js/.github/workflows/docs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:

- uses: actions/setup-node@v3
with:
node-version: '14'
node-version: '18'

- name: restore lock files
uses: actions/cache@master # must use unreleased master to cache multiple paths
Expand Down
4 changes: 4 additions & 0 deletions auto-merge/js/experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ All notable changes to experimental packages in this project will be documented

### :bug: (Bug Fix)

* fix(sdk-node): remove the explicit dependency on @opentelemetry/exporter-jaeger that was kept on the previous release
* '@opentelemetry/exporter-jaeger' is no longer be a dependency of this package. To continue using '@opentelemetry/exporter-jaeger', please install it manually.
* NOTE: `@opentelemetry/exporter-jaeger` is deprecated, consider switching to one of the alternatives described [here](https://www.npmjs.com/package/@opentelemetry/exporter-jaeger)

### :books: (Refine Doc)

### :house: (Internal)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"@opentelemetry/resources": "1.17.1",
"@types/mocha": "10.0.2",
"@types/node": "18.6.5",
"@types/sinon": "10.0.18",
"@types/sinon": "10.0.19",
"codecov": "3.8.3",
"cpx": "1.5.0",
"cross-var": "1.1.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"@opentelemetry/otlp-exporter-base": "0.44.0",
"@types/mocha": "10.0.2",
"@types/node": "18.6.5",
"@types/sinon": "10.0.18",
"@types/sinon": "10.0.19",
"codecov": "3.8.3",
"cpx": "1.5.0",
"cross-var": "1.1.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"@opentelemetry/api": "1.6.0",
"@types/mocha": "10.0.2",
"@types/node": "18.6.5",
"@types/sinon": "10.0.18",
"@types/sinon": "10.0.19",
"codecov": "3.8.3",
"cpx": "1.5.0",
"cross-var": "1.1.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"@opentelemetry/api": "1.6.0",
"@types/mocha": "10.0.2",
"@types/node": "18.6.5",
"@types/sinon": "10.0.18",
"@types/sinon": "10.0.19",
"codecov": "3.8.3",
"cpx": "1.5.0",
"cross-var": "1.1.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"@opentelemetry/semantic-conventions": "1.17.1",
"@types/mocha": "10.0.2",
"@types/node": "18.6.5",
"@types/sinon": "10.0.18",
"@types/sinon": "10.0.19",
"codecov": "3.8.3",
"cross-var": "1.1.0",
"lerna": "7.1.5",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"@types/mocha": "10.0.2",
"@types/node": "18.6.5",
"@types/semver": "7.5.3",
"@types/sinon": "10.0.18",
"@types/sinon": "10.0.19",
"codecov": "3.8.3",
"cross-var": "1.1.0",
"lerna": "7.1.5",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@
"@types/node": "18.6.5",
"@types/request-promise-native": "1.0.19",
"@types/semver": "7.5.3",
"@types/sinon": "10.0.18",
"@types/sinon": "10.0.19",
"@types/superagent": "4.1.19",
"axios": "1.5.1",
"codecov": "3.8.3",
"cross-var": "1.1.0",
"lerna": "7.1.5",
"mocha": "10.2.0",
"nock": "13.3.3",
"nock": "13.3.4",
"nyc": "15.1.0",
"request": "2.88.2",
"request-promise-native": "1.0.9",
Expand Down
Loading

0 comments on commit d8fab86

Please sign in to comment.