自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

PHP 服務(wù)實(shí)現(xiàn)性能剖析、跟蹤和可觀察性實(shí)踐

開發(fā) 前端
數(shù)據(jù)上報(bào)至鏈路追蹤控制臺(tái)后,鏈路追蹤組件進(jìn)行實(shí)時(shí)聚合計(jì)算和持久化,形成鏈路明細(xì)、性能總覽、實(shí)時(shí)拓?fù)涞缺O(jiān)控?cái)?shù)據(jù)。您可以據(jù)此進(jìn)行問題排查與診斷。

簡介

鏈路追蹤Tracing Analysis為分布式應(yīng)用的開發(fā)者提供了完整的調(diào)用鏈路還原、調(diào)用請求量統(tǒng)計(jì)、鏈路拓?fù)洹?yīng)用依賴分析等工具,可以幫助開發(fā)者快速分析和診斷分布式應(yīng)用架構(gòu)下的性能瓶頸,提高微服務(wù)時(shí)代下的開發(fā)診斷效率。

官方地址:https://github.com/openzipkin/zipkin

Zipkin運(yùn)行架構(gòu)

圖片圖片

產(chǎn)品架構(gòu)(鏈路追蹤)

圖片圖片

鏈路追蹤的主要工作流程

  1. 客戶側(cè)的應(yīng)用程序通過集成鏈路追蹤的多語言客戶端SDK上報(bào)服務(wù)調(diào)用數(shù)據(jù)。鏈路追蹤支持多種開源社區(qū)的SDK,且支持OpenTracing標(biāo)準(zhǔn)。
  2. 數(shù)據(jù)上報(bào)至鏈路追蹤控制臺(tái)后,鏈路追蹤組件進(jìn)行實(shí)時(shí)聚合計(jì)算和持久化,形成鏈路明細(xì)、性能總覽、實(shí)時(shí)拓?fù)涞缺O(jiān)控?cái)?shù)據(jù)。您可以據(jù)此進(jìn)行問題排查與診斷。
  3. 調(diào)用鏈數(shù)據(jù)可對接下游阿里云產(chǎn)品,例如LogSearch、CloudMonitor、MaxCompute等,用于離線分析、報(bào)警等場景。

業(yè)務(wù)場景

隨著業(yè)務(wù)越來越復(fù)雜,系統(tǒng)也隨之進(jìn)行各種拆分,特別是隨著微服務(wù)架構(gòu)和容器技術(shù)的興起,看似簡單的一個(gè)應(yīng)用,后臺(tái)可能有幾十個(gè)甚至幾百個(gè)服務(wù)在支撐;一個(gè)前端的請求可能需要多次的服務(wù)調(diào)用最后才能完成;當(dāng)請求變慢或者不可用時(shí),我們無法得知是哪個(gè)后臺(tái)服務(wù)引起的,這時(shí)就需要解決如何快速定位服務(wù)故障點(diǎn),zipkin分布式跟蹤系統(tǒng)就能很好的解決這樣的問題。

圖片圖片

請求&響應(yīng)

微服務(wù)架構(gòu)下,一次請求后端會(huì)經(jīng)歷多個(gè)服務(wù)調(diào)用(所有請求鏈有相同的traceId和不同的spanId),都會(huì)沿著traceText帶到每一個(gè)服務(wù)中。

數(shù)據(jù)是如何上報(bào)的?

直接上報(bào)數(shù)據(jù)

圖片圖片

不通過Agent而直接上報(bào)數(shù)據(jù)的原理(傳統(tǒng)框架。PHP-FPM + Nginx模式)

  • ThinkPHP6.0
  • Laravel
  • Yii2.0

通過Agent上報(bào)數(shù)據(jù)

圖片圖片

通過Agent上報(bào)數(shù)據(jù)的原理(現(xiàn)代化框架。命令行模式)。

  • webman
  • Swoole

安裝

通過composer安裝:

composer require openzipkin/zipkin

使用

創(chuàng)建Tracer

