Skip to content

Commit be3fa9e

Browse files
authored
Enable resource renaming tests for Node.js (#5821)
1 parent 0009703 commit be3fa9e

File tree

6 files changed

+87
-22
lines changed

6 files changed

+87
-22
lines changed

manifests/nodejs.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ refs:
7777
- &ref_5_77_0 '>=5.77.0'
7878
- &ref_5_79_0 '>=5.79.0'
7979
- &ref_5_80_0 '>=5.80.0'
80+
- &ref_5_81_0 '>=5.81.0'
8081
tests/:
8182
apm_tracing_e2e/:
8283
test_otel.py:
@@ -1522,7 +1523,13 @@ tests/:
15221523
test_profiling.py:
15231524
Test_Profile: *ref_5_16_0 #actual version unknown
15241525
test_protobuf.py: missing_feature
1525-
test_resource_renaming.py: missing_feature
1526+
test_resource_renaming.py:
1527+
Test_Resource_Renaming_HTTP_Endpoint_Tag:
1528+
'*': *ref_5_81_0
1529+
nextjs: missing_feature # Next.js have file based routing, resource renaming is supported if we are not able to get route from insturmantation that we can't reprocude it.
1530+
Test_Resource_Renaming_Stats_Aggregation_Keys:
1531+
'*': *ref_5_81_0
1532+
nextjs: missing_feature
15261533
test_sampling_rates.py:
15271534
Test_SampleRateFunction: *ref_5_54_0
15281535
Test_SamplingDecisionAdded: *ref_5_17_0 # real version unknown

tests/test_resource_renaming.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from utils import bug, scenarios, weblog, interfaces, features
1+
from utils import bug, scenarios, weblog, interfaces, features, missing_feature, context
22
from utils._weblog import HttpResponse
33

44

@@ -36,15 +36,22 @@ def test_http_endpoint_basic(self):
3636
assert get_endpoint_tag(self.r_param_str) == "/resource_renaming/files/{param:str}"
3737
assert get_endpoint_tag(self.r_param_str_2) == "/resource_renaming/files/{param:str}"
3838

39+
def setup_http_endpoint_root(self):
40+
"""Setup requests for root endpoint testing"""
41+
self.r_root = weblog.get("/")
42+
43+
@missing_feature(context.weblog_variant == "fastify", reason="Fasitfy root route is always '/' not empty string")
44+
def test_http_endpoint_root(self):
45+
"""Test that root endpoint is handled correctly"""
46+
assert get_endpoint_tag(self.r_root) == "/"
47+
3948
def setup_http_endpoint_edge_cases(self):
4049
"""Setup requests for edge case testing"""
41-
self.r_root = weblog.get("/")
4250
self.r_long_path = weblog.get("/resource_renaming/a/b/c/d/e/f/g/h/i/j/k/l/m")
4351
self.r_empty_segments = weblog.get("/resource_renaming//double//slash")
4452

4553
def test_http_endpoint_edge_cases(self):
4654
"""Test that edge cases are handled correctly"""
47-
assert get_endpoint_tag(self.r_root) == "/"
4855
assert get_endpoint_tag(self.r_long_path) == "/resource_renaming/a/b/c/d/e/f/g"
4956
assert get_endpoint_tag(self.r_empty_segments) == "/resource_renaming/double/slash"
5057

utils/_context/_scenarios/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ class _Scenarios:
178178
"DD_APPSEC_RULES": "/appsec_blocking_rule.json",
179179
"DD_TRACE_RESOURCE_RENAMING_ALWAYS_SIMPLIFIED_ENDPOINT": "true",
180180
"DD_TRACE_COMPUTE_STATS": "true",
181+
"DD_TRACE_STATS_COMPUTATION_ENABLED": "true",
181182
},
182183
weblog_volumes={"./tests/appsec/blocking_rule.json": {"bind": "/appsec_blocking_rule.json", "mode": "ro"}},
183184
doc="Misc tests for appsec blocking",
@@ -628,6 +629,7 @@ class _Scenarios:
628629
"DD_LOGS_INJECTION": "true",
629630
"DD_TRACE_RESOURCE_RENAMING_ENABLED": "true",
630631
"DD_TRACE_RESOURCE_RENAMING_ALWAYS_SIMPLIFIED_ENDPOINT": "true",
632+
"DD_TRACE_STATS_COMPUTATION_ENABLED": "true",
631633
"DD_TRACE_COMPUTE_STATS": "true",
632634
},
633635
appsec_enabled=False,

