diff --git a/src/connections/functions/destination-functions.md b/src/connections/functions/destination-functions.md index f109a68a4a..f5d5dc5cc6 100644 --- a/src/connections/functions/destination-functions.md +++ b/src/connections/functions/destination-functions.md @@ -79,9 +79,9 @@ 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 +### 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. +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. diff --git a/src/connections/functions/index.md b/src/connections/functions/index.md index 637420393f..473255aa5d 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 and insert functions, the handler is event-specfic. For example, `onTrack()` or `onIdentify()`. + +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. @@ -54,4 +64,4 @@ IP Allowlisting uses a NAT gateway to route outbound Functions traffic from Segm IP Allowlisting is available for customers on Business Tier plans. -To learn more, visit [Segment's IP Allowlisting documentation](/docs/connections/destinations/#ip-allowlisting). \ No newline at end of file +To learn more, visit [Segment's IP Allowlisting documentation](/docs/connections/destinations/#ip-allowlisting). diff --git a/src/connections/functions/insert-functions.md b/src/connections/functions/insert-functions.md index f40678d9df..f0a0e9fa9c 100644 --- a/src/connections/functions/insert-functions.md +++ b/src/connections/functions/insert-functions.md @@ -113,9 +113,9 @@ To ensure the Destination processes an event payload modified by the function, r ### 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. +When declaring settings variables, make sure to 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 insert functions is event-specific, for example, `onTrack()`, `onIdentify()`, and so on. +The handler for insert functions is event-specific. For example, `onTrack()`, `onIdentify()`, and so on. ### Errors and error handling diff --git a/src/connections/functions/source-functions.md b/src/connections/functions/source-functions.md index 86bc3ccf36..cd7a161495 100644 --- a/src/connections/functions/source-functions.md +++ b/src/connections/functions/source-functions.md @@ -263,9 +263,9 @@ The `Segment.set()` method accepts an object with the following fields: ### 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. +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()`. +The handler for source functions is `onRequest()`. ### Runtime and dependencies