【PHP】关于系统性能追踪工具molten

随声附和 提交于 2019-11-25 23:17:10

一、简介

关于molten的介绍网上有很多,是一个全链路追踪的工具,Molten可以看做是phptrace的的升级版(流行的php问题定位工具譬如phptrace,xhprof,这些工具可以自行Google,看看如何使用)

Molten能干啥

molten追踪php核心调用库运行时信息并且按照zipkin/optracing格式输出信息。
可以追踪`curl,pdo,mysqli,redis,mongodb,memcached` 这么多的运行信息。

你可以直接看到你耗时比较久的接口的运行信息,调用信息,一眼就能看到哪个地方慢

二、安装

zipkin 开源分布式系统调用跟踪工具(java写的工具),所以系统需要安装Java或者docker
molten为php作对应的数据收集

1、安装Java

最简单的方式rpm 安装,我这里可以直接安装

1

yum -y install java

查看Java安装情况

1

2

3

4

[root@localhost~]# java -version

openjdk version "1.8.0_212"

OpenJDK Runtime Environment (build 1.8.0_212-b04)

OpenJDK 64-Bit Server VM (build 25.212-b04, mixed mode)

2、安装依赖 

1

yum -y install libevent-devel

3、安装molten

1

2

3

4

5

6

7

cd /usr/local/src

git clone https://github.com/chuan-yun/Molten.git

 

cd Molten

phpize

./configure --enable-zipkin-header=yes

make && make install

4、配置molte.ini

1

2

3

4

5

6

7

8

9

10

11

12

[molten]

extension="molten.so"

molten.enable="1"

molten.service_name="local"

molten.tracing_cli=1

molten.open_report=1

molten.sampling_type=2

molten.sampling_rate=1

molten.notify_uri="http://127.0.0.1:9411/zipkin/"

molten.sink_http_uri="http://127.0.0.1:9411/api/v1/spans"

molten.sink_type=4

molten.sink_log_path="/tmp/logs/"

5、下载并启动zipkin

1

2

3

curl -sSL https://zipkin.apache.org/quickstart.sh | bash -s

 

java -jar zipkin.jar --logging.level.zipkin=DEBUG 2>&1 >/dev/null &

启动界面

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

                                    ********

                                  **        **

                                 *            *

                                **            **

                                **            **

                                 **          **

                                  **        **

                                    ********

                                      ****

                                      ****

        ****                          ****

     ******                           ****                                 ***

  ****************************************************************************

    *******                           ****                                 ***

        ****                          ****

                                       **

                                       **

 

 

             *****      **     *****     ** **       **     **   **

               **       **     **  *     ***         **     **** **

              **        **     *****     ****        **     **  ***

             ******     **     **        **  **      **     **   **

 

:: Powered by Spring Boot ::         (v2.1.5.RELEASE)

 

2019-05-21 22:26:30.113  INFO 19403 --- [           main] z.s.ZipkinServer                         : Starting ZipkinServer v2.14.0 on iZwz9ccq9xttkykppxhkwtZ with PID 19403

2019-05-21 22:26:30.123 DEBUG 19403 --- [           main] z.s.ZipkinServer                         : Running with Spring Boot v2.1.5.RELEASE, Spring v5.1.7.RELEASE

2019-05-21 22:26:30.123  INFO 19403 --- [           main] z.s.ZipkinServer                         : The following profiles are active: shared

2019-05-21 22:26:34.586  INFO 19403 --- [           main] c.l.a.c.Flags                            : com.linecorp.armeria.verboseExceptions: false (default)

2019-05-21 22:26:34.587  INFO 19403 --- [           main] c.l.a.c.Flags                            : com.linecorp.armeria.verboseSocketExceptions: false (default)

2019-05-21 22:26:34.587  INFO 19403 --- [           main] c.l.a.c.Flags                            : com.linecorp.armeria.verboseResponses: false (default)

2019-05-21 22:26:34.667  INFO 19403 --- [           main] c.l.a.c.Flags                            : com.linecorp.armeria.useEpoll: true (default)

2019-05-21 22:26:34.927  INFO 19403 --- [           main] c.l.a.c.Flags                            : com.linecorp.armeria.useOpenSsl: true (default)

2019-05-21 22:26:34.930  INFO 19403 --- [           main] c.l.a.c.Flags                            : com.linecorp.armeria.maxNumConnections: 2147483647 (default)

2019-05-21 22:26:34.930  INFO 19403 --- [           main] c.l.a.c.Flags                            : com.linecorp.armeria.numCommonWorkers: 2 (default)

2019-05-21 22:26:34.931  INFO 19403 --- [           main] c.l.a.c.Flags                            : com.linecorp.armeria.numCommonBlockingTaskThreads: 200 (default)

2019-05-21 22:26:34.941  INFO 19403 --- [           main] c.l.a.c.Flags                            : com.linecorp.armeria.defaultMaxRequestLength: 10485760 (default)

