Skip to content

insert variable scoping sections to insert functions + source functions [DOC-1065] #7540

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
4 changes: 2 additions & 2 deletions src/connections/functions/destination-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
12 changes: 11 additions & 1 deletion src/connections/functions/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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).
To learn more, visit [Segment's IP Allowlisting documentation](/docs/connections/destinations/#ip-allowlisting).
4 changes: 2 additions & 2 deletions src/connections/functions/insert-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions src/connections/functions/source-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down