Skip to content

Commit 411eb57

Browse files
authored
fix(aap): Fix endpoint operation name for detected endpoints (#7217)
1 parent 27353de commit 411eb57

3 files changed

Lines changed: 49 additions & 12 deletions

File tree

integration-tests/appsec/endpoints-collection.spec.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,15 @@ describe('Endpoints collection', () => {
169169
assert.strictEqual(trueCount, 1)
170170

171171
// Check that all expected endpoints were found
172+
const expectedOperationName = `${framework}.request`
172173
expectedEndpoints.forEach(expected => {
173174
const found = endpointsFound.find(e =>
174175
e.method === expected.method && e.path === expected.path
175176
)
176177

177178
assert.ok(found)
178179
assert.strictEqual(found.type, 'REST')
179-
assert.strictEqual(found.operation_name, 'http.request')
180+
assert.strictEqual(found.operation_name, expectedOperationName)
180181
assert.strictEqual(found.resource_name, `${expected.method} ${expected.path}`)
181182
})
182183

packages/dd-trace/src/telemetry/endpoints.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ let updateRetryData
1515

1616
/**
1717
* Keep track of endpoints that still need to be sent.
18-
* Map key is `${METHOD} ${PATH}`, value is { method, path }
18+
* Map key is `${METHOD} ${PATH}`, value is { method, path, operationName }
1919
*/
2020
const pendingEndpoints = new Map()
2121
const wildcardEndpoints = new Set()
@@ -32,11 +32,11 @@ function scheduleFlush () {
3232
setImmediate(flushAndSend).unref()
3333
}
3434

35-
function recordEndpoint (method, path) {
35+
function recordEndpoint (method, path, operationName) {
3636
const key = endpointKey(method, path)
3737
if (pendingEndpoints.has(key)) return
3838

39-
pendingEndpoints.set(key, { method: method.toUpperCase(), path })
39+
pendingEndpoints.set(key, { method: method.toUpperCase(), path, operationName })
4040
scheduleFlush()
4141
}
4242

@@ -46,7 +46,7 @@ function onFastifyRoute (routeData) {
4646

4747
const methods = Array.isArray(routeOptions.method) ? routeOptions.method : [routeOptions.method]
4848
for (const method of methods) {
49-
recordEndpoint(method, routeOptions.path)
49+
recordEndpoint(method, routeOptions.path, 'fastify.request')
5050
}
5151
}
5252

@@ -56,7 +56,7 @@ function onExpressRoute ({ method, path }) {
5656
// If wildcard already recorded for this path, skip specific methods
5757
if (wildcardEndpoints.has(path)) return
5858

59-
recordEndpoint(method, path)
59+
recordEndpoint(method, path, 'express.request')
6060

6161
// If this is a wildcard event, record it and mark path as wildcarded
6262
if (method === '*') {
@@ -66,17 +66,17 @@ function onExpressRoute ({ method, path }) {
6666

6767
// Express automatically adds HEAD support for GET routes
6868
if (method.toUpperCase() === 'GET') {
69-
recordEndpoint('HEAD', path)
69+
recordEndpoint('HEAD', path, 'express.request')
7070
}
7171
}
7272

7373
function buildEndpointObjects (endpoints) {
74-
return endpoints.map(({ method, path }) => {
74+
return endpoints.map(({ method, path, operationName }) => {
7575
return {
7676
type: 'REST',
7777
method,
7878
path,
79-
operation_name: 'http.request',
79+
operation_name: operationName,
8080
resource_name: endpointKey(method, path)
8181
}
8282
})

packages/dd-trace/test/telemetry/endpoints.spec.js

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,21 +107,21 @@ describe('endpoints telemetry', () => {
107107
type: 'REST',
108108
method: 'GET',
109109
path: '/api',
110-
operation_name: 'http.request',
110+
operation_name: 'fastify.request',
111111
resource_name: 'GET /api'
112112
},
113113
{
114114
type: 'REST',
115115
method: 'POST',
116116
path: '/api',
117-
operation_name: 'http.request',
117+
operation_name: 'fastify.request',
118118
resource_name: 'POST /api'
119119
},
120120
{
121121
type: 'REST',
122122
method: 'PUT',
123123
path: '/api',
124-
operation_name: 'http.request',
124+
operation_name: 'fastify.request',
125125
resource_name: 'PUT /api'
126126
}
127127
])
@@ -170,6 +170,42 @@ describe('endpoints telemetry', () => {
170170
assert.deepStrictEqual(resources, ['GET /test', 'HEAD /test'])
171171
})
172172

173+
it('should use express.request as operation_name for express routes', () => {
174+
expressRouteCh.publish({ method: 'POST', path: '/express-test' })
175+
176+
scheduledCallbacks.forEach(cb => cb())
177+
178+
sinon.assert.calledOnce(sendData)
179+
const payload = sendData.firstCall.args[4]
180+
assertObjectContains(payload.endpoints, [
181+
{
182+
type: 'REST',
183+
method: 'POST',
184+
path: '/express-test',
185+
operation_name: 'express.request',
186+
resource_name: 'POST /express-test'
187+
}
188+
])
189+
})
190+
191+
it('should use express.request as operation_name for router routes', () => {
192+
routerRouteCh.publish({ method: 'DELETE', path: '/router-test' })
193+
194+
scheduledCallbacks.forEach(cb => cb())
195+
196+
sinon.assert.calledOnce(sendData)
197+
const payload = sendData.firstCall.args[4]
198+
assertObjectContains(payload.endpoints, [
199+
{
200+
type: 'REST',
201+
method: 'DELETE',
202+
path: '/router-test',
203+
operation_name: 'express.request',
204+
resource_name: 'DELETE /router-test'
205+
}
206+
])
207+
})
208+
173209
it('should record express wildcard and ignore subsequent specific methods for same path', () => {
174210
expressRouteCh.publish({ method: '*', path: '/all' })
175211
expressRouteCh.publish({ method: 'GET', path: '/all' })

0 commit comments

Comments
 (0)