-
Notifications
You must be signed in to change notification settings - Fork 407
Expand file tree
/
Copy pathserver.js
More file actions
49 lines (40 loc) · 1.57 KB
/
Copy pathserver.js
File metadata and controls
49 lines (40 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
'use strict'
const assert = require('node:assert/strict')
const guard = require('../startup-guard')
const clearTimeoutGuard = require('../timeout-guard')('appsec server')
const assertReplayValidated = require('./mock-native-appsec')
require('../noop-request')
// AppSec is enabled from env config.
// eslint-disable-next-line import/order -- the request and WAF mocks must load before tracer initialization
const tracer = require('../../..').init()
// Fail loudly if the tracer did not load: a broken require would otherwise
// measure a plain server and silently "pass".
assert.equal(typeof tracer.startSpan, 'function', 'tracer did not initialize')
assert.strictEqual(tracer._tracer._config.appsec.enabled, Boolean(Number(process.env.DD_APPSEC_ENABLED)))
tracer._tracer._processor._exporter = { export () {} }
// eslint-disable-next-line import/order -- the tracer must load before http to instrument it
const http = require('http')
const { port, reqs, warmup } = require('./common')
let responsesFinished = 0
function onResponseFinish () {
responsesFinished++
if (responsesFinished === warmup) {
assertReplayValidated()
guard.loopStart()
} else if (responsesFinished === reqs + warmup) {
guard.done(0.1)
server.close()
}
}
/**
* @param {import('node:http').IncomingMessage} req
* @param {import('node:http').ServerResponse} res
*/
function handleRequest (req, res) {
res.once('finish', onResponseFinish)
res.writeHead(404)
res.end('Hello, World!')
}
const server = http.createServer(handleRequest)
server.once('close', clearTimeoutGuard)
server.listen(port)