You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/connections/functions/destination-functions.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -140,7 +140,7 @@ If your function fails, you can check the error details and logs in the **Output
140
140
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.
141
141
142
142
> info ""
143
-
> Batching is available for destination functions only.
143
+
> Batching is available for destination and destination insert functions only.
Copy file name to clipboardExpand all lines: src/connections/functions/index.md
-3Lines changed: 0 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -35,9 +35,6 @@ Learn more about [destination functions](/docs/connections/functions/destination
35
35
#### Destination insert functions
36
36
Destination insert functions help you enrich your data with code before you send it to downstream destinations.
37
37
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
-
41
38
Use cases:
42
39
- Implement custom logic and enrich data with third party sources
43
40
- Transform outgoing data with advanced filtration and computation
@@ -14,9 +14,6 @@ Use Destination Insert Functions to enrich, transform, or filter your data befor
14
14
15
15
**Customize filtration for your destinations**: Create custom logic with nested if-else statements, regex, custom business rules, and more to filter event data.
16
16
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
-
20
17
21
18
## Create destination insert functions
22
19
@@ -237,21 +234,172 @@ To enable your insert function:
237
234
238
235
To prevent your insert function from processing data, toggle Enable Function off.
239
236
240
-
{% comment %}
241
-
## Batching the insert function
242
-
243
-
Insert functions support batching with the `onBatch` handler.
237
+
## Batching the destination insert function
244
238
245
239
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.
246
240
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.
250
251
251
252
> 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
+
asyncfunctiononBatch(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:
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
+
asyncfunctiononBatch(events, settings) {
310
+
// group events by type
311
+
consteventsByType= {}
312
+
for (consteventof events) {
313
+
if (!(event.typein eventsByType)) {
314
+
eventsByType[event.type] = []
315
+
}
316
+
eventsByType[event.type].push(event)
317
+
}
318
+
319
+
// concurrently process sub-batches of a specific event type
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**.
0 commit comments