Giter Club home page Giter Club logo

transfer-statistics's Introduction

TransferStatistics

TransferStatistics 使用webman开发的一个应用监控系统,用于查看应用调用记录、请求量、调用耗时、调用分析等。

系统使用 UDP 接收上报数据;使用 Redis 存储、汇总数据

所需环境

PHP版本不低于7.3,并安装 Redis 拓展

安装

composer安装

创建项目

composer create-project hsk99/transfer-statistics

下载安装

1、下载 或 git clone https://github.com/hsk99/transfer-statistics

2、执行命令 composer install

配置

1、config/redis.php 设置 Redis

2、config/app.php 设置 登录用户名、密码

3、config/server.php 设置 WebServer

4、config/process.php 设置 采集服务

运行

执行命令 php start.php start

查看统计

浏览器访问 http://ip地址:8788

上报数据

使用类库:Client/StatisticClient.php

1、webman 中间件使用

config/app.php 增加 statisticAddress 项,设置 上报地址

<?php

namespace app\common\middleware;

use Webman\MiddlewareInterface;
use Webman\Http\Response;
use Webman\Http\Request;

class Statistic implements MiddlewareInterface
{
    public function process(Request $request, callable $next): Response
    {
        $ip         = $request->getRealIp($safe_mode = true);
        $controller = $request->controller;
        $action     = $request->action;
        $transfer   = $controller . '::' . $action;

        // 开始计时
        $unique = StatisticClient::tick('project', $ip, $transfer);

        $response = $next($request);

        $code    = $response->getStatusCode();
        $success = $code < 400;
        $details = [
            'ip'              => $request->getRealIp($safe_mode = true) ?? '',   // 请求客户端IP
            'url'             => $request->fullUrl() ?? '',                      // 请求URL
            'method'          => $request->method() ?? '',                       // 请求方法
            'request_param'   => $request->all() ?? [],                          // 请求参数
            'request_header'  => $request->header() ?? [],                       // 请求头
            'cookie'          => $request->cookie() ?? [],                       // 请求cookie
            'session'         => $request->session()->all() ?? [],               // 请求session
            'response_code'   => $response->getStatusCode() ?? '',               // 响应码
            'response_header' => $response->getHeaders() ?? [],                  // 响应头
            'response_body'   => $success ?: (string)$response->rawBody(),       // 响应数据(发生异常)
        ];
        // 数据上报
        StatisticClient::report($unique, 'project', $ip, $transfer, $success, $code, json_encode($details, 320));

        return $response;
    }
}

2、通用使用

<?php

require_once __DIR__ . '/StatisticClient.php';

// 设置上报地址
StatisticClient::$remoteAddress = 'udp://127.0.0.1:8789';

$project  = 'test';       // 应用
$ip       = '127.0.0.1';  // 客户端IP
$transfer = 'test';       // 调用

// 计时
$unique = StatisticClient::tick($project, $ip, $transfer);

// 模拟运行
usleep(mt_rand(5, 200));

// 上报
$code    = mt_rand(100, 600);           // 状态码
$success = $code < 400 ? true : false;  // 是否成功
$details = json_encode([
    'project'  => $project,
    'ip'       => $ip,
    'transfer' => $transfer,
    'code'     => $code,
    'success'  => $success
], 320);  // 详情(JSON格式)
StatisticClient::report($unique, $project, $ip, $transfer, $success, $code, $details);

3、SQL监控(ThinkORM 示例)

\think\facade\Db::listen(function ($sql, $runtime, $master) {
    switch (true) {
        case is_numeric($runtime):
            $transfer = $sql;
            $cost     = $runtime;
            break;
        case !is_numeric($runtime) && 'CONNECT' === substr($sql, 0, 7):
            @preg_match("/UseTime:([0-9]+(\\.[0-9]+)?|[0-9]+(\\.[0-9]+))/", $sql, $result);
            if (count($result) > 1) {
                $transfer = substr($sql, strpos($sql, "s ] ") + 4);
                $cost     = $result[1];
            } else {
                $transfer = $sql;;
                $cost     = 0;
            }
            break;
        default:
            $transfer = $sql;;
            $cost     = 0;
            break;
    }
    StatisticClient::report('', 'projectSql', '127.0.0.1', $transfer, true, 1, json_encode([
        'sql'     => $sql,
        'runtime' => $cost . 's',
        'master'  => $master,
    ], 320), $cost);
});

transfer-statistics's People

Contributors

hsk99 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.