Skip to content

Commit

Permalink
fix: improve on-events script logs (#3049)
Browse files Browse the repository at this point in the history
<!-- Describe the problem and your solution --> 
- each scripts has its own logs operation
- can filter by `Type = Events`

![Screenshot 2024-11-22 at 16 36
59](https://github.com/user-attachments/assets/dd9a33e3-36b2-43bb-8c5b-bbad53b8eec5)

<!-- Issue ticket number and link (if applicable) -->
https://linear.app/nango/issue/NAN-2219/on-events-script-follow-up

<!-- Testing instructions (skip if just adding/editing providers) -->
### How to tests:
1. Set `on-events` scripts in your nango.yaml
```
        on-events:
            post-connection-creation:
                - setup
            pre-connection-deletion:
                - cleanup

```
2. run `nango generate` and modify the scripts
3. run `nango deploy dev`
4. Go to the dashboard and create/delete a connection for the
integration you added the scripts into
5. Check the logs
  • Loading branch information
TBonnin authored Nov 25, 2024
1 parent b7f985d commit e23709b
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 42 deletions.
7 changes: 4 additions & 3 deletions packages/logs/lib/models/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface FormatMessageData {
environment?: { id: number; name: string } | undefined;
connection?: { id: number; name: string } | undefined;
integration?: { id: number; name: string; provider: string } | undefined;
syncConfig?: { id: number; name: string } | undefined;
syncConfig?: { id: number; name: string } | undefined; // TODO: rename to script or something similar because it also apply to actions and on-events scripts
meta?: MessageRow['meta'];
}

Expand Down Expand Up @@ -125,7 +125,6 @@ export const operationTypeToMessage: Record<ConcatOperationList, string> = {
'action:run': 'Action execution',
'admin:impersonation': 'Admin logged into another account',
'auth:create_connection': 'Create connection',
'auth:delete_connection': 'Delete connection',
'auth:post_connection': 'post connection execution',
'auth:refresh_token': 'Token refresh',
'auth:connection_test': 'Connection test',
Expand All @@ -140,7 +139,9 @@ export const operationTypeToMessage: Record<ConcatOperationList, string> = {
'sync:run': 'Sync execution',
'sync:unpause': 'Sync schedule started',
'webhook:incoming': 'Received a webhook',
'webhook:forward': 'Forwarding Webhook'
'webhook:forward': 'Forwarding Webhook',
'events:post_connection_creation': 'Post connection creation script execution',
'events:pre_connection_deletion': 'Pre connection creation script execution'
};

/**
Expand Down
3 changes: 3 additions & 0 deletions packages/logs/lib/otlp/otlpSpan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,8 @@ function shouldTrace(operation: OperationRow): boolean {
if (operation.operation.type === 'webhook') {
return true;
}
if (operation.operation.type === 'events') {
return true;
}
return false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const validation = z
'all',
'action',
'sync',
'events',
'sync:init',
'sync:cancel',
'sync:pause',
Expand Down
37 changes: 18 additions & 19 deletions packages/server/lib/hooks/connection/on/connection-created.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { RecentlyCreatedConnection } from '@nangohq/shared';
import { onEventScriptService } from '@nangohq/shared';
import type { LogContextGetter } from '@nangohq/logs';
import { defaultOperationExpiration } from '@nangohq/logs';
import { getOrchestrator } from '../../../utils/utils.js';

export async function postConnectionCreation(
Expand All @@ -18,26 +19,28 @@ export async function postConnectionCreation(
return;
}

const postConnectionCreationScripts = await onEventScriptService.getByConfig(config_id, 'post-connection-creation');
const event = 'post-connection-creation';

const postConnectionCreationScripts = await onEventScriptService.getByConfig(config_id, event);

if (postConnectionCreationScripts.length === 0) {
return;
}

const logCtx = await logContextGetter.create(
{ operation: { type: 'auth', action: 'post_connection' } },
{
account,
environment,
integration: { id: config_id, name: connection.provider_config_key, provider },
connection: { id: connection.id, name: connection.connection_id }
}
);

let failed = false;
for (const script of postConnectionCreationScripts) {
const { name, file_location: fileLocation, version } = script;

const logCtx = await logContextGetter.create(
{ operation: { type: 'events', action: 'post_connection_creation' }, expiresAt: defaultOperationExpiration.action() },
{
account,
environment,
integration: { id: config_id, name: connection.provider_config_key, provider: provider },
connection: { id: connection.id, name: connection.connection_id },
syncConfig: { id: script.id, name: script.name },
meta: { event }
}
);
const res = await getOrchestrator().triggerOnEventScript({
connection: createdConnection.connection,
version,
Expand All @@ -46,13 +49,9 @@ export async function postConnectionCreation(
logCtx
});
if (res.isErr()) {
failed = true;
await logCtx.failed();
} else {
await logCtx.success();
}
}

if (failed) {
await logCtx.failed();
} else {
await logCtx.success();
}
}
37 changes: 19 additions & 18 deletions packages/server/lib/hooks/connection/on/connection-deleted.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { configService, onEventScriptService } from '@nangohq/shared';
import type { LogContextGetter } from '@nangohq/logs';
import { defaultOperationExpiration } from '@nangohq/logs';
import { getOrchestrator } from '../../../utils/utils.js';
import type { DBTeam, DBEnvironment, Connection } from '@nangohq/types';

Expand All @@ -17,27 +18,31 @@ export async function preConnectionDeletion({
if (!connection.config_id || !connection.id) {
return;
}
const preConnectionDeletionScripts = await onEventScriptService.getByConfig(connection.config_id, 'pre-connection-deletion');

const event = 'pre-connection-deletion';
const preConnectionDeletionScripts = await onEventScriptService.getByConfig(connection.config_id, event);

if (preConnectionDeletionScripts.length === 0) {
return;
}

const provider = await configService.getProviderName(connection.provider_config_key);
const logCtx = await logContextGetter.create(
{ operation: { type: 'auth', action: 'delete_connection' } },
{
account: team,
environment: environment,
integration: { id: connection.config_id, name: connection.provider_config_key, provider: provider || 'unknown' },
connection: { id: connection.id, name: connection.connection_id }
}
);

let failed = false;
for (const script of preConnectionDeletionScripts) {
const { name, file_location: fileLocation, version } = script;

const logCtx = await logContextGetter.create(
{ operation: { type: 'events', action: 'pre_connection_deletion' }, expiresAt: defaultOperationExpiration.action() },
{
account: team,
environment: environment,
integration: { id: connection.config_id, name: connection.provider_config_key, provider: provider || 'unknown' },
connection: { id: connection.id, name: connection.connection_id },
syncConfig: { id: script.id, name: script.name },
meta: { event }
}
);

const res = await getOrchestrator().triggerOnEventScript({
connection,
version,
Expand All @@ -46,13 +51,9 @@ export async function preConnectionDeletion({
logCtx
});
if (res.isErr()) {
failed = true;
await logCtx.failed();
} else {
await logCtx.success();
}
}

if (failed) {
await logCtx.failed();
} else {
await logCtx.success();
}
}
18 changes: 16 additions & 2 deletions packages/types/lib/logs/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,16 @@ export interface OperationAction {
type: 'action';
action: 'run';
}

export interface OperationOnEvents {
type: 'events';
action: 'post_connection_creation' | 'pre_connection_deletion';
}

// TODO: rename to OperationConnection
export interface OperationAuth {
type: 'auth';
action: 'create_connection' | 'delete_connection' | 'refresh_token' | 'post_connection' | 'connection_test';
action: 'create_connection' | 'refresh_token' | 'post_connection' | 'connection_test';
}
export interface OperationAdmin {
type: 'admin';
Expand All @@ -60,7 +66,15 @@ export interface OperationDeploy {
type: 'deploy';
action: 'prebuilt' | 'custom';
}
export type OperationList = OperationSync | OperationProxy | OperationAction | OperationWebhook | OperationDeploy | OperationAuth | OperationAdmin;
export type OperationList =
| OperationSync
| OperationProxy
| OperationAction
| OperationWebhook
| OperationOnEvents
| OperationDeploy
| OperationAuth
| OperationAdmin;

/**
* Full schema
Expand Down
1 change: 1 addition & 0 deletions packages/webapp/src/pages/Logs/constants.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export const typesOptions = [
]
},
{ value: 'action', name: 'Action' },
{ value: 'events', name: 'Events' },
{ value: 'proxy', name: 'Proxy' },
{ value: 'deploy', name: 'Deploy' },
{ value: 'auth', name: 'Auth' },
Expand Down

0 comments on commit e23709b

Please # to comment.