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

feat: Add attachments API #5004

Merged
merged 34 commits into from
May 18, 2022
Merged

feat: Add attachments API #5004

merged 34 commits into from
May 18, 2022

Conversation

timfish
Copy link
Collaborator

@timfish timfish commented Apr 28, 2022

Closes #4996

This PR:

  • Adds Attachment interface and extends AttachmentItemHeaders with required fields
  • Adds addAttachment, getAttachments and clearAttachments to Scope
  • Adds attachments: Attachment[] to EventHint
  • In the private capture/process/prepare/send methods in the BaseClient, the EventHint argument has moved as it's no longer optional
    • We can't mutate the hint if it's undefined!
  • Copies attachments from the scope to the hint in _prepareEvent
  • Adds optional textEncoder to InternalBaseTransportOptions and overrides this in the node client to support node 8-10
    • Manually pass new TextEncoder() in many of the tests so they pass on node.js 8-10
  • Adds binary serialisation support for envelopes
    • serializeEnvelope returns string | Uint8Array which all transports supported without modification
    • Defaults to concatenating strings when no attachments are found. String concatenation is about 10x faster than the binary serialisation
  • Rewrites parseEnvelope in testutils.ts so that it can parse binary envelopes
    • Is there a common test utils so this can be used elsewhere?

Bonus

  • This will significantly simplify sending minidumps from the Electron SDK

@AbhiPrasad AbhiPrasad added this to the 7.0.0 milestone Apr 28, 2022
@AbhiPrasad
Copy link
Member

AbhiPrasad commented Apr 28, 2022

We could move the envelope serialisation to the client so we can have multiple implementations?

I like this idea. I think that it should be client defined, and we pass it into the transport as an option. We did something similar with @lforst's work on client reports. See: #5008

// core/src/baseclient.ts
this._transport = options.transport({
  serializeEnvelope: mySpecificSerializeEnvelopeImpl,
  ...options.transportOptions,
  url,
});

What to do about attachment rate limits?

The changes in #5008 should mean that we respect rate limits per envelope items. See:

// Drop rate limited items from envelope
forEachEnvelopeItem(envelope, (item, type) => {
const envelopeItemDataCategory = envelopeItemTypeToDataCategory(type);
if (isRateLimited(rateLimits, envelopeItemDataCategory)) {
options.recordDroppedEvent('ratelimit_backoff', envelopeItemDataCategory);
} else {
filteredEnvelopeItems.push(item);
}
});

This will significantly simplify sending minidumps from the Electron SDK

Awesome ❤️

This was a quick first pass - I'll come back and do another review tomorrow when I get some time :)

@timfish timfish force-pushed the feat/attachments branch from c16c1fa to 8d717d5 Compare May 1, 2022 04:50
@timfish timfish force-pushed the feat/attachments branch from 8d717d5 to 23f4b29 Compare May 1, 2022 04:58
@timfish
Copy link
Collaborator Author

timfish commented May 1, 2022

We don't just need TextEncoder when serialising the envelope, we also need it outside the transport when creating the attachment envelope items. Attachments must include a length (in case that attachment contains newlines) and the easiest way to know the byte length of a utf8 string is to just convert it to bytes.

@timfish timfish force-pushed the feat/attachments branch 5 times, most recently from 35fd7ea to 2788c6c Compare May 1, 2022 22:11
@timfish timfish force-pushed the feat/attachments branch from 2788c6c to fe7f49f Compare May 1, 2022 22:47
@timfish

This comment was marked as resolved.

@timfish
Copy link
Collaborator Author

timfish commented May 16, 2022

@AbhiPrasad I think this is ready for a 2nd pass.

It definitely still needs some tests to cover the changes around mutable hints + beforeSend + event processors.

Copy link
Member

@AbhiPrasad AbhiPrasad left a comment

Choose a reason for hiding this comment

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

Were we able to test if babel will polyfill TextEncoder if they set the according browserslist?

@@ -86,7 +86,7 @@ export function getNativeFetchImplementation(): FetchImpl {
* @param url report endpoint
* @param body report payload
*/
export function sendReport(url: string, body: string): void {
export function sendReport(url: string, body: string | Uint8Array): void {
Copy link
Member

Choose a reason for hiding this comment

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

Why are we changing the func signature here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

serializeEnvelope now returns string | Uint8Array. In this case it will never actually be a Uint8Array because there's no attachment but both sendBeacon and fetch take body: string | Uint8Array so it seemed the best thing to do.

packages/core/src/baseclient.ts Outdated Show resolved Hide resolved
packages/core/src/baseclient.ts Outdated Show resolved Hide resolved
packages/core/src/baseclient.ts Outdated Show resolved Hide resolved
packages/node/src/transports/http.ts Outdated Show resolved Hide resolved
@timfish
Copy link
Collaborator Author

timfish commented May 16, 2022

Were we able to test if babel will polyfill TextEncoder if they set the according browserslist?

It looks like babel now just expects you to import core-js if you want things polyfilled.

@AbhiPrasad
Copy link
Member

Awesome, I think after the transport changes get extracted, we should be good to go to merge this in!

@timfish
Copy link
Collaborator Author

timfish commented May 17, 2022

I think after the transport changes get extracted, we should be good to go to merge this in!

That is done already!

@AbhiPrasad
Copy link
Member

I think after the transport changes get extracted, we should be good to go to merge this in!

That is done already!

Woooooops

simpson

Copy link
Member

@AbhiPrasad AbhiPrasad left a comment

Choose a reason for hiding this comment

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

I think this is a good starting point for us - @getsentry/team-web-sdk-frontend I would appreciate another set of eyes to confirm!

Copy link
Member

@lforst lforst left a comment

Choose a reason for hiding this comment

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

Looks great. Thank you Tim!

@timfish
Copy link
Collaborator Author

timfish commented May 17, 2022

How quickly can we get this into a release so I can test it all in this PR 😆

@AbhiPrasad
Copy link
Member

I think we can merge tommorow, so get it out in a release by end of this week!

@AbhiPrasad AbhiPrasad merged commit 457b806 into getsentry:7.x May 18, 2022
@timfish timfish deleted the feat/attachments branch May 19, 2022 12:35
@lforst lforst mentioned this pull request May 24, 2022
AbhiPrasad pushed a commit that referenced this pull request May 30, 2022
- Adds `Attachment` interface and extends `AttachmentItemHeaders` with required fields
- Adds `addAttachment`, `getAttachments` and `clearAttachments` to `Scope`
- Adds `attachments: Attachment[]` to `EventHint`
- In the private capture/process/prepare/send methods in the `BaseClient`, the `EventHint` argument has moved as it's no longer optional
  - We can't mutate the hint if it's undefined!
- Copies attachments from the scope to the hint in `_prepareEvent`
- Adds optional `textEncoder` to `InternalBaseTransportOptions` and overrides this in the node client to support node 8-10
  - Manually pass `new TextEncoder()` in many of the tests so they pass on node.js 8-10
- Adds binary serialisation support for envelopes
  - `serializeEnvelope` returns `string | Uint8Array` which all transports supported without modification
  - Defaults to concatenating strings when no attachments are found. String concatenation is about 10x faster than the binary serialisation 
- Rewrites `parseEnvelope` in `testutils.ts` so that it can parse binary envelopes
@AbhiPrasad AbhiPrasad mentioned this pull request May 30, 2022
@pnutmath
Copy link

pnutmath commented Jun 8, 2022

After upgrading sentry to v7 getting below error. I am using sentry with node express project - @timfish @AbhiPrasad

  message: 'finalScope.getAttachments is not a function or its return value is not iterable',
  code: undefined,
  sentry: '888a4d2d434b4e1c9b5dcea80eb99791',
  stack: 'TypeError: finalScope.getAttachments is not a function or its return value is not iterable\n' +
    '    at NodeClient._prepareEvent (/srv/server/node_modules/@sentry/core/cjs/baseclient.js:403:69)\n' +
    '    at NodeClient._prepareEvent (/srv/server/node_modules/@sentry/node/cjs/client.js:151:18)\n' +
    '    at NodeClient._processEvent (/srv/server/node_modules/@sentry/core/cjs/baseclient.js:598:17)\n' +
    '    at NodeClient._captureEvent (/srv/server/node_modules/@sentry/core/cjs/baseclient.js:554:17)\n' +
    '    at NodeClient.captureEvent (/srv/server/node_modules/@sentry/core/cjs/baseclient.js:147:12)\n' +
    '    at NodeClient.captureEvent (/srv/server/node_modules/@sentry/node/cjs/client.js:93:18)\n' +
    '    at Hub._invokeClient (/srv/server/node_modules/winston-sentry-log/node_modules/@sentry/hub/dist/hub.js:384:35)\n' +
    '    at Hub.captureEvent (/srv/server/node_modules/winston-sentry-log/node_modules/@sentry/hub/dist/hub.js:171:14)\n' +
    '    at Transaction.finish (/srv/server/node_modules/@sentry/tracing/cjs/transaction.js:142:22)\n' +
    '    at Immediate.<anonymous> (/srv/server/node_modules/@sentry/node/cjs/handlers.js:51:21)\n' +
    '    at processImmediate (internal/timers.js:464:21)\n' +
    '    at process.topLevelDomainCallback (domain.js:152:15)\n' +
    '    at process.callbackTrampoline (internal/async_hooks.js:128:24)'

@lforst
Copy link
Member

lforst commented Jun 8, 2022

Hi @pnutmath I moved your comment into a separate issue #5229

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants