Skip to content

pinpoint in phpmyadmin

eeliu edited this page Aug 12, 2024 · 5 revisions

English | 中文 | 한국어

Use PINPOINT PHP in phpmyadmin

why we post this article ?

Share the experience of monitoring php project

what you will get ?

  1. How to user pinpoint in PHP, like phpmyadmin
  2. How to customize pinpoint php

Let start ...

Install pinpoint php agent

  1. Install phpmyadmin from https://www.phpmyadmin.net/.
  2. Install pinpoint_php_ext
    1. pecl install pinpoint_php or curl -sL https://github.com/pinpoint-apm/pinpoint-c-agent/releases/latest/download/install_pinpoint_php.sh | sh

      if step 2.1 failed, please share your environment to use by post an issue,

    2. On windows try to download pinpoint.dll from https://pecl.php.net/package/pinpoint_php  windows_dll
      • if you can't access pecl.php.net, try to download from github release. github release
  3. Enable pinpoint_php into your php

detail setting https://github.com/pinpoint-apm/pinpoint-c-agent/blob/dev/DOC/PHP/Readme.md#steps

[pinpoint_php]
extension=pinpoint_php.so
pinpoint_php.CollectorHost=tcp:collector-agent-host:10000
pinpoint_php.DebugReport=true
...
  1. Add pinpoint-php-aop requirements composer require -w pinpoint-apm/pinpoint-php-aop
  2. Congratulations, only the last steps. Add pinpoint into your entry file phpmyadmin:
<?php
// www/html/index.php
...
require AUTOLOAD_FILE;

class MyAdminRequestPlugin extends Pinpoint\Plugins\DefaultRequestPlugin
{
    public function __construct()
    {
        $blackUri = ['/favicon.ico'];
        // if uri in blackUri, skips it 
        if (!in_array($_SERVER['REQUEST_URI'], $blackUri)) {
            parent::__construct();
        }
    }
    public function __destruct()
    {
        // do nothing
    }
}
define('APPLICATION_NAME', 'cd.dev.test.php'); // your application name
define('APPLICATION_ID', 'cd.dev.phpmyadmin');  // your application id
define('PP_REQ_PLUGINS', MyAdminRequestPlugin::class);
require_once __DIR__ . '/vendor/pinpoint-apm/pinpoint-php-aop/auto_pinpointed.php';

...

until now , agent installation is done

Install collector-agent

Open your phpmyadmin web, you will get such logging from php. collector-agent

This is caused from lack of collector-agent.

what is collector-agent: https://github.com/pinpoint-apm/pinpoint-c-agent/tree/dev/DOC/collector-agent

Before you install collector-agent, please ensure pinpoint works fine.

Install pinpoint

By docker-compose

https://github.com/pinpoint-apm/pinpoint-docker

Install collector-agent

docker run -itd -p 10000:10000 --env-file ./env.list ghcr.io/pinpoint-apm/pinpoint-c-agent/collector-agent:latest

## env.list dev-pinpoint  address of pinpoint collector
PP_COLLECTOR_AGENT_SPAN_IP=dev-pinpoint
PP_COLLECTOR_AGENT_SPAN_PORT=9993
PP_COLLECTOR_AGENT_AGENT_IP=dev-pinpoint
PP_COLLECTOR_AGENT_AGENT_PORT=9991
PP_COLLECTOR_AGENT_STAT_IP=dev-pinpoint
PP_COLLECTOR_AGENT_STAT_PORT=9992
...

Detail: https://github.com/pinpoint-apm/pinpoint-c-agent/blob/dev/DOC/collector-agent/readme.md

Check your request on pinpoint-web

service call map

service call

call stack map

call stack

How to customize pinpoint PHP agent

I had skipped an important part, pinpoint entry. Here let me introduce it here,

what is it ?

require_once __DIR__ . '/vendor/autoload.php'; // !!! must insert right behind system/framework autoload

define('APPLICATION_NAME', 'cd.dev.test.php'); // your application name
define('APPLICATION_ID', 'cd.dev.phpmyadmin');  // your application id
define('PP_REQ_PLUGINS', \Pinpoint\Plugins\DefaultRequestPlugin::class); //
require_once __DIR__ . '/vendor/pinpoint-apm/pinpoint-php-aop/auto_pinpointed.php';

PP_REQ_PLUGINS: it helps user to customize some special feature.

  1. Skip some requests. ( also called black list )
    public function __construct()
    {
        $blackUri = ['/favicon.ico'];
        // if uri in blackUri, skips it 
        if (!in_array($_SERVER['REQUEST_URI'], $blackUri)) {
            parent::__construct();
        }
    }

A full example : https://github.com/pinpoint-apm/pinpoint-c-agent/blob/dev/testapps/php_phpmyadmin/index.php#L38-L44

  1. Add hook/monitoring on user class or built-in function

    1. Built-in function/method
      • load the plugin before that function/method was called
      • follow plugins template
      pinpoint_join_cut(
          "built-in-function/method",$on_before_callback,$on_end_callback,$on_exception_callback
      )

      There are many examples into https://github.com/pinpoint-apm/pinpoint-php-aop/blob/dev/lib/Pinpoint/Plugins/SysV2/

    2. User class (must load by auto_loader)
      • Extends \Pinpoint\Plugins\DefaultRequestPlugin and override joinedClassSet
      • Append your AspectClassHandle instance
          public function joinedClassSet(): array
          {
              // don't forget passing the parent AspectClassHandle
              $cls = parent::joinedClassSet();
              $classHandler = new Pinpoint\Common\AspectClassHandle(\User\ABC::class);
              $classHandler->addJoinPoint('foo_method', \Pinpoint\Plugins\Common\CommonPlugin::class);
              $cls[] = $classHandler;
              return $cls;
          }

      There are many examples into https://github.com/pinpoint-apm/pinpoint-php-aop/blob/dev/lib/Pinpoint/Plugins/Yii2PerRequestPlugins.php,https://github.com/pinpoint-apm/pinpoint-php-aop/blob/dev/lib/Pinpoint/Plugins/DefaultRequestPlugin.php

Clone this wiki locally