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

Add docs for new attachments API #5073

Merged
merged 3 commits into from
May 30, 2022
Merged
Changes from 2 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
80 changes: 45 additions & 35 deletions src/includes/enriching-events/add-attachment/javascript.mdx
Original file line number Diff line number Diff line change
@@ -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 <PlatformLink to="/configuration/options/#before-send"><PlatformIdentifier name="before-send" /></PlatformLink>
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;
});
```