Skip to content

Commit 8cb5345

Browse files
committed
DIFN GA
1 parent 9fcd29a commit 8cb5345

File tree

3 files changed

+161
-16
lines changed

3 files changed

+161
-16
lines changed

src/connections/functions/destination-functions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ If your function fails, you can check the error details and logs in the **Output
140140
Batch handlers are an extension of destination functions. When you define an `onBatch` handler alongside the handler functions for single events (for example: `onTrack` or `onIdentity`), you're telling Segment that the destination function can accept and handle batches of events.
141141

142142
> info ""
143-
> Batching is available for destination functions only.
143+
> Batching is available for destination and destination insert functions only.
144144
145145
### When to use batching
146146

src/connections/functions/index.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@ Learn more about [destination functions](/docs/connections/functions/destination
3535
#### Destination insert functions
3636
Destination insert functions help you enrich your data with code before you send it to downstream destinations.
3737

38-
> info "Destination Insert Functions in Public Beta"
39-
> Destination Insert Functions is in Public Beta, and Segment is actively working on this feature. Some functionality may change before it becomes generally available. [Contact Segment](https://segment.com/help/contact/){:target="_blank"} with any feedback or questions.
40-
4138
Use cases:
4239
- Implement custom logic and enrich data with third party sources
4340
- Transform outgoing data with advanced filtration and computation

src/connections/functions/insert-functions.md

Lines changed: 160 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ Use Destination Insert Functions to enrich, transform, or filter your data befor
1414

1515
**Customize filtration for your destinations**: Create custom logic with nested if-else statements, regex, custom business rules, and more to filter event data.
1616

17-
> info "Destination Insert Functions in Public Beta"
18-
> Destination Insert Functions is in Public Beta, and Segment is actively working on this feature. Some functionality may change before it becomes generally available. [Contact Segment](https://segment.com/help/contact/){:target="_blank"} with any feedback or questions.
19-
2017

2118
## Create destination insert functions
2219

@@ -237,21 +234,172 @@ To enable your insert function:
237234

238235
To prevent your insert function from processing data, toggle Enable Function off.
239236

240-
{% comment %}
241-
## Batching the insert function
242-
243-
Insert functions support batching with the `onBatch` handler.
237+
## Batching the destination insert function
244238

245239
Batch handlers are an extension of insert functions. When you define an `onBatch` handler alongside the handler functions for single events (for example, `onTrack` or `onIdentity`), you're telling Segment that the insert function can accept and handle batches of events.
246240

247-
Note the following limitations for batching with insert functions:
248-
- The batch request and response size is limited to 6mb.
249-
- Max count begins with 100 and goes up to 1,000.
241+
> info ""
242+
> Batching is available for insert and destination functions only.
243+
244+
### When to use batching
245+
246+
Consider creating a batch handler if:
247+
248+
- **You have a high-throughput function and want to reduce cost.** When you define a batch handler, Segment invokes the function once per *batch*, rather than once per event. As long as the function’s execution time isn’t adversely affected, the reduction in invocations should lead to a reduction in cost.
249+
250+
- **Your destination supports batching**. When your downstream destination supports sending data downstream in batches you can define a batch handler to avoid throttling. Batching for functions is independent of batch size supported by the destination. Segment automatically handles batch formation for destinations internally.
250251

251252
> info ""
252-
> Batching is available for insert and destination functions only. Read more about batching [in the Destination Functions docs](/docs/connections/functions/destination-functions/#batching-the-destination-function).
253+
> If a batched function receives too low a volume of events (under one event per second) to be worth batching, Segment may not invoke the batch handler.
254+
255+
### Define the batch handler
256+
257+
Segment collects the events over a short period of time and combines them into a batch. The system flushes them when the batch reaches a certain number of events, or when the batch has been waiting for a specified wait time.
258+
259+
To create a batch handler, define an `onBatch` function within your destination insert function. You can also use the "Default Batch" template found in the Functions editor to get started quickly.
260+
261+
```js
262+
async function onBatch(events, settings){
263+
// handle the batch of events
264+
}
265+
```
266+
267+
> info ""
268+
> The `onBatch` handler is an optional extension. Destination insert functions must still contain single event handlers as a fallback, in cases where Segment does not receive enough events to execute the batch.
269+
270+
The handler function receives an array of events. The events can be of any supported type and a single batch may contain more than one event type. Handler functions can also receive function settings. Here is an example of what a batch can look like:
271+
272+
```json
273+
[
274+
{
275+
"type": "identify",
276+
"userId": "019mr8mf4r",
277+
"traits": {
278+
"email": "[email protected]",
279+
"name": "Jake Peterson",
280+
"age": 26
281+
}
282+
},
283+
{
284+
"type": "track",
285+
"userId": "019mr8mf4r",
286+
"event": "Song Played",
287+
"properties": {
288+
"name": "Fallin for You",
289+
"artist": "Dierks Bentley"
290+
}
291+
},
292+
{
293+
"type": "track",
294+
"userId": "971mj8mk7p",
295+
"event": "Song Played",
296+
"properties": {
297+
"name": "Get Right",
298+
"artist": "Jennifer Lopez"
299+
}
300+
}
301+
]
302+
```
303+
304+
### Configure the event types within a batch
305+
306+
Segment batches together any event _of any type_ that it sees over a short period of time to increase batching efficiency and give you the flexibility to decide how batches are created. If you want to split batches by event type, you can implement this in your functions code by writing a handler.
307+
308+
```js
309+
async function onBatch(events, settings) {
310+
// group events by type
311+
const eventsByType = {}
312+
for (const event of events) {
313+
if (!(event.type in eventsByType)) {
314+
eventsByType[event.type] = []
315+
}
316+
eventsByType[event.type].push(event)
317+
}
318+
319+
// concurrently process sub-batches of a specific event type
320+
const promises = Object.entries(eventsByType).map(([type, events]) => {
321+
switch (type) {
322+
case 'track':
323+
return onTrackBatch(events, settings)
324+
case 'identify':
325+
return onIdentifyBatch(events, settings)
326+
// ...handle other event types here...
327+
}
328+
})
329+
return Promise.all(promises)
330+
}
331+
332+
async function onTrackBatch(events, settings) {
333+
// handle a batch of track events
334+
}
335+
336+
async function onIdentifyBatch(events, settings) {
337+
// handle a batch of identify events
338+
}
339+
```
340+
341+
### Configure your batch parameters
342+
343+
By default, Functions waits up to 10 seconds to form a batch of 20 events. You can increase the number of events included in each batch (up to 400 events per batch) by contacting [Segment support](https://segment.com/help/contact/){:target="_blank"}. Segment recommends users who wish to include fewer than 20 events per batch use destination functions without the `onBatch` handler.
344+
345+
### Test the batch handler
346+
347+
The [Functions editing environment](/docs/connections/functions/environment/) supports testing batch handlers.
348+
349+
To test the batch handler:
350+
1. In the right panel of the Functions editor, click **customize the event yourself** to enter Manual Mode.
351+
2. Add events as a JSON array, with one event per element.
352+
3. Click **Run** to preview the batch handler with the specified events.
353+
354+
> note ""
355+
> The Sample Event option tests single events only. You must use Manual Mode to add more than one event so you can test batch handlers.
356+
357+
The editor displays logs and request traces from the batch handler.
358+
359+
The [Public API](/docs/api/public-api) Functions/Preview endpoint also supports testing batch handlers. The payload must be a batch of events as a JSON array.
360+
361+
362+
### Handling batching errors
363+
364+
Standard [function error types](/docs/connections/functions/destination-functions/#destination-functions-error-types) apply to batch handlers. Segment attempts to retry the batch in the case of Timeout or Retry errors. For all other error types, Segment discards the batch. It's also possible to report a partial failure by returning status of each event in the batch. Segment retries only the failed events in a batch until those events are successful or until they result in a permanent error.
365+
366+
```json
367+
[
368+
{
369+
"status": 200
370+
},
371+
{
372+
"status": 400,
373+
"errormessage": "Bad Request"
374+
},
375+
{
376+
"status": 200
377+
},
378+
{
379+
"status": 500,
380+
"errormessage": "Error processing request"
381+
},
382+
{
383+
"status": 500,
384+
"errormessage": "Error processing request"
385+
},
386+
{
387+
"status": 200
388+
},
389+
]
390+
```
391+
392+
After receiving the response from the `onBatch` handler, Segment only retries **event_4** and **event_5**.
393+
394+
| Error Type | Result |
395+
| ---------------------- | ------- |
396+
| Bad Request | Discard |
397+
| Invalid Settings | Discard |
398+
| Message Rejected | Discard |
399+
| RetryError | Retry |
400+
| Timeout | Retry |
401+
| Unsupported Event Type | Discard |
253402

254-
{% endcomment %}
255403

256404
{% comment %}
257405

0 commit comments

Comments
 (0)