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

track serverless functions executions #7963

Merged
merged 2 commits into from
Oct 22, 2024
Merged

track serverless functions executions #7963

merged 2 commits into from
Oct 22, 2024

Conversation

anamarn
Copy link
Contributor

@anamarn anamarn commented Oct 22, 2024

Solves:
https://github.com/twentyhq/private-issues/issues/74

TLDR
When a serverless function is executed, the result is send to tinybird event data source.

In order to test:

  1. Set ANALYTICS_ENABLED to true
  2. Put your TINYBIRD_INGEST_TOKEN from twenty_event_playground in your .env file
  3. Don't forget to run the worker
  4. Create your serverless function and run it
  5. The event should be logged on the event datasource in twenty_event_playground

What is the structure of the payload of a serverless function event?

Here are two examples of the payload:
{"duration":37,"status":"SUCCESS","functionId":"a9fd87c0-af86-4e17-be3a-a6d3d961678a","functionName":"testingFunction"}

{"duration":34,"status":"ERROR","errorType":"ReferenceError","functionId":"a9fd87c0-af86-4e17-be3a-a6d3d961678a","functionName":"testingFunction"}

Possible improvments

  • Change the status(str) to success(bool)
  • Enrich data in the payload

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Summary

This pull request implements analytics tracking for serverless function executions, sending results to a Tinybird event data source.

  • Added AnalyticsModule to ServerlessFunctionModule imports in packages/twenty-server/src/engine/metadata-modules/serverless-function/serverless-function.module.ts
  • Integrated AnalyticsService in packages/twenty-server/src/engine/metadata-modules/serverless-function/serverless-function.service.ts to create events after function execution
  • Implemented event payload structure including duration, status, error type, function ID, and name
  • Requires ANALYTICS_ENABLED set to true and TINYBIRD_INGEST_TOKEN configured in .env file for testing

2 file(s) reviewed, 2 comment(s)
Edit PR Review Bot Settings | Greptile

Comment on lines +138 to +142
this.analyticsService.create(
eventInput,
'serverless-function',
workspaceId,
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Ensure that the 'serverless-function' string is defined as a constant to avoid typos

Suggested change
this.analyticsService.create(
eventInput,
'serverless-function',
workspaceId,
);
const ANALYTICS_SERVICE_NAME = 'serverless-function';
this.analyticsService.create(
eventInput,
ANALYTICS_SERVICE_NAME,
workspaceId,
);

Comment on lines +130 to +132
...(resultServerlessFunction.error && {
errorType: resultServerlessFunction.error.errorType,
}),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Use optional chaining for error property access

Suggested change
...(resultServerlessFunction.error && {
errorType: resultServerlessFunction.error.errorType,
}),
...(resultServerlessFunction.error?.errorType ? {
errorType: resultServerlessFunction.error.errorType,
} : {}),

Copy link
Member

@FelixMalfait FelixMalfait left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change name to serverlessFunction.executed as discussed :). Otherwise all good for me!

@anamarn anamarn merged commit 29bd4e5 into main Oct 22, 2024
18 checks passed
@anamarn anamarn deleted the feat/track-serverless branch October 22, 2024 15:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants