Skip to content

Commit 170793c

Browse files
rochdevtlhunter
authored andcommitted
add benchmark and incomplete implementation for event based tracing
1 parent c84e190 commit 170793c

File tree

12 files changed

+790
-0
lines changed

12 files changed

+790
-0
lines changed

benchmark/sirun/plugin-koa/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This creates 100,000 HTTP requests to a Koa server.
2+
3+
The variants are with the tracer, without it, and with a new internal tracer.

benchmark/sirun/plugin-koa/agent.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict'
2+
3+
const express = require('express')
4+
const bodyParser = require('body-parser')
5+
6+
const app = express()
7+
8+
let requests = 0
9+
let bytes = 0
10+
11+
app.use(bodyParser.raw({ limit: '50mb', type: () => true }))
12+
app.use('*', (req, res) => {
13+
requests++
14+
bytes += req.body.length
15+
16+
console.log(`Requests: ${requests}`) // eslint-disable-line no-console
17+
console.log(`Bytes: ${bytes}`) // eslint-disable-line no-console
18+
19+
res.status(200).send()
20+
})
21+
22+
app.listen(8126)
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
'use strict'
2+
3+
const http = require('http')
4+
const https = require('https')
5+
const { dockerId, storage } = require('../../../../packages/datadog-core')
6+
const tracerVersion = require('../../../../package.json').version
7+
8+
const httpAgent = new http.Agent({ keepAlive: true, maxSockets: 1 })
9+
const httpsAgent = new https.Agent({ keepAlive: true, maxSockets: 1 })
10+
11+
const DD_TRACE_AGENT_URL = process.env.DD_TRACE_AGENT_URL || process.env.DD_TRACE_URL
12+
13+
class Client {
14+
request (options, done) {
15+
if (options.count === 0) return
16+
17+
const url = new URL(DD_TRACE_AGENT_URL || 'http://127.0.0.1:8126')
18+
const isSecure = url.protocol === 'https:'
19+
const isUnix = url.protocol === 'unix:'
20+
const client = isSecure ? https : http
21+
const agent = isSecure ? httpsAgent : httpAgent
22+
const data = options.data
23+
const timeout = 2000
24+
const httpOptions = {
25+
agent,
26+
protocol: url.protocol,
27+
hostname: url.hostname,
28+
port: url.port,
29+
socketPath: isUnix && url.pathname,
30+
path: options.path,
31+
method: 'PUT',
32+
headers: {
33+
'Content-Length': String(data.length),
34+
'Content-Type': 'application/msgpack',
35+
'Datadog-Container-ID': dockerId || '',
36+
'Datadog-Meta-Lang': 'nodejs',
37+
'Datadog-Meta-Lang-Version': process.version,
38+
'Datadog-Meta-Lang-Interpreter': process.jsEngine || 'v8',
39+
'Datadog-Meta-Tracer-Version': tracerVersion,
40+
'X-Datadog-Trace-Count': String(options.count)
41+
},
42+
timeout
43+
}
44+
45+
const onResponse = res => {
46+
let data = ''
47+
48+
res.setTimeout(timeout)
49+
res.on('data', chunk => {
50+
data += chunk
51+
})
52+
res.on('end', () => {
53+
if (res.statusCode >= 200 && res.statusCode <= 299) {
54+
try {
55+
const response = data
56+
done(null, response)
57+
} catch (e) {
58+
done(e)
59+
}
60+
} else {
61+
const statusCode = res.statusCode
62+
const statusText = http.STATUS_CODES[res.statusCode]
63+
const error = new Error(`Error from the agent: ${statusCode} ${statusText}`)
64+
65+
error.status = statusCode
66+
67+
done(error, null)
68+
}
69+
})
70+
}
71+
72+
const makeRequest = onError => {
73+
const store = storage.getStore()
74+
75+
storage.enterWith({ noop: true })
76+
77+
const req = client.request(httpOptions, onResponse)
78+
79+
req.on('error', onError)
80+
81+
req.setTimeout(timeout, req.abort)
82+
req.write(data)
83+
req.end()
84+
85+
storage.enterWith(store)
86+
}
87+
88+
makeRequest(() => makeRequest(done)) // retry once on error
89+
}
90+
}
91+
92+
module.exports = { Client }

0 commit comments

Comments
 (0)