Skip to content

Commit 2f74ac0

Browse files
committed
refactor: export traced channels as self-contained objects
Consumers import { queryChannel } or { cursorNextChannel } and use .trace() or .channel directly. Adding a new channel is a single line in tracing.js with no export changes needed.
1 parent 030504d commit 2f74ac0

7 files changed

Lines changed: 50 additions & 56 deletions

File tree

lib/aggregate.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const { buildMiddlewareFilter } = require('./helpers/buildMiddlewareFilter');
1515
const stringifyFunctionOperators = require('./helpers/aggregate/stringifyFunctionOperators');
1616
const utils = require('./utils');
1717
const { modelSymbol } = require('./helpers/symbols');
18-
const { trace } = require('./tracing');
18+
const { queryChannel } = require('./tracing');
1919
const read = Query.prototype.read;
2020
const readConcern = Query.prototype.readConcern;
2121

@@ -1073,7 +1073,7 @@ Aggregate.prototype.exec = async function exec() {
10731073
this._optionsForExec();
10741074

10751075
const _this = this;
1076-
return trace(async function maybeTracedConnectionAggregate() {
1076+
return queryChannel.trace(async function maybeTracedConnectionAggregate() {
10771077
const cursor = await _this._connection.client.db().aggregate(_this._pipeline, _this.options);
10781078
return await cursor.toArray();
10791079
}, () => ({
@@ -1104,7 +1104,7 @@ Aggregate.prototype.exec = async function exec() {
11041104
stringifyFunctionOperators(this._pipeline);
11051105

11061106
const _this = this;
1107-
return trace(async function maybeTracedAggregateExec() {
1107+
return queryChannel.trace(async function maybeTracedAggregateExec() {
11081108
const preFilter = buildMiddlewareFilter(_this.options, 'pre');
11091109
const postFilter = buildMiddlewareFilter(_this.options, 'post');
11101110

lib/cursor/aggregationCursor.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const eachAsync = require('../helpers/cursor/eachAsync');
1010
const immediate = require('../helpers/immediate');
1111
const kareem = require('kareem');
1212
const util = require('util');
13-
const { traceCursorNext } = require('../tracing');
13+
const { cursorNextChannel } = require('../tracing');
1414

1515
/**
1616
* An AggregationCursor is a concurrency primitive for processing aggregation
@@ -280,7 +280,7 @@ AggregationCursor.prototype.next = async function next() {
280280
}
281281
const _this = this;
282282
const model = this.agg._model;
283-
return traceCursorNext(function maybeTracedAggCursorNext() {
283+
return cursorNextChannel.trace(function maybeTracedAggCursorNext() {
284284
return new Promise((resolve, reject) => {
285285
_next(_this, (err, res) => {
286286
if (err != null) {

lib/cursor/queryCursor.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const kareem = require('kareem');
1212
const immediate = require('../helpers/immediate');
1313
const { once } = require('events');
1414
const util = require('util');
15-
const { traceCursorNext } = require('../tracing');
15+
const { cursorNextChannel } = require('../tracing');
1616

1717
/**
1818
* A QueryCursor is a concurrency primitive for processing query results
@@ -312,7 +312,7 @@ QueryCursor.prototype.next = async function next() {
312312
throw new MongooseError('Cannot call `next()` on a closed cursor');
313313
}
314314
const _this = this;
315-
return traceCursorNext(function maybeTracedQueryCursorNext() {
315+
return cursorNextChannel.trace(function maybeTracedQueryCursorNext() {
316316
return new Promise((resolve, reject) => {
317317
_next(_this, function(error, doc) {
318318
if (error) {

lib/model.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
const Aggregate = require('./aggregate');
88
const ChangeStream = require('./cursor/changeStream');
99
const Document = require('./document');
10-
const { trace } = require('./tracing');
10+
const { queryChannel } = require('./tracing');
1111
const DocumentNotFoundError = require('./error/notFound');
1212
const EventEmitter = require('events').EventEmitter;
1313
const Kareem = require('kareem');
@@ -665,7 +665,7 @@ Model.prototype.save = async function save(options) {
665665
this.$__.saveOptions = options;
666666

667667
const _this = this;
668-
return trace(async function maybeTracedSave() {
668+
return queryChannel.trace(async function maybeTracedSave() {
669669
try {
670670
await _this.$__save(options);
671671
} catch (error) {
@@ -3027,7 +3027,7 @@ Model.insertMany = function insertMany(arr, options) {
30273027
}
30283028

30293029
const ThisModel = this;
3030-
return trace(function maybeTracedInsertMany() { return _insertMany.call(ThisModel, arr, options); }, () => ({
3030+
return queryChannel.trace(function maybeTracedInsertMany() { return _insertMany.call(ThisModel, arr, options); }, () => ({
30313031
operation: 'insertMany',
30323032
collection: ThisModel.collection.name,
30333033
database: ThisModel.db?.name,
@@ -3403,7 +3403,7 @@ Model.bulkWrite = function bulkWrite(ops, options) {
34033403
}
34043404

34053405
const ThisModel = this;
3406-
return trace(function maybeTracedBulkWrite() { return _bulkWrite.call(ThisModel, ops, options); }, () => ({
3406+
return queryChannel.trace(function maybeTracedBulkWrite() { return _bulkWrite.call(ThisModel, ops, options); }, () => ({
34073407
operation: 'bulkWrite',
34083408
collection: ThisModel.collection.name,
34093409
database: ThisModel.db?.name,

lib/query.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const updateValidators = require('./helpers/updateValidators');
4242
const util = require('util');
4343
const utils = require('./utils');
4444
const queryMiddlewareFunctions = require('./constants').queryMiddlewareFunctions;
45-
const { trace } = require('./tracing');
45+
const { queryChannel } = require('./tracing');
4646

4747
const queryOptionMethods = new Set([
4848
'allowDiskUse',
@@ -4780,7 +4780,7 @@ Query.prototype.exec = async function exec(op) {
47804780
this._execCount++;
47814781

47824782
const _this = this;
4783-
return trace(async function maybeTracedQueryExec() {
4783+
return queryChannel.trace(async function maybeTracedQueryExec() {
47844784
let skipWrappedFunction = null;
47854785
try {
47864786
await _this._hooks.execPre('exec', _this, []);

lib/tracing.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,7 @@ function createTracedChannel(name) {
3535
return { channel: ch, trace };
3636
}
3737

38-
const query = createTracedChannel('mongoose:query');
39-
const cursorNext = createTracedChannel('mongoose:cursor:next');
40-
4138
module.exports = {
42-
channel: query.channel,
43-
cursorNextChannel: cursorNext.channel,
44-
trace: query.trace,
45-
traceCursorNext: cursorNext.trace,
46-
shouldTrace
39+
queryChannel: createTracedChannel('mongoose:query'),
40+
cursorNextChannel: createTracedChannel('mongoose:cursor:next')
4741
};

0 commit comments

Comments
 (0)