|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const http = require('http') |
| 4 | +const https = require('https') |
| 5 | +const request = require('request-promise-native') |
| 6 | +const nock = require('nock') |
| 7 | +const { expect } = require('chai') |
| 8 | +const { Tracer, Tags } = require('opentracing') |
| 9 | +const cls = require('../cls') |
| 10 | +const instrumentation = require('./httpClient') |
| 11 | + |
| 12 | +describe('instrumentation: httpClient', () => { |
| 13 | + let tracer |
| 14 | + let mockChildSpan |
| 15 | + |
| 16 | + beforeEach(function () { |
| 17 | + tracer = new Tracer() |
| 18 | + mockChildSpan = { |
| 19 | + setTag: this.sandbox.spy(), |
| 20 | + log: this.sandbox.spy(), |
| 21 | + finish: this.sandbox.spy() |
| 22 | + } |
| 23 | + |
| 24 | + this.sandbox.stub(cls, 'startChildSpan').callsFake(() => mockChildSpan) |
| 25 | + |
| 26 | + instrumentation.patch(http, tracer) |
| 27 | + instrumentation.patch(https, tracer) |
| 28 | + |
| 29 | + nock.disableNetConnect() |
| 30 | + }) |
| 31 | + |
| 32 | + afterEach(() => { |
| 33 | + instrumentation.unpatch(http) |
| 34 | + instrumentation.unpatch(https) |
| 35 | + nock.enableNetConnect() |
| 36 | + nock.cleanAll() |
| 37 | + }) |
| 38 | + |
| 39 | + describe('#patch', () => { |
| 40 | + it('should start and finish span with http', () => { |
| 41 | + nock('http://risingstack.com') |
| 42 | + .get('/') |
| 43 | + .reply(200) |
| 44 | + |
| 45 | + return request('http://risingstack.com') |
| 46 | + .then(() => { |
| 47 | + expect(cls.startChildSpan).to.be.calledWith(tracer, instrumentation.OPERATION_NAME) |
| 48 | + expect(mockChildSpan.setTag).to.have.calledWith(Tags.HTTP_URL, 'http://risingstack.com:80') |
| 49 | + expect(mockChildSpan.setTag).to.have.calledWith(Tags.HTTP_METHOD, 'GET') |
| 50 | + expect(mockChildSpan.setTag).to.have.calledWith(Tags.SPAN_KIND_RPC_CLIENT, true) |
| 51 | + expect(mockChildSpan.setTag).to.have.calledWith(Tags.HTTP_STATUS_CODE, 200) |
| 52 | + expect(mockChildSpan.finish).to.have.callCount(1) |
| 53 | + }) |
| 54 | + }) |
| 55 | + |
| 56 | + it('should start and finish span with https', () => { |
| 57 | + nock('https://risingstack.com') |
| 58 | + .get('/') |
| 59 | + .reply(200) |
| 60 | + |
| 61 | + return request('https://risingstack.com') |
| 62 | + .then(() => { |
| 63 | + expect(cls.startChildSpan).to.be.calledWith(tracer, instrumentation.OPERATION_NAME) |
| 64 | + expect(mockChildSpan.setTag).to.have.calledWith(Tags.HTTP_URL, 'http://risingstack.com:443') |
| 65 | + expect(mockChildSpan.setTag).to.have.calledWith(Tags.HTTP_METHOD, 'GET') |
| 66 | + expect(mockChildSpan.setTag).to.have.calledWith(Tags.SPAN_KIND_RPC_CLIENT, true) |
| 67 | + expect(mockChildSpan.setTag).to.have.calledWith(Tags.HTTP_STATUS_CODE, 200) |
| 68 | + expect(mockChildSpan.finish).to.have.callCount(1) |
| 69 | + }) |
| 70 | + }) |
| 71 | + |
| 72 | + it('should flag wrong status codes as error', () => { |
| 73 | + nock('https://risingstack.com') |
| 74 | + .get('/') |
| 75 | + .reply(400) |
| 76 | + |
| 77 | + return request('https://risingstack.com') |
| 78 | + .catch(() => { |
| 79 | + expect(mockChildSpan.setTag).to.be.calledWith(Tags.ERROR, true) |
| 80 | + }) |
| 81 | + }) |
| 82 | + |
| 83 | + it('should flag error', () => { |
| 84 | + nock('https://risingstack.com') |
| 85 | + .get('/') |
| 86 | + .replyWithError('My Error') |
| 87 | + |
| 88 | + return request('https://risingstack.com') |
| 89 | + .catch((err) => { |
| 90 | + expect(mockChildSpan.setTag).to.be.calledWith(Tags.ERROR, true) |
| 91 | + expect(mockChildSpan.log).to.be.calledWith({ |
| 92 | + event: 'error', |
| 93 | + 'error.object': err.cause, |
| 94 | + message: err.cause.message, |
| 95 | + stack: err.cause.stack |
| 96 | + }) |
| 97 | + }) |
| 98 | + }) |
| 99 | + }) |
| 100 | +}) |
0 commit comments