-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Peter Marton
committed
Aug 23, 2017
1 parent
b142b94
commit 8374502
Showing
5 changed files
with
86 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
'use strict' | ||
|
||
const mongodbCore = require('mongodb-core') | ||
const monk = require('monk') | ||
const { expect } = require('chai') | ||
const { Tracer, Tags } = require('opentracing') | ||
const cls = require('../cls') | ||
const instrumentation = require('./mongodbCore') | ||
|
||
describe('instrumentation: mongodb-core', () => { | ||
let tracer | ||
let mockChildSpan | ||
let db | ||
let dbSites | ||
|
||
beforeEach(function (done) { | ||
tracer = new Tracer() | ||
mockChildSpan = { | ||
setTag: this.sandbox.spy(), | ||
log: this.sandbox.spy(), | ||
finish: this.sandbox.spy() | ||
} | ||
|
||
this.sandbox.stub(cls, 'startChildSpan').callsFake(() => mockChildSpan) | ||
|
||
instrumentation.patch(mongodbCore, tracer) | ||
|
||
db = monk('localhost/mydb', done) | ||
dbSites = db.get('sites') | ||
}) | ||
|
||
afterEach(() => { | ||
instrumentation.unpatch(mongodbCore) | ||
}) | ||
|
||
describe('#patch', () => { | ||
it('should start and finish span', async () => { | ||
const site = { | ||
name: 'risingstack', | ||
url: 'https://risingstack.com' | ||
} | ||
const result = await dbSites.insert(site) | ||
|
||
expect(result).to.be.eql(site) | ||
|
||
expect(cls.startChildSpan).to.be.calledWith(tracer, `${instrumentation.OPERATION_NAME}_insert`) | ||
expect(mockChildSpan.setTag).to.have.calledWith(Tags.DB_TYPE, instrumentation.DB_TYPE) | ||
expect(mockChildSpan.setTag).to.have.calledWith(Tags.DB_STATEMENT, JSON.stringify([site])) | ||
}) | ||
|
||
it('should flag error', async () => { | ||
db.close() // trigger error | ||
|
||
try { | ||
await dbSites.insert({ | ||
name: 'risingstack', | ||
url: 'https://risingstack.com' | ||
}) | ||
} catch (err) { | ||
expect(mockChildSpan.setTag).to.be.calledWith(Tags.ERROR, true) | ||
expect(mockChildSpan.log).to.be.calledWith({ | ||
event: 'error', | ||
'error.object': err, | ||
message: err.message, | ||
stack: err.stack | ||
}) | ||
return | ||
} | ||
|
||
throw new Error('Uncaught exception') | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters