|
| 1 | +/* |
| 2 | + * Copyright 2020 New Relic Corporation. All rights reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +'use strict' |
| 7 | + |
| 8 | +const tap = require('tap') |
| 9 | + |
| 10 | +const helper = require('../../../lib/agent_helper') |
| 11 | +const { assertMetrics } = require('../../../lib/metrics_helper') |
| 12 | +const { runTest } = require('./common') |
| 13 | + |
| 14 | +const simulateAsyncWork = async () => { |
| 15 | + const delay = Math.floor(Math.random() * 100) |
| 16 | + await new Promise((resolve) => setTimeout(resolve, delay)) |
| 17 | + return delay |
| 18 | +} |
| 19 | + |
| 20 | +tap.test('Restify with async handlers should work the same as with sync', (t) => { |
| 21 | + t.autoend() |
| 22 | + |
| 23 | + let agent = null |
| 24 | + let restify = null |
| 25 | + let server = null |
| 26 | + |
| 27 | + t.beforeEach(() => { |
| 28 | + agent = helper.instrumentMockedAgent() |
| 29 | + |
| 30 | + restify = require('restify') |
| 31 | + server = restify.createServer() |
| 32 | + }) |
| 33 | + |
| 34 | + t.afterEach(() => { |
| 35 | + return new Promise((resolve) => { |
| 36 | + helper.unloadAgent(agent) |
| 37 | + if (server) { |
| 38 | + server.close(resolve) |
| 39 | + } else { |
| 40 | + resolve() |
| 41 | + } |
| 42 | + }) |
| 43 | + }) |
| 44 | + |
| 45 | + /* very similar synchronous tests are in transaction-naming */ |
| 46 | + |
| 47 | + t.test('transaction name for single async route', (t) => { |
| 48 | + t.plan(1) |
| 49 | + |
| 50 | + server.get('/path1', async (req, res) => { |
| 51 | + res.send() |
| 52 | + }) |
| 53 | + |
| 54 | + runTest({ agent, server, t, endpoint: '/path1', expectedName: 'GET//path1' }) |
| 55 | + }) |
| 56 | + |
| 57 | + t.test('transaction name for async route with sync middleware', (t) => { |
| 58 | + t.plan(1) |
| 59 | + |
| 60 | + server.use((req, res, next) => { |
| 61 | + next() |
| 62 | + }) |
| 63 | + server.get('/path1', async (req, res) => { |
| 64 | + res.send() |
| 65 | + }) |
| 66 | + |
| 67 | + runTest({ agent, server, t, endpoint: '/path1', expectedName: 'GET//path1' }) |
| 68 | + }) |
| 69 | + |
| 70 | + t.test('transaction name for async route with async middleware', (t) => { |
| 71 | + t.plan(1) |
| 72 | + |
| 73 | + server.use(async (req) => { |
| 74 | + req.test = await simulateAsyncWork() |
| 75 | + }) |
| 76 | + server.get('/path1', async (req, res) => { |
| 77 | + res.send() |
| 78 | + }) |
| 79 | + |
| 80 | + runTest({ agent, server, t, endpoint: '/path1', expectedName: 'GET//path1' }) |
| 81 | + }) |
| 82 | + |
| 83 | + t.test('transaction name for async route with multiple async middleware', (t) => { |
| 84 | + t.plan(4) |
| 85 | + |
| 86 | + server.use(async (req) => { |
| 87 | + t.pass('should enter first `use` middleware') |
| 88 | + req.test = await simulateAsyncWork() |
| 89 | + }) |
| 90 | + // eslint-disable-next-line no-unused-vars |
| 91 | + server.use(async (req) => { |
| 92 | + t.pass('should enter second `use` middleware') |
| 93 | + req.test2 = await simulateAsyncWork() |
| 94 | + }) |
| 95 | + server.get('/path1', async (req, res) => { |
| 96 | + t.pass('should enter route handler') |
| 97 | + res.send() |
| 98 | + }) |
| 99 | + |
| 100 | + runTest({ agent, server, t, endpoint: '/path1', expectedName: 'GET//path1' }) |
| 101 | + }) |
| 102 | +}) |
| 103 | + |
| 104 | +tap.test('Restify metrics for async handlers', (t) => { |
| 105 | + t.autoend() |
| 106 | + |
| 107 | + let agent = null |
| 108 | + let restify = null |
| 109 | + t.beforeEach(() => { |
| 110 | + agent = helper.instrumentMockedAgent() |
| 111 | + |
| 112 | + restify = require('restify') |
| 113 | + }) |
| 114 | + |
| 115 | + t.afterEach(() => { |
| 116 | + helper.unloadAgent(agent) |
| 117 | + }) |
| 118 | + |
| 119 | + t.test('should generate middleware metrics for async handlers', (t) => { |
| 120 | + // Metrics for this transaction with the right name. |
| 121 | + const expectedMiddlewareMetrics = [ |
| 122 | + [{ name: 'WebTransaction/Restify/GET//foo/:bar' }], |
| 123 | + [{ name: 'WebTransactionTotalTime/Restify/GET//foo/:bar' }], |
| 124 | + [{ name: 'Apdex/Restify/GET//foo/:bar' }], |
| 125 | + |
| 126 | + // Unscoped middleware metrics. |
| 127 | + [{ name: 'Nodejs/Middleware/Restify/middleware//' }], |
| 128 | + [{ name: 'Nodejs/Middleware/Restify/middleware2//' }], |
| 129 | + [{ name: 'Nodejs/Middleware/Restify/handler//foo/:bar' }], |
| 130 | + |
| 131 | + // Scoped middleware metrics. |
| 132 | + [ |
| 133 | + { |
| 134 | + name: 'Nodejs/Middleware/Restify/middleware//', |
| 135 | + scope: 'WebTransaction/Restify/GET//foo/:bar' |
| 136 | + } |
| 137 | + ], |
| 138 | + [ |
| 139 | + { |
| 140 | + name: 'Nodejs/Middleware/Restify/middleware2//', |
| 141 | + scope: 'WebTransaction/Restify/GET//foo/:bar' |
| 142 | + } |
| 143 | + ], |
| 144 | + [ |
| 145 | + { |
| 146 | + name: 'Nodejs/Middleware/Restify/handler//foo/:bar', |
| 147 | + scope: 'WebTransaction/Restify/GET//foo/:bar' |
| 148 | + } |
| 149 | + ] |
| 150 | + ] |
| 151 | + |
| 152 | + const server = restify.createServer() |
| 153 | + t.teardown(() => server.close()) |
| 154 | + |
| 155 | + server.use(async function middleware() { |
| 156 | + t.ok(agent.getTransaction(), 'should be in transaction context') |
| 157 | + }) |
| 158 | + |
| 159 | + server.use(async function middleware2() { |
| 160 | + t.ok(agent.getTransaction(), 'should be in transaction context') |
| 161 | + }) |
| 162 | + |
| 163 | + server.get('/foo/:bar', async function handler(req, res) { |
| 164 | + t.ok(agent.getTransaction(), 'should be in transaction context') |
| 165 | + res.send({ message: 'done' }) |
| 166 | + }) |
| 167 | + |
| 168 | + server.listen(0, function () { |
| 169 | + const port = server.address().port |
| 170 | + const url = `http://localhost:${port}/foo/bar` |
| 171 | + |
| 172 | + helper.makeGetRequest(url, function (error) { |
| 173 | + t.error(error) |
| 174 | + |
| 175 | + assertMetrics(agent.metrics, expectedMiddlewareMetrics, false, false) |
| 176 | + t.end() |
| 177 | + }) |
| 178 | + }) |
| 179 | + }) |
| 180 | +}) |
0 commit comments