Skip to content

Commit

Permalink
test(instrumentation/express): cover
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Marton committed Jul 5, 2017
1 parent ed39285 commit be1a4be
Show file tree
Hide file tree
Showing 4 changed files with 250 additions and 1 deletion.
160 changes: 160 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@
"request": "2.81.0",
"request-promise-native": "1.0.4",
"sinon": "2.3.6",
"sinon-chai": "2.11.0"
"sinon-chai": "2.11.0",
"super-request": "1.2.0"
},
"engines": {
"node": ">=8.0.0"
Expand Down
1 change: 1 addition & 0 deletions src/instrumentation/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ function unpatch (express) {
module.exports = {
module: 'express',
supportedVersions: ['4.x'],
TAG_REQUEST_PATH,
OPERATION_NAME,
patch,
unpatch
Expand Down
87 changes: 87 additions & 0 deletions src/instrumentation/express.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
'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('./express')

describe('instrumentation: express', () => {
let tracer
let mockSpan

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

this.sandbox.stub(cls, 'startRootSpan').callsFake(() => mockSpan)

instrumentation.patch(express, tracer)
})

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

describe('#patch', () => {
it('should create a span without parent', async () => {
// test
const app = express()
app.get('/', (req, res) => res.send('ok'))

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

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

expect(mockSpan.setTag).to.be.calledWith(Tags.HTTP_URL, 'http://127.0.0.1/')
expect(mockSpan.setTag).to.be.calledWith(Tags.HTTP_METHOD, 'GET')
expect(mockSpan.setTag).to.be.calledWith(Tags.SPAN_KIND_RPC_SERVER, true)
expect(mockSpan.log).to.be.calledWith({ peerRemoteAddress: '::ffff:127.0.0.1' })
expect(mockSpan.setTag).to.be.calledWith(instrumentation.TAG_REQUEST_PATH, '/')
expect(mockSpan.setTag).to.be.calledWith(Tags.HTTP_STATUS_CODE, 200)
expect(mockSpan.finish).to.have.callCount(1)
})

it('should create a span with parent', async () => {
const headers = {}
const parentSpan = tracer.startSpan('http_request')
tracer.inject(parentSpan, headers)

const app = express()
app.get('/', (req, res) => res.send('ok'))

await request(app)
.get('/')
.headers(headers)
.expect(200)
.end()

expect(cls.startRootSpan).to.be.calledWith(tracer, instrumentation.OPERATION_NAME, parentSpan.context())
})

it('should set error tag for > 3xx statu code', async () => {
const app = express()
app.get('/', (req, res) => {
res.statusCode = 400
res.send('ok')
})

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

expect(mockSpan.setTag).to.be.calledWith(Tags.HTTP_STATUS_CODE, 400)
expect(mockSpan.setTag).to.be.calledWith(Tags.ERROR, true)
expect(mockSpan.finish).to.have.callCount(1)
})
})
})

0 comments on commit be1a4be

Please sign in to comment.