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

fix(logs): truncate only non required properties #2751

Merged
merged 4 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
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
10 changes: 8 additions & 2 deletions packages/shared/lib/sdk/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
MAX_LOG_PAYLOAD,
stringifyAndTruncateValue,
stringifyObject,
truncateJsonString
truncateJson
} from '@nangohq/utils';
import type { SyncConfig } from '../models/Sync.js';
import type { RunnerFlags } from '../services/sync/run.utils.js';
Expand Down Expand Up @@ -835,7 +835,13 @@ export class NangoAction {
// The idea is to always log something instead of silently crashing without overloading persist
if (data.length > MAX_LOG_PAYLOAD) {
log.message += ` ... (truncated, payload was too large)`;
data = truncateJsonString(stringifyObject({ activityLogId: this.activityLogId, log }), MAX_LOG_PAYLOAD);
// Truncating can remove mandatory field so we only try to truncate meta
if (log.meta) {
data = stringifyObject({
activityLogId: this.activityLogId,
log: { ...log, meta: truncateJson(log.meta) as MessageRowInsert['meta'] }
});
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

should we also truncate request and response?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

mmh I didn't think about that indeed. It's annoying because truncating a json object is really slow, I wish we had a way to do path traversal and determine length without having to stringify each node.
Let's see if we need after this PR

}

return await this.persistApi({
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/lib/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export function stringifyAndTruncateValue(value: any, maxSize: number = MAX_LOG_
* Will entirely remove properties that are too big
*/
export function truncateJson(value: Record<string, any>, maxSize: number = MAX_LOG_PAYLOAD) {
return JSON.parse(truncateJsonPkg(JSON.stringify(value), maxSize).jsonString);
return JSON.parse(truncateJsonPkg(stringifyObject(value), maxSize).jsonString);
}

/**
Expand Down
Loading