2019-05-21 22:26:34.942  INFO 19403 --- [           main] c.l.a.c.Flags                            : com.linecorp.armeria.defaultMaxResponseLength: 10485760 (default)

2019-05-21 22:26:34.942  INFO 19403 --- [           main] c.l.a.c.Flags                            : com.linecorp.armeria.defaultRequestTimeoutMillis: 10000 (default)

2019-05-21 22:26:34.942  INFO 19403 --- [           main] c.l.a.c.Flags                            : com.linecorp.armeria.defaultResponseTimeoutMillis: 15000 (default)

2019-05-21 22:26:34.943  INFO 19403 --- [           main] c.l.a.c.Flags                            : com.linecorp.armeria.defaultConnectTimeoutMillis: 3200 (default)

2019-05-21 22:26:34.943  INFO 19403 --- [           main] c.l.a.c.Flags                            : com.linecorp.armeria.defaultWriteTimeoutMillis: 1000 (default)

2019-05-21 22:26:34.947  INFO 19403 --- [           main] c.l.a.c.Flags                            : com.linecorp.armeria.defaultServerIdleTimeoutMillis: 15000 (default)

2019-05-21 22:26:34.948  INFO 19403 --- [           main] c.l.a.c.Flags                            : com.linecorp.armeria.defaultClientIdleTimeoutMillis: 10000 (default)

2019-05-21 22:26:34.948  INFO 19403 --- [           main] c.l.a.c.Flags                            : com.linecorp.armeria.defaultHttp2InitialConnectionWindowSize: 1048576 (default)

2019-05-21 22:26:34.949  INFO 19403 --- [           main] c.l.a.c.Flags                            : com.linecorp.armeria.defaultHttp2InitialStreamWindowSize: 1048576 (default)

2019-05-21 22:26:34.951  INFO 19403 --- [           main] c.l.a.c.Flags                            : com.linecorp.armeria.defaultHttp2MaxFrameSize: 16384 (default)

2019-05-21 22:26:34.952  INFO 19403 --- [           main] c.l.a.c.Flags                            : com.linecorp.armeria.defaultHttp2MaxStreamsPerConnection: 2147483647 (default)

2019-05-21 22:26:34.952  INFO 19403 --- [           main] c.l.a.c.Flags                            : com.linecorp.armeria.defaultHttp2MaxHeaderListSize: 8192 (default)

2019-05-21 22:26:34.956  INFO 19403 --- [           main] c.l.a.c.Flags                            : com.linecorp.armeria.defaultHttp1MaxInitialLineLength: 4096 (default)

2019-05-21 22:26:34.957  INFO 19403 --- [           main] c.l.a.c.Flags                            : com.linecorp.armeria.defaultHttp1MaxHeaderSize: 8192 (default)

2019-05-21 22:26:34.957  INFO 19403 --- [           main] c.l.a.c.Flags                            : com.linecorp.armeria.defaultHttp1MaxChunkSize: 8192 (default)

2019-05-21 22:26:34.957  INFO 19403 --- [           main] c.l.a.c.Flags                            : com.linecorp.armeria.defaultUseHttp2Preface: true (default)

2019-05-21 22:26:34.957  INFO 19403 --- [           main] c.l.a.c.Flags                            : com.linecorp.armeria.defaultUseHttp1Pipelining: false (default)

2019-05-21 22:26:34.958  INFO 19403 --- [           main] c.l.a.c.Flags                            : com.linecorp.armeria.defaultBackoffSpec: exponential=200:10000,jitter=0.2 (default)

2019-05-21 22:26:34.958  INFO 19403 --- [           main] c.l.a.c.Flags                            : com.linecorp.armeria.defaultMaxTotalAttempts: 10 (default)

2019-05-21 22:26:34.965  INFO 19403 --- [           main] c.l.a.c.Flags                            : com.linecorp.armeria.routeCache: maximumSize=4096 (default)

2019-05-21 22:26:34.966  INFO 19403 --- [           main] c.l.a.c.Flags                            : com.linecorp.armeria.compositeServiceCache: maximumSize=256 (default)

2019-05-21 22:26:34.966  INFO 19403 --- [           main] c.l.a.c.Flags                            : com.linecorp.armeria.parsedPathCache: maximumSize=4096 (default)

2019-05-21 22:26:34.966  INFO 19403 --- [           main] c.l.a.c.Flags                            : com.linecorp.armeria.headerValueCache: maximumSize=4096 (default)

2019-05-21 22:26:34.967  INFO 19403 --- [           main] c.l.a.c.Flags                            : com.linecorp.armeria.cachedHeaders: :authority,:scheme,:method,accept-encoding,content-type (default)

2019-05-21 22:26:34.974  INFO 19403 --- [           main] c.l.a.c.Flags                            : com.linecorp.armeria.annotatedServiceExceptionVerbosity: unhandled (default)

2019-05-21 22:26:34.979  INFO 19403 --- [           main] c.l.a.c.Flags                            : Using /dev/epoll

2019-05-21 22:26:34.979  INFO 19403 --- [           main] c.l.a.c.Flags                            : Using OpenSSL: BoringSSL, 0x1010007f

