Skip to content

Commit be1a4be

Browse files
author
Peter Marton
committed
test(instrumentation/express): cover
1 parent ed39285 commit be1a4be

File tree

4 files changed

+250
-1
lines changed

4 files changed

+250
-1
lines changed

Diff for: package-lock.json

+160
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@
5151
"request": "2.81.0",
5252
"request-promise-native": "1.0.4",
5353
"sinon": "2.3.6",
54-
"sinon-chai": "2.11.0"
54+
"sinon-chai": "2.11.0",
55+
"super-request": "1.2.0"
5556
},
5657
"engines": {
5758
"node": ">=8.0.0"

Diff for: src/instrumentation/express.js

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ function unpatch (express) {
7878
module.exports = {
7979
module: 'express',
8080
supportedVersions: ['4.x'],
81+
TAG_REQUEST_PATH,
8182
OPERATION_NAME,
8283
patch,
8384
unpatch

Diff for: src/instrumentation/express.spec.js

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
'use strict'
2+
3+
const request = require('super-request')
4+
const { expect } = require('chai')
5+
const { Tracer, Tags } = require('opentracing')
6+
const express = require('express')
7+
const cls = require('../cls')
8+
const instrumentation = require('./express')
9+
10+
describe('instrumentation: express', () => {
11+
let tracer
12+
let mockSpan
13+
14+
beforeEach(function () {
15+
tracer = new Tracer()
16+
mockSpan = {
17+
setTag: this.sandbox.spy(),
18+
log: this.sandbox.spy(),
19+
finish: this.sandbox.spy()
20+
}
21+
22+
this.sandbox.stub(cls, 'startRootSpan').callsFake(() => mockSpan)
23+
24+
instrumentation.patch(express, tracer)
25+
})
26+
27+
afterEach(() => {
28+
instrumentation.unpatch(express)
29+
})
30+
31+
describe('#patch', () => {
32+
it('should create a span without parent', async () => {
33+
// test
34+
const app = express()
35+
app.get('/', (req, res) => res.send('ok'))
36+
37+
await request(app)
38+
.get('/')
39+
.expect(200)
40+
.end()
41+
42+
expect(cls.startRootSpan).to.be.calledWith(tracer, instrumentation.OPERATION_NAME)
43+
44+
expect(mockSpan.setTag).to.be.calledWith(Tags.HTTP_URL, 'http://127.0.0.1/')
45+
expect(mockSpan.setTag).to.be.calledWith(Tags.HTTP_METHOD, 'GET')
46+
expect(mockSpan.setTag).to.be.calledWith(Tags.SPAN_KIND_RPC_SERVER, true)
47+
expect(mockSpan.log).to.be.calledWith({ peerRemoteAddress: '::ffff:127.0.0.1' })
48+
expect(mockSpan.setTag).to.be.calledWith(instrumentation.TAG_REQUEST_PATH, '/')
49+
expect(mockSpan.setTag).to.be.calledWith(Tags.HTTP_STATUS_CODE, 200)
50+
expect(mockSpan.finish).to.have.callCount(1)
51+
})
52+
53+
it('should create a span with parent', async () => {
54+
const headers = {}
55+
const parentSpan = tracer.startSpan('http_request')
56+
tracer.inject(parentSpan, headers)
57+
58+
const app = express()
59+
app.get('/', (req, res) => res.send('ok'))
60+
61+
await request(app)
62+
.get('/')
63+
.headers(headers)
64+
.expect(200)
65+
.end()
66+
67+
expect(cls.startRootSpan).to.be.calledWith(tracer, instrumentation.OPERATION_NAME, parentSpan.context())
68+
})
69+
70+
it('should set error tag for > 3xx statu code', async () => {
71+
const app = express()
72+
app.get('/', (req, res) => {
73+
res.statusCode = 400
74+
res.send('ok')
75+
})
76+
77+
await request(app)
78+
.get('/')
79+
.expect(400)
80+
.end()
81+
82+
expect(mockSpan.setTag).to.be.calledWith(Tags.HTTP_STATUS_CODE, 400)
83+
expect(mockSpan.setTag).to.be.calledWith(Tags.ERROR, true)
84+
expect(mockSpan.finish).to.have.callCount(1)
85+
})
86+
})
87+
})

0 commit comments

Comments
 (0)