Skip to content

Commit 3cd1798

Browse files
[STRATCONN-5657] - Add batch keys to webhook action destination (#2878)
* [STRATCONN-5657] - Add batch keys to webhook action destination * Add stats * Use histogram * Add test for batch keys
1 parent d61c50f commit 3cd1798

File tree

5 files changed

+109
-2
lines changed

5 files changed

+109
-2
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?.histogram('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-audiences/send.types.ts

+4
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,8 @@ export interface Payload {
2525
data?: {
2626
[k: string]: unknown
2727
}
28+
/**
29+
* The mapping keys to batch events together by.
30+
*/
31+
batch_keys?: string[]
2832
}

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

+71
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,77 @@ 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.histogram).toHaveBeenCalledWith(
239+
'webhook.configurable_batch_keys.unique_keys',
240+
2,
241+
statsContext.tags
242+
)
243+
})
244+
})
245+
246+
it('should have batch keys default to url, method, headers', () => {
247+
const action = testDestination.definition.actions.send
248+
expect(action.fields.batch_keys).toBeDefined()
249+
expect(action.fields.batch_keys?.default).toBeDefined()
250+
expect(action.fields.batch_keys?.default).toEqual(['url', 'method', 'headers'])
180251
})
181252
})
182253
}

packages/destination-actions/src/destinations/webhook/send/generated-types.ts

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

+20-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ const action: ActionDefinition<Settings, Payload> = {
4545
description: 'Payload to deliver to webhook URL (JSON-encoded).',
4646
type: 'object',
4747
default: { '@path': '$.' }
48+
},
49+
batch_keys: {
50+
label: 'Batch Keys',
51+
description: 'The mapping keys to batch events together by.',
52+
type: 'string',
53+
multiple: true,
54+
required: false,
55+
unsafe_hidden: true,
56+
default: ['url', 'method', 'headers']
4857
}
4958
},
5059
perform: (request, { payload }) => {
@@ -59,9 +68,19 @@ const action: ActionDefinition<Settings, Payload> = {
5968
throw error
6069
}
6170
},
62-
performBatch: (request, { payload }) => {
71+
performBatch: (request, { payload, statsContext }) => {
6372
// Expect these to be the same across the payloads
6473
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?.histogram('webhook.configurable_batch_keys.unique_keys', set.size, tags)
82+
}
83+
6584
try {
6685
return request(url, {
6786
method: method as RequestMethod,

0 commit comments

Comments
 (0)