PHPackages                             extraswoft/jaeger - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Logging &amp; Monitoring](/categories/logging)
4. /
5. extraswoft/jaeger

ActiveLibrary[Logging &amp; Monitoring](/categories/logging)

extraswoft/jaeger
=================

jaeger-sdk for swoft

v0.0.2.2(7y ago)204.9k6[2 issues](https://github.com/masixun71/swoft-jaeger/issues)Apache-2.0PHP

Since Nov 6Pushed 7y ago3 watchersCompare

[ Source](https://github.com/masixun71/swoft-jaeger)[ Packagist](https://packagist.org/packages/extraswoft/jaeger)[ RSS](/packages/extraswoft-jaeger/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (3)Versions (23)Used By (0)

[![](https://camo.githubusercontent.com/eea5374085bbc8f3c2603a9bfec7de47f65d1133ff71ba25cd7800fd366fc6a6/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f76657273696f6e2d76302e302e302e392d7265642e737667)](https://camo.githubusercontent.com/eea5374085bbc8f3c2603a9bfec7de47f65d1133ff71ba25cd7800fd366fc6a6/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f76657273696f6e2d76302e302e302e392d7265642e737667)[![](https://camo.githubusercontent.com/40132ea396da1fc0c9f978249f670b77aef6e66ffb0f44987e0e2ba3113a175d/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d2533453d372e312d6f72616e67652e737667)](https://camo.githubusercontent.com/40132ea396da1fc0c9f978249f670b77aef6e66ffb0f44987e0e2ba3113a175d/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d2533453d372e312d6f72616e67652e737667)[![](https://camo.githubusercontent.com/6111524cc44700e3d9915944f83dcb7e7b4ee92d35ff93b17a8c5a0957d3120a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f73776f6f6c652d2533453d342e302d626c75652e737667)](https://camo.githubusercontent.com/6111524cc44700e3d9915944f83dcb7e7b4ee92d35ff93b17a8c5a0957d3120a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f73776f6f6c652d2533453d342e302d626c75652e737667)

简介
==

[](#简介)

本项目属于swoft的jaeger client,非侵入式地对项目环境进行跟踪并且异步上报到jaeger server,可以和其他的swoft项目或者其他语言（java，go）进行全链路监控,能监控`mysql`,`redis`,`httpClient`的正常异常情况。并且上传是使用`thrift`，udp 上传，效率较高。 目前新版本(v0.0.0.7以上)支持log记录，不使用udp，提高效率

之前我还写过zipkin的sdk，链接如下[swoft-zipkin](https://github.com/masixun71/swoft-zipkin)

环境要求
====

[](#环境要求)

1.扩展要求
------

[](#1扩展要求)

- sockets ,因为上传需要用到udp传输，所以需要该扩展

配置步骤
====

[](#配置步骤)

1.composer
----------

[](#1composer)

```
       "minimum-stability": "dev",
	   "prefer-stable": true,
```

先给composer.json添加如下语句，因为我们项目里引入的`opentracing/opentracing`官方最新的也只是一个beta包，不加会导致无法引入

```
composer require extraswoft/jaeger
```

引入本项目的包

2.config/properties/app.php 添加
------------------------------

[](#2configpropertiesappphp-添加)

#### 需要在app文件，beanScan里加上扫描我们的命名空间

[](#需要在app文件beanscan里加上扫描我们的命名空间)

```
      'beanScan' => [
		"ExtraSwoft\\Jaeger\\",
    ],
```

3.config/beans/base.php添加我们的中间件
-------------------------------

[](#3configbeansbasephp添加我们的中间件)

```
    'serverDispatcher' => [
        'middlewares' => [
            \Swoft\View\Middleware\ViewMiddleware::class,
			JaegerMiddleware::class,
//             \Swoft\Devtool\Middleware\DevToolMiddleware::class,
            // \Swoft\Session\Middleware\SessionMiddleware::class,
        ]
    ],
```

4.在.env配置文件中添加以下配置
------------------

[](#4在env配置文件中添加以下配置)

##### ZIPKIN\_HOST: jager\_client 的地址

[](#zipkin_host-jager_client-的地址)

##### ZIPKIN\_RAND: 采样率，1为100%, 最小可设为0.0001，线上环境建议采样

[](#zipkin_rand--采样率1为100-最小可设为00001线上环境建议采样)

##### ENV: 配置环境，通过配置环境可以共用一个jaegerServer

[](#env--配置环境通过配置环境可以共用一个jaegerserver)

##### JAEGER\_OPEN: 是否开启jaeger，可不同环境不同配置

[](#jaeger_open--是否开启jaeger可不同环境不同配置)

##### JAEGER\_MODE: 1:使用udp传输，2:写入日志，日志依赖 `@log` 的配置(小时区分日志)

[](#jaeger_mode--1使用udp传输2写入日志日志依赖-log-的配置小时区分日志)

```
 #jaeger
JAEGER_RATE=1
JAEGER_SERVER_HOST=172.21.134.20:6831
JAEGER_OPEN=true
JAEGER_MODE=2
ENV=test
```

5.httpClient 的修改
----------------

[](#5httpclient-的修改)

当我们使用swoft官方的httpClient的时候，需要使用我们客户端的adapter，挂上钩子

```
$client = new Client(['adapter' => new AddJaegerAdapter()]);
```

当然，你也可以看下我们适配器的源码放到自己的适配器里，比较简单

源码修改
====

[](#源码修改)

因为在mysql，redis和http的请求上没有钩子函数，所以我们需要自己实现，只要在请求开始和结束加上事件触发即可。建议自己或者公司项目直接fork官方的[swoft-component](https://github.com/swoft-cloud/swoft-component),然后根据自己需要开发，并且隔一段时间同步最新代码，在swoft里面composer使用component这个仓库。

1.mysql（协程）
-----------

[](#1mysql协程)

### src/db/src/Db.php中，在$connection-&gt;prepare($sql);前添加(注意命名空间加入)

[](#srcdbsrcdbphp中在connection-preparesql前添加注意命名空间加入)

```
         Log::profileStart($profileKey);
+        App::trigger('Mysql', 'start', $profileKey, $sql);
         $connection->prepare($sql);
         $params = self::transferParams($params);
         $result = $connection->execute($params);
```

### src/db/src/DbCoResult.php中，在Log::profileEnd($this-&gt;profileKey);后添加(注意命名空间加入)

[](#srcdbsrcdbcoresultphp中在logprofileendthis-profilekey后添加注意命名空间加入)

```
         $this->release();

         Log::profileEnd($this->profileKey);
+        App::trigger('Mysql', 'end', $this->profileKey, true);
         return $result;
```

2.redis (非协程),协程可以根据自己需要添加
--------------------------

[](#2redis-非协程协程可以根据自己需要添加)

### src/redis/src/Redis.php(注意命名空间加入)

[](#srcredissrcredisphp注意命名空间加入)

### 在 $result = $connection-&gt;$method(...$params);前后添加

[](#在-result--connection-methodparams前后添加)

```
         $connectPool = App::getPool($this->poolName);
         /* @var ConnectionInterface $client */
         $connection = $connectPool->getConnection();
+        App::trigger('Redis', 'start', $method, $params);
         $result     = $connection->$method(...$params);
         $connection->release(true);
+        App::trigger('Redis', 'end', true);

         return $result;
```

3.httpClient (协程)
-----------------

[](#3httpclient-协程)

### src/http-client/src/Adapter/CoroutineAdapter.php

[](#srchttp-clientsrcadaptercoroutineadapterphp)

### 在 $client-&gt;execute($path);前添加

[](#在-client-executepath前添加)

```
 if ($query !== '') $path .= '?' . $query;

         $client->setDefer();
+        App::trigger('HttpClient', 'start', $request, $options);
         $client->execute($path);

         App::profileEnd($profileKey);
```

### src/http-client/src/HttpCoResult.php

[](#srchttp-clientsrchttpcoresultphp)

### 在 得到$response后添加

[](#在-得到response后添加)

```
         $response = $this->createResponse()
            ->withBody(new SwooleStream($result ?? ''))
            ->withHeaders($headers ?? [])
            ->withStatus($this->deduceStatusCode($client));
        App::trigger('HttpClient', 'end', $response);

        return $response;
```

捕获异常
====

[](#捕获异常)

在全局的`SwoftExceptionHandler.php`里面添加对`MysqlException`和`RedisException`异常的钩子，保证我们能把异常的情况也上报了

```
else if ($throwable instanceof MysqlException) {
        App::trigger('Mysql', 'end', 'all', false, $throwable->getMessage());
} else if ($throwable instanceof RedisException) {
        App::trigger('Redis', 'end' , false, $throwable->getMessage());
}
```

完成
==

[](#完成)

完成以上修改后，重新composer引入新的`swoft-component`包，然后重启项目就可以了
---------------------------------------------------

[](#完成以上修改后重新composer引入新的swoft-component包然后重启项目就可以了)

log记录
=====

[](#log记录)

新版本支持将数据写入log，但由于原生字符串包含换行符，导致写入日志变成多行而不是一行，所以在写入日志时 会特殊处理, 变为: 'jaegerMsg:' . urlencode($thriftFile) . "\\n"

获取header
========

[](#获取header)

因为链路传递是通过header来服务的，所以项目里面当有特殊的client时，想要获取header来传递可以使用下面方法

```
\Swoft::getBean(TracerManager::class)->getHeader();
```

Jaeger 的搭建
==========

[](#jaeger-的搭建)

我把我的搭建心得都放在了下面这篇文章里面了 [全链路监控Jaeger搭建实战](https://www.jianshu.com/p/ffc597bb4ce8)

但是如果你懒得看，也可以直接用下面的

```
docker run -d -e COLLECTOR_ZIPKIN_HTTP_PORT=9411 -p5775:5775/udp -p6831:6831/udp -p6832:6832/udp \
  -p5778:5778 -p16686:16686 -p14268:14268 -p9411:9411 jaegertracing/all-in-one:latest
```

直接起就搭建完成了，默认client 接受端口是6831

效果图
---

[](#效果图)

[![jaegerUI.png](https://camo.githubusercontent.com/4e836d2db2563b7a25730d916f6798e4eacc4b67627146a68c8b376c7a9c1e23/68747470733a2f2f75706c6f61642d696d616765732e6a69616e7368752e696f2f75706c6f61645f696d616765732f31323839303338332d613233323562333836323932366132322e706e673f696d6167654d6f6772322f6175746f2d6f7269656e742f7374726970253743696d61676556696577322f322f772f31323430)](https://camo.githubusercontent.com/4e836d2db2563b7a25730d916f6798e4eacc4b67627146a68c8b376c7a9c1e23/68747470733a2f2f75706c6f61642d696d616765732e6a69616e7368752e696f2f75706c6f61645f696d616765732f31323839303338332d613233323562333836323932366132322e706e673f696d6167654d6f6772322f6175746f2d6f7269656e742f7374726970253743696d61676556696577322f322f772f31323430)

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity28

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~8 days

Total

22

Last Release

2573d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/2b3c64e496bcef59a9a12a74f9383e80acd78ea2fa4836d6ddb8f7f8873b0b69?d=identicon)[masixun71](/maintainers/masixun71)

---

Top Contributors

[![masixun71](https://avatars.githubusercontent.com/u/18737991?v=4)](https://github.com/masixun71 "masixun71 (1 commits)")

---

Tags

phpswoolejaegeropentracingswoft

### Embed Badge

![Health badge](/badges/extraswoft-jaeger/health.svg)

```
[![Health](https://phpackages.com/badges/extraswoft-jaeger/health.svg)](https://phpackages.com/packages/extraswoft-jaeger)
```

###  Alternatives

[jonahgeorge/jaeger-client-php

Jaeger Bindings for PHP OpenTracing API

1484.4M18](/packages/jonahgeorge-jaeger-client-php)[jukylin/jaeger-php

php client for jaeger

2261.6M6](/packages/jukylin-jaeger-php)[lvht/jaeger

php client for jaeger

1240.5k](/packages/lvht-jaeger)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
