From 14aae927f3048f89ac476364f09bd75bf253c4bb Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Tue, 24 May 2022 16:39:03 +0100 Subject: [PATCH 1/3] v7 attachments --- .../add-attachment/javascript.mdx | 80 +++++++++++-------- 1 file changed, 45 insertions(+), 35 deletions(-) diff --git a/src/includes/enriching-events/add-attachment/javascript.mdx b/src/includes/enriching-events/add-attachment/javascript.mdx index 7adec9e4f4383..d1fbce423c962 100644 --- a/src/includes/enriching-events/add-attachment/javascript.mdx +++ b/src/includes/enriching-events/add-attachment/javascript.mdx @@ -1,40 +1,50 @@ -To add an attachment, create an event processor that uploads files to the -attachment endpoint in a multipart form data request. This requires associating -the attachment with the event using its ID: +Attachments live on the `Scope`, and will be sent with all events. ```javascript -public attachmentUrlFromDsn(dsn: Dsn, eventId: string) { - const { host, path, projectId, port, protocol, user } = dsn; - return `${protocol}://${host}${port !== '' ? `:${port}` : ''}${ - path !== '' ? `/${path}` : '' - }/api/${projectId}/events/${eventId}/attachments/?sentry_key=${user}&sentry_version=7&sentry_client=custom-javascript`; -} - -Sentry.addGlobalEventProcessor((event: Event) => { - try { - const client = Sentry.getCurrentHub().getClient(); - const endpoint = attachmentUrlFromDsn( - client.getDsn(), - event.event_id - ); - const formData = new FormData(); - formData.append( - 'my-attachment', - new Blob([JSON.stringify({ logEntries: ["my log"] })], { - type: 'application/json', - }), - 'logs.json' - ); - fetch(endpoint, { - method: 'POST', - body: formData, - }).catch((ex) => { - // we have to catch this otherwise it throws an infinite loop in Sentry - console.error(ex); - }); +// Add an attachment +Sentry.configureScope(scope => { + scope.addAttachment({ filename: "attachment.txt", data: "Some content" }); +}); + +// Clear attachments +Sentry.configureScope(scope => { + scope.clearAttachments(); +}); +``` + +An attachment has the following fields: + +`filename` + +: The filename is required and will be displayed in Sentry. + +`data` + +: The content of the attachment is required and is either a `string` or `Uint8Array`. + +`contentType` + +: The type of content stored in this attachment. Any [MIME +type](https://www.iana.org/assignments/media-types/media-types.xhtml) may be +used; the default is `application/octet-stream`. + +## Add or Modify Attachments Before Sending + +It's possible to add, remove or modify attachments before an event is sent via +the [`beforeSend`](/platform-redirect/?next=/configuration/options/#before-send) +hook or a global event processor. + +```javascript +Sentry.init({ + dsn: "___PUBLIC_DSN___", + beforeSend: (event, hint) => { + hint.attachments = [{ filename: "screenshot.png", data: captureScreen() }]; return event; - } catch (ex) { - console.error(ex); - } + }, +}); + +Sentry.addGlobalEventProcessor((event, hint) => { + hint.attachments = [{ filename: "log.txt", data: readLogFile() }]; + return event; }); ``` From b54d34de5f8f7d68e5b50ee83bfffd6b64a7be03 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Tue, 24 May 2022 17:25:02 +0100 Subject: [PATCH 2/3] Fix link --- src/includes/enriching-events/add-attachment/javascript.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/includes/enriching-events/add-attachment/javascript.mdx b/src/includes/enriching-events/add-attachment/javascript.mdx index d1fbce423c962..14fc6e6b2befc 100644 --- a/src/includes/enriching-events/add-attachment/javascript.mdx +++ b/src/includes/enriching-events/add-attachment/javascript.mdx @@ -31,7 +31,7 @@ used; the default is `application/octet-stream`. ## Add or Modify Attachments Before Sending It's possible to add, remove or modify attachments before an event is sent via -the [`beforeSend`](/platform-redirect/?next=/configuration/options/#before-send) +the hook or a global event processor. ```javascript From 716ff130e74461ed7bd451cd8c3466ad0e2b76a2 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Wed, 25 May 2022 16:02:46 +0100 Subject: [PATCH 3/3] Apply suggestions from code review Co-authored-by: Isabel <76437239+imatwawana@users.noreply.github.com> --- src/includes/enriching-events/add-attachment/javascript.mdx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/includes/enriching-events/add-attachment/javascript.mdx b/src/includes/enriching-events/add-attachment/javascript.mdx index 14fc6e6b2befc..a69c3abd7fd1c 100644 --- a/src/includes/enriching-events/add-attachment/javascript.mdx +++ b/src/includes/enriching-events/add-attachment/javascript.mdx @@ -1,4 +1,4 @@ -Attachments live on the `Scope`, and will be sent with all events. +Attachments live on the `Scope` and will be sent with all events. ```javascript // Add an attachment @@ -16,7 +16,7 @@ An attachment has the following fields: `filename` -: The filename is required and will be displayed in Sentry. +: The filename is required and will be displayed in [sentry.io](https://sentry.io). `data` @@ -30,7 +30,7 @@ used; the default is `application/octet-stream`. ## Add or Modify Attachments Before Sending -It's possible to add, remove or modify attachments before an event is sent via +It's possible to add, remove, or modify attachments before an event is sent by way of the hook or a global event processor.