|
| 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