logstash收集PHP性能分析(xhprof)日志

10 篇文章 0 订阅
2 篇文章 0 订阅

PHP框架是Laravel,环境linux

在项目根目录的bootstrap目录下新建profile.php:

<?php

if (extension_loaded('xhprof')) {

    xhprof_enable(XHPROF_FLAGS_MEMORY | XHPROF_FLAGS_NO_BUILTINS);
    register_shutdown_function(function () {
        if (function_exists('fastcgi_finish_request')) {
            fastcgi_finish_request();
        }
        $xhprof_data = xhprof_disable();
        file_put_contents(getXhprofDir(), formatResult($xhprof_data));
    });

    function getXhprofDir()
    {
        $path = ini_get('xhprof.output_dir') ?: '/tmp';
        if (!file_exists($path)) {
            mkdir($path, 0777);
        }
        return $path . '/' . uniqid() . '.xhprof.xhprof';
    }

    function formatResult($xhprof_data) {
        foreach ($xhprof_data as $key => $value) {
            $xhprof_data[$key]['function'] = $key;
        }
        $xhprof_data = array_values($xhprof_data);
        $xhprof_data_json = json_encode($xhprof_data);
	//去掉json_encode之后[{'a':1,'b':2},{'c':3}]俩边的[],将俩个},{之间的,逗号替换成\t,之后logstash需要用来做分隔符
        return str_replace("},{", "}\\t{", substr($xhprof_data_json, 1, strlen($xhprof_data_json) - 2));
    }
}

然后在bootstrap/autoload.php中引入,如果在index.php中引入的话,在php artisan的调用就要重新引入一次,所以房子autoload中。

在logstash/config下新建xhprof.conf:

input {
	file {
        path => "/tmp/xhprof/*.xhprof.xhprof"
    }
}
filter {
    split {
        field => "message"
        terminator => "\t"
    }
    json {
    	source => "message"
    	remove_field => "message"
    }
}
output {
	elasticsearch {
        hosts => "127.0.0.1:9200"
        index => "profile-%{+YYYY.MM.dd}"
    }
    stdout {
        codec => rubydebug
    }
}

启动bin/logstash -f config/xhprof.conf

最后在kibana中新增一个index pattern: profile-* 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李昂的数字之旅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值