Skip to content

Commit

Permalink
fix(es): add indexed retry object for http logs (#3543)
Browse files Browse the repository at this point in the history
## Changes

Contributes to
https://linear.app/nango/issue/NAN-2736/new-retry-strategy-for-proxy
Contributes to
https://linear.app/nango/issue/NAN-2683/log-retries-in-a-way-that-it-can-be-use-to-graph

- Prepare the **retry** field
It will contain information related to each http log, whether they are
part of a retry, the attempt number, and the previous wait time. It's
all based on theoretical test so hopefully I nailed it.
Note it's the first undefined field I create, I realized nullable field
are not ideal in terms of storage cost, so I had to change also some
types
  • Loading branch information
bodinsamuel authored Feb 20, 2025
1 parent 2f8dec2 commit 04abcd3
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 5 deletions.
13 changes: 13 additions & 0 deletions packages/logs/lib/es/__snapshots__/index.integration.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,19 @@ exports[`mapping > should create one index automatically on log 1`] = `
},
},
},
"retry": {
"properties": {
"attempt": {
"type": "integer",
},
"max": {
"type": "integer",
},
"waited": {
"type": "integer",
},
},
},
"source": {
"type": "keyword",
},
Expand Down
8 changes: 7 additions & 1 deletion packages/logs/lib/es/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,19 @@ const props: Record<keyof MessageRow, estypes.MappingProperty> = {
headers: { type: 'object', enabled: false }
}
},

response: {
properties: {
code: { type: 'integer' },
headers: { type: 'object', enabled: false }
}
},
retry: {
properties: {
max: { type: 'integer' },
attempt: { type: 'integer' },
waited: { type: 'integer' }
}
},

createdAt: { type: 'date' },
updatedAt: { type: 'date' },
Expand Down
5 changes: 4 additions & 1 deletion packages/logs/lib/models/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ export function getFormattedOperation(
meta: meta || data.meta || null,

userId: user?.id || data.userId || null,
parentId: null
parentId: null,

expiresAt: data.expiresAt || defaultOperationExpiration.sync()
};
}
export function getFormattedMessage(data: Partial<MessageRow>, { meta }: FormatMessageData = {}): MessageRow {
Expand Down Expand Up @@ -88,6 +90,7 @@ export function getFormattedMessage(data: Partial<MessageRow>, { meta }: FormatM
error: data.error || null,
request: data.request || null,
response: data.response || null,
retry: data.retry || undefined,
meta: meta || data.meta || null,

createdAt: data.createdAt || now.toISOString(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ export const logBodySchema = z.object({
.optional()
.default(null),
meta: z.record(z.any()).nullable().optional().default(null),
createdAt: z.string()
createdAt: z.string(),
retry: z.object({ max: z.number(), waited: z.number(), attempt: z.number() }).optional()
})
});

Expand Down
16 changes: 14 additions & 2 deletions packages/types/lib/logs/messages.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Merge } from 'type-fest';
import type { Merge, SetNonNullable } from 'type-fest';

/**
* Level of the log and operation
Expand Down Expand Up @@ -138,6 +138,13 @@ export interface MessageRow {
headers: Record<string, string>;
} | null;
meta: MessageMeta | null;
retry?:
| {
attempt: number;
max: number;
waited: number;
}
| undefined;

// Dates
createdAt: string;
Expand All @@ -151,7 +158,12 @@ export interface MessageRow {
* What is required to insert a Message
*/
export type OperationRowInsert = Omit<Merge<Partial<MessageRow>, { operation: OperationList }>, 'message'>;
export type OperationRow = Merge<Required<OperationRowInsert>, { message: string; accountId: number; accountName: string }>;
export type OperationRow = SetNonNullable<
Omit<MessageRow, 'operation'>,
'id' | 'message' | 'source' | 'createdAt' | 'accountId' | 'accountName' | 'type' | 'level' | 'expiresAt' | 'state'
> & {
operation: OperationList;
};

/**
* What is required to insert a Message
Expand Down

0 comments on commit 04abcd3

Please sign in to comment.