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(utils): Normalize when serializing envelope #5851

Merged
merged 2 commits into from Sep 29, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 18 additions & 2 deletions packages/utils/src/envelope.ts
Expand Up @@ -8,6 +8,7 @@ import {
TextEncoderInternal,
} from '@sentry/types';

import { normalize } from './normalize';
import { dropUndefinedKeys } from './object';

/**
Expand Down Expand Up @@ -67,9 +68,24 @@ export function serializeEnvelope(envelope: Envelope, textEncoder?: TextEncoderI
}

for (const item of items) {
const [itemHeaders, payload] = item as typeof items[number];
const [itemHeaders, payload] = item;

append(`\n${JSON.stringify(itemHeaders)}\n`);
append(typeof payload === 'string' || payload instanceof Uint8Array ? payload : JSON.stringify(payload));

if (typeof payload === 'string' || payload instanceof Uint8Array) {
append(payload);
} else {
let stringifiedPayload: string;
try {
stringifiedPayload = JSON.stringify(payload);
} catch (e) {
// In case, despite all our efforts to keep `payload` circular-dependency-free, `JSON.strinify()` still
// fails, we try again after normalizing it again with infinite normalization depth. This of course has a
// performance impact but in this case a performance hit is better than throwing.
stringifiedPayload = JSON.stringify(normalize(payload));
Copy link
Member

Choose a reason for hiding this comment

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

Do you think we should throw an error to Sentry as well here? Maybe attach the event_id so folks can report back about why this is happening (like we can inspect the fully normalized payload).

Copy link
Member Author

Choose a reason for hiding this comment

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

It would be nice to log, what parts of the payload contained the circular reference, just so we can close these gaps. We discussed some ideas:

  • Capture an additional exception here with the circ-dep path and let users report back to us.
  • Set a specific field in extra and show a warning on the Sentry product to report back to us.
  • Set a specific field in the payload with the circ-dep path and log that field in relay.

}
append(stringifiedPayload);
}
}

return typeof parts === 'string' ? parts : concatBuffers(parts);
Expand Down