|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const axios = require('axios') |
| 4 | +const semver = require('semver') |
| 5 | +const agent = require('../../dd-trace/test/plugins/agent') |
| 6 | +const { NODE_MAJOR } = require('../../../version') |
| 7 | + |
| 8 | +const host = 'localhost' |
| 9 | + |
| 10 | +describe('Plugin', () => { |
| 11 | + let fastify |
| 12 | + let app |
| 13 | + |
| 14 | + describe('fastify', () => { |
| 15 | + withVersions('fastify', 'fastify', (version, _, specificVersion) => { |
| 16 | + if (NODE_MAJOR <= 18 && semver.satisfies(specificVersion, '>=5')) return |
| 17 | + |
| 18 | + afterEach(() => { |
| 19 | + app.close() |
| 20 | + }) |
| 21 | + |
| 22 | + withExports('fastify', version, ['default', 'fastify'], '>=3', getExport => { |
| 23 | + describe('with tracer config codeOriginForSpans.enabled: true', () => { |
| 24 | + if (semver.satisfies(specificVersion, '<4')) return // TODO: Why doesn't it work on older versions? |
| 25 | + |
| 26 | + before(() => { |
| 27 | + return agent.load( |
| 28 | + ['fastify', 'find-my-way', 'http'], |
| 29 | + [{}, {}, { client: false }], |
| 30 | + { codeOriginForSpans: { enabled: true } } |
| 31 | + ) |
| 32 | + }) |
| 33 | + |
| 34 | + after(() => { |
| 35 | + return agent.close({ ritmReset: false }) |
| 36 | + }) |
| 37 | + |
| 38 | + beforeEach(() => { |
| 39 | + fastify = getExport() |
| 40 | + app = fastify() |
| 41 | + |
| 42 | + if (semver.intersects(version, '>=3')) { |
| 43 | + return app.register(require('../../../versions/middie').get()) |
| 44 | + } |
| 45 | + }) |
| 46 | + |
| 47 | + it('should add code_origin tag on entry spans when feature is enabled', done => { |
| 48 | + let routeRegisterLine |
| 49 | + |
| 50 | + // Wrap in a named function to have at least one frame with a function name |
| 51 | + function wrapperFunction () { |
| 52 | + routeRegisterLine = getNextLineNumber() |
| 53 | + app.get('/user', function userHandler (request, reply) { |
| 54 | + reply.send() |
| 55 | + }) |
| 56 | + } |
| 57 | + |
| 58 | + const callWrapperLine = getNextLineNumber() |
| 59 | + wrapperFunction() |
| 60 | + |
| 61 | + app.listen(() => { |
| 62 | + const port = app.server.address().port |
| 63 | + |
| 64 | + agent |
| 65 | + .use(traces => { |
| 66 | + const spans = traces[0] |
| 67 | + const tags = spans[0].meta |
| 68 | + |
| 69 | + expect(tags).to.have.property('_dd.code_origin.type', 'entry') |
| 70 | + |
| 71 | + expect(tags).to.have.property('_dd.code_origin.frames.0.file', __filename) |
| 72 | + expect(tags).to.have.property('_dd.code_origin.frames.0.line', routeRegisterLine) |
| 73 | + expect(tags).to.have.property('_dd.code_origin.frames.0.column').to.match(/^\d+$/) |
| 74 | + expect(tags).to.have.property('_dd.code_origin.frames.0.method', 'wrapperFunction') |
| 75 | + expect(tags).to.not.have.property('_dd.code_origin.frames.0.type') |
| 76 | + |
| 77 | + expect(tags).to.have.property('_dd.code_origin.frames.1.file', __filename) |
| 78 | + expect(tags).to.have.property('_dd.code_origin.frames.1.line', callWrapperLine) |
| 79 | + expect(tags).to.have.property('_dd.code_origin.frames.1.column').to.match(/^\d+$/) |
| 80 | + expect(tags).to.not.have.property('_dd.code_origin.frames.1.method') |
| 81 | + expect(tags).to.have.property('_dd.code_origin.frames.1.type', 'Context') |
| 82 | + |
| 83 | + expect(tags).to.not.have.property('_dd.code_origin.frames.2.file') |
| 84 | + }) |
| 85 | + .then(done) |
| 86 | + .catch(done) |
| 87 | + |
| 88 | + axios |
| 89 | + .get(`http://localhost:${port}/user`) |
| 90 | + .catch(done) |
| 91 | + }) |
| 92 | + }) |
| 93 | + |
| 94 | + it('should point to where actual route handler is configured, not the prefix', done => { |
| 95 | + let routeRegisterLine |
| 96 | + |
| 97 | + app.register(function v1Handler (app, opts, done) { |
| 98 | + routeRegisterLine = getNextLineNumber() |
| 99 | + app.get('/user', function userHandler (request, reply) { |
| 100 | + reply.send() |
| 101 | + }) |
| 102 | + done() |
| 103 | + }, { prefix: '/v1' }) |
| 104 | + |
| 105 | + app.listen(() => { |
| 106 | + const port = app.server.address().port |
| 107 | + |
| 108 | + agent |
| 109 | + .use(traces => { |
| 110 | + const spans = traces[0] |
| 111 | + const tags = spans[0].meta |
| 112 | + |
| 113 | + expect(tags).to.have.property('_dd.code_origin.type', 'entry') |
| 114 | + |
| 115 | + expect(tags).to.have.property('_dd.code_origin.frames.0.file', __filename) |
| 116 | + expect(tags).to.have.property('_dd.code_origin.frames.0.line', routeRegisterLine) |
| 117 | + expect(tags).to.have.property('_dd.code_origin.frames.0.column').to.match(/^\d+$/) |
| 118 | + expect(tags).to.have.property('_dd.code_origin.frames.0.method', 'v1Handler') |
| 119 | + expect(tags).to.not.have.property('_dd.code_origin.frames.0.type') |
| 120 | + |
| 121 | + expect(tags).to.not.have.property('_dd.code_origin.frames.1.file') |
| 122 | + }) |
| 123 | + .then(done) |
| 124 | + .catch(done) |
| 125 | + |
| 126 | + axios |
| 127 | + .get(`http://localhost:${port}/v1/user`) |
| 128 | + .catch(done) |
| 129 | + }) |
| 130 | + }) |
| 131 | + |
| 132 | + it('should point to route handler even if passed through a middleware', function testCase (done) { |
| 133 | + app.use(function middleware (req, res, next) { |
| 134 | + next() |
| 135 | + }) |
| 136 | + |
| 137 | + const routeRegisterLine = getNextLineNumber() |
| 138 | + app.get('/user', function userHandler (request, reply) { |
| 139 | + reply.send() |
| 140 | + }) |
| 141 | + |
| 142 | + app.listen({ host, port: 0 }, () => { |
| 143 | + const port = app.server.address().port |
| 144 | + |
| 145 | + agent |
| 146 | + .use(traces => { |
| 147 | + const spans = traces[0] |
| 148 | + const tags = spans[0].meta |
| 149 | + |
| 150 | + expect(tags).to.have.property('_dd.code_origin.type', 'entry') |
| 151 | + |
| 152 | + expect(tags).to.have.property('_dd.code_origin.frames.0.file', __filename) |
| 153 | + expect(tags).to.have.property('_dd.code_origin.frames.0.line', routeRegisterLine) |
| 154 | + expect(tags).to.have.property('_dd.code_origin.frames.0.column').to.match(/^\d+$/) |
| 155 | + expect(tags).to.have.property('_dd.code_origin.frames.0.method', 'testCase') |
| 156 | + expect(tags).to.have.property('_dd.code_origin.frames.0.type', 'Context') |
| 157 | + |
| 158 | + expect(tags).to.not.have.property('_dd.code_origin.frames.1.file') |
| 159 | + }) |
| 160 | + .then(done) |
| 161 | + .catch(done) |
| 162 | + |
| 163 | + axios |
| 164 | + .get(`http://localhost:${port}/user`) |
| 165 | + .catch(done) |
| 166 | + }) |
| 167 | + }) |
| 168 | + |
| 169 | + // TODO: In Fastify, the route is resolved before the middleware is called, so we actually can get the line |
| 170 | + // number of where the route handler is defined. However, this might not be the right choice and it might be |
| 171 | + // better to point to the middleware. |
| 172 | + it.skip('should point to middleware if middleware responds early', function testCase (done) { |
| 173 | + const middlewareRegisterLine = getNextLineNumber() |
| 174 | + app.use(function middleware (req, res, next) { |
| 175 | + res.end() |
| 176 | + }) |
| 177 | + |
| 178 | + app.get('/user', function userHandler (request, reply) { |
| 179 | + reply.send() |
| 180 | + }) |
| 181 | + |
| 182 | + app.listen({ host, port: 0 }, () => { |
| 183 | + const port = app.server.address().port |
| 184 | + |
| 185 | + agent |
| 186 | + .use(traces => { |
| 187 | + const spans = traces[0] |
| 188 | + const tags = spans[0].meta |
| 189 | + |
| 190 | + expect(tags).to.have.property('_dd.code_origin.type', 'entry') |
| 191 | + |
| 192 | + expect(tags).to.have.property('_dd.code_origin.frames.0.file', __filename) |
| 193 | + expect(tags).to.have.property('_dd.code_origin.frames.0.line', middlewareRegisterLine) |
| 194 | + expect(tags).to.have.property('_dd.code_origin.frames.0.column').to.match(/^\d+$/) |
| 195 | + expect(tags).to.have.property('_dd.code_origin.frames.0.method', 'testCase') |
| 196 | + expect(tags).to.have.property('_dd.code_origin.frames.0.type', 'Context') |
| 197 | + |
| 198 | + expect(tags).to.not.have.property('_dd.code_origin.frames.1.file') |
| 199 | + }) |
| 200 | + .then(done) |
| 201 | + .catch(done) |
| 202 | + |
| 203 | + axios |
| 204 | + .get(`http://localhost:${port}/user`) |
| 205 | + .catch(done) |
| 206 | + }) |
| 207 | + }) |
| 208 | + }) |
| 209 | + }) |
| 210 | + }) |
| 211 | + }) |
| 212 | +}) |
| 213 | + |
| 214 | +function getNextLineNumber () { |
| 215 | + return String(Number(new Error().stack.split('\n')[2].match(/:(\d+):/)[1]) + 1) |
| 216 | +} |
0 commit comments