Tracer對象可以用來創(chuàng)建Span對象(記錄分布式操作時(shí)間)。Tracer對象還配置了上報(bào)數(shù)據(jù)的網(wǎng)關(guān)地址、本機(jī)IP、采樣頻率等數(shù)據(jù),您可以通過調(diào)整采樣率來減少因上報(bào)數(shù)據(jù)產(chǎn)生的開銷。

function create_tracing($endpointName, $ipv4)
{
    $endpoint = Endpoint::create($endpointName, $ipv4, null, 2555);
    /* Do not copy this logger into production.
     * Read https://github.com/Seldaek/monolog/blob/master/doc/01-usage.md#log-levels
     */
    $logger = new \Monolog\Logger('log');
    $logger->pushHandler(new \Monolog\Handler\ErrorLogHandler());
    $reporter = new Zipkin\Reporters\Http(\Zipkin\Reporters\Http\CurlFactory::create());
    $sampler = BinarySampler::createAsAlwaysSample();
    $tracing = TracingBuilder::create()
        ->havingLocalEndpoint($endpoint)
        ->havingSampler($sampler)
        ->havingReporter($reporter)
        ->build();
    return $tracing;
}

記錄請求數(shù)據(jù)

$rootSpan = $tracer->newTrace();
$rootSpan->setName('encode');
$rootSpan->start();

try {
  doSomethingExpensive();
} finally {
  $rootSpan->finish();
}

以上代碼用于記錄請求的根操作,如果需要記錄請求的上一步和下一步操作,則需要傳入上下文。示例:

$span = $tracer->newChild($parentSpan->getContext());
$span->setName('encode');
$span->start();
try {
  doSomethingExpensive();
} finally {
  $span->finish();
}

總體流程

Client Span                                                Server Span
┌──────────────────┐                                       ┌──────────────────┐
│                  │                                       │                  │
│   TraceContext   │           Http Request Headers        │   TraceContext   │
│ ┌──────────────┐ │          ┌───────────────────┐        │ ┌──────────────┐ │
│ │ TraceId      │ │          │ X-B3-TraceId      │        │ │ TraceId      │ │
│ │              │ │          │                   │        │ │              │ │
│ │ ParentSpanId │ │ Inject   │ X-B3-ParentSpanId │Extract │ │ ParentSpanId │ │
│ │              ├─┼─────────>│                   ├────────┼>│              │ │
│ │ SpanId       │ │          │ X-B3-SpanId       │        │ │ SpanId       │ │
│ │              │ │          │                   │        │ │              │ │
│ │ Sampled      │ │          │ X-B3-Sampled      │        │ │ Sampled      │ │
│ └──────────────┘ │          └───────────────────┘        │ └──────────────┘ │
│                  │                                       │                  │
└──────────────────┘                                       └──────────────────┘

webman應(yīng)用

1. 開通ARMS

開通ARMS地址 https://arms.console.aliyun.com/ (一般有15天試用)

2. 獲得數(shù)據(jù)上報(bào)接入點(diǎn)url

進(jìn)入 https://tracing.console.aliyun.com/#/globalSetting/cn-hangzhou/process 按照圖示獲得接入點(diǎn)url地址。

圖片圖片

如果你的服務(wù)器在阿里云上可以用阿里云vpc網(wǎng)絡(luò)接入點(diǎn),本示例用的是阿里云公網(wǎng)接入點(diǎn)。

安裝

通過composer安裝:

composer require openzipkin/zipkin

使用

1. 編寫中間件

鏈路監(jiān)控中間件 app\middleware\ArmsMiddleware.php

<?php
/**
 * @desc 全鏈路監(jiān)控中間件
 * @author Tinywan(ShaoBo Wan)
 * @date 2021/12/6 14:06
 */
declare(strict_types=1);

namespace app\middleware;

use Monolog\Handler\ErrorLogHandler;
use Monolog\Logger;
use support\Log;
use think\facade\Db;
use Webman\MiddlewareInterface;
use Webman\Http\Response;
use Webman\Http\Request;
use Zipkin\Reporters\Http;
use Zipkin\TracingBuilder;
use Zipkin\Samplers\BinarySampler;
use Zipkin\Endpoint;
use Workerman\Timer;
use const Zipkin\Tags\SQL_QUERY;

