Skip to content
This repository was archived by the owner on Jan 21, 2026. It is now read-only.

Commit d227b6d

Browse files
authored
feat: support mongodb-core@3 (#760)
PR-URL: #760
1 parent bc4895f commit d227b6d

5 files changed

Lines changed: 128 additions & 109 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ The trace agent can do automatic tracing of the following web frameworks:
6666
The agent will also automatically trace RPCs from the following modules:
6767
* Outbound HTTP requests through `http`, `https`, and `http2` core modules
6868
* [grpc](https://www.npmjs.com/package/grpc) client (version ^1.1)
69-
* [mongodb-core](https://www.npmjs.com/package/mongodb-core) (version 1)
70-
* [mongoose](https://www.npmjs.com/package/mongoose) (version 4)
69+
* [mongodb-core](https://www.npmjs.com/package/mongodb-core) (version 1 - 3)
70+
* [mongoose](https://www.npmjs.com/package/mongoose) (version 4 - 5)
7171
* [mysql](https://www.npmjs.com/package/mysql) (version ^2.9)
7272
* [mysql2](https://www.npmjs.com/package/mysql2) (version 1)
7373
* [pg](https://www.npmjs.com/package/mysql2) (versions 6 - 7)

src/plugins/plugin-mongodb-core.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
var shimmer = require('shimmer');
1919

20-
var SUPPORTED_VERSIONS = '1 - 2';
20+
var SUPPORTED_VERSIONS = '1 - 3';
2121

2222
function createNextWrap(api) {
2323
return function nextWrap(next) {

test/fixtures/plugin-fixtures.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,21 @@
112112
"mongodb-core": "^2.0.4"
113113
}
114114
},
115+
"mongodb-core3": {
116+
"dependencies": {
117+
"mongodb-core": "^3.0.6"
118+
}
119+
},
115120
"mongoose4": {
116121
"dependencies": {
117122
"mongoose": "^4.1.9"
118123
}
119124
},
125+
"mongoose5": {
126+
"dependencies": {
127+
"mongoose": "^5.1.3"
128+
}
129+
},
120130
"mysql-2": {
121131
"dependencies": {
122132
"mysql": "^2.9.0"

test/plugins/test-trace-mongodb.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ var RESULT_SIZE = 5;
2929

3030
var versions = {
3131
mongodb1: './fixtures/mongodb-core1',
32-
mongodb2: './fixtures/mongodb-core2'
32+
mongodb2: './fixtures/mongodb-core2',
33+
mongodb3: './fixtures/mongodb-core3',
3334
};
3435

3536
describe('mongodb', function() {

test/plugins/test-trace-mongoose.ts

Lines changed: 113 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -26,136 +26,144 @@ var common = require('./common'/*.js*/);
2626

2727
var assert = require('assert');
2828

29-
describe('test-trace-mongoose', function() {
29+
describe('mongoose integration tests', function() {
3030
var agent;
31-
var mongoose;
3231
var Simple;
3332
before(function() {
3433
agent = require('../../..').start({
3534
projectId: '0',
3635
samplingRate: 0
3736
});
38-
39-
mongoose = require('./fixtures/mongoose4');
40-
mongoose.Promise = global.Promise;
41-
42-
var Schema = mongoose.Schema;
43-
var simpleSchema = new Schema({
44-
f1: String,
45-
f2: Boolean,
46-
f3: Number
47-
});
48-
49-
Simple = mongoose.model('Simple', simpleSchema);
5037
});
38+
39+
const versions = [4, 5];
40+
for (const version of versions) {
41+
describe(`mongoose@${version}`, () => {
42+
let mongoose;
43+
44+
before(() => {
45+
mongoose = require(`./fixtures/mongoose${version}`);
46+
mongoose.Promise = global.Promise;
47+
48+
var Schema = mongoose.Schema;
49+
var simpleSchema = new Schema({
50+
f1: String,
51+
f2: Boolean,
52+
f3: Number
53+
});
5154

52-
beforeEach(function(done) {
53-
var sim = new Simple({
54-
f1: 'sim',
55-
f2: true,
56-
f3: 42
57-
});
58-
mongoose.connect('mongodb://localhost:27017/testdb', function(err) {
59-
assert(!err, 'Skipping: error connecting to mongo at localhost:27017.');
60-
sim.save(function(err) {
61-
assert(!err);
62-
common.cleanTraces();
63-
done();
55+
Simple = mongoose.model('Simple', simpleSchema);
6456
});
65-
});
66-
});
6757

68-
afterEach(function(done) {
69-
mongoose.connection.db.dropDatabase(function(err) {
70-
assert(!err);
71-
mongoose.connection.close(function(err) {
72-
assert(!err);
73-
common.cleanTraces();
74-
done();
58+
beforeEach(function(done) {
59+
var sim = new Simple({
60+
f1: 'sim',
61+
f2: true,
62+
f3: 42
63+
});
64+
mongoose.connect('mongodb://localhost:27017/testdb', function(err) {
65+
assert(!err, 'Skipping: error connecting to mongo at localhost:27017.');
66+
sim.save(function(err) {
67+
assert(!err);
68+
common.cleanTraces();
69+
done();
70+
});
71+
});
7572
});
76-
});
77-
});
7873

79-
it('should accurately measure create time', function(done) {
80-
var data = new Simple({
81-
f1: 'val',
82-
f2: false,
83-
f3: 1729
84-
});
85-
common.runInTransaction(function(endTransaction) {
86-
data.save(function(err) {
87-
endTransaction();
88-
assert(!err);
89-
var trace = common.getMatchingSpan(mongoPredicate.bind(null, 'mongo-insert'));
90-
assert(trace);
91-
done();
74+
afterEach(function(done) {
75+
mongoose.connection.db.dropDatabase(function(err) {
76+
assert(!err);
77+
mongoose.connection.close(function(err) {
78+
assert(!err);
79+
common.cleanTraces();
80+
done();
81+
});
82+
});
9283
});
93-
});
94-
});
9584

96-
it('should accurately measure update time', function(done) {
97-
common.runInTransaction(function(endTransaction) {
98-
Simple.findOne({f1: 'sim'}, function(err, res) {
99-
assert(!err);
100-
res.f2 = false;
101-
res.save(function(err) {
102-
endTransaction();
103-
assert(!err);
104-
var trace = common.getMatchingSpan(mongoPredicate.bind(null, 'mongo-update'));
105-
assert(trace);
106-
done();
85+
it('should accurately measure create time', function(done) {
86+
var data = new Simple({
87+
f1: 'val',
88+
f2: false,
89+
f3: 1729
90+
});
91+
common.runInTransaction(function(endTransaction) {
92+
data.save(function(err) {
93+
endTransaction();
94+
assert(!err);
95+
var trace = common.getMatchingSpan(mongoPredicate.bind(null, 'mongo-insert'));
96+
assert(trace);
97+
done();
98+
});
10799
});
108100
});
109-
});
110-
});
111101

112-
it('should accurately measure retrieval time', function(done) {
113-
common.runInTransaction(function(endTransaction) {
114-
Simple.findOne({f1: 'sim'}, function(err, res) {
115-
endTransaction();
116-
assert(!err);
117-
var trace = common.getMatchingSpan(mongoPredicate.bind(null, 'mongo-cursor'));
118-
assert(trace);
119-
done();
102+
it('should accurately measure update time', function(done) {
103+
common.runInTransaction(function(endTransaction) {
104+
Simple.findOne({f1: 'sim'}, function(err, res) {
105+
assert(!err);
106+
res.f2 = false;
107+
res.save(function(err) {
108+
endTransaction();
109+
assert(!err);
110+
var trace = common.getMatchingSpan(mongoPredicate.bind(null, 'mongo-update'));
111+
assert(trace);
112+
done();
113+
});
114+
});
115+
});
120116
});
121-
});
122-
});
123117

124-
it('should accurately measure delete time', function(done) {
125-
common.runInTransaction(function(endTransaction) {
126-
Simple.remove({f1: 'sim'}, function(err, res) {
127-
endTransaction();
128-
assert(!err);
129-
var trace = common.getMatchingSpan(mongoPredicate.bind(null, 'mongo-remove'));
130-
assert(trace);
131-
done();
118+
it('should accurately measure retrieval time', function(done) {
119+
common.runInTransaction(function(endTransaction) {
120+
Simple.findOne({f1: 'sim'}, function(err, res) {
121+
endTransaction();
122+
assert(!err);
123+
var trace = common.getMatchingSpan(mongoPredicate.bind(null, 'mongo-cursor'));
124+
assert(trace);
125+
done();
126+
});
127+
});
132128
});
133-
});
134-
});
135129

136-
it('should not break if no parent transaction', function(done) {
137-
Simple.findOne({f1: 'sim'}, function(err, res) {
138-
assert(!err);
139-
assert(res);
140-
done();
141-
});
142-
});
130+
it('should accurately measure delete time', function(done) {
131+
common.runInTransaction(function(endTransaction) {
132+
Simple.remove({f1: 'sim'}, function(err, res) {
133+
endTransaction();
134+
assert(!err);
135+
var trace = common.getMatchingSpan(mongoPredicate.bind(null, 'mongo-remove'));
136+
assert(trace);
137+
done();
138+
});
139+
});
140+
});
143141

144-
it('should remove trace frames from stack', function(done) {
145-
common.runInTransaction(function(endTransaction) {
146-
Simple.findOne({f1: 'sim'}, function(err, res) {
147-
endTransaction();
148-
assert(!err);
149-
var trace = common.getMatchingSpan(mongoPredicate.bind(null, 'mongo-cursor'));
150-
var labels = trace.labels;
151-
var stackTrace = JSON.parse(labels[TraceLabels.STACK_TRACE_DETAILS_KEY]);
152-
// Ensure that our patch is on top of the stack
153-
assert(
154-
stackTrace.stack_frame[0].method_name.indexOf('next_trace') !== -1);
155-
done();
142+
it('should not break if no parent transaction', function(done) {
143+
Simple.findOne({f1: 'sim'}, function(err, res) {
144+
assert(!err);
145+
assert(res);
146+
done();
147+
});
148+
});
149+
150+
it('should remove trace frames from stack', function(done) {
151+
common.runInTransaction(function(endTransaction) {
152+
Simple.findOne({f1: 'sim'}, function(err, res) {
153+
endTransaction();
154+
assert(!err);
155+
var trace = common.getMatchingSpan(mongoPredicate.bind(null, 'mongo-cursor'));
156+
var labels = trace.labels;
157+
var stackTrace = JSON.parse(labels[TraceLabels.STACK_TRACE_DETAILS_KEY]);
158+
// Ensure that our patch is on top of the stack
159+
assert(
160+
stackTrace.stack_frame[0].method_name.indexOf('next_trace') !== -1);
161+
done();
162+
});
163+
});
156164
});
157165
});
158-
});
166+
}
159167
});
160168

161169
function mongoPredicate(id, span) {

0 commit comments

Comments
 (0)