Skip to content

Commit

Permalink
support Yii2 (pinpoint-apm#25)
Browse files Browse the repository at this point in the history
* yii2, db connection
* aop library support "extend \PDO " style
* ci is failed
  • Loading branch information
eeliu authored and liu.mingyi committed Dec 20, 2021
1 parent 97384b1 commit b8b5a7b
Show file tree
Hide file tree
Showing 48 changed files with 308 additions and 117 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ jobs:
tools: composer:v2
- run: |
composer install --prefer-dist
php vendor/bin/phpunit --configuration PHPUnit.xml --testsuit pinpoint --testdox
vendor/bin/phpunit --configuration PHPUnit.xml --testsuit pinpoint --testdox
5 changes: 2 additions & 3 deletions PHPUnit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false">
stopOnFailure="true">
<testsuites>
<testsuite name="pinpoint">
<directory>lib/pinpoint/test</directory>
<directory>lib/Pinpoint/test</directory>
</testsuite>
</testsuites>
<filter>
Expand Down
3 changes: 2 additions & 1 deletion auto_pinpointed.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
use Pinpoint\Plugins\PerRequestPlugins;
define('CLASS_PREFIX','Proxied_');

PinpointDriver::getInstance()->start();
if(defined('PP_REQ_PLUGINS') && class_exists(PP_REQ_PLUGINS)){
$plugins = PP_REQ_PLUGINS;
$plugins::instance();
}else{
PerRequestPlugins::instance();
}

PinpointDriver::getInstance()->start();
2 changes: 1 addition & 1 deletion lib/Pinpoint/Common/ClassFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public function fileNodeDoneCB(&$node, $loaderName)
$fullPath = AOP_CACHE_DIR.'/'.str_replace('\\','/',$loaderName).'.php';
$context= $this->_astPrinter->prettyPrintFile($node);
RenderAopClass::getInstance()->insertMapping($loaderName,$fullPath);
Util::saveObj($context,$fullPath);
Utils::saveObj($context,$fullPath);
}

abstract function handleAfterTravers(&$nodes);
Expand Down
12 changes: 7 additions & 5 deletions lib/Pinpoint/Common/GenProxiedClassFileHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ public function handleEnterNew_(&$node)
assert($node instanceof Node\Expr\New_);
$node->class = $this->renderClassName($node->class,$this->t_covertCls);
return $node;

}

