Skip to content

expand endpoint wildcard on fastify #6266

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

Draft
wants to merge 1 commit into
base: ishabi/endpoints-collection-fastify
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
13 changes: 10 additions & 3 deletions integration-tests/appsec/endpoints-collection.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,15 @@ describe('Endpoints collection', () => {
// Using app.route()
{ method: 'POST', path: '/multi-method' },

// Wildcard route
{ method: '*', path: '/wildcard' },
// Wildcard route expanded (fastify does not support CONNECT)
{ method: 'GET', path: '/wildcard' },
{ method: 'POST', path: '/wildcard' },
{ method: 'PUT', path: '/wildcard' },
{ method: 'DELETE', path: '/wildcard' },
{ method: 'HEAD', path: '/wildcard' },
{ method: 'PATCH', path: '/wildcard' },
{ method: 'OPTIONS', path: '/wildcard' },
{ method: 'TRACE', path: '/wildcard' },

// Nested routes with Router
{ method: 'PUT', path: '/v1/nested/:id' },
Expand Down Expand Up @@ -91,7 +98,7 @@ describe('Endpoints collection', () => {
})
}
}
}, 'app-endpoints', 5_000, 2)
}, 'app-endpoints', 5_000, 3)

const trueCount = isFirstFlags.filter(v => v === true).length
expect(trueCount).to.equal(1)
Expand Down
9 changes: 2 additions & 7 deletions packages/dd-trace/src/telemetry/endpoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,8 @@ function onFastifyRoute (routeData) {

const methods = Array.isArray(routeOptions.method) ? routeOptions.method : [routeOptions.method]

// Check if this is a wildcard route fastify.all()
if (methods.length === 8) {
recordEndpoint('*', routeOptions.path)
} else {
for (const method of methods) {
recordEndpoint(method, routeOptions.path)
}
for (const method of methods) {
recordEndpoint(method, routeOptions.path)
}
}

Expand Down
13 changes: 11 additions & 2 deletions packages/dd-trace/test/telemetry/endpoints.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ describe('endpoints telemetry', () => {
expect(Boolean(secondPayload.is_first)).to.equal(false)
})

it('should record fastify wildcard when all methods provided', () => {
it('should record all methods when fastify.all() is used', () => {
fastifyRouteCh.publish({
routeOptions: {
method: ['GET', 'POST', 'PUT', 'DELETE', 'HEAD', 'PATCH', 'OPTIONS', 'TRACE'],
Expand All @@ -114,7 +114,16 @@ describe('endpoints telemetry', () => {
expect(sendData).to.have.been.calledOnce
const payload = sendData.firstCall.args[4]
const resources = payload.endpoints.map(e => e.resource_name)
expect(resources).to.deep.equal(['* /all'])
expect(resources).to.have.members([
'GET /all',
'POST /all',
'PUT /all',
'DELETE /all',
'HEAD /all',
'PATCH /all',
'OPTIONS /all',
'TRACE /all'
])
})
})

Expand Down
Loading