上下文

自定义上下文允许您将任意数据(字符串、列表、字典)附加到事件。

自定义上下文允许您将任意数据附加到事件。通常,此上下文在其生命周期内捕获的任何问题中共享。您无法搜索这些数据,但可以在问题页面上查看:

Custom contexts as viewed on the Additional Data section of an event

如果您需要能够搜索自定义数据,请使用 标签

附加自定义数据的最佳方式是使用结构化上下文。上下文必须始终是一个对象,其值可以是任意的。

然后,使用 set_context 并给上下文一个唯一名称:

The configureScope helper will setup the scope for all events being captured by the Sentry SDK.

Copied
\Sentry\configureScope(function (\Sentry\State\Scope $scope): void {
    $scope->setContext('character', [
        'name' => 'Mighty Fighter',
        'age' => 19,
        'attack_type' => 'melee'
    ]);
});

If you need to modify the scope for a single event, you can use the withScope helper instead, which does not keep the scope changes made.

Copied
\Sentry\withScope(function (\Sentry\State\Scope $scope) use ($e): void {
    $scope->setContext('character', [
        'name' => 'Mighty Fighter',
        'age' => 19,
        'attack_type' => 'melee'
    ]);

    \Sentry\captureMessage('The fighter is out of energy!');
    // or: \Sentry\captureException($e);
});

对上下文名称没有限制。在上下文对象中,所有键都是允许的,除了 type,因为它用于内部用途。

了解更多关于常见上下文的约定,请参阅 上下文接口开发者文档

在发送上下文时,请考虑有效负载大小限制。Sentry 不建议在上下文中发送整个应用程序状态和大数据块。如果超出最大有效负载大小,Sentry 将响应 HTTP 错误 413 Payload Too Large 并拒绝事件。

Sentry SDK 会尽量适应您发送的数据并修剪大的上下文有效负载。某些 SDK 可以截断事件的部分内容;更多详情,请参阅 SDK 数据处理开发者文档

额外数据已弃用,建议使用结构化上下文。

Sentry 以前支持通过 set_extra 添加非结构化的“额外数据”。