Skip to content

Commit 8975579

Browse files
committed
add is_first logic
1 parent bf3ec5d commit 8975579

File tree

5 files changed

+34
-12
lines changed

5 files changed

+34
-12
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,12 @@ describe('Endpoints collection', () => {
7272

7373
const expectedEndpoints = getExpectedEndpoints(framework)
7474
const endpointsFound = []
75+
const isFirstFlags = []
7576

7677
await agent.assertTelemetryReceived(msg => {
7778
const { payload } = msg
7879
if (payload.request_type === 'app-endpoints') {
79-
expect(payload.payload.is_first).to.be.true
80+
isFirstFlags.push(Boolean(payload.payload.is_first))
8081

8182
if (payload.payload.endpoints) {
8283
payload.payload.endpoints.forEach(endpoint => {
@@ -92,6 +93,9 @@ describe('Endpoints collection', () => {
9293
}
9394
}, 'app-endpoints', 5_000, 2)
9495

96+
const trueCount = isFirstFlags.filter(v => v === true).length
97+
expect(trueCount).to.equal(1)
98+
9599
// Check that all expected endpoints were found
96100
expectedEndpoints.forEach(expected => {
97101
const found = endpointsFound.find(e =>

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ app.route({
3131
app.all('/wildcard', async (_, reply) => reply.send('ok'))
3232

3333
// Nested routes with Router
34-
app.register(async function (fastify) {
35-
fastify.put('/nested/:id', async (_, reply) => reply.send('ok'))
34+
app.register(async function (router) {
35+
router.put('/nested/:id', async (_, reply) => reply.send('ok'))
3636
}, { prefix: '/v1' })
3737

3838
// Deeply nested routes
39-
app.register(async function (fastify) {
40-
fastify.register(async function (subFastify) {
41-
subFastify.get('/deep', async (_, reply) => reply.send('ok'))
42-
subFastify.post('/deep/:id', async (_, reply) => reply.send('ok'))
39+
app.register(async function (router) {
40+
router.register(async function (subRouter) {
41+
subRouter.get('/deep', async (_, reply) => reply.send('ok'))
42+
subRouter.post('/deep/:id', async (_, reply) => reply.send('ok'))
4343
}, { prefix: '/sub' })
4444
}, { prefix: '/api' })
4545

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ let updateRetryData
1717
*/
1818
const pendingEndpoints = new Map()
1919
let flushScheduled = false
20+
let isFirstPayload = true
2021

2122
function endpointKey (method, path) {
2223
return `${method} ${path}`
@@ -81,7 +82,7 @@ function flushAndSend () {
8182
}
8283

8384
const payloadObj = {
84-
is_first: true,
85+
is_first: isFirstPayload,
8586
endpoints: buildEndpointObjects(batchEndpoints)
8687
}
8788

@@ -99,6 +100,10 @@ function flushAndSend () {
99100

100101
sendData(config, application, host, reqType, payload, updateRetryData)
101102

103+
if (isFirstPayload) {
104+
isFirstPayload = false
105+
}
106+
102107
// If more endpoints accumulated while sending, schedule another flush.
103108
if (pendingEndpoints.size) scheduleFlush()
104109
}

packages/dd-trace/test/config.spec.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,6 +1448,7 @@ describe('Config', () => {
14481448
appsec: {
14491449
apiSecurity: {
14501450
enabled: true,
1451+
endpointCollectionEnabled: false,
14511452
endpointCollectionMessageLimit: 150
14521453
},
14531454
blockedTemplateGraphql: BLOCKED_TEMPLATE_GRAPHQL_PATH,
@@ -1555,7 +1556,7 @@ describe('Config', () => {
15551556

15561557
expect(config).to.have.nested.property('apmTracingEnabled', true)
15571558
expect(config).to.have.nested.property('appsec.apiSecurity.enabled', true)
1558-
expect(config).to.have.nested.property('appsec.apiSecurity.endpointCollectionEnabled', true)
1559+
expect(config).to.have.nested.property('appsec.apiSecurity.endpointCollectionEnabled', false)
15591560
expect(config).to.have.nested.property('appsec.apiSecurity.endpointCollectionMessageLimit', 150)
15601561
expect(config).to.have.nested.property('appsec.blockedTemplateGraphql', BLOCKED_TEMPLATE_GRAPHQL)
15611562
expect(config).to.have.nested.property('appsec.blockedTemplateHtml', BLOCKED_TEMPLATE_HTML)
@@ -1720,9 +1721,7 @@ describe('Config', () => {
17201721
expect(config).to.have.deep.property('appsec', {
17211722
apiSecurity: {
17221723
enabled: true,
1723-
sampleDelay: 30,
1724-
endpointCollectionEnabled: true,
1725-
endpointCollectionMessageLimit: 300
1724+
sampleDelay: 30
17261725
},
17271726
blockedTemplateGraphql: undefined,
17281727
blockedTemplateHtml: undefined,

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,20 @@ describe('endpoints telemetry', () => {
8686
expect(resources).to.include('POST /api')
8787
})
8888

89+
it('should set is_first=true only for the first payload', () => {
90+
fastifyRouteCh.publish({ routeOptions: { method: 'GET', path: '/one' } })
91+
fastifyRouteCh.publish({ routeOptions: { method: 'POST', path: '/two' } })
92+
93+
scheduledCallbacks.forEach(cb => cb())
94+
95+
expect(sendData.callCount).to.equal(2)
96+
const firstPayload = sendData.firstCall.args[4]
97+
const secondPayload = sendData.secondCall.args[4]
98+
99+
expect(firstPayload).to.have.property('is_first', true)
100+
expect(Boolean(secondPayload.is_first)).to.equal(false)
101+
})
102+
89103
it('should record fastify wildcard when all methods provided', () => {
90104
fastifyRouteCh.publish({
91105
routeOptions: {

0 commit comments

Comments
 (0)