@error-reporting/php 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +19 -2
- package/composer.json +6 -0
- package/package.json +16 -4
- package/sdk.php +110 -0
package/README.md
CHANGED
|
@@ -1,3 +1,20 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @error-reporting/php
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
需要 PHP 8.0 或以上,启用 URL stream,使用 HTTPS 时需要 OpenSSL。把 npm 安装目录与应用一起部署。
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
npm install @error-reporting/php
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
```php
|
|
10
|
+
require __DIR__ . '/node_modules/@error-reporting/php/sdk.php';
|
|
11
|
+
|
|
12
|
+
\ErrorReporting\Init(['dsn' => $dsn, 'environment' => 'production', 'release' => 'git-sha']);
|
|
13
|
+
|
|
14
|
+
// 在 catch 块里补报已处理的异常。
|
|
15
|
+
\ErrorReporting\CaptureException($error, ['requestId' => 'request-42']);
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
`Init()` 也可从 `ERROR_REPORTING_DSN`、`ERROR_REPORTING_ENVIRONMENT` 和 `ERROR_REPORTING_RELEASE` 读取配置,
|
|
19
|
+
并接入当前进程的未捕获异常处理。Web 请求在应用引导阶段初始化一次。
|
|
20
|
+
`CaptureException` 同步发送,成功返回事件 ID,失败返回 `null`。
|
package/composer.json
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@error-reporting/php",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Error Reporting Platform SDK for PHP",
|
|
5
|
+
"files": [
|
|
6
|
+
"sdk.php",
|
|
7
|
+
"composer.json"
|
|
8
|
+
],
|
|
9
|
+
"publishConfig": {
|
|
10
|
+
"access": "public",
|
|
11
|
+
"registry": "https://registry.npmjs.org/"
|
|
12
|
+
},
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://gitee.com/qglove/error-reporting-platform.git",
|
|
16
|
+
"directory": "packages/native/php"
|
|
17
|
+
}
|
|
18
|
+
}
|
package/sdk.php
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
declare(strict_types=1);
|
|
3
|
+
|
|
4
|
+
namespace ErrorReporting;
|
|
5
|
+
|
|
6
|
+
/** Configure the SDK once and chain existing uncaught exception handling.
|
|
7
|
+
* @param array{dsn?: string, environment?: string, release?: string} $options SDK configuration.
|
|
8
|
+
*/
|
|
9
|
+
function Init(array $options = []): void
|
|
10
|
+
{
|
|
11
|
+
Client::init($options);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** Send an exception, returning the event ID when the receiver accepts it.
|
|
15
|
+
* @param \Throwable $error Exception to report.
|
|
16
|
+
* @param array{requestId?: string} $context Optional request correlation.
|
|
17
|
+
*/
|
|
18
|
+
function CaptureException(\Throwable $error, array $context = []): ?string
|
|
19
|
+
{
|
|
20
|
+
return Client::capture($error, $context);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** @internal Implementation of the PHP SDK entry points. */
|
|
24
|
+
final class Client
|
|
25
|
+
{
|
|
26
|
+
private static ?array $config = null;
|
|
27
|
+
|
|
28
|
+
/** Configure the HTTP endpoint and install the exception handler. */
|
|
29
|
+
public static function init(array $options): void
|
|
30
|
+
{
|
|
31
|
+
if (self::$config !== null) {
|
|
32
|
+
throw new \LogicException('error reporting is already initialized');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
$dsn = parse_url(trim($options['dsn'] ?? (getenv('ERROR_REPORTING_DSN') ?: '')));
|
|
36
|
+
if (!$dsn || !in_array($dsn['scheme'] ?? '', ['http', 'https'], true) || empty($dsn['host'])
|
|
37
|
+
|| !preg_match('/^[a-zA-Z0-9]+$/D', $dsn['user'] ?? '')
|
|
38
|
+
|| !preg_match('~^/[0-9]{1,24}/?$~D', $dsn['path'] ?? '')
|
|
39
|
+
|| isset($dsn['pass']) || isset($dsn['query']) || isset($dsn['fragment'])) {
|
|
40
|
+
throw new \InvalidArgumentException('invalid error reporting DSN');
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
$port = isset($dsn['port']) ? ':' . $dsn['port'] : '';
|
|
44
|
+
self::$config = [
|
|
45
|
+
'endpoint' => $dsn['scheme'] . '://' . $dsn['host'] . $port . '/api/' . trim($dsn['path'], '/') . '/envelope/',
|
|
46
|
+
'key' => $dsn['user'],
|
|
47
|
+
'environment' => $options['environment'] ?? (getenv('ERROR_REPORTING_ENVIRONMENT') ?: 'development'),
|
|
48
|
+
'release' => $options['release'] ?? (getenv('ERROR_REPORTING_RELEASE') ?: ''),
|
|
49
|
+
];
|
|
50
|
+
|
|
51
|
+
$previous = set_exception_handler(static function (\Throwable $error) use (&$previous): void {
|
|
52
|
+
CaptureException($error);
|
|
53
|
+
if ($previous !== null) {
|
|
54
|
+
$previous($error);
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
throw $error;
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Build a bounded JSON envelope and send it without following redirects. */
|
|
62
|
+
public static function capture(\Throwable $error, array $context): ?string
|
|
63
|
+
{
|
|
64
|
+
if (self::$config === null) {
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
try {
|
|
69
|
+
$id = bin2hex(random_bytes(16));
|
|
70
|
+
$frames = [['filename' => substr($error->getFile(), 0, 500), 'lineno' => $error->getLine()]];
|
|
71
|
+
foreach (array_slice($error->getTrace(), 0, 49) as $frame) {
|
|
72
|
+
$frames[] = [
|
|
73
|
+
'filename' => substr($frame['file'] ?? '', 0, 500),
|
|
74
|
+
'function' => substr(($frame['class'] ?? '') . ($frame['type'] ?? '') . ($frame['function'] ?? ''), 0, 180),
|
|
75
|
+
'lineno' => $frame['line'] ?? null,
|
|
76
|
+
];
|
|
77
|
+
}
|
|
78
|
+
$event = [
|
|
79
|
+
'event_id' => $id, 'timestamp' => microtime(true), 'platform' => 'php',
|
|
80
|
+
'environment' => substr(self::$config['environment'], 0, 80),
|
|
81
|
+
'release' => substr(self::$config['release'], 0, 160),
|
|
82
|
+
'tags' => (object)(isset($context['requestId']) ? ['request_id' => substr($context['requestId'], 0, 128)] : []),
|
|
83
|
+
'exception' => ['values' => [[
|
|
84
|
+
'type' => substr(get_class($error), 0, 120), 'value' => substr($error->getMessage(), 0, 2000),
|
|
85
|
+
'stacktrace' => ['frames' => array_reverse($frames)],
|
|
86
|
+
]]],
|
|
87
|
+
];
|
|
88
|
+
$body = "{\"event_id\":\"$id\"}\n{\"type\":\"event\"}\n"
|
|
89
|
+
. json_encode($event, JSON_THROW_ON_ERROR | JSON_INVALID_UTF8_SUBSTITUTE) . "\n";
|
|
90
|
+
$stream = stream_context_create(['http' => [
|
|
91
|
+
'method' => 'POST', 'timeout' => 2, 'follow_location' => 0, 'ignore_errors' => true,
|
|
92
|
+
'header' => "Content-Type: application/x-sentry-envelope\r\nX-Sentry-Auth: Sentry sentry_version=7, sentry_key="
|
|
93
|
+
. self::$config['key'] . "\r\nConnection: close\r\n",
|
|
94
|
+
'content' => $body,
|
|
95
|
+
]]);
|
|
96
|
+
$response = @fopen(self::$config['endpoint'], 'rb', false, $stream);
|
|
97
|
+
if ($response === false) {
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
try {
|
|
101
|
+
$status = stream_get_meta_data($response)['wrapper_data'][0] ?? '';
|
|
102
|
+
return preg_match('~^HTTP/\S+ 2[0-9]{2}(?: |$)~', $status) ? $id : null;
|
|
103
|
+
} finally {
|
|
104
|
+
fclose($response);
|
|
105
|
+
}
|
|
106
|
+
} catch (\Throwable) {
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|