diff --git a/src/connections/functions/destination-functions.md b/src/connections/functions/destination-functions.md index ae56dfd98f..f5d5dc5cc6 100644 --- a/src/connections/functions/destination-functions.md +++ b/src/connections/functions/destination-functions.md @@ -79,6 +79,12 @@ To change which event type the handler listens to, you can rename it to the name > info "" > 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. +### Variable scoping + +When declaring settings variables, declare them in the function handler rather than globally in your function. This prevents you from leaking the settings values across other function instances. + +The handler for destination functions is event-specific. For example, you might have an `onTrack()`or `onIdentify()` function handler. + ### Errors and error handling {% include content/functions/errors-and-error-handling.md %} diff --git a/src/connections/functions/index.md b/src/connections/functions/index.md index 637420393f..f01ff29879 100644 --- a/src/connections/functions/index.md +++ b/src/connections/functions/index.md @@ -12,6 +12,16 @@ Functions let you create your own sources and destinations directly within your ## What can you do with Functions? Functions can help you bring external data into Segment ([Source functions](/docs/connections/functions/source-functions)) and send data in Segment out to external destinations ([Destination functions](/docs/connections/functions/destination-functions)). Use [Insert functions](/docs/connections/functions/insert-functions) to transform data before it reaches your downstream destinations. Functions are scoped to your specific workspace. If you're a technology partner and want to build a new integration and publish it in Segment's catalog, see the [Developer Center documentation](/docs/partners/). +### Variable scoping in Functions +Functions are powered by AWS Lambda functions on the backend. This means you will need to declare any settings variables you create in the function handler rather than globally in your function. + +- For Source functions, the handler is onRequest(). +- For Destination functions and Insert functions, the handler is event-specfic. For example onTrack(), onIdentify(), etc. + +If you declare functions globally in your function code, you risk leaking those settings values across function instances if you have more than one per function codebase. Make sure that you scope settings to the handler functions and pass those settings values as arguments to any helper functions that may need them to ensure context remains scoped to each function instance. + +Learn more about this at in the [AWS Documentation](https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtime-environment.html#:~:text=Avoid%20global%20variables,static%20initialization%20performance){:target="_blank"}. + #### Source functions Source functions receive external data from a webhook and can create Segment events, objects, or both. Source functions have access to the full power of JavaScript so you can validate and transform the incoming data and even make external API requests to annotate your data. diff --git a/src/connections/functions/insert-functions.md b/src/connections/functions/insert-functions.md index e364ba6c94..864647bc3a 100644 --- a/src/connections/functions/insert-functions.md +++ b/src/connections/functions/insert-functions.md @@ -111,6 +111,12 @@ To ensure the Destination processes an event payload modified by the function, r > info "" > 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. +### Variable scoping + +When declaring settings variables, make sure to declare them in the function handler rather than globally in your Function. This prevents you leaking the settings values across other function instances. + +The handler for Insert functions is event-specific, for example, `onTrack()`, `onIdentify()`, etc. + ### Errors and error handling Segment considers a function's execution successful if it finishes without error. You can `throw` an error to create a failure on purpose. Use these errors to validate event data before processing it to ensure the function works as expected. diff --git a/src/connections/functions/source-functions.md b/src/connections/functions/source-functions.md index 43ca32fc88..9c45551c0d 100644 --- a/src/connections/functions/source-functions.md +++ b/src/connections/functions/source-functions.md @@ -261,6 +261,12 @@ The `Segment.set()` method accepts an object with the following fields: > warning "" > When you use the `set()` method, you won't see events in the Source Debugger. Segment only sends events to connected warehouses. +### Variable scoping + +Declare settings variables in the function handler, rather than globally in your function. This prevents you from leaking the settings values across other function instances. + +The handler for Source functions is `onRequest()`. + ### Runtime and dependencies {% include content/functions/runtime.md %}