PHP项目Laravel Telescope请求慢阈值

wen PHP项目 7

本文目录导读:

PHP项目Laravel Telescope请求慢阈值

  1. 配置文件位置
  2. 设置慢请求阈值
  3. 常用的慢请求配置选项
  4. 使用环境变量
  5. 高级配置示例
  6. 动态修改阈值
  7. 检查当前配置
  8. 注意事项

在 Laravel Telescope 中,你可以通过配置文件来设置请求慢阈值,以下是详细的配置方法:

配置文件位置

配置文件位于 config/telescope.php 中。

设置慢请求阈值

<?php
return [
    // ... 其他配置
    /*
    |--------------------------------------------------------------------------
    | 请求监视配置
    |--------------------------------------------------------------------------
    |
    | 这里可以配置请求监控的各种参数,包括慢请求阈值等
    |
    */
    'watchers' => [
        Watchers\RequestWatcher::class => [
            // 慢请求阈值(毫秒)
            'size_limit' => 5000, // 5秒
            // 或者使用严格模式
            'strict' => false,
        ],
    ],
];

常用的慢请求配置选项

基础设置

'watchers' => [
    Watchers\RequestWatcher::class => [
        // 请求大小限制(KB)
        'size_limit' => 5000,
        // 是否记录慢请求
        'slow_response' => true,
        // 慢请求响应时间阈值(毫秒)
        'slow_response_threshold' => 1000, // 1秒
        // 记录请求体
        'record_body' => true,
        // 记录请求头
        'record_headers' => true,
    ],
],

按环境配置

'watchers' => [
    Watchers\RequestWatcher::class => [
        'enabled' => env('TELESCOPE_REQUEST_WATCHER', true),
        // 生产环境的阈值
        'slow_response_threshold' => env('TELESCOPE_SLOW_REQUEST_THRESHOLD', 2000),
        // 本地开发环境的阈值
        'local_slow_response_threshold' => 500,
        // 是否在非调试模式下记录慢请求
        'record_slow_requests_only' => false,
    ],
],

使用环境变量

.env 文件中配置:

# 设置慢请求阈值(毫秒)
TELESCOPE_SLOW_REQUEST_THRESHOLD=1500
# 自定义其他配置
TELESCOPE_REQUEST_SIZE_LIMIT=10000

高级配置示例

// config/telescope.php
'watchers' => [
    Watchers\RequestWatcher::class => [
        'enabled' => true,
        // 慢请求阈值(毫秒)
        'slow_response_threshold' => 1000,
        // 请求大小限制(KB)
        'size_limit' => 10000,
        // 要排除的路径
        'exclude_paths' => [
            'api/ping',
            'health-check',
            'telescope*',
        ],
        // 要记录的方法
        'methods' => ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'],
        // 是否记录表单数据
        'record_form_data' => true,
        // 是否记录文件上传
        'record_files' => true,
        // 是否记录 cookie
        'record_cookies' => false,
    ],
],

动态修改阈值

你还可以在运行时动态设置:

use Laravel\Telescope\Telescope;
Telescope::recordRequest(function ($watcher) {
    // 动态设置慢请求阈值
    $watcher->slowResponseThreshold(2000);
    // 或按请求条件设置
    if (request()->is('api/export/*')) {
        $watcher->slowResponseThreshold(10000);
    }
});

检查当前配置

# 查看 Telescope 配置
php artisan config:show telescope
# 清除配置缓存
php artisan config:clear

注意事项

  1. 默认阈值通常设置为 1 秒(1000 毫秒)
  2. 生产环境建议设置较高的阈值(2-5 秒)
  3. 可以根据不同的路由或控制器设置不同的阈值
  4. 通过 Telescope 仪表板可以查看历史请求的响应时间

这样配置后,Telescope 就会根据你的阈值设置来标记和记录慢请求了。

抱歉,评论功能暂时关闭!