public function handleEnterClassConstFetch(&$node)
{
assert($node instanceof Node\Expr\ClassConstFetch);
Expand Down Expand Up @@ -194,10 +194,12 @@ public function handleLeaveMethodNode(&$node)
public function handleFullyQualifiedNode(&$node)
{
assert($node instanceof Node\Name\FullyQualified);

$newNode = new Node\Name($node->toString());

return $newNode;
$name = $node->toString();
if( isset($this->t_covertCls[$name]) ) {
return new Node\Name\FullyQualified($this->t_covertCls[$name]);
}else{
return $node;
}
}

public function addRequiredFile($fullName)
Expand Down
5 changes: 4 additions & 1 deletion lib/Pinpoint/Common/NpCoderVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ public function enterNode(Node $node)
}
elseif ($node instanceof Node\Expr\ClassConstFetch){
return $this->proxiedClassFile->handleEnterClassConstFetch($node);
}elseif ($node instanceof Node\Expr\New_){
}
elseif ($node instanceof Node\Name\FullyQualified) {
return $this->proxiedClassFile->handleFullyQualifiedNode($node);
}elseif ($node instanceof Node\Expr\New_) {
return $this->proxiedClassFile->handleEnterNew_($node);
}elseif ($node instanceof Node\Expr\FuncCall){
return $this->proxiedClassFile->handleEnterFuncCall($node);
Expand Down
1 change: 0 additions & 1 deletion lib/Pinpoint/Common/OriginFileVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public function runAllVisitor(string $fullPath,array $aopFuncInfo=[], array $nam
$code = file_get_contents($fullPath);
$stmts = $this->phpFileParser->parse($code);
$this->traverser->traverse($stmts);

}

private function getVisitor(string& $fullPath,array& $aopFuncInfo=[], array& $naming=[])
Expand Down
24 changes: 16 additions & 8 deletions lib/Pinpoint/Common/PinpointDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ final private function __construct()
{
}

public static function getAutoGenFiles()
private static function getAutoGenFiles()
{
$files = [];

foreach (static::$autoGenDirs as $dir)
{
if(is_dir($dir))
{
Util::scanDir($dir,"/Plugin.php$/",$files);
Utils::scanDir($dir,"/Plugin.php$/",$files);
}
}

Expand All @@ -65,12 +65,13 @@ public function start()

RenderAopClass::getInstance();
/// checking the cached file exist, if exist load it
if(Util::checkCacheReady())
if(Utils::checkCacheReady())
{
RenderAopClass::getInstance()->createFrom(Util::loadCachedClass());
RenderAopClass::getInstance()->createFrom(Utils::loadCachedClass());
RenderAopClassLoader::start();
return ;
}

VendorAdaptorClassLoader::enable();
$pluFiles = static::getAutoGenFiles();
foreach ($pluFiles as $file)
Expand All @@ -89,7 +90,7 @@ public function start()
{
foreach ($naming['appendFiles'] as $class)
{
$fullPath = Util::findFile($class);
$fullPath = Utils::findFile($class);
if(!$fullPath)
continue;
$visitor = new OriginFileVisitor();
Expand All @@ -103,7 +104,7 @@ public function start()
{
if(empty($class))
continue;
$fullPath = Util::findFile($class);
$fullPath = Utils::findFile($class);
if(!$fullPath )
continue;
$visitor = new OriginFileVisitor();
Expand All @@ -114,10 +115,17 @@ public function start()
}
}
// save render aop class into index file
Util::saveCachedClass(RenderAopClass::getInstance()->getLoadeMap());
Utils::saveCachedClass(RenderAopClass::getInstance()->getLoadeMap());
// enable RenderAop class loader
RenderAopClassLoader::start();
}


/**
* start /tail are the spl_autoload_functions checking order
* @param callable $start
* @param callable $tail
*/
public function addClassFinderHelper(callable $start, callable $tail){
Utils::addLoaderPatch($start,$tail);
}
}
2 changes: 1 addition & 1 deletion lib/Pinpoint/Common/PluginVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ private function loadCommentFunc(&$node,$mode)
{
foreach( $node->getComments() as &$doc)
{
$funArray = Util::parseUserFunc(trim($doc->getText()));
$funArray = Utils::parseUserFunc(trim($doc->getText()));

foreach ($funArray as $func)
{
Expand Down
29 changes: 26 additions & 3 deletions lib/Pinpoint/Common/Util.php → lib/Pinpoint/Common/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,33 @@

namespace Pinpoint\Common;

class Util
class Utils
{
const U_Method= 1;
const U_Function= 2;
const U_INDEX_FILE_PATH = AOP_CACHE_DIR.'__class_index_table';

static private $loaderPatch =[null,null];

public static function addLoaderPatch(callable $start,callable $tail=null){
static::$loaderPatch[0] = $start;
static::$loaderPatch[1] = $tail;
}

/**
* locate a class (via VendorAdaptorClassLoader)
* @param $class
* @return bool|string
*/
public static function findFile($class):string
{
if(is_callable(static::$loaderPatch[0])){
$files = call_user_func(static::$loaderPatch[0],$class);
if ($files){
return $files;
}
}

$splLoaders = spl_autoload_functions();
foreach ($splLoaders as &$loader) {

Expand All @@ -44,8 +58,17 @@ public static function findFile($class):string
if($address){
return realpath($address);
}
}else{
throw new \Exception("unknown loader");
}
// keep peace with unknown loader
// else{
// throw new \Exception("unknown loader");
// }
}

if(is_callable(static::$loaderPatch[1])){
$files = call_user_func(static::$loaderPatch[1],$class);
if ($files){
return $files;
}
}

Expand Down
19 changes: 16 additions & 3 deletions lib/Pinpoint/Common/VendorAdaptorClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,24 @@ public static function enable()
$loaders = spl_autoload_functions();
foreach ($loaders as &$loader) {
if ( is_array($loader) && is_callable($loader) ) {// common ClassLoader style


try{
// replace composer loader with aopclassLoader
$_loader = new self($loader);
}catch (Exception $e){
/**
* if current loader not compatible, just ignore it!
* why?
* 1. pinpoint-php-aop only works on known framework! Exception will not expose to usr
* 2. Keep this loader, as it will handled its class. (We could write patch for this loader)
* 3. Pinpoint classloader is the highest priority
*/
continue;
}

// unregister composer loader
spl_autoload_unregister($loader);
// replace composer loader with aopclassLoader
$_loader = new self($loader);

spl_autoload_register(array($_loader,'loadClass'),true,false);
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Pinpoint/Plugins/AutoGen/GuzzleHttp/GuzzlePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
******************************************************************************/
namespace Pinpoint\Plugins\AutoGen\GuzzleHttp;

use Pinpoint\Plugins\Common\Candy;
use Pinpoint\Plugins\Common\PinTrace;
use Pinpoint\Plugins\Sys\curl\CurlUtil;

///@hook:GuzzleHttp\Client::request
class GuzzlePlugin extends Candy
class GuzzlePlugin extends PinTrace
{
///@hook:GuzzleHttp\Psr7\Request::__construct
function onBefore()
Expand Down
4 changes: 2 additions & 2 deletions lib/Pinpoint/Plugins/AutoGen/MongoPlugin/MongoPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@


namespace Pinpoint\Plugins;
use Pinpoint\Plugins\Common\Candy;
use Pinpoint\Plugins\Common\PinTrace;

class MongoPlugin extends Candy
class MongoPlugin extends PinTrace
{

/**
Expand Down
41 changes: 41 additions & 0 deletions lib/Pinpoint/Plugins/AutoGen/Yii2/ConnectionPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php declare(strict_types=1);
#-------------------------------------------------------------------------------
# Copyright 2020 NAVER Corp
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy
# of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
#-------------------------------------------------------------------------------

namespace Pinpoint\Plugins\AutoGen\Yii2;
use Pinpoint\Plugins\Common\Trace;
use Pinpoint\Plugins\Sys\PDO\PDO;
use Yii;
class ConnectionPlugin extends Trace {
/**
* @hook:yii\db\Connection::createPdoInstance
*/
function onEnd(&$ret)
{
// god bless, the dsn,username,password,attributes is private
if( !$ret instanceof PDO){
// after Aop rendering, only $pdoClass is PDO
// https://github.com/yiisoft/yii2/blob/08da35e511e83b2184e6dfa46ec8232058ff4b2d/framework/db/Connection.php#L718-L723
$con = $this->who;
$dsn = $con->dsn;
if (strncmp('sqlite:@', $dsn, 8) === 0) {
$dsn = 'sqlite:' . Yii::getAlias(substr($dsn, 7));
}
$ret = new PDO($dsn, $con->username, $con->password, $con->attributes);
}
}

}
2 changes: 1 addition & 1 deletion lib/Pinpoint/Plugins/Common/CommonPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

namespace Pinpoint\Plugins\Common;

class CommonPlugin extends Candy
class CommonPlugin extends PinTrace
{
///@hook:app\DBcontrol::connectDb
public function onBefore(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
namespace Pinpoint\Plugins\Common;
require_once "PluginsDefines.php";

abstract class Candy
abstract class PinTrace
{
protected $apId;
protected $who;
Expand Down
33 changes: 25 additions & 8 deletions lib/Pinpoint/Plugins/Common/Trace.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);
/******************************************************************************
* Copyright 2020 NAVER Corp. *
* *
Expand All @@ -14,17 +14,34 @@
* See the License for the specific language governing permissions and *
* limitations under the License. *
******************************************************************************/
/*
* User: eeliu
* Date: 12/20/21
* Time: 5:12 PM
*/

namespace Pinpoint\Plugins\Common;

class Trace{

public static function generateSpanID()
class Trace
{
protected $apId;
protected $who;
protected $args;
protected $ret=null;

public function __construct($apId,$who,&...$args)
{
try {
return mt_rand();
} catch (\Exception $e) {
return rand();
}
$this->apId = $apId;
$this->who = $who;
$this->args = &$args;
}

public function __destruct(){}

function onBefore(){}

function onEnd(&$ret){}

public function onException($e) {}
}
Loading

0 comments on commit b8b5a7b

Please sign in to comment.