Skip to content

Commit

Permalink
feat: support mongodb-core@3
Browse files Browse the repository at this point in the history
  • Loading branch information
kjin committed May 29, 2018
1 parent be8f4e6 commit fed9ef9
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 109 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ The trace agent can do automatic tracing of the following web frameworks:
The agent will also automatically trace RPCs from the following modules:
* Outbound HTTP requests through `http`, `https`, and `http2` core modules
* [grpc](https://www.npmjs.com/package/grpc) client (version ^1.1)
* [mongodb-core](https://www.npmjs.com/package/mongodb-core) (version 1)
* [mongoose](https://www.npmjs.com/package/mongoose) (version 4)
* [mongodb-core](https://www.npmjs.com/package/mongodb-core) (version 1 - 3)
* [mongoose](https://www.npmjs.com/package/mongoose) (version 4 - 5)
* [mysql](https://www.npmjs.com/package/mysql) (version ^2.9)
* [mysql2](https://www.npmjs.com/package/mysql2) (version 1)
* [pg](https://www.npmjs.com/package/mysql2) (versions 6 - 7)
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/plugin-mongodb-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

var shimmer = require('shimmer');

var SUPPORTED_VERSIONS = '1 - 2';
var SUPPORTED_VERSIONS = '1 - 3';

function createNextWrap(api) {
return function nextWrap(next) {
Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/plugin-fixtures.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,21 @@
"mongodb-core": "^2.0.4"
}
},
"mongodb-core3": {
"dependencies": {
"mongodb-core": "^3.0.6"
}
},
"mongoose4": {
"dependencies": {
"mongoose": "^4.1.9"
}
},
"mongoose5": {
"dependencies": {
"mongoose": "^5.1.3"
}
},
"mysql-2": {
"dependencies": {
"mysql": "^2.9.0"
Expand Down
3 changes: 2 additions & 1 deletion test/plugins/test-trace-mongodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ var RESULT_SIZE = 5;

var versions = {
mongodb1: './fixtures/mongodb-core1',
mongodb2: './fixtures/mongodb-core2'
mongodb2: './fixtures/mongodb-core2',
mongodb3: './fixtures/mongodb-core3',
};

describe('mongodb', function() {
Expand Down
218 changes: 113 additions & 105 deletions test/plugins/test-trace-mongoose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,136 +26,144 @@ var common = require('./common'/*.js*/);

var assert = require('assert');

describe('test-trace-mongoose', function() {
describe('mongoose integration tests', function() {
var agent;
var mongoose;
var Simple;
before(function() {
agent = require('../../..').start({
projectId: '0',
samplingRate: 0
});

mongoose = require('./fixtures/mongoose4');
mongoose.Promise = global.Promise;

var Schema = mongoose.Schema;
var simpleSchema = new Schema({
f1: String,
f2: Boolean,
f3: Number
});

Simple = mongoose.model('Simple', simpleSchema);
});

const versions = [4, 5];
for (const version of versions) {
describe(`mongoose@${version}`, () => {
let mongoose;

before(() => {
mongoose = require(`./fixtures/mongoose${version}`);
mongoose.Promise = global.Promise;

var Schema = mongoose.Schema;
var simpleSchema = new Schema({
f1: String,
f2: Boolean,
f3: Number
});

beforeEach(function(done) {
var sim = new Simple({
f1: 'sim',
f2: true,
f3: 42
});
mongoose.connect('mongodb://localhost:27017/testdb', function(err) {
assert(!err, 'Skipping: error connecting to mongo at localhost:27017.');
sim.save(function(err) {
assert(!err);
common.cleanTraces();
done();
Simple = mongoose.model('Simple', simpleSchema);
});
});
});

afterEach(function(done) {
mongoose.connection.db.dropDatabase(function(err) {
assert(!err);
mongoose.connection.close(function(err) {
assert(!err);
common.cleanTraces();
done();
beforeEach(function(done) {
var sim = new Simple({
f1: 'sim',
f2: true,
f3: 42
});
mongoose.connect('mongodb://localhost:27017/testdb', function(err) {
assert(!err, 'Skipping: error connecting to mongo at localhost:27017.');
sim.save(function(err) {
assert(!err);
common.cleanTraces();
done();
});
});
});
});
});

it('should accurately measure create time', function(done) {
var data = new Simple({
f1: 'val',
f2: false,
f3: 1729
});
common.runInTransaction(function(endTransaction) {
data.save(function(err) {
endTransaction();
assert(!err);
var trace = common.getMatchingSpan(mongoPredicate.bind(null, 'mongo-insert'));
assert(trace);
done();
afterEach(function(done) {
mongoose.connection.db.dropDatabase(function(err) {
assert(!err);
mongoose.connection.close(function(err) {
assert(!err);
common.cleanTraces();
done();
});
});
});
});
});

it('should accurately measure update time', function(done) {
common.runInTransaction(function(endTransaction) {
Simple.findOne({f1: 'sim'}, function(err, res) {
assert(!err);
res.f2 = false;
res.save(function(err) {
endTransaction();
assert(!err);
var trace = common.getMatchingSpan(mongoPredicate.bind(null, 'mongo-update'));
assert(trace);
done();
it('should accurately measure create time', function(done) {
var data = new Simple({
f1: 'val',
f2: false,
f3: 1729
});
common.runInTransaction(function(endTransaction) {
data.save(function(err) {
endTransaction();
assert(!err);
var trace = common.getMatchingSpan(mongoPredicate.bind(null, 'mongo-insert'));
assert(trace);
done();
});
});
});
});
});

it('should accurately measure retrieval time', function(done) {
common.runInTransaction(function(endTransaction) {
Simple.findOne({f1: 'sim'}, function(err, res) {
endTransaction();
assert(!err);
var trace = common.getMatchingSpan(mongoPredicate.bind(null, 'mongo-cursor'));
assert(trace);
done();
it('should accurately measure update time', function(done) {
common.runInTransaction(function(endTransaction) {
Simple.findOne({f1: 'sim'}, function(err, res) {
assert(!err);
res.f2 = false;
res.save(function(err) {
endTransaction();
assert(!err);
var trace = common.getMatchingSpan(mongoPredicate.bind(null, 'mongo-update'));
assert(trace);
done();
});
});
});
});
});
});

it('should accurately measure delete time', function(done) {
common.runInTransaction(function(endTransaction) {
Simple.remove({f1: 'sim'}, function(err, res) {
endTransaction();
assert(!err);
var trace = common.getMatchingSpan(mongoPredicate.bind(null, 'mongo-remove'));
assert(trace);
done();
it('should accurately measure retrieval time', function(done) {
common.runInTransaction(function(endTransaction) {
Simple.findOne({f1: 'sim'}, function(err, res) {
endTransaction();
assert(!err);
var trace = common.getMatchingSpan(mongoPredicate.bind(null, 'mongo-cursor'));
assert(trace);
done();
});
});
});
});
});

it('should not break if no parent transaction', function(done) {
Simple.findOne({f1: 'sim'}, function(err, res) {
assert(!err);
assert(res);
done();
});
});
it('should accurately measure delete time', function(done) {
common.runInTransaction(function(endTransaction) {
Simple.remove({f1: 'sim'}, function(err, res) {
endTransaction();
assert(!err);
var trace = common.getMatchingSpan(mongoPredicate.bind(null, 'mongo-remove'));
assert(trace);
done();
});
});
});

it('should remove trace frames from stack', function(done) {
common.runInTransaction(function(endTransaction) {
Simple.findOne({f1: 'sim'}, function(err, res) {
endTransaction();
assert(!err);
var trace = common.getMatchingSpan(mongoPredicate.bind(null, 'mongo-cursor'));
var labels = trace.labels;
var stackTrace = JSON.parse(labels[TraceLabels.STACK_TRACE_DETAILS_KEY]);
// Ensure that our patch is on top of the stack
assert(
stackTrace.stack_frame[0].method_name.indexOf('next_trace') !== -1);
done();
it('should not break if no parent transaction', function(done) {
Simple.findOne({f1: 'sim'}, function(err, res) {
assert(!err);
assert(res);
done();
});
});

it('should remove trace frames from stack', function(done) {
common.runInTransaction(function(endTransaction) {
Simple.findOne({f1: 'sim'}, function(err, res) {
endTransaction();
assert(!err);
var trace = common.getMatchingSpan(mongoPredicate.bind(null, 'mongo-cursor'));
var labels = trace.labels;
var stackTrace = JSON.parse(labels[TraceLabels.STACK_TRACE_DETAILS_KEY]);
// Ensure that our patch is on top of the stack
assert(
stackTrace.stack_frame[0].method_name.indexOf('next_trace') !== -1);
done();
});
});
});
});
});
}
});

function mongoPredicate(id, span) {
Expand Down

0 comments on commit fed9ef9

Please sign in to comment.