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
+160-2Lines changed: 160 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,7 @@ integration_type: feature
9
9
10
10
Destination functions allow you to transform and annotate your Segment events and send them to any external tool or API without worrying about setting up or maintaining any infrastructure.
11
11
12
-
All functions are scoped to your workspace, so members of other workspaces won't be able to view and use them.
12
+
All functions are scoped to your workspace, so members of other workspaces won't be able to view or use them.
13
13
14
14
> info ""
15
15
> Functions is available to all customer plan types with a free allotment of usage hours. Read more about [Functions usage limits](/docs/connections/functions/usage/), or see [your workspace’s Functions usage stats](https://app.segment.com/goto-my-workspace/settings/usage?metric=functions).
@@ -51,6 +51,7 @@ Destination functions can define handlers for each message type in the [Segment
51
51
-`onGroup`
52
52
-`onAlias`
53
53
-`onDelete`
54
+
-`onBatch`
54
55
55
56
Each of the functions above accepts two arguments:
56
57
@@ -142,6 +143,9 @@ You can read more about [error handling](#destination-functions-logs-and-errors)
142
143
{% include content/functions/runtime.md %}
143
144
144
145
146
+
<!-- TODO - could also go into the `runtime.md` include above, if applied identically to both types of functions.
147
+
## Batching in functions -->
148
+
145
149
## Create settings and secrets
146
150
147
151
{% include content/functions/settings.md %}
@@ -186,6 +190,150 @@ If your function fails, you can check the error details and logs in the **Output
186
190
-**Error Message** - This shows the error surfaced from your function.
187
191
-**Logs** - This section displays any messages to `console.log()` from the function.
188
192
193
+
## Batching the destination function (Beta)
194
+
195
+
> warning ""
196
+
> Batch handling for Functions is currently available as an early access beta release. By enabling batch handlers for your function, you acknowledge that your use of batch handlers is subject to [Segment’s Beta Terms and Conditions](https://segment.com/legal/first-access-beta-preview), or the applicable terms governing Beta Releases found in your subscription agreement with Segment.
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.
201
+
202
+
> info ""
203
+
> Batching is available to destination functions only.
204
+
205
+
### When to use batching
206
+
207
+
Consider creating a batch handler if:
208
+
209
+
-**Your function sends data to a service that has a batch endpoint.** Batch endpoints may allow you both to send more data downstream and stay within the rate limits imposed by the service. Batch handlers that use one or more batch endpoints improve the efficiency of the function, and enable it to scale more easily. Specifically, you can use batch handlers to build [list-based](/docs/personas/using-personas-data/#personas-destination-types-event-vs-list) Personas destinations.
210
+
-**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.
211
+
212
+
> info ""
213
+
> 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.
214
+
215
+
### Define the batch handler
216
+
217
+
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.
218
+
219
+
To create a batch handler, define an `onBatch` function within your destination function.
220
+
You can also use the "Default Batch" template found in the Functions editor to get started quickly.
221
+
222
+
```js
223
+
asyncfunctiononBatch(events, settings){
224
+
// handle the batch of events
225
+
}
226
+
```
227
+
228
+
> info ""
229
+
> The `onBatch` handler is an optional extension. Destination functions must still contain single event handlers as a fallback, in cases where Segment does not receive enough events to execute the batch.
230
+
231
+
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 also receive function settings.
232
+
233
+
For example, you could send the array of events to an external services batch endpoint:
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.
251
+
252
+
If your downstream endpoint requires events of a single type, you can write a handler that groups events by type, and then handles the events.
253
+
254
+
```js
255
+
asyncfunctiononBatch(events, settings) {
256
+
// group events by type
257
+
consteventsByType= {}
258
+
for (consteventof events) {
259
+
if (!(event.typein eventsByType)) {
260
+
eventsByType[event.type] = []
261
+
}
262
+
eventsByType[event.type].push(event)
263
+
}
264
+
265
+
// concurrently process sub-batches of a specific event type
You cannot yet configure batch parameters (either in the code or UI) in this version of the beta. If you would like to change your batch parameters, contact [[email protected]](mailto:[email protected]) with information about your specific use case, the reason you need to adjust parameters, and the URL to your destination function.
290
+
291
+
### Avoid writing batch and single event handlers
292
+
293
+
Your function might not get enough event traffic to create a batch of short windows of time. In this case, your function invokes the single event handler. If you need to consolidate your code into an `onBatch` handler, you can define single event handlers that call `onBatch`.
294
+
295
+
```js
296
+
asyncfunctiononTrack(event, settings) {
297
+
returnonBatch([event], settings)// defer to onBatch
298
+
}
299
+
300
+
asyncfunctiononIdentify(event, settings) {
301
+
returnonBatch([event], settings) // defer to onBatch
302
+
}
303
+
304
+
// do the same for onAlias, onGroup, onPage, onScreen, onDelete
305
+
306
+
asyncfunctiononBatch(events, settings) {
307
+
// handle batch of events
308
+
}
309
+
```
310
+
311
+
### Test the batch handler
312
+
313
+
The [Functions editing environment](/docs/connections/functions/environment/) supports testing batch handlers. In the right panel of the Functions editor, click **customize the event yourself** to enter Manual Mode. Add events as a JSON array, with one event per element. Click **Run** to preview the batch handler with the specified events.
> 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.
319
+
320
+
The editor displays logs and request traces from the batch handler.
321
+
322
+
The [Config API](/docs/config-api/) Functions/Preview endpoint also supports testing batch handlers. The payload must be a batch of events as a JSON array.
323
+
324
+
### Handling batching errors
325
+
326
+
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. If only part of a batch succeeds, Segment does not retry the failing part of the batch.
327
+
328
+
| Error Type | Result |
329
+
| ---------------------- | ------- |
330
+
| Bad Request | Discard |
331
+
| Invalid Settings | Discard |
332
+
| Message Rejected | Discard |
333
+
| RetryError | Retry |
334
+
| Timeout | Retry |
335
+
| Unsupported Event Type | Discard |
336
+
189
337
## Save and deploy the function
190
338
191
339
Once you finish building your destination function, click **Configure** to name it, then click **Create Function** to save it.
@@ -194,7 +342,7 @@ Once you do that, the destination function appears on the **Functions** page in
194
342
195
343
If you're editing an existing function, you can **Save** changes without updating instances of the function that are already deployed and running.
196
344
197
-
You can also choose to **Save & Deploy** to save the changes, and then choose which already-deployed functions to update with your changes. [You might need additional permissions](#functions-permissions) to update existing functions.
345
+
You can also choose to **Save & Deploy** to save the changes, and then choose which of the already-deployed functions to update with your changes. [You might need additional permissions](#functions-permissions) to update existing functions.
198
346
199
347
## Destination functions logs and errors
200
348
@@ -307,3 +455,13 @@ No, destination functions are currently available as cloud-mode destinations onl
307
455
##### How do I publish a destination to the public Segment catalog?
308
456
309
457
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/) and check out our [partner docs](/docs/partners/).
458
+
459
+
##### How does batching affect visibility?
460
+
461
+
The [Event Delivery tab](/docs/connections/event-delivery/) continues to show metrics for individual events, even if they are batched by your function code. For more information, see [Destination functions logs and errors](#destination-functions-logs-and-errors).
462
+
463
+
##### How does batching impact function use and cost?
464
+
465
+
A function's use depends on the number of times it is invoked, and the amount of time it takes to execute. When you enable batching, Segment invokes your function _once per batch_ rather than once per event. The volume of events flowing through the function determines the number of batches, which determines the number of invocations.
466
+
467
+
If you're sending your batch to an external service, the execution time of the function depends on the end-to-end latency of that service's batch endpoint, which may be higher than an endpoint that receives a single event.
0 commit comments