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

Add Zipkin trace capturing with output to JSON. #22106

Merged
merged 1 commit into from
Feb 12, 2021
Merged
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
66 changes: 66 additions & 0 deletions bench/capture-trace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const http = require('http')
const fs = require('fs')

const PORT = 9411
const HOST = '0.0.0.0'

const traces = []

const onReady = () => console.log(`Listening on http://${HOST}:${PORT}`)
const onRequest = async (req, res) => {
if (
req.method !== 'POST' ||
req.url !== '/api/v2/spans' ||
(req.headers && req.headers['content-type']) !== 'application/json'
) {
res.writeHead(200)
return res.end()
}

try {
const body = JSON.parse(await getBody(req))
for (const traceEvent of body) {
traces.push(traceEvent)
}
res.writeHead(200)
} catch (err) {
console.warn(err)
res.writeHead(500)
}

res.end()
}

const getBody = (req) =>
new Promise((resolve, reject) => {
let data = ''
req.on('data', (chunk) => {
data += chunk
})
req.on('end', () => {
if (!req.complete) {
return reject('Connection terminated before body was received.')
}
resolve(data)
})
req.on('aborted', () => reject('Connection aborted.'))
req.on('error', () => reject('Connection error.'))
})

const main = () => {
const args = process.argv.slice(2)
const outFile = args[0] || `./trace-${Date.now()}.json`

process.on('SIGINT', () => {
console.log(`\nSaving to ${outFile}...`)
fs.writeFileSync(outFile, JSON.stringify(traces, null, 2))
process.exit()
})

const server = http.createServer(onRequest)
server.listen(PORT, HOST, onReady)
}

if (require.main === module) {
main()
}