Skip to content

Commit d3d3cbc

Browse files
authored
Merge pull request #7540 from segmentio/functions-variable-scoping
insert variable scoping sections to insert functions + source functions [DOC-1065]
2 parents f25be24 + ba2c0d6 commit d3d3cbc

File tree

4 files changed

+15
-5
lines changed

4 files changed

+15
-5
lines changed

src/connections/functions/destination-functions.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ To change which event type the handler listens to, you can rename it to the name
7979
> info ""
8080
> 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.
8181
82-
### Variable scoping
82+
### Variable scoping
8383

84-
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.
84+
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.
8585

8686
The handler for destination functions is event-specific. For example, you might have an `onTrack()`or `onIdentify()` function handler.
8787

src/connections/functions/index.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@ Functions let you create your own sources and destinations directly within your
1212
## What can you do with Functions?
1313
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/).
1414

15+
### Variable scoping in Functions
16+
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.
17+
18+
- For source functions, the handler is `onRequest()`.
19+
- For destination and insert functions, the handler is event-specfic. For example, `onTrack()` or `onIdentify()`.
20+
21+
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.
22+
23+
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"}.
24+
1525
#### Source functions
1626
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.
1727

src/connections/functions/insert-functions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ To ensure the destination processes an event payload modified by the function, r
113113

114114
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.
115115

116-
The handler for insert functions is event-specific, for example, `onTrack()`, `onIdentify()`, and so on.
116+
The handler for insert functions is event-specific. For example, `onTrack()`, `onIdentify()`, and so on.
117117

118118
### Errors and error handling
119119

src/connections/functions/source-functions.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,9 @@ The `Segment.set()` method accepts an object with the following fields:
262262
263263
### Variable scoping
264264

265-
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.
265+
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.
266266

267-
The handler for Source functions is `onRequest()`.
267+
The handler for source functions is `onRequest()`.
268268

269269
### Runtime and dependencies
270270

0 commit comments

Comments
 (0)