crypt4/jantung-php

1.0.1 2023-06-17 22:15 UTC

This package is auto-updated.

Last update: 2024-09-18 00:51:52 UTC


README

Build Status

Jantung PHP 客户端

Jantung 是一个简单的错误追踪器,用于监控您的应用程序崩溃。此包为 PHP 开发。

安装

composer require crypt4/jantung-php

添加新指标

您可以根据需要在您的应用程序/框架中添加新指标。

请注意,所有指标都将转换为关联数组。

为了创建自己的指标,您需要扩展类 Crypt4\Jantung\Metric\Base 并在 metrics() 方法中实现您的指标详细信息,该方法始终返回一个数组。您可能需要在指标中定义为点表示法。

但是,Jantung 将将其转换为关联数组。

以下是一个为 Laravel 框架捕获 HTTP 请求的示例。

<?php

namespace App\Metric;

use Crypt4\Jantung\Support\Arr;
use Crypt4\Jantung\Metric\Base;
use Illuminate\Support\Str;

class Http extends Base
{
    public function metrics(): array
    {
        $startTime = defined('LARAVEL_START') ? LARAVEL_START : request()->server('REQUEST_TIME_FLOAT');

        return [
            'http.client.duration' => $startTime ? floor((microtime(true) - $startTime) * 1000) : null,
            'http.scheme' => request()->getScheme(),
            'http.route' => request()->getRequestUri(),
            'http.method' => request()->getMethod(),
            'http.status_code' => http_response_code(),
            'http.query' => request()->getQueryString(),
            'http.uri' => str_replace(request()->root(), '', request()->fullUrl()) ?: '/',
            'http.headers' => Arr::undot(collect(request()->headers->all())
                ->map(function ($header) {
                    return $header[0];
                })
                ->reject(function ($header, $key) {
                    return in_array($key, [
                        'authorization', config('jantung.header-key'), 'jantung-key',
                    ]);
                })
                ->toArray()),
        ];
    }
}

一旦您声明了您的指标,您就可以在您的应用程序中使用它

use App\Metrics\Http;
use Crypt4\Jantung\Metric\Metric;

$metric = new Metric();

$metric->add(new Http());

$metric->toArray();

如果您是从 Laravel 框架添加,您只需将其添加到 config/jantung.php

'metrics' => [
    \App\Metrics\Http::class,
];

类图