获取更多动态,关注公众号:黑客下午茶

Attachments

Attention: This is a preview API available since 4.0.0-alpha.2. This API is not in stable state yet. It may be renamed, changed or even removed in a future version.

Be aware that attachments don't work yet with crashes.

Sentry 可以通过在事件旁边存储其他文件(例如日志文件)作为附件来增强崩溃报告。 附件使崩溃中的文件不仅可以上传到Sentry,而且可以持久保存以进行进一步调查。

Creating Attachments

The simplest way to create an attachment is to use a path. The SDK will read the contents of the file each time it prepares an event or transaction, then adds the attachment to the same envelope. If the SDK can't read the file, the SDK logs an error message and drops the attachment.

Copied
import io.sentry.Sentry;
import io.sentry.Attachment;

Attachment attachment = new Attachment("your/path/file.log");

Alternately, use bytes to initialize an attachment. When doing so, you also need to specify a filename.

Copied
import io.sentry.Sentry;
import io.sentry.Attachment;

Attachment attachment = new Attachment(bytes, "file.log");

If your SDK supports offline caching, which is typical for mobile, each attachment is stored to disk for each event or transaction you capture when the device is offline. When using large attachments, this storage can consume the disk space if the device is offline for a longer time period. You can specify maximum attachment size to drop large attachments and avoid this issue.

In addition, you can set these parameters:

filename
The filename is the name of the file to display in Sentry. When using bytes you have to specify a filename, whereas with a path you don't as the SKD is going to use the last path component.

contentType
The type of content stored in this attachment. Any MIME type may be used; the default is application/octet-stream.

Uploading Attachments

Attachments live on the Scope. You can either add an attachment on the global scope to be sent with every event or add it on the local Scope to just send the attachment with one specific event.

Copied
import io.sentry.Sentry;
import io.sentry.Attachment;

Attachment fileAttachment = new Attachment("your/path/file.log");

// Global Scope
Sentry.configureScope(
    scope -> {
      scope.addAttachment(fileAttachment);
    });

// Local Scope
Sentry.withScope(
      scope -> {
          scope.addAttachment(fileAttachment);

          Sentry.captureMessage("my message");
      });

附件存留 30 天;如果超出了配额中包含的总存储空间,则不会存储附件。您可以随时删除附件或其包含的事件。删除附件不会影响您的配额(quota) - Sentry 存储附件后立即将其计入您的配额。

详细了解附件如何影响您的 quota

Access to Attachments

要限制对附件的访问,请导航到您组织的 General Settings,然后选择 Attachments Access 下拉菜单来设置适当的访问权限 - 您的组织的任何成员、组织帐单所有者、成员(member)、管理员(admin)、经理(manager)或所有者(owner)。

Attachments Access

默认情况下,启用存储后,将授予所有成员访问权限。如果成员无权访问该项目,则无法下载附件。该按钮将在 Sentry 中显示为灰色。成员只能查看附件已存储。

Viewing Attachments

附件显示在所显示事件的 Issue Details 页面的底部。

Attachments Access Denied

或者,附件也会显示在 Issue Details 页面上的 Attachments 选项卡中,您可以在其中查看附件的类型以及相关事件。单击 Event ID,以打开该特定事件的 Issue Details 信息。

Attachments List Example

Maximum Attachment Size

The maximum size for each attachment is set on SentryOptions.maxAttachmentSize. The scale is bytes and the default is 20 MiB. Please also check the maximum attachment size of Relay to make sure your attachments don't get discarded there.

Copied
import io.sentry.Sentry;

Sentry.init(options -> {
    options.setMaxAttachmentSize(5 * 1024 * 1024); // 5 MiB
});
You can edit this page on GitHub.