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(log): handle large payload #2740

Merged
merged 5 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
40 changes: 39 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/server/lib/controllers/sync.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
import type { LogContext } from '@nangohq/logs';
import { defaultOperationExpiration, logContextGetter } from '@nangohq/logs';
import type { LastAction } from '@nangohq/records';
import { getHeaders, isHosted } from '@nangohq/utils';
import { getHeaders, isHosted, truncateJson } from '@nangohq/utils';
import { records as recordsService } from '@nangohq/records';
import type { RequestLocals } from '../utils/express.js';
import { getOrchestrator } from '../utils/utils.js';
Expand Down Expand Up @@ -303,7 +303,7 @@ class SyncController {
integration: { id: provider.id!, name: connection.provider_config_key, provider: provider.provider },
connection: { id: connection.id!, name: connection.connection_id },
syncConfig: { id: syncConfig.id!, name: syncConfig.sync_name },
meta: { input }
meta: truncateJson({ input })
}
);

Expand Down
28 changes: 21 additions & 7 deletions packages/shared/lib/sdk/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,20 @@ import axios, { AxiosError } from 'axios';
import { getPersistAPIUrl } from '../utils/utils.js';
import type { IntegrationWithCreds } from '@nangohq/node';
import type { UserProvidedProxyConfiguration } from '../models/Proxy.js';
import { getLogger, httpRetryStrategy, metrics, retryWithBackoff } from '@nangohq/utils';
import {
getLogger,
httpRetryStrategy,
metrics,
retryWithBackoff,
MAX_LOG_PAYLOAD,
stringifyAndTruncateValue,
stringifyObject,
truncateJsonString
} from '@nangohq/utils';
import type { SyncConfig } from '../models/Sync.js';
import type { RunnerFlags } from '../services/sync/run.utils.js';
import { validateData } from './dataValidation.js';
import { NangoError } from '../utils/error.js';
import { stringifyAndTruncateLog, stringifyObject } from './utils.js';
import type { DBTeam, MessageRowInsert } from '@nangohq/types';

const logger = getLogger('SDK');
Expand Down Expand Up @@ -720,7 +728,7 @@ export class NangoAction {
type: 'log',
level: oldLevelToNewLevel[level],
source: 'user',
message: stringifyAndTruncateLog(message, 99_000),
message: stringifyAndTruncateValue(message),
meta,
createdAt: new Date().toISOString(),
environmentId: this.environmentId
Expand Down Expand Up @@ -817,17 +825,23 @@ export class NangoAction {
try {
response = await retryWithBackoff(
async () => {
let data = stringifyObject({ activityLogId: this.activityLogId, log });

// We try to keep log object under an acceptable size, before reaching network
// 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);
}

return await this.persistApi({
method: 'POST',
url: `/environment/${this.environmentId}/log`,
headers: {
Authorization: `Bearer ${this.nango.secretKey}`,
'Content-Type': 'application/json'
},
data: stringifyObject({
activityLogId: this.activityLogId,
log
})
data
});
},
{ retry: httpRetryStrategy }
Expand Down
35 changes: 0 additions & 35 deletions packages/shared/lib/sdk/utils.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
"dayjs-plugin-utc": "^0.1.2",
"dd-trace": "5.21.0",
"exponential-backoff": "^3.1.1",
"fast-safe-stringify": "2.1.1",
"form-data": "4.0.0",
"js-yaml": "^4.1.0",
"jsonwebtoken": "^9.0.2",
Expand Down
5 changes: 1 addition & 4 deletions packages/shared/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,5 @@
"path": "../orchestrator"
}
],
"include": [
"lib/**/*",
"../utils/lib/vitest.d.ts",
]
"include": ["lib/**/*", "../utils/lib/vitest.d.ts"]
}
1 change: 1 addition & 0 deletions packages/utils/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export * from './environment/parse.js';
export * from './errors.js';
export * from './logger.js';
export * from './id.js';
export * from './json.js';
export * from './result.js';
export * from './encryption.js';
export * as metrics from './telemetry/metrics.js';
Expand Down
60 changes: 60 additions & 0 deletions packages/utils/lib/json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import safeStringify from 'fast-safe-stringify';
import truncateJsonPkg from 'truncate-json';

export const MAX_LOG_PAYLOAD = 99_000; // in bytes

