Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unidici tests #946

Merged
merged 3 commits into from
Oct 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion test/versioned/undici/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@
"requests.tap.js"
]
}
]
],
"engines": {
"node": ">=12"
}
}
99 changes: 98 additions & 1 deletion test/versioned/undici/requests.tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const tap = require('tap')
const { DESTINATIONS } = require('../../../lib/config/attribute-filter')
const helper = require('../../lib/agent_helper')
const metrics = require('../../lib/metrics_helper')
const http = require('http')
const https = require('https')

tap.test('Undici request tests', (t) => {
t.autoend()
Expand Down Expand Up @@ -62,6 +64,67 @@ tap.test('Undici request tests', (t) => {
})
})

t.test('should add HTTP port to segment name when provided', (t) => {
const server = http.createServer((req, res) => {
req.resume()
res.end('http')
})

t.teardown(() => {
server.close()
})

server.listen(0)

helper.runInTransaction(agent, async (transaction) => {
const { port } = server.address()
await undici.request(`http://localhost:${port}`)

metrics.assertSegments(transaction.trace.root, [`External/localhost:${port}/`], {
exact: false
})

transaction.end()
t.end()
})
})

t.test('should add HTTPS port to segment name when provided', async (t) => {
const [key, cert, ca] = await helper.withSSL()
const server = https.createServer({ key, cert }, (req, res) => {
res.write('SSL response')
res.end()
})

t.teardown(() => {
server.close()
})

server.listen(0)

await helper.runInTransaction(agent, async (transaction) => {
const { port } = server.address()

const client = new undici.Client(`https://localhost:${port}`, {
tls: {
ca
}
})

t.teardown(() => {
client.close()
})

await client.request({ path: '/', method: 'GET' })

metrics.assertSegments(transaction.trace.root, [`External/localhost:${port}/`], {
exact: false
})

transaction.end()
})
})

t.test('should add attributes to external segment', (t) => {
helper.runInTransaction(agent, async (tx) => {
const { statusCode } = await undici.request('https://httpbin.org', {
Expand Down Expand Up @@ -154,6 +217,41 @@ tap.test('Undici request tests', (t) => {
})
})

t.test('segments should end on error', (t) => {
const socketEndServer = http.createServer(function badHandler(req) {
req.socket.end()
})

t.teardown(() => {
socketEndServer.close()
})

socketEndServer.listen(0)

helper.runInTransaction(agent, async (transaction) => {
const { port } = socketEndServer.address()
const req = undici.request(`http://localhost:${port}`)

try {
await req
} catch (error) {
metrics.assertSegments(transaction.trace.root, [`External/localhost:${port}/`], {
exact: false
})

const segments = transaction.trace.root.children
const segment = segments[segments.length - 1]

t.ok(segment.timer.start, 'should have started')
t.ok(segment.timer.hasEnd(), 'should have ended')

transaction.end()

t.end()
}
})
})

t.test('400 status', (t) => {
helper.runInTransaction(agent, async (tx) => {
const { statusCode } = await undici.request('https://httpbin.org', {
Expand Down Expand Up @@ -201,7 +299,6 @@ tap.test('Undici request tests', (t) => {
})

t.test('pipeline', (t) => {
// eslint-disable-next-line node/no-unsupported-features/node-builtins
const { pipeline, PassThrough, Readable, Writable } = require('stream')
helper.runInTransaction(agent, async (tx) => {
pipeline(
Expand Down