utils/build/docker/nodejs/express/app.js

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const api = require('@opentelemetry/api')
2525
const iast = require('./iast')
2626
const dsm = require('./dsm')
2727
const di = require('./debugger')
28+
const { OpenFeature } = require('@openfeature/server-sdk')
2829

2930
const { spawnSync } = require('child_process')
3031

@@ -666,18 +667,6 @@ app.get('/add_event', (req, res) => {
666667

667668
require('./rasp')(app)
668669

669-
const startServer = () => {
670-
return new Promise((resolve) => {
671-
app.listen(7777, '0.0.0.0', () => {
672-
tracer.trace('init.service', () => {})
673-
console.log('listening')
674-
resolve()
675-
})
676-
})
677-
}
678-
679-
// apollo-server does not support Express 5 yet https://github.com/apollographql/apollo-server/issues/7928
680-
const { OpenFeature } = require('@openfeature/server-sdk')
681670
let openFeatureClient = null
682671

683672
// Initialize OpenFeature provider if FFE is enabled
@@ -724,12 +713,33 @@ app.post('/ffe', async (req, res) => {
724713
}
725714
})
726715

716+
// apollo-server does not support Express 5 yet https://github.com/apollographql/apollo-server/issues/7928
727717
const initGraphQL = () => {
728718
return graphQLEnabled
729719
? require('./graphql')(app)
730720
: Promise.resolve()
731721
}
732722

723+
const startServer = () => {
724+
return new Promise((resolve) => {
725+
const server = http.createServer((req, res) => {
726+
if (req.url.startsWith('/resource_renaming')) {
727+
res.writeHead(200)
728+
res.end('OK')
729+
} else {
730+
// Everything else goes to Express
731+
app(req, res)
732+
}
733+
})
734+
735+
server.listen(7777, '0.0.0.0', () => {
736+
tracer.trace('init.service', () => {})
737+
console.log('listening')
738+
resolve()
739+
})
740+
})
741+
}
742+
733743
initGraphQL()
734744
.then(startServer)
735745
.catch(error => {

utils/build/docker/nodejs/express4-typescript/app.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -412,9 +412,29 @@ app.get('/set_cookie', (req: Request, res: Response) => {
412412

413413
require('./rasp')(app)
414414

415-
require('./graphql')(app).then(() => {
416-
app.listen(7777, '0.0.0.0', () => {
417-
tracer.trace('init.service', () => {})
418-
console.log('listening')
415+
const startServer = () => {
416+
return new Promise((resolve) => {
417+
const server = http.createServer((req: http.IncomingMessage, res: http.ServerResponse) => {
418+
if (req.url?.startsWith('/resource_renaming')) {
419+
res.writeHead(200)
420+
res.end('OK')
421+
} else {
422+
// Everything else goes to Express
423+
app(req, res)
424+
}
425+
})
426+
427+
server.listen(7777, '0.0.0.0', () => {
428+
tracer.trace('init.service', () => {})
429+
console.log('listening')
430+
resolve(true)
431+
})
432+
})
433+
}
434+
435+
require('./graphql')(app)
436+
.then(startServer)
437+
.catch((error: Error) => {
438+
console.error('Failed to start server:', error)
439+
process.exit(1)
419440
})
420-
})

utils/build/docker/nodejs/fastify/app.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,31 @@ const tracer = require('dd-trace').init({
66
})
77

88
const { promisify } = require('util')
9-
const fastify = require('fastify')({ logger: true })
109
const axios = require('axios')
1110
const crypto = require('crypto')
1211
const http = require('http')
1312
const winston = require('winston')
1413

14+
const server = http.createServer()
15+
16+
const fastify = require('fastify')({
17+
logger: true,
18+
serverFactory: (handler) => {
19+
server.on('request', (req, res) => {
20+
if (req.url.startsWith('/resource_renaming')) {
21+
// Handle resource renaming with HTTP server directly
22+
res.writeHead(200)
23+
res.end('OK')
24+
} else {
25+
// Everything else goes to Fastify
26+
handler(req, res)
27+
}
28+
})
29+
30+
return server
31+
}
32+
})
33+
1534
const iast = require('./iast')
1635
const dsm = require('./dsm')
1736
const di = require('./debugger')

0 commit comments

Comments
 (0)