Skip to content

Commit 63287e2

Browse files
Add stats
1 parent fdfca40 commit 63287e2

File tree

3 files changed

+86
-3
lines changed

3 files changed

+86
-3
lines changed

packages/destination-actions/src/destinations/webhook-audiences/index.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,18 @@ const destination: AudienceDestinationDefinition<Settings, AudienceSettings> = {
139139
settings
140140
})
141141
},
142-
performBatch: (request, { payload, settings, audienceSettings }) => {
142+
performBatch: (request, { payload, settings, audienceSettings, statsContext }) => {
143143
const extras = parseExtraSettingsJson(audienceSettings?.extras)
144144

145+
if (statsContext) {
146+
const { statsClient, tags } = statsContext
147+
const set = new Set()
148+
for (const p of payload) {
149+
set.add(`${p.url} ${p.method} ${JSON.stringify(p.headers)}`)
150+
}
151+
statsClient?.incr('webhook.configurable_batch_keys.unique_keys', set.size, tags)
152+
}
153+
145154
// Call the same performBatch function from the regular webhook destination
146155
// and add in our extraSettings
147156
return send.performBatch!(request, {

packages/destination-actions/src/destinations/webhook/__test__/webhook.test.ts

+64
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,70 @@ export const baseWebhookTests = (def: DestinationDefinition<any>) => {
177177
})
178178
).rejects.toThrow(PayloadValidationError)
179179
})
180+
181+
it('should incr stats by unique url, headers and method count', async () => {
182+
const testEvent1 = {
183+
properties: {
184+
url: 'https://example.build',
185+
method: 'POST',
186+
headers: {
187+
'sample-header': 'value1'
188+
}
189+
}
190+
}
191+
const testEvent2 = {
192+
properties: {
193+
url: 'https://example.build',
194+
method: 'PUT',
195+
headers: {
196+
'sample-header': 'value1'
197+
}
198+
}
199+
}
200+
const events = [createTestEvent(testEvent1), createTestEvent(testEvent2)]
201+
202+
nock(testEvent1.properties.url).post('/').reply(200, { success: true })
203+
204+
const statsClient = {
205+
incr: jest.fn(),
206+
observe: jest.fn(),
207+
_name: jest.fn(),
208+
_tags: jest.fn(),
209+
set: jest.fn,
210+
histogram: jest.fn()
211+
}
212+
const statsContext = {
213+
statsClient,
214+
tags: ['test:tag']
215+
}
216+
217+
const responses = await testDestination.testBatchAction('send', {
218+
events,
219+
mapping: {
220+
url: {
221+
'@path': '$.properties.url'
222+
},
223+
method: {
224+
'@path': '$.properties.method'
225+
},
226+
headers: {
227+
'sample-header': {
228+
'@path': '$.properties.sample-header'
229+
}
230+
}
231+
},
232+
useDefaultMappings: true,
233+
statsContext
234+
})
235+
236+
expect(responses.length).toBe(1)
237+
expect(responses[0].status).toBe(200)
238+
expect(statsClient.incr).toHaveBeenCalledWith(
239+
'webhook.configurable_batch_keys.unique_keys',
240+
2,
241+
statsContext.tags
242+
)
243+
})
180244
})
181245
})
182246
}

packages/destination-actions/src/destinations/webhook/send/index.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ const action: ActionDefinition<Settings, Payload> = {
5353
multiple: true,
5454
required: false,
5555
unsafe_hidden: true,
56-
default: ['url', 'method']
56+
default: ['url', 'method', 'headers']
5757
}
5858
},
5959
perform: (request, { payload }) => {
@@ -68,9 +68,19 @@ const action: ActionDefinition<Settings, Payload> = {
6868
throw error
6969
}
7070
},
71-
performBatch: (request, { payload }) => {
71+
performBatch: (request, { payload, statsContext }) => {
7272
// Expect these to be the same across the payloads
7373
const { url, method, headers } = payload[0]
74+
75+
if (statsContext) {
76+
const { statsClient, tags } = statsContext
77+
const set = new Set()
78+
for (const p of payload) {
79+
set.add(`${p.url} ${p.method} ${JSON.stringify(p.headers)}`)
80+
}
81+
statsClient?.incr('webhook.configurable_batch_keys.unique_keys', set.size, tags)
82+
}
83+
7484
try {
7585
return request(url, {
7686
method: method as RequestMethod,

0 commit comments

Comments
 (0)