Skip to content

Commit

Permalink
test(instrumentation/expressError): add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Marton committed Jul 6, 2017
1 parent be1a4be commit 8795641
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 27 deletions.
6 changes: 0 additions & 6 deletions src/instrumentation/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,11 @@ const opentracing = require('opentracing')
const shimmer = require('shimmer')
const methods = require('methods').concat('use', 'route', 'param', 'all')
const cls = require('../cls')
const { isExpressV4 } = require('./util')

const OPERATION_NAME = 'http_server'
const TAG_REQUEST_PATH = 'request_path'

function patch (express, tracer) {
// support only express@4
if (!isExpressV4(express)) {
return
}

function applicationActionWrap (method) {
return function expressActionTrace (...args) {
if (!this._jaeger_trace_patched && !this._router) {
Expand Down
6 changes: 0 additions & 6 deletions src/instrumentation/expressError.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,10 @@
const shimmer = require('shimmer')
const opentracing = require('opentracing')
const cls = require('../cls')
const { isExpressV4 } = require('./util')

const OPERATION_NAME = 'express_error_handler'

function patch (express, tracer) {
// support only express@4
if (!isExpressV4(express)) {
return
}

let errorHandlerLayer

function expressErrorHandler (err, req, res, next) {
Expand Down
90 changes: 90 additions & 0 deletions src/instrumentation/expressError.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
'use strict'

const request = require('super-request')
const { expect } = require('chai')
const { Tracer, Tags } = require('opentracing')
const express = require('express')
const cls = require('../cls')
const instrumentation = require('./expressError')

describe('instrumentation: expressError', () => {
let tracer
let mockRootSpan
let mockChildSpan

beforeEach(function () {
tracer = new Tracer()
mockRootSpan = {
setTag: this.sandbox.spy()
}
mockChildSpan = {
setTag: this.sandbox.spy(),
log: this.sandbox.spy(),
finish: this.sandbox.spy()
}

this.sandbox.stub(cls, 'getRootSpan').callsFake(() => mockRootSpan)
this.sandbox.stub(cls, 'startChildSpan').callsFake(() => mockChildSpan)

instrumentation.patch(express, tracer)
})

afterEach(() => {
instrumentation.unpatch(express)
})

describe('#patch', () => {
it('should catch error when error middleware is presented', async () => {
const err = new Error('My Error')
const app = express()

app.get('/', (req, res, next) => next(err))
app.use((error, req, res, next) => {
res.statusCode = 500
res.end()
next()
})

await request(app)
.get('/')
.expect(500)
.end()

expect(cls.startChildSpan).to.be.calledWith(tracer, instrumentation.OPERATION_NAME)

expect(mockRootSpan.setTag).to.be.calledWith(Tags.ERROR, true)
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
})
expect(mockChildSpan.finish).to.have.callCount(1)
})

it.skip('should catch error when error middleware is not presented', async () => {
const err = new Error('My Error')
const app = express()

app.get('/', (req, res, next) => next(err))

await request(app)
.get('/')
.expect(500)
.end()

expect(cls.startChildSpan).to.be.calledWith(tracer, instrumentation.OPERATION_NAME)

expect(mockRootSpan.setTag).to.be.calledWith(Tags.ERROR, true)
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
})
expect(mockChildSpan.finish).to.have.callCount(1)
})
})
})
15 changes: 0 additions & 15 deletions src/instrumentation/util.js

This file was deleted.

0 comments on commit 8795641

Please sign in to comment.