Laravel 哨兵会减慢应用程序吗?

问题描述 投票:0回答:3

当然,如果发生异常,则哨兵服务器的响应速度无关紧要,但如果它是哨兵的日志? Api 客户端在发送日志请求之前不会收到响应?

正如我在引擎盖下看到的那样,它使用带有承诺的 Guzzle 来发送请求,但我不确定在承诺被解决/拒绝之前是否会发送响应。

php laravel promise guzzle sentry
3个回答
0
投票

更新

是的。对此它是同步的

https://github.com/getsentry/sentry-laravel/discussions/691#discussioncomment-5858747

解决方法:

如果:在异常处理程序中,您发送带有消息“意外错误”的响应,您将异常缓存在 '哨兵异常_'。获取mypid()。 然后在 laravel.com/docs/8.x/middleware#terminable-middleware 中从缓存中检索异常(如果设置)并在发送响应后将其发送给哨兵。一切都在尝试中。如果它抛出只记录异常。无论如何从缓存中删除异常。

注意: 如果进程由于内存或超时而终止,则不会使用此中间件发送异常。所以,也许下面的工作解决方案更好。

旧答案:

注意: 队列也可能不起作用,但这是一个不错的权衡。您可以记录错误,然后尝试将 ti 发送给哨兵。

如果您使用作业来报告未处理的异常和报告已处理的异常,则不会。

如果你派遣一个工作,那么你必须处理无限循环以防工作失败:https://docs.sentry.io/platforms/php/guides/laravel/usage查看队列作业。

如果它使用 curl_multi_init 那么是的,请求将花费最慢的请求。无论哪种方式,它都是 synk,阻塞响应,直到发送所有并行请求并且每个请求都收到一个答案(承诺响应)。

编辑:

哨兵有2个部分: laravel具体包:https://docs.sentry.io/platforms/php/guides/laravel/

```bash
composer require sentry/sentry-laravel
```

Enable capturing unhandled exception to report to Sentry by making the following change to your `App/Exceptions/Handler.php`:

```php {filename:App/Exceptions/Handler.php}
public function register()
{
    $this->reportable(function (Throwable $e) {
        if (app()->bound('sentry')) {
            app('sentry')->captureException($e);
        }
    });
}

Sdk(上面用到的)https://docs.sentry.io/platforms/php/

To install the SDK you will need to be using [Composer]([https://getcomposer.org/)
in your project. To install it please see the [docs](https://getcomposer.org/download/).

This is our "core" SDK, meaning that all the important code regarding error handling lives here.
If you are happy with using the HTTP client we recommend install the SDK like: [`sentry/sdk`](https://github.com/getsentry/sentry-php-sdk)

```bash
composer require sentry/sdk
```

This package (`sentry/sentry`) is not tied to any specific library that sends HTTP messages. Instead,
it uses [Httplug](https://github.com/php-http/httplug) to let users choose whichever
PSR-7 implementation and HTTP client they want to use.

If you just want to get started quickly you should run the following command:

```bash
composer require sentry/sentry php-http/curl-client
```

This is basically what our metapackage (`sentry/sdk`) provides.

This will install the library itself along with an HTTP client adapter that uses
cURL as transport method (provided by Httplug). You do not have to use those
packages if you do not want to. The SDK does not care about which transport method
you want to use because it's an implementation detail of your application. You may
use any package that provides [`php-http/async-client-implementation`](https://packagist.org/providers/php-http/async-client-implementation)
and [`http-message-implementation`](https://packagist.org/providers/psr/http-message-implementation).

### Configuration

```php
\Sentry\init(['dsn' => '___PUBLIC_DSN___' ]);
```

### Usage

```php
try {
    thisFunctionThrows(); // -> throw new \Exception('foo bar');
} catch (\Exception $exception) {
    \Sentry\captureException($exception);
}

https://develop.sentry.dev/sdk/overview/:

生产就绪的 SDK 需要以下项目:

DSN configuration
**Graceful failures (e.g. Sentry server is unreachable)**
Setting attributes (e.g. tags and extra data)
Support for Linux, Windows and OS X (where applicable)

以下需要基于功能的支持:

If cookie data is available, it’s not sent by default
If POST data is available, it’s not sent by default

此外,强烈建议使用以下功能:

Automated error capturing (e.g. uncaught exception handlers)
Logging framework integration
**Non-blocking event submission**

0
投票

简短的回答是是但不是。让我解释一下

为什么是?
如您所述,Sentry 使用

Guzzle
和异步请求将数据发送到服务器。像往常一样,它有生命周期并在后台运行在服务器内存上。

那为什么不呢?
Sentry 使用 Guzzle 并承诺在不等待响应的情况下将错误数据发送到其服务器,从而允许应用程序继续运行而无需等待进程完成。主要优点是 Sentry 可以帮助您识别性能瓶颈并通过提供对错误和异常的洞察力来提高应用程序的性能。.

与服务器性能相比,这对服务器来说简直就是小菜一碟。 (因为你已经在运行 Laravel 服务器)


优化技巧

  1. 使用
    'traces_sample_rate' => 0.5 # Send 50% of the errors
  2. 使用
    'before_send'
    跳过/过滤不需要的错误

通过配置并使用它。


例子

use Illuminate\Support\Facades\Route;

Route::get('/log', function () {
    try {
        throw new Exception('This is a test exception');
    } catch (Exception $e) {
        app('sentry')->captureException($e);
    }

    return 'Response sent to the client';
});

此代码不会等待哨兵的响应。 Laravel Sentry SDK 旨在捕获和发送异常而不阻塞应用程序的响应。 响应

'Response sent to the client'
会立即发送给客户端,不管Sentry是否处理完异常


0
投票

将 DSN 更改为我的本地 url 并为哨兵请求添加路由:

Route::post('/api//envelope',function () {
    info('sleep');
    sleep(10);
    info('end sleep');
    return 'sentry';
})->withoutMiddleware(VerifyCsrfToken::class);

然后添加调试路由:

Route::get('/debug-sentry', function () {
    try {
        throw new \Exception('This is a test exception');
    } catch (\Exception $e) {
        app('sentry')->captureException($e);
    }

    return 'Response sent to the client';
});

打开 /debug/sentry 需要 ~5s 因为:

SDK: Failed to send the event to Sentry. Reason: "Idle timeout reached for......

这证明响应不会在承诺履行之前发送并且哨兵影响应用程序性能

© www.soinside.com 2019 - 2024. All rights reserved.