Skip to content

Commit

Permalink
初步版本
Browse files Browse the repository at this point in the history
实现Inspector 的 segment 和 context 使用
  • Loading branch information
yangweijie committed Jan 14, 2022
1 parent 791aa5f commit 4ad7f7d
Show file tree
Hide file tree
Showing 13 changed files with 554 additions and 2 deletions.
20 changes: 20 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = false

[*.{vue,js,scss}]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false
11 changes: 11 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
* text=auto

/tests export-ignore
.gitattributes export-ignore
.gitignore export-ignore
.scrutinizer.yml export-ignore
.travis.yml export-ignore
phpunit.php export-ignore
phpunit.xml.dist export-ignore
phpunit.xml export-ignore
.php_cs export-ignore
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.idea
*.DS_Store
/vendor
/coverage
sftp-config.json
composer.lock
.subsplit
.php_cs.cache
27 changes: 27 additions & 0 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
$header = <<<EOF
This file is part of the yangweijie/think-inspector.
(c) yangweijie <917647288@qq.com>
This source file is subject to the MIT license that is bundled.
EOF;

return PhpCsFixer\Config::create()
->setRiskyAllowed(true)
->setRules(array(
'@Symfony' => true,
'header_comment' => array('header' => $header),
'array_syntax' => array('syntax' => 'short'),
'ordered_imports' => true,
'no_useless_else' => true,
'no_useless_return' => true,
'php_unit_construct' => true,
'php_unit_strict' => true,
))
->setFinder(
PhpCsFixer\Finder::create()
->exclude('vendor')
->in(__DIR__)
)
;
133 changes: 131 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,131 @@
# think-inspector
inspector for ThinkPHP
<h1 align="center"> think-inspector </h1>

<p align="center"> inspector package for thinkphp.</p>


## Installing

```shell
$ composer require yangweijie/think-inspector -vvv
```

## Usage

### 配置

#### key

https://docs.inspector.dev/ 注册后创建应用的 key 用于区分不同应用 前后端 应该也是独立应用

#### enable

是否启用监听

#### max_items

每个监听会话里 允许添加的最大片段数。 超过 添加不进去

#### env

目前没用到 计划 用于区分 不同环境的


#### single

是否区分 不同url 命令

true 意味着 启动监听时的path 是动态独立 , 然后 monitor 面版 会通过这些path 去区分 不同的监听 分组

false web 请求 path 为 WEB 命令行 为 命令行 这样方便查找监听记录 只用 在 WEB 或 命令行里 再按时间 进行查看

### 注入

在本服务中 向 app 注入了 inspector 对象 (自定义类) inspector 对象的 ins 属性 是原始 inspector-apm 的对象

inspector 可以调用 inspector-apm 里 inspector 类的方法。

为了 方便 设置 框架层的 片段 覆写了 startSegment 和 end 方法 。

只需要 `$this->app->inspector->startSegment('type', 'label');` 到 需要结束时 `$this->app->inspector->end('type');` 即可

当然如果你 业务中手动添加 监听的 片段

也可以 类似于 库里 console 覆写 doRun 方法
~~~
$ret = null;
$label = sprintf("运行【%s】命令", $input->getFirstArgument());
$this->inspector->addSegment(function () use ($input, $output, &$ret){
// trace($input);
$ret = parent::doRun($input, $output); // 包裹自己的代码
}, 'runCmd', $label);
~~~

#### context

参考 本库实现的

~~~
if($inspector->currentTransaction() && $inspector->currentTransaction() != null){
$inspector->currentTransaction()->addContext('请求信息', $data);
}
~~~

同样 上下文 加入时 会产生多个tab 最好不用 timeline 、url 、request 系统内置的。


### 命令行监听

目前 命令行 监听 需要 开启 应用 全局 Provid.php 里 配置 映射的类

~~~
<?php
use app\ExceptionHandle;
use app\Request;
use think\Inspector\Console;
// 容器Provider定义文件
return [
'think\Console' => Console::class,
'think\exception\Handle' => ExceptionHandle::class,
'think\Request' => Request::class,
];
~~~

否则不生效,尝试 服务 register 方法里 bind 无效。


### 事件

本库自定义了 一个 AppInit 类 动态绑定了 框架的 AppInit

对于 web 应用 会经过 AppInit、HttpRun、HttpEnd 等事件

请求信息 在 服务 boot 阶段就可以拿到 就不需要在 HttpRun 里 开启了。

web 中会多一个summary 信息 显示 请求耗时 内存 加载文件数量 以前在trace 为 console 里看到的信息

并且 Db 事件 记录了 每条sql 的时间 和原始执行语句 方便后面 根据 timeline 里 长短 来进行性能优化

