Skip to content

Commit 5c24670

Browse files
authoredFeb 12, 2021
Add Zipkin trace capturing with output to JSON. (#22106)
@timneutkens this adds a trace capturing script alongside the other tracing sundry. This will be utilized by the performance automation. Additionally, its nice to have an option other than running the ZipKin JAR to get at the raw data.
1 parent 5f41abd commit 5c24670

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed
 

‎bench/capture-trace.js

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
const http = require('http')
2+
const fs = require('fs')
3+
4+
const PORT = 9411
5+
const HOST = '0.0.0.0'
6+
7+
const traces = []
8+
9+
const onReady = () => console.log(`Listening on http://${HOST}:${PORT}`)
10+
const onRequest = async (req, res) => {
11+
if (
12+
req.method !== 'POST' ||
13+
req.url !== '/api/v2/spans' ||
14+
(req.headers && req.headers['content-type']) !== 'application/json'
15+
) {
16+
res.writeHead(200)
17+
return res.end()
18+
}
19+
20+
try {
21+
const body = JSON.parse(await getBody(req))
22+
for (const traceEvent of body) {
23+
traces.push(traceEvent)
24+
}
25+
res.writeHead(200)
26+
} catch (err) {
27+
console.warn(err)
28+
res.writeHead(500)
29+
}
30+
31+
res.end()
32+
}
33+
34+
const getBody = (req) =>
35+
new Promise((resolve, reject) => {
36+
let data = ''
37+
req.on('data', (chunk) => {
38+
data += chunk
39+
})
40+
req.on('end', () => {
41+
if (!req.complete) {
42+
return reject('Connection terminated before body was received.')
43+
}
44+
resolve(data)
45+
})
46+
req.on('aborted', () => reject('Connection aborted.'))
47+
req.on('error', () => reject('Connection error.'))
48+
})
49+
50+
const main = () => {
51+
const args = process.argv.slice(2)
52+
const outFile = args[0] || `./trace-${Date.now()}.json`
53+
54+
process.on('SIGINT', () => {
55+
console.log(`\nSaving to ${outFile}...`)
56+
fs.writeFileSync(outFile, JSON.stringify(traces, null, 2))
57+
process.exit()
58+
})
59+
60+
const server = http.createServer(onRequest)
61+
server.listen(PORT, HOST, onReady)
62+
}
63+
64+
if (require.main === module) {
65+
main()
66+
}

0 commit comments

Comments
 (0)
Please sign in to comment.