2019-05-21 22:26:35.504  INFO 19403 --- [           main] c.l.a.s.d.DocStringExtractor             : Using com.linecorp.armeria.thrift.jsonDir: META-INF/armeria/thrift

2019-05-21 22:26:35.538  INFO 19403 --- [           main] c.l.a.c.u.SystemInfo                     : Hostname: izwz9ccq9xttkykppxhkwtz (from /proc/sys/kernel/hostname)

2019-05-21 22:26:36.036  INFO 19403 --- [oss-http-*:9411] c.l.a.s.Server                           : Serving HTTP at /0.0.0.0:9411 - http://127.0.0.1:9411/

2019-05-21 22:26:36.037  INFO 19403 --- [           main] c.l.a.s.ArmeriaAutoConfiguration         : Armeria server started at ports: {/0.0.0.0:9411=ServerPort(/0.0.0.0:9411, [http])}

2019-05-21 22:26:36.139  INFO 19403 --- [           main] c.d.d.core                               : DataStax Java driver 3.7.1 for Apache Cassandra

2019-05-21 22:26:36.166  INFO 19403 --- [           main] c.d.d.c.GuavaCompatibility               : Detected Guava >= 19 in the classpath, using modern compatibility layer

2019-05-21 22:26:37.149  INFO 19403 --- [           main] c.d.d.c.ClockFactory                     : Using native clock to generate timestamps.

2019-05-21 22:26:37.577  INFO 19403 --- [           main] z.s.ZipkinServer                         : Started ZipkinServer in 9.103 seconds (JVM running for 11.001)

6、分析数据

可以打开 http://127.0.0.1:9411/zipkin/  进行数据分析了

 

 

 

三、功能

现在Molten提供curl、pdo、mysqli、redis、mongodb、memcached等组件的拦截,这些信息拦截都是应用透明,开箱即用,输出zipkin格式非常方便。

查看molten是否正常启动了,可以访问:http://your.domain/molten/status:

输出内容格式让人感到疑惑,其实这个是prometheus监控exporter输出格式,输出这种格式方便和prometheus集成。

 

还可以通过POST http://your.domain/molten/status修改探针采样的方式,内容如下:

1

{"enable":1,"samplingType":2,"samplingRate":20,"samplingRequest":100}

其中samplingType是采样类型:1是根据采样率采样,2是根据每分钟的请求数。

samplingRate是采样率,即多少个请求采样一个,samplingRequest是每分钟的前多少个请求被采样。采样是降低性能损耗的有效方式,通过还能够根据系统的承载量动态的调整。

还可通过molten.sink_type设置数据输出方式, 1是输出到文件中, 2输出到标准输出,4 通过http发送。

 

Molten在phptrace数据分析的基础上进行大量的开发, 本身性能在开启采样率的情况下影响较小。

应用影响如下:

四、增加trace方法

分析源码文件 molten_intercept.c可以看到在 `mo_intercept_ctor` 方法中,通过在 MINIT 阶段注入相关的方法实现了对调用的监控。

其中 PDO

1

2

3

4

5

PDOStatement@execute

PDO@exec

PDO@query

PDO@commit

PDO@prepare

其中 mysqli

1

2

3

4

5

6

7

mysqli_connect mysqli@__construct

mysqli_real_connect mysqli@real_connect

mysqli_query mysqli@query

mysqli_prepare mysqli@prepare

mysqli_stmt_prepare mysqli_stmt@prepare

mysqli_stmt_execute mysqli_stmt@execute

mysqli@commit

其中 curl 

1

2

3

4

curl_setopt

curl_exec

curl_setopt_array

curl_reset

可以看到 molten 对 PHP 常用的远程调用服务方法都做了监控,但还不够全面,更新也不够及时,如果我们想对其他方法监控(比如 rpc、predis、kafka 等)可以做一些改造。

参考其他的方法追加代码

1

2

3

4

5

6

/* predis */

{

    ADD_INTERCEPTOR_TAG(pit, Predis\\Client);

    INIT_INTERCEPTOR_ELE(Predis\\Client@__construct, &default_capture, &default_predis_record);

    INIT_INTERCEPTOR_ELE(Predis\\Client@__call, &default_capture, &default_predis_record);

}

针对 predis 编写predis_request_record方法

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

/* {{{ predis default method record */

static void default_predis_record(mo_interceptor_t *pit, mo_frame_t *frame)

{

    zval *span = build_com_record(pit, frame, 0);

 

    merge_span_extra(span, frame);

 

    pit->psb->span_add_ba_ex(span,  "componet""Predis\\Client", frame->exit_time, pit->pct, BA_NORMAL);

 

    pit->psb->span_add_ba_ex(span,  "db.type""redis", frame->exit_time, pit->pct, BA_NORMAL);

 

    /* check exception */

    SET_DEFAULT_EXCEPTION(frame, pit);

 

    /* add span */

    mo_chain_add_span(pit->pct->pcl, span);

}

  

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!