而 命令行 不经过 应用的事件 只 记录了 加载全部命令 和执行当前命令的时间

## TODO

[x]测试tp5


## Contributing

You can contribute in one of three ways:

1. File bug reports using the [issue tracker](https://github.com/yangweijie/think-inspector/issues).
2. Answer questions or fix bugs on the [issue tracker](https://github.com/yangweijie/think-inspector/issues).
3. Contribute new features or update the wiki.

_The code contribution process is not very formal. You just need to make sure that you follow the PSR-0, PSR-1, and PSR-2 coding guidelines. Any new code contributions must be accompanied by unit tests where applicable._

## License

MIT
38 changes: 38 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "yangweijie\/think-inspector",
"description": "inspector package for thinkphp",
"license": "MIT",
"authors": [
{
"name": "yangweijie",
"email": "917647288@qq.com"
}
],
"keywords": [
"php",
"thinkphp",
"inspector",
"monitoring",
"trace"
],
"require": {
"php": ">=7.2.0",
"topthink/framework": "6.0.*",
"inspector-apm/inspector-php":"^3.7"
},
"autoload": {
"psr-4": {
"think\\Inspector\\": "src"
}
},
"extra": {
"think": {
"services": [
"think\\Inspector\\InspectorService"
],
"config": {
"inspector": "config/config.php"
}
}
}
}
10 changes: 10 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

return [
'key' => '',
'enable' => true,
'max_items' => 100,
'env' => 'test',
'single' => false,

];
Empty file added src/.gitkeep
Empty file.
36 changes: 36 additions & 0 deletions src/AppInit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
declare (strict_types = 1);

namespace think\Inspector;

use think\facade\Config;

class AppInit
{
/**
* 事件监听处理
*
* @return mixed
*/
public function handle($event)
{
$config = Config::get('inspector');
if($config['enable']){
$this->app = app();
if(!$this->app->runningInConsole()){
if(!isset($this->app->inspector)){
$this->app->bind('inspector', Inspector::class);
}
$ins = $this->app->inspector;
if($ins->needTransaction()){
$path = Inspector::getPath($config['single']);
// trace('init path '. $path);
$trans = $ins->startTransaction($path);
$ins->hasTrans = true;
}
// trace('AppInit');
$ins->startSegment('app', Inspector::summaryInfo());
}
}
}
}
69 changes: 69 additions & 0 deletions src/Console.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
declare (strict_types = 1);

namespace think\Inspector;

use think\App;
use think\Console as ParentConsole;
use think\console\Input;
use think\console\Output;

class Console extends ParentConsole
{
private $inspector = null;
// protected $app = null;

/**
* 初始化
*/
protected function initialize()
{
parent::initialize();
$this->inspector = $this->app->inspector;
if($this->inspector->hasTrans == false){
$path = Inspector::getPath($this->inspector->options['single']);
$this->inspector->startTransaction($path);
}
// trace($this->inspector->segments);
}

/**
* 加载指令
* @access protected
*/
protected function loadCommands(): void
{
$this->inspector->addSegment(function () {
parent::loadCommands();
}, 'loadCommands', '加载全部命令');
}

/**
* 执行指令
* @access public
* @param Input $input
* @param Output $output
* @return int
*/
public function doRun(Input $input, Output $output)
{
$ret = null;
$label = sprintf("运行【%s】命令", $input->getFirstArgument());
$this->inspector->addSegment(function () use ($input, $output, &$ret){
// trace($input);
$ret = parent::doRun($input, $output);
}, 'runCmd', $label);
$data = [
'Server/Request Data' => $this->app->request->server(),
];

if($this->inspector->currentTransaction() && $this->inspector->currentTransaction() != null){
$this->inspector->currentTransaction()->addContext('请求信息', $data);
$this->inspector->currentTransaction()->addContext('summary', Inspector::summaryInfo());
}
}

public function __destruct(){

}
}
27 changes: 27 additions & 0 deletions src/ExceptionInspectorHandle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace think\Inspector;

use think\exception\Handle;
use think\Response;
use Throwable;

class ExceptionInspectorHandle extends Handle
{
public function render($request, Throwable $e): Response
{
if(!isset($this->app->inspector)){
$this->app->bind('inspector', Inspector::class);
}
// Inspector 记录异常
if ($this->app->get('inspector') != null && $this->app->get('inspector')->options['enable']) {
$this->app->inspector->reportException($e);
if($this->app->inspector->currentTransaction() != null){
$this->app->inspector->currentTransaction()->setResult($e->getCode());
}
}

// 其他错误交给系统处理
return parent::render($request, $e);
}
}
Loading

0 comments on commit 4ad7f7d

Please sign in to comment.