class ArmsMiddleware implements MiddlewareInterface
{
    /**
     * @desc: 方法描述
     * @param Request $request
     * @param callable $next
     * @return Response
     * @author Tinywan(ShaoBo Wan)
     */
    public function process(Request $request, callable $next) : Response
    {
        static $tracing = null, $tracer = null;
        if (!$tracing) {
            $endpoint = Endpoint::create('開源技術(shù)小棧', $request->getRealIp(), null, 2555);
            $logger = new Logger('log');
            $logger->pushHandler(new ErrorLogHandler());
            $reporter = new Http(['endpoint_url' => config('security')['endpoint_url']]);
            $sampler = BinarySampler::createAsAlwaysSample();
            $tracing = TracingBuilder::create()
                ->havingLocalEndpoint($endpoint)
                ->havingSampler($sampler)
                ->havingReporter($reporter)
                ->build();
            $tracer = $tracing->getTracer();
            // 55秒上報(bào)一次,盡量將上報(bào)對業(yè)務(wù)的影響減少到最低
            Timer::add(55, function () use ($tracer) {
                $tracer->flush();
            });
            register_shutdown_function(function () use ($tracer) {
                $tracer->flush();
            });
        }

        $rootSpan = $tracer->newTrace();
        $rootSpan->setName($request->controller."::".$request->action);
        $rootSpan->start();
        $request->rootSpan = $rootSpan;
        $request->tracer = $tracer;
        $result = $next($request);

        // 統(tǒng)計(jì)sql(日志在內(nèi)存)
        if (class_exists(Db::class)) {
            $logs = Db::getDbLog(true);
            if (!empty($logs['sql'])) {
                foreach ($logs['sql'] as $sql) {
                    $sqlSpan = $tracer->newChild($rootSpan->getContext());
                    $sqlSpan->setName(SQL_QUERY);
                    $sqlSpan->start();
                    $sqlSpan->tag('db.statement', $sql);
                    $sqlSpan->finish();
                }
            }
        }

        $rootSpan->finish();

        return $result;
    }
}

2. 配置中間件

在 config/middleware.php 中添加全局中間件如下:

return [
    '' => [
        \app\middleware\ArmsMiddleware::class,
    ],
    ...
];

3. 查看監(jiān)控

訪問地址 https://tracing.console.aliyun.com/ ,效果類似如下:

圖片圖片

接口監(jiān)控

圖片圖片

數(shù)據(jù)庫監(jiān)控

圖片圖片

責(zé)任編輯:武曉燕 來源: 開源技術(shù)小棧
相關(guān)推薦

2024-03-27 14:43:07

.NET Core后端監(jiān)控可觀測性

2021-07-12 11:24:00

流利說可觀察性平臺(tái)阿里云

2021-06-06 22:39:48

網(wǎng)絡(luò)安全監(jiān)控網(wǎng)絡(luò)攻擊

2022-08-12 06:26:54

微服務(wù)架構(gòu)

2021-01-26 09:11:16

數(shù)字體驗(yàn)DEM網(wǎng)絡(luò)可觀察性

2023-06-12 16:45:20

數(shù)據(jù)管理

2024-06-18 10:16:49

2021-10-26 10:26:25

云計(jì)算云計(jì)算環(huán)境云應(yīng)用

2021-11-14 22:14:08

人工智能機(jī)器學(xué)習(xí)工具

2023-02-23 19:28:09

ODD測試

2021-06-27 17:18:23

網(wǎng)絡(luò)可觀察性網(wǎng)絡(luò)網(wǎng)絡(luò)運(yùn)營

2023-08-24 08:00:00

開發(fā)Java可觀察性

2022-07-18 13:37:56

云計(jì)算云原生可觀察性

2023-02-21 08:00:00

2022-12-29 10:16:12

觀察性系統(tǒng)監(jiān)視

2023-11-17 08:00:54

Tetragon執(zhí)行工具

2021-09-26 09:50:21

開發(fā)技能程序

2023-03-10 14:03:57

2023-01-28 13:42:16

2023-03-23 13:48:00

工具應(yīng)用場景選型
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)