Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed traceid does not exists when using coroutine handler. #2443

Merged
merged 9 commits into from Sep 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG-2.0.md
Expand Up @@ -9,6 +9,7 @@
## Fixed

- [#2427](https://github.com/hyperf/hyperf/pull/2427) Fixed model event dispatcher does not works for `Pivot` and `MorphPivot`.
- [#2443](https://github.com/hyperf/hyperf/pull/2443) Fixed traceid does not exists when using coroutine handler.

## Optimized

Expand Down
76 changes: 49 additions & 27 deletions docs/zh-cn/swoole-tracker.md
Expand Up @@ -32,30 +32,28 @@ Swoole Tracker 能够帮助企业自动分析并汇总统计关键系统调用

### 安装扩展

注册完账户后,进入[控制台](https://www.swoole-cloud.com/dashboard/catdemo/),并申请试用,下载对应客户端
注册完账户后,进入[控制台](https://business.swoole.com/SwooleTracker/catdemo),并申请试用,下载对应的安装脚本

相关文档,请移步 [试用文档](https://www.kancloud.cn/swoole-inc/ee-base-wiki/1214079) 或 [详细文档](https://www.kancloud.cn/swoole-inc/ee-help-wiki/1213080)

> 具体文档地址,以从控制台下载的对应客户端中展示的为准。

将客户端中的所有文件以及以下两个文件复制到项目目录 `.build` 中
将脚本以及以下两个文件复制到项目目录 `.build` 中

1. `entrypoint.sh`

```bash
#!/usr/bin/env bash
#!/usr/bin/env sh

/opt/swoole/script/php/swoole_php /opt/swoole/node-agent/src/node.php &

php /opt/www/bin/hyperf.php start

```

2. `swoole-tracker.ini`
2. `swoole_tracker.ini`

```bash
[swoole_tracker]
extension=/opt/swoole_tracker.so
extension=/opt/.build/swoole_tracker.so
apm.enable=1 #打开总开关
apm.sampling_rate=100 #采样率 例如:100%

Expand All @@ -66,8 +64,15 @@ apm.enable_memcheck=1 #开启内存泄漏检测 默认0 关闭状态
然后将下面的 `Dockerfile` 复制到项目根目录中。

```dockerfile
FROM hyperf/hyperf:7.2-alpine-cli
LABEL maintainer="Hyperf Developers <group@hyperf.io>" version="1.0" license="MIT"
# Default Dockerfile
#
# @link https://www.hyperf.io
# @document https://hyperf.wiki
# @contact group@hyperf.io
# @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE

FROM hyperf/hyperf:7.4-alpine-v3.11-cli
LABEL maintainer="Hyperf Developers <group@hyperf.io>" version="1.0" license="MIT" app.name="Hyperf"

##
# ---------- env settings ----------
Expand All @@ -76,54 +81,59 @@ LABEL maintainer="Hyperf Developers <group@hyperf.io>" version="1.0" license="MI
ARG timezone

ENV TIMEZONE=${timezone:-"Asia/Shanghai"} \
COMPOSER_VERSION=1.8.6 \
APP_ENV=prod
APP_ENV=prod \
SCAN_CACHEABLE=(true)

# update
RUN set -ex \
&& apk update \
# install composer
&& cd /tmp \
&& wget https://github.com/composer/composer/releases/download/${COMPOSER_VERSION}/composer.phar \
&& wget https://mirrors.aliyun.com/composer/composer.phar \
&& chmod u+x composer.phar \
&& mv composer.phar /usr/local/bin/composer \
# show php version and extensions
&& php -v \
&& php -m \
&& php --ri swoole \
# ---------- some config ----------
&& cd /etc/php7 \
# - config PHP
&& { \
echo "upload_max_filesize=100M"; \
echo "post_max_size=108M"; \
echo "memory_limit=1024M"; \
echo "upload_max_filesize=128M"; \
echo "post_max_size=128M"; \
echo "memory_limit=1G"; \
echo "date.timezone=${TIMEZONE}"; \
} | tee conf.d/99-overrides.ini \
} | tee conf.d/99_overrides.ini \
# - config timezone
&& ln -sf /usr/share/zoneinfo/${TIMEZONE} /etc/localtime \
&& echo "${TIMEZONE}" > /etc/timezone \
# ---------- clear works ----------
&& rm -rf /var/cache/apk/* /tmp/* /usr/share/man \
&& echo -e "\033[42;37m Build Completed :).\033[0m\n"

COPY . /opt/www
WORKDIR /opt/www/.build
COPY .build /opt/.build
WORKDIR /opt/.build

# 这里的地址,以客户端中显示的为准
RUN ./deploy_env.sh www.swoole-cloud.com \
RUN chmod +x swoole-tracker-install.sh \
&& ./swoole-tracker-install.sh \
&& chmod 755 entrypoint.sh \
&& cp swoole_tracker72.so /opt/swoole_tracker.so \
&& cp swoole-tracker.ini /etc/php7/conf.d/swoole-tracker.ini \
&& cp swoole-tracker/swoole_tracker74.so /opt/.build/swoole_tracker.so \
&& cp swoole_tracker.ini /etc/php7/conf.d/98_swoole_tracker.ini \
&& php -m

WORKDIR /opt/www

RUN composer install --no-dev \
&& composer dump-autoload -o \
&& php /opt/www/bin/hyperf.php
# Composer Cache
# COPY ./composer.* /opt/www/
# RUN composer install --no-dev --no-scripts

COPY . /opt/www
RUN composer install --no-dev -o && php bin/hyperf.php

EXPOSE 9501

ENTRYPOINT ["sh", ".build/entrypoint.sh"]
ENTRYPOINT ["sh", "/opt/.build/entrypoint.sh"]

```

## 使用
Expand Down Expand Up @@ -157,3 +167,15 @@ return [
],
];
```

若使用 `jsonrpc-http` 协议实现了 `RPC` 服务,则还需要在 `config/autoload/aspects.php` 配置以下 `Aspect`:

```php
<?php

return [
Hyperf\SwooleTracker\Aspect\CoroutineHandlerAspect::class,
];
```


2 changes: 2 additions & 0 deletions phpstan.neon
Expand Up @@ -41,3 +41,5 @@ parameters:
- '#Method Hyperf\\Utils\\Serializer\\ScalarNormalizer::denormalize\(\) should return array\|object but returns#'
- '#Function get_debug_type invoked with 1 parameter, 0 required#'
- '#gc_status not found#'
- '#Used function getSwooleTracker.* not found#'
- '#Function getSwooleTracker.* not found#'
54 changes: 54 additions & 0 deletions src/swoole-tracker/src/Aspect/CoroutineHandlerAspect.php
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\SwooleTracker\Aspect;

use Hyperf\Di\Aop\AbstractAspect;
use Hyperf\Di\Aop\ProceedingJoinPoint;
use Hyperf\Guzzle\CoroutineHandler;
use Psr\Container\ContainerInterface;
use Swoole\Coroutine\Http\Client;
use SwooleTracker\Stats;
use function getSwooleTrackerSpanId;
use function getSwooleTrackerTraceId;

class CoroutineHandlerAspect extends AbstractAspect
{
public $classes = [
CoroutineHandler::class . '::execute',
];

/**
* @var ContainerInterface
*/
protected $container;

public function __construct(ContainerInterface $container)
{
$this->container = $container;
}

public function process(ProceedingJoinPoint $proceedingJoinPoint)
{
if (class_exists(Stats::class) && $client = $proceedingJoinPoint->getArguments()[0] ?? null) {
if ($client instanceof Client && function_exists('getSwooleTrackerTraceId') && function_exists('getSwooleTrackerSpanId')) {
$client->setHeaders(array_merge(
[
'x-swoole-traceid' => getSwooleTrackerTraceId(),
'x-swoole-spanid' => getSwooleTrackerSpanId(),
],
$client->requestHeaders
));
}
}
return $proceedingJoinPoint->process();
}
}