|
| 1 | +--- |
| 2 | +title: Source Insert Functions |
| 3 | +--- |
| 4 | + |
| 5 | +> info "Source Insert Function is in Public Beta" |
| 6 | +> Source Insert Function is in public beta, and Segment is actively working on this feature. Some functionality may change before it becomes generally available. |
| 7 | + |
| 8 | +## Create source insert functions |
| 9 | + |
| 10 | +There are two ways you can access source insert functions from your Segment workspace: |
| 11 | +- From the [Connections catalog](#using-the-catalog). |
| 12 | +- From the [Sources tab](#using-the-sources-tab). |
| 13 | + |
| 14 | +### Using the catalog |
| 15 | + |
| 16 | +To create a source insert function from Segment’s catalog: |
| 17 | + |
| 18 | +1. Navigate to **Connections** > **Catalog** > **Functions** and click **New Function**. |
| 19 | + |
| 20 | +2. In the Select Function Type screen, select **Source Insert** and click **Next: Build Function**. |
| 21 | + |
| 22 | +3. Write and test your function code. Enter a sample event and click **Run** to test the function. |
| 23 | + |
| 24 | +4. Click **Next: Configure & Create** to add a function name, description (optional), and function logo (optional). |
| 25 | + |
| 26 | +5. Click **Create Function** to save your insert function. The new source insert function will be displayed in the Functions tab. |
| 27 | + |
| 28 | +#### Coding the source insert function |
| 29 | + |
| 30 | +Insert functions can define handlers for the following message types: |
| 31 | +- `onIdentity` |
| 32 | +- `onTrack` |
| 33 | +- `onPage` |
| 34 | +- `onScreen` |
| 35 | +- `onGroup` |
| 36 | +- `onAlias` |
| 37 | +- `onDelete` |
| 38 | + |
| 39 | +The default source code template includes handlers for all event types. Just implement the types you need and skip the ones you don’t. For event types you want to send through, return the event in the respective event handler. |
| 40 | + |
| 41 | +### Using the Sources tab |
| 42 | + |
| 43 | +You can also create a source insert function from Sources. |
| 44 | + |
| 45 | +1. Navigate to **Connections** > **Sources**. |
| 46 | +2. Select your source and go to the **Functions** tab. |
| 47 | +3. Click **Create insert function** to create your insert function from scratch. |
| 48 | + |
| 49 | +### Connecting a source insert function to a source |
| 50 | + |
| 51 | +For data to flow downstream after transformation, you need to connect your source insert function to a source. To do this, follow the steps below: |
| 52 | + |
| 53 | +1. Select the insert function you want to connect to the source. You can edit, delete, and connect the insert function in the side pane. |
| 54 | +2. Click *Connect a source**. |
| 55 | +3. Select the source you want to connect from the dropdown and click **Connect to Source** to connect. |
| 56 | + |
| 57 | +### Errors and error handling |
| 58 | + |
| 59 | +Segment considers a function’s execution successful if it completes without error. You can `throw` an error to create a failure on purpose. Use these errors to validate event data before processing to make sure the function works as expected. |
| 60 | + |
| 61 | +You can `throw` the following pre-defined error types to indicate that the function ran successfully, but the data was not deliverable: |
| 62 | + |
| 63 | +- `EventNotSupported` |
| 64 | +- `InvalidEventPayload` |
| 65 | +- `ValidationError` |
| 66 | +- `RetryError` |
| 67 | +- `DropEvent` |
| 68 | + |
| 69 | +This is similar to Source, Destination, and Insert Functions. |
| 70 | + |
| 71 | +The errors listed are all permanent errors, except `RetryError`. When a `RetryError` is thrown, Segment will reattempt to run the function a set number of times before permanently erroring out. |
| 72 | + |
| 73 | +### Source insert functions FAQs |
| 74 | + |
| 75 | +**The Delivery Overview tab does not display any errors that occur within the source insert function.** |
| 76 | +If errors arise or events are dropped, they will appear as if the events never flowed from the source. |
| 77 | + |
| 78 | +**Can I test the function with different event types?** |
| 79 | +You can test the function with different event types from the **Test** tab in the code editor, similar to other functions. |
| 80 | + |
| 81 | +**Can I use source insert functions for all types of Source?** |
| 82 | +Source insert functions only work for event sources, not object sources or rETL. |
| 83 | + |
| 84 | +**What is the expected latency for a source insert function?** |
| 85 | +Typically, it takes a source insert function between 200 milliseconds to 5 seconds to complete, based on the complexity of the function. The default timeout is 5 seconds but can be increased to 60 seconds. |
| 86 | + |
| 87 | +**What is the runtime environment?** |
| 88 | +Segment supports Node.js V18. The dependencies listed [here](https://segment.com/docs/connections/functions/insert-functions/#runtime-and-dependencies){:target=”_blank”} are installed in the function. The following dependencies are also installed: |
| 89 | +- `@azure/identity` exposed as `azure.identity` |
| 90 | +- `@azure/event-hubs` exposed as `azure.eventHubs` |
| 91 | +- `@azure/synapse` exposed as `azure.synapse` |
| 92 | +- `jsftp` exposed as `jsftp` |
| 93 | +- `crypto-js` exposed as `cryptojslib.cryptojs` |
| 94 | +- `akeyless` exposed as `akeylessLabs.akeyless` |
| 95 | +- `akeyless-cloud-id` exposed as `akeylessLabs.akeylessCloudId` |
| 96 | +- `@onesignal/node-onesignal` exposed as `oneSignal.oneSignal` |
| 97 | +- `pg` exposed as `pg.pg` |
| 98 | +- `snowflake-sdk` exposed as `snowflakesdk.snowflakesdk` |
| 99 | +- `@clickhouse/client` exposed as `clickHouseClient.clickHouseClient` |
| 100 | +- `aws4` exposed as `aws4.aws4` |
| 101 | +- `@sentry/node` exposed as `sentrylib.sentry` |
| 102 | + |
0 commit comments