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/_includes/content/functions/logs.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
If your function throws an error, execution halts immediately. Segment captures the event, any outgoing requests/responses, any logs the function might have printed, as well as the error itself.
2
2
3
-
Segment then displays the captured error information in the [Event Delivery](/docs/connections/event-delivery/) page for your function. You can use this information to find and fix unexpected errors.
3
+
Segment then displays the captured error information in the [Event Delivery](/docs/connections/event-delivery/) page for your destination. You can use this information to find and fix unexpected errors.
4
4
5
5
You can throw [an error or a custom error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error){:target="_blank"} and you can also add helpful context in logs using the [`console` API](https://developer.mozilla.org/en-US/docs/Web/API/console){:target="_blank"}. For example:
Copy file name to clipboardExpand all lines: src/connections/functions/destination-functions.md
+3-3Lines changed: 3 additions & 3 deletions
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.
144
144
145
145
### When to use batching
146
146
@@ -269,7 +269,7 @@ To test the batch handler:
269
269
2. Add events as a JSON array, with one event per element.
270
270
3. Click **Run** to preview the batch handler with the specified events.
271
271
272
-
> note ""
272
+
> info ""
273
273
> 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.
274
274
275
275
The editor displays logs and request traces from the batch handler.
@@ -306,7 +306,7 @@ Standard [function error types](/docs/connections/functions/destination-function
306
306
]
307
307
```
308
308
309
-
After receiving the response from the `onBatch` handler, Segment only retries **event_4** and **event_5**.
309
+
For example, after receiving the responses above from the `onBatch` handler, Segment only retries **event_4** and **event_5**.
Copy file name to clipboardExpand all lines: src/connections/functions/index.md
-3Lines changed: 0 additions & 3 deletions
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 +13,6 @@ Use Destination Insert Functions to enrich, transform, or filter your data befor
14
13
15
14
**Customize filtration for your destinations**: Create custom logic with nested if-else statements, regex, custom business rules, and more to filter event data.
16
15
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
16
21
17
## Create destination insert functions
22
18
@@ -237,21 +233,181 @@ To enable your insert function:
237
233
238
234
To prevent your insert function from processing data, toggle Enable Function off.
239
235
240
-
{% comment %}
241
-
## Batching the insert function
242
-
243
-
Insert functions support batching with the `onBatch` handler.
236
+
## Batching the destination insert function
244
237
245
238
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
239
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.
240
+
> info ""
241
+
> Batching is available for destination and destination insert functions only.
242
+
243
+
### When to use batching
244
+
245
+
Consider creating a batch handler if:
246
+
247
+
-**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.
248
+
249
+
-**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.
250
250
251
251
> 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).
252
+
> 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.
253
+
254
+
### Define the batch handler
255
+
256
+
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.
257
+
258
+
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.
259
+
260
+
```js
261
+
asyncfunctiononBatch(events, settings){
262
+
// handle the batch of events
263
+
return 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 doesn't 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
constbatchResult= [].concat(...results); // Combine arrays into a single array
332
+
return batchResult;
333
+
} catch (error) {
334
+
thrownewRetryError(error.message);
335
+
}
336
+
}
337
+
338
+
asyncfunctiononTrackBatch(events, settings) {
339
+
// handle a batch of track events
340
+
return events
341
+
}
342
+
343
+
asyncfunctiononIdentifyBatch(events, settings) {
344
+
// handle a batch of identify events
345
+
return events
346
+
}
347
+
```
348
+
349
+
### Configure your batch parameters
350
+
351
+
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 insert functions without the `onBatch` handler.
352
+
353
+
### Test the batch handler
354
+
355
+
The [Functions editing environment](/docs/connections/functions/environment/) supports testing batch handlers.
356
+
357
+
To test the batch handler:
358
+
1. In the right panel of the Functions editor, click **customize the event yourself** to enter Manual Mode.
359
+
2. Add events as a JSON array, with one event per element.
360
+
3. Click **Run** to preview the batch handler with the specified events.
361
+
362
+
> info ""
363
+
> 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.
364
+
365
+
The editor displays logs and request traces from the batch handler.
366
+
367
+
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.
368
+
369
+
370
+
### Handling batching errors
371
+
372
+
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.
373
+
374
+
```json
375
+
[
376
+
{
377
+
"status": 200
378
+
},
379
+
{
380
+
"status": 400,
381
+
"errormessage": "Bad Request"
382
+
},
383
+
{
384
+
"status": 200
385
+
},
386
+
{
387
+
"status": 500,
388
+
"errormessage": "Error processing request"
389
+
},
390
+
{
391
+
"status": 500,
392
+
"errormessage": "Error processing request"
393
+
},
394
+
{
395
+
"status": 200
396
+
},
397
+
]
398
+
```
399
+
400
+
For example, after receiving the responses above from the `onBatch` handler, Segment only retries **event_4** and **event_5**.
401
+
402
+
| Error Type | Result |
403
+
| ---------------------- | ------- |
404
+
| Bad Request | Discard |
405
+
| Invalid Settings | Discard |
406
+
| Message Rejected | Discard |
407
+
| RetryError | Retry |
408
+
| Timeout | Retry |
409
+
| Unsupported Event Type | Discard |
253
410
254
-
{% endcomment %}
255
411
256
412
{% comment %}
257
413
@@ -321,11 +477,6 @@ No, destination insert functions are currently available as cloud-mode destinati
321
477
322
478
No, an insert function can only be connected to one destination.
323
479
324
-
##### How do I publish a destination to the public Segment catalog?
325
-
326
-
If you are a partner, looking to publish your destination and distribute your app through Segment catalog, visit the [Developer Center](https://segment.com/partners/developer-center/){:target="_blank"} and check out the Segment [partner docs](/docs/partners/).
0 commit comments