/**
* Safely stringify an object (mostly handle circular ref and known problematic keys)
*/
export function stringifyObject(value: any): string {
return safeStringify.stableStringify(
value,
(key, value) => {
if (value instanceof Buffer || key === '_sessionCache') {
return '[Buffer]';
}
if (key === 'Authorization') {
return '[Redacted]';
}
return value;
},
undefined,
{ depthLimit: 10, edgesLimit: 20 }
);
}

/**
* Stringify and truncate unknown value
*/
export function stringifyAndTruncateValue(value: any, maxSize: number = MAX_LOG_PAYLOAD): string {
if (value === null) {
return 'null';
}
if (value === undefined) {
return 'undefined';
}

let msg = typeof value === 'string' ? value : truncateJsonString(stringifyObject(value), maxSize);

if (msg && msg.length > maxSize) {
msg = `${msg.substring(0, maxSize)}... (truncated)`;
}

return msg;
}

/**
* Truncate a JSON
* 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);
}

/**
* Truncate a JSON as string
* Will entirely remove properties that are too big
*/
export function truncateJsonString(value: string, maxSize: number = MAX_LOG_PAYLOAD): string {
return truncateJsonPkg(value, maxSize).jsonString;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

should this file be called something like stringUtils.ts instead of json?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I asked myself the same question: it depends on how you interpret the name of the files: whether the input is important or the output.
This one takes a value as string but others takes Record, either they all related to json (no matter the encoding) or I move truncateJsonString in string.ts
Wdyt?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't feel strongly either side, json is not a bad name it is just a bit generic. Another option could be logFormatUtils.ts or something similar because those functions are mostly used in the context of logging. Feel free to keep it as it is though

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think we have many spots where json stringification could be an issue (that's why I moved it there actually); I'll leave it as is

Original file line number Diff line number Diff line change
@@ -1,52 +1,52 @@
import { describe, expect, it } from 'vitest';
import { stringifyAndTruncateLog } from './utils.js';
import { stringifyAndTruncateValue } from './json.js';

describe('stringifyAndTruncateLog', () => {
describe('stringifyAndTruncateValue', () => {
it('should not break a small string', () => {
const str = stringifyAndTruncateLog('hello');
const str = stringifyAndTruncateValue('hello');
expect(str).toStrictEqual('hello');
});

it('should limit a string', () => {
const str = stringifyAndTruncateLog('hello', 2);
const str = stringifyAndTruncateValue('hello', 2);
expect(str).toStrictEqual('he... (truncated)');
});

it('should limit an object', () => {
const str = stringifyAndTruncateLog({ foo: 'bar' }, 10);
expect(str).toStrictEqual('{"foo":"ba... (truncated)');
const str = stringifyAndTruncateValue({ foo: 'bar' }, 10);
expect(str).toStrictEqual('{}');
});

it('should handle undefined', () => {
const str = stringifyAndTruncateLog(undefined);
const str = stringifyAndTruncateValue(undefined);
expect(str).toStrictEqual('undefined');
});

it('should handle null', () => {
const str = stringifyAndTruncateLog(null);
const str = stringifyAndTruncateValue(null);
expect(str).toStrictEqual('null');
});

it('should handle object', () => {
const str = stringifyAndTruncateLog({ foo: 1 });
const str = stringifyAndTruncateValue({ foo: 1 });
expect(str).toStrictEqual('{"foo":1}');
});

it('should handle array', () => {
const str = stringifyAndTruncateLog([{ foo: 1 }, 2]);
const str = stringifyAndTruncateValue([{ foo: 1 }, 2]);
expect(str).toStrictEqual('[{"foo":1},2]');
});

it('should handle circular', () => {
const obj: Record<string, any> = { foo: 'bar' };
obj['circular'] = obj;
const str = stringifyAndTruncateLog(obj);
const str = stringifyAndTruncateValue(obj);
expect(str).toStrictEqual('{"circular":"[Circular]","foo":"bar"}');
});

it('should redac known keys', () => {
const obj: Record<string, any> = { Authorization: 'super secret key' };
const str = stringifyAndTruncateLog(obj);
const str = stringifyAndTruncateValue(obj);
expect(str).toStrictEqual('{"Authorization":"[Redacted]"}');
});
});
2 changes: 2 additions & 0 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
"axios": "^1.7.4",
"dd-trace": "5.21.0",
"exponential-backoff": "3.1.1",
"fast-safe-stringify": "2.1.1",
"http-proxy-agent": "7.0.2",
"https-proxy-agent": "7.0.4",
"nanoid": "5.0.7",
"serialize-error": "11.0.3",
"truncate-json": "3.0.0",
"winston": "3.13.0",
"zod": "3.23.8"
},
Expand Down
Loading