Skip to content
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

insert variable scoping sections to insert functions + source functions #7540

Draft
wants to merge 4 commits into
base: develop
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/connections/functions/destination-functions.md
Original file line number Diff line number Diff line change
@@ -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 %}
10 changes: 10 additions & 0 deletions src/connections/functions/index.md
Original file line number Diff line number Diff line change
@@ -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.

6 changes: 6 additions & 0 deletions src/connections/functions/insert-functions.md
Original file line number Diff line number Diff line change
@@ -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.
6 changes: 6 additions & 0 deletions src/connections/functions/source-functions.md
Original file line number Diff line number Diff line change
@@ -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 %}