Skip to content

Commit

Permalink
feat(plugins): db plugins failed
Browse files Browse the repository at this point in the history
- plugins failed in simple-php
  • Loading branch information
eeliu committed Apr 25, 2024
1 parent 6c53ce9 commit 94aa222
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 97 deletions.
39 changes: 27 additions & 12 deletions lib/Pinpoint/Common/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ class Logger
{
private $log = null;
private static $_inst;
private $level_ = 2;
private function __construct()
{

}

private function defaultLogger(string $message, $context)
Expand All @@ -41,6 +43,11 @@ public static function Inst()
return self::$_inst;
}

public function setLoggerLevel(int $level)
{
$this->level_ = $level;
}

public function setLogger($logger)
{
assert(method_exists($logger, 'debug'));
Expand All @@ -51,27 +58,35 @@ public function setLogger($logger)

public function debug($message, array $context = [])
{
if ($this->log != null) {
$this->log->debug($message, $context);
} else {
$this->defaultLogger($message, $context);
if ($this->level_ <= 0) {
if ($this->log != null) {
$this->log->debug($message, $context);
} else {
$this->defaultLogger($message, $context);
}
}
}

public function info($message, array $context = [])
{
if ($this->log != null) {
$this->log->info($message, $context);
} else {
$this->defaultLogger($message, $context);
if ($this->level_ <= 1) {
if ($this->log != null) {
$this->log->info($message, $context);
} else {
$this->defaultLogger($message, $context);
}
}

}
public function warning($message, array $context = [])
{
if ($this->log != null) {
$this->log->warning($message, $context);
} else {
$this->defaultLogger($message, $context);
if ($this->level_ <= 2) {
if ($this->log != null) {
$this->log->warning($message, $context);
} else {
$this->defaultLogger($message, $context);
}
}

}
}
4 changes: 2 additions & 2 deletions lib/Pinpoint/Plugins/Common/Trace.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@

class Trace extends AbstractMonitor
{
public function __construct($apId, $who, &...$args)
public function __construct($monitor_name, $who, &...$args)
{
parent::__construct($apId, $who, $args);
parent::__construct($monitor_name, $who, $args);
}

public function __destruct()
Expand Down
21 changes: 11 additions & 10 deletions lib/Pinpoint/Plugins/Common/defines.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,20 @@
define('PP_PHP_RETURN', 14);
define('PP_PROXY_HTTP_HEADER', 300);
define('PP_SQL_ID', 20);
define("PP_SQL", 21);
define("PP_SQL_METADATA", 22);
define("PP_SQL_PARAM", 23);
define("PP_SQL_BINDVALUE", 24);
define("PP_SQL", 21);
define("PP_SQL_METADATA", 22);
define("PP_SQL_PARAM", 23);
define("PP_SQL_BINDVALUE", 24);
define("PP_STRING_ID", 30);
define("PP_SQL_FORMAT", "SQL");
define("PP_HTTP_URL", 40);
define("PP_HTTP_PARAM", 41);
define("PP_HTTP_PARAM_ENTITY", 42);
define("PP_HTTP_COOKIE", 45);
define("PP_HTTP_STATUS_CODE", 46);
define("PP_HTTP_PARAM", 41);
define("PP_HTTP_PARAM_ENTITY", 42);
define("PP_HTTP_COOKIE", 45);
define("PP_HTTP_STATUS_CODE", 46);
define("PP_MEMORY_USAGE", 330);
define("PP_HTTP_INTERNAL_DISPLAY", 48);
define("PP_HTTP_IO", 49);
define("PP_HTTP_INTERNAL_DISPLAY", 48);
define("PP_HTTP_IO", 49);
define("PP_MESSAGE_QUEUE_URI ", 100);
define("PP_KAFKA_TOPIC", 140);
define("PP_PHP_METHOD", "1501");
Expand Down Expand Up @@ -86,6 +86,7 @@
define("PP_SPAN_ID", "sid");
define("PP_NOT_SAMPLED", "s0");
define("PP_SAMPLED", "s1");
define("PP_MONGODB_EXE_QUERY", "2651");
# key for support UT
# https://github.com/pinpoint-apm/pinpoint-c-agent/issues/534
define("PP_ROUTE_KEY", '__pinpoint__route');
Expand Down
21 changes: 10 additions & 11 deletions lib/Pinpoint/Plugins/GuzzleHttp/GuzzlePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,28 @@ class GuzzlePlugin extends PinTrace
///@hook:GuzzleHttp\Psr7\Request::__construct
function onBefore()
{
if(strpos($this->apId, "Request::__construct") !== false){
pinpoint_add_clue(PP_DESTINATION,CurlUtil::getHostFromURL((string)($this->args[1])));
pinpoint_add_clues(PP_HTTP_URL,$this->args[1]);
pinpoint_add_clue(PP_SERVER_TYPE,PP_PHP_REMOTE);
if (strpos($this->monitor_name, "Request::__construct") !== false) {
pinpoint_add_clue(PP_DESTINATION, CurlUtil::getHostFromURL((string) ($this->args[1])));
pinpoint_add_clues(PP_HTTP_URL, $this->args[1]);
pinpoint_add_clue(PP_SERVER_TYPE, PP_PHP_REMOTE);

$n_headers =[] ;
if( is_array($this->args[2]) && array_key_exists('headers',$this->args[2]))
{
$n_headers = [];
if (is_array($this->args[2]) && array_key_exists('headers', $this->args[2])) {
$n_headers = $this->args[2];
}
$n_headers = array_merge($n_headers,CurlUtil::getPPHeader($this->args[1]));
$n_headers = array_merge($n_headers, CurlUtil::getPPHeader($this->args[1]));
$this->args[2] = $n_headers;
}
}

function onEnd(&$ret)
{
pinpoint_add_clue(PP_NEXT_SPAN_ID,pinpoint_get_context(PP_NEXT_SPAN_ID));
pinpoint_add_clues(PP_HTTP_STATUS_CODE,(string)($ret->getStatusCode()));
pinpoint_add_clue(PP_NEXT_SPAN_ID, pinpoint_get_context(PP_NEXT_SPAN_ID));
pinpoint_add_clues(PP_HTTP_STATUS_CODE, (string) ($ret->getStatusCode()));
}

function onException($e)
{
pinpoint_add_clue(PP_ADD_EXCEPTION,$e->getMessage());
pinpoint_add_clue(PP_ADD_EXCEPTION, $e->getMessage());
}
}
15 changes: 8 additions & 7 deletions lib/Pinpoint/Plugins/MongoPlugin/MongoPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
#-------------------------------------------------------------------------------


namespace Pinpoint\Plugins;
namespace Pinpoint\Plugins\MongoPlugin;

use Pinpoint\Plugins\Common\PinTrace;

class MongoPlugin extends PinTrace
Expand All @@ -31,14 +32,14 @@ class MongoPlugin extends PinTrace
*/
function onBefore()
{
if(strpos($this->apId, "Client::__construct")) {
$url=$this->args[0];
pinpoint_add_clue(SERVER_TYPE,MONGODB_EXE_QUERY);
pinpoint_add_clue(DESTINATION, $url);
if (strpos($this->monitor_name, "Client::__construct")) {
$url = $this->args[0][0][0];
pinpoint_add_clue(PP_SERVER_TYPE, PP_MONGODB_EXE_QUERY);
pinpoint_add_clue(PP_DESTINATION, $url);
return;
}
pinpoint_add_clue(SERVER_TYPE,MONGODB_EXE_QUERY);
pinpoint_add_clues(PHP_ARGS, print_r($this->args[0],true));
pinpoint_add_clue(PP_SERVER_TYPE, PP_MONGODB_EXE_QUERY);
pinpoint_add_clues(PP_DESTINATION, print_r($this->args[0], true));
}

function onEnd(&$ret)
Expand Down
36 changes: 14 additions & 22 deletions lib/Pinpoint/Plugins/Sys/PDO/PDO.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,58 +20,50 @@ class PDO extends \PDO
{
public $dsn;

public function __construct ($dsn, $username=null, $passwd=null, $options=[])
public function __construct($dsn, $username = null, $passwd = null, $options = [])
{
$this->dsn = $dsn;
parent::__construct($dsn, $username, $passwd, $options);
}

private function doPDOQuery($statement, $mode = \PDO::ATTR_DEFAULT_FETCH_MODE, $arg3 = null, array $ctorargs = array())
public function query($query, $fetchMode = 0)
{
$args = \pinpoint_get_func_ref_args();
$var = new PreparePlugin("PDO::query",$this,...$args);
try{
$var = new PreparePlugin("PDO::query", $this, $query, $fetchMode);
try {
$var->onBefore();
$ret = parent::query(...$args);
$ret = parent::query($query, $fetchMode);
$var->onEnd($ret);
return $ret;
}catch (\Exception $e){
} catch (\Exception $e) {
$var->onException($e);
throw new \Exception($e);
}

}

public function query()
{
return $this->doPDOQuery(...\pinpoint_get_func_ref_args());
}


public function exec($statement)
{
$var = new PDOExec("PDO::exec",$this,$statement);
try{
$var = new PDOExec("PDO::exec", $this, $statement);
try {
$var->onBefore();
$ret = parent::exec($statement);
$var->onEnd($ret);
return $ret;
}catch (\Exception $e){
} catch (\Exception $e) {
$var->onException($e);
throw new \Exception($e);
}
}


public function prepare($statement, $driver_options = array())
public function prepare($statement, $driver_options = array())
{
$var = new PreparePlugin("PDO::prepare",$this,$statement,$driver_options);
try{
$var = new PreparePlugin("PDO::prepare", $this, $statement, $driver_options);
try {
$var->onBefore();
$ret = parent::prepare($statement,$driver_options);
$ret = parent::prepare($statement, $driver_options);
$var->onEnd($ret);
return $ret;
}catch (\Exception $e){
} catch (\Exception $e) {
$var->onException($e);
throw new \Exception($e);
}
Expand Down
20 changes: 10 additions & 10 deletions lib/Pinpoint/Plugins/Sys/PDO/PreparePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@
use Pinpoint\Plugins\Common\PinTrace;

class PreparePlugin extends PinTrace

{
function onBefore()
{
// todo stp, should follow the dsn
$dbInfo = $this->parseDb($this->who->dsn);
pinpoint_add_clue(PP_SERVER_TYPE,PP_MYSQL);
pinpoint_add_clue(PP_SQL_FORMAT, $this->args[0]);
pinpoint_add_clue(PP_DESTINATION,$dbInfo['host']);
pinpoint_add_clue(PP_SERVER_TYPE, PP_MYSQL);
pinpoint_add_clue(PP_SQL_FORMAT, $this->args[0][0][0]);
pinpoint_add_clue(PP_DESTINATION, $dbInfo['host']);
}
function onEnd(&$ret)
{
Expand All @@ -37,19 +36,20 @@ function onEnd(&$ret)

function onException($e)
{
pinpoint_add_clue(PP_ADD_EXCEPTION,$e->getMessage());
pinpoint_add_clue(PP_ADD_EXCEPTION, $e->getMessage());
}

function parseDb($dsn){
function parseDb($dsn)
{

$db_url = parse_url($dsn);
parse_str(str_replace(';','&',$db_url['path']),$dbInfo);
$db_url = parse_url($dsn);
parse_str(str_replace(';', '&', $db_url['path']), $dbInfo);

if($db_url['scheme'] == 'sqlite'){ // treat sqllite as mysql
if ($db_url['scheme'] == 'sqlite') { // treat sqllite as mysql
$dbInfo['host'] = 'localhost-sqlite';
}

$dbInfo['scheme']= $db_url['scheme'];
$dbInfo['scheme'] = $db_url['scheme'];

return $dbInfo;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/Pinpoint/Plugins/Sys/mysqli/MysqliPreparePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ class MysqliPreparePlugin extends PinTrace
function onBefore()
{
$myqli = $this->who;
pinpoint_add_clue(PP_SERVER_TYPE,PP_MYSQL);
pinpoint_add_clue(PP_SQL_FORMAT, $this->args[0]);
pinpoint_add_clue(PP_DESTINATION,$myqli->host_info);
pinpoint_add_clue(PP_SERVER_TYPE, PP_MYSQL);
pinpoint_add_clue(PP_SQL_FORMAT, $this->args[0][0][0]);
pinpoint_add_clue(PP_DESTINATION, $myqli->host_info);
}

function onEnd(&$ret)
Expand Down
6 changes: 3 additions & 3 deletions lib/Pinpoint/Plugins/Sys/mysqli/MysqliQueryPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ class MysqliQueryPlugin extends PinTrace
function onBefore()
{
$myqli = $this->who;
pinpoint_add_clue(PP_SERVER_TYPE,PP_MYSQL);
pinpoint_add_clue(PP_SQL_FORMAT,$this->args[0]);
pinpoint_add_clue(PP_DESTINATION,$myqli->host_info);
pinpoint_add_clue(PP_SERVER_TYPE, PP_MYSQL);
pinpoint_add_clue(PP_SQL_FORMAT, $this->args[0][0][0]);
pinpoint_add_clue(PP_DESTINATION, $myqli->host_info);
}

function onEnd(&$ret)
Expand Down
Loading

0 comments on commit 94aa222

Please sign in to comment.