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

12
12
13
13
You can configure the details about this setting, which change how it's displayed to anyone using your function:
14
14
@@ -25,4 +25,4 @@ Click **Add Setting** to save the new setting.
25
25
26
26
Once you save a setting, it appears in the **Settings** tab for the function. You can edit or delete settings from this tab.
Copy file name to clipboardExpand all lines: src/connections/functions/destination-functions.md
+53-35Lines changed: 53 additions & 35 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,27 +11,25 @@ Destination functions allow you to transform and annotate your Segment events an
11
11
All functions are scoped to your workspace, so members of other workspaces can't view or use them.
12
12
13
13
> info ""
14
-
> 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).
14
+
> 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){:target="_blank"}.
15
15
16
-

16
+

17
17
18
18
19
19
> note ""
20
20
> Destination functions does not accept data from [Object Cloud sources](/docs/connections/sources/#object-cloud-sources).
21
21
22
22
## Create a destination function
23
23
24
-
1. From your workspace, go to the Catalog and click the [Functions tab](https://app.segment.com/goto-my-workspace/functions/catalog).
24
+
1. From your workspace, go to the Catalog and click the [Functions tab](https://app.segment.com/goto-my-workspace/functions/catalog){:target="_blank"}.
25
25
2. Click **New Function**.
26
26
3. Select **Destination Function** and click **Build**.
27
27
28
28
> success ""
29
-
> **Tip:** Want to see some example functions? Check out the templates available in the Functions UI, or in the open-source [Segment Functions Library](https://github.com/segmentio/functions-library). (Contributions welcome!)
29
+
> **Tip:** Want to see some example functions? Check out the templates available in the Functions UI, or in the open-source [Segment Functions Library](https://github.com/segmentio/functions-library){:target="_blank"}. (Contributions welcome!)
30
30
31
31
When you click **Build**, a code editor appears. Use the editor to write the code for your function, configure settings, and test the function's behavior.
32
32
33
-

34
-
35
33
## Code the destination function
36
34
37
35
Segment invokes a separate part of the function (called a "handler") for each event type that you send to your destination function.
@@ -78,7 +76,7 @@ async function onTrack(event) {
78
76
To change which event type the handler listens to, you can rename it to the name of the message type. For example, if you rename this function `onIdentify`, it listens for "Identify" events instead.
79
77
80
78
> info ""
81
-
> Functions' runtime includes a `fetch()` polyfill using a `node-fetch` package. Check out the [node-fetch documentation](https://www.npmjs.com/package/node-fetch) for usage examples.
79
+
> Functions' runtime includes a `fetch()` polyfill using a `node-fetch` package. Check out the [node-fetch documentation](https://www.npmjs.com/package/node-fetch){:target="_blank"} for usage examples.
82
80
83
81
### Errors and error handling
84
82
@@ -140,17 +138,14 @@ You can read more about [error handling](#destination-functions-logs-and-errors)
140
138
{% include content/functions/runtime.md %}
141
139
142
140
143
-
<!-- TODO - could also go into the `runtime.md` include above, if applied identically to both types of functions.
144
-
## Batching in functions -->
141
+
<!-- TODO - could also go into the `runtime.md` include above, if applied identically to both types of functions. -->
145
142
146
143
## Create settings and secrets
147
144
148
145
{% include content/functions/settings.md %}
149
146
150
147
Next, fill out this setting's value in the **Test** tab, so you can run the function and verify that the correct setting value is passed. (This value is only for testing your function.)
151
148
152
-
{:width="500"}
153
-
154
149
Now that you've configured a setting and entered a test value, you can add code to read its value and run the function, as in the example below:
155
150
156
151
```js
@@ -162,8 +157,6 @@ async function onTrack(request, settings) {
162
157
163
158
When you deploy your destination function in your workspace, you fill out the settings on the destination configuration page, similar to how you would configure a normal destination.
164
159
165
-

166
-
167
160
## Test the destination function
168
161
169
162
You can test your code directly from the editor in two ways:
@@ -172,32 +165,27 @@ You can test your code directly from the editor in two ways:
172
165
173
166
Click **Use Sample Event** and select the source to use events from.
174
167
175
-

168
+

176
169
177
170
Click **Run** to test your function with the event you selected.
178
171
179
172
### Test using manual input
180
173
181
174
You can also manually include your own JSON payload of a Segment event, instead of fetching a sample from one of your workspace sources.

184
177
185
178
If your function fails, you can check the error details and logs in the **Output** section.
186
179
187
180
-**Error Message** - This shows the error surfaced from your function.
188
181
-**Logs** - This section displays any messages to `console.log()` from the function.
189
182
190
-
## Batching the destination function (Beta)
191
-
192
-
> warning ""
193
-
> Batch handling for Functions is 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.
194
-
>
195
-
> If you notice any bugs or have any general feedback on this new feature, please fill out [this form](https://airtable.com/shr9TU4huO0PK0DSU).
183
+
## Batching the destination function
196
184
197
185
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.
198
186
199
187
> info ""
200
-
> Batching is available to destination functions only.
188
+
> Batching is available for destination functions only.
201
189
202
190
### When to use batching
203
191
@@ -283,13 +271,16 @@ async function onIdentifyBatch(events, settings) {
283
271
284
272
### Configure your batch parameters
285
273
286
-
You cannot configure batch parameters (either in the code or UI) in this version of the beta. Functions waits up to 10 seconds to form a batch of 20 events. If you would like to change your batch parameters, please fill out [this form](https://airtable.com/shr9TU4huO0PK0DSU) and Support will contact you once this is made available.
274
+
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.
287
275
288
276
### Test the batch handler
289
277
290
-
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.
278
+
The [Functions editing environment](/docs/connections/functions/environment/) supports testing batch handlers.
1. In the right panel of the Functions editor, click **customize the event yourself** to enter Manual Mode.
282
+
2. Add events as a JSON array, with one event per element.
283
+
3. Click **Run** to preview the batch handler with the specified events.
293
284
294
285
> note ""
295
286
> 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.
@@ -300,7 +291,35 @@ The [Config API](/docs/config-api/) Functions/Preview endpoint also supports tes
300
291
301
292
### Handling batching errors
302
293
303
-
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.
294
+
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 is 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.
295
+
296
+
```json
297
+
[
298
+
{
299
+
"status": 200
300
+
},
301
+
{
302
+
"status": 400,
303
+
"errormessage": "Bad Request"
304
+
},
305
+
{
306
+
"status": 200
307
+
},
308
+
{
309
+
"status": 500,
310
+
"errormessage": "Error processing request"
311
+
},
312
+
{
313
+
"status": 500,
314
+
"errormessage": "Error processing request"
315
+
},
316
+
{
317
+
"status": 200
318
+
},
319
+
]
320
+
```
321
+
322
+
After receiving the response from the `onBatch` handler, only **event_3** and **event_4** will be retried.
304
323
305
324
| Error Type | Result |
306
325
| ---------------------- | ------- |
@@ -311,6 +330,7 @@ Standard [function error types](/docs/connections/functions/destination-function
311
330
| Timeout | Retry |
312
331
| Unsupported Event Type | Discard |
313
332
333
+
314
334
## Save and deploy the function
315
335
316
336
Once you finish building your destination function, click **Configure** to name it, then click **Create Function** to save it.
@@ -325,12 +345,12 @@ You can also choose to **Save & Deploy** to save the changes, and then choose wh
325
345
326
346
A function can throw errors, or Segment might encounter errors while invoking your function. You can view these errors in the [Event Delivery](/docs/connections/event-delivery/) tab for your Destination as in the example below.
327
347
328
-

348
+

329
349
330
350
### Destination functions error types
331
351
332
352
-**Bad Request** - Any error thrown by the function code that is not covered by the other errors.
333
-
-**Invalid Settings** - A configuration error prevented Segment from executing your code. If this error persists for more than an hour, [contact Segment Support](https://segment.com/help/contact/).
353
+
-**Invalid Settings** - A configuration error prevented Segment from executing your code. If this error persists for more than an hour, [contact Segment Support](https://segment.com/help/contact/){:target="_blank"}.
334
354
-**Message Rejected** - Your code threw `InvalidEventPayload` or `ValidationError` due to invalid input.
335
355
-**Unsupported Event Type** - Your code does not implement a specific event type (`onTrack()`, etc.) or threw a `EventNotSupported` error.
336
356
-**Retry** - Your code threw `RetryError` indicating that the function should be retried.
@@ -343,9 +363,7 @@ If your function throws an error, execution halts immediately. Segment captures
343
363
344
364
Segment then displays the captured error information in the [Event Delivery](/docs/connections/event-delivery/) page for your destination function. You can use this information to find and fix unexpected errors.
345
365
346
-

347
-
348
-
You can throw [an error or a custom error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error) and you can also add helpful context in logs using the [`console` API](https://developer.mozilla.org/en-US/docs/Web/API/console). For example:
366
+
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:
349
367
350
368
```js
351
369
asyncfunctiononTrack(event, settings) {
@@ -397,7 +415,7 @@ async function getAccessToken () {
397
415
398
416
### Editing and deleting functions
399
417
400
-
If you are a **Workspace Owner** or **Functions Admin**, you can manage your function from the [Functions](https://app.segment.com/goto-my-workspace/functions/catalog) page.
418
+
If you are a **Workspace Owner** or **Functions Admin**, you can manage your function from the [Functions](https://app.segment.com/goto-my-workspace/functions/catalog){:target="_blank"} page.
401
419
402
420
403
421
### Monitoring destination functions
@@ -432,19 +450,19 @@ Yes, Functions access is logged in the [Audit Trail](/docs/segment-app/iam/audit
432
450
433
451
##### Does Segment retry failed function invocations?
434
452
435
-
Segment retries 9 times over the course of 4 hours. This increases the number of attempts for messages, so we try to re-deliver them another 4 times after some backoff. Segment doesn't retry if your function throws a [non-recoverable error](#errors-and-error-handling).
453
+
Segment retries 9 times over the course of 4 hours. This increases the number of attempts for messages, so Segment tries to re-deliver them another 4 times after some backoff. Segment doesn't retry if your function throws a [non-recoverable error](#errors-and-error-handling).
436
454
437
455
##### Are events guaranteed to send data in order?
438
456
439
457
No, Segment can't guarantee the order in which the events are delivered to an endpoint.
440
458
441
459
##### Can I create a device-mode destination?
442
460
443
-
No, destination functions are currently available as cloud-mode destinations only. We're in the early phases of exploration and discovery for supporting customer "web plugins" for custom device-mode destinations and other use cases, but this is unsupported today.
461
+
No, destination functions are currently available as cloud-mode destinations only. Segment is in the early phases of exploration and discovery for supporting customer "web plugins" for custom device-mode destinations and other use cases, but this is unsupported today.
444
462
445
463
##### How do I publish a destination to the public Segment catalog?
446
464
447
-
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/).
465
+
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