Skip to content

Commit

Permalink
feat(async): support async invocation (#69)
Browse files Browse the repository at this point in the history
* feat(async): support async invocation

- kakfa plugins

* fix missing branch
  • Loading branch information
eeliu authored Oct 21, 2024
1 parent b8f7def commit 1bc80a7
Show file tree
Hide file tree
Showing 12 changed files with 447 additions and 33 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ name: Build

on: [push, pull_request, workflow_dispatch]

env:
PINPOINT_C_AGENT_BRANCH: 0.7


jobs:
PHPUnit:
runs-on: ubuntu-latest
Expand All @@ -15,7 +19,7 @@ jobs:
with:
php-version: ${{ matrix.php-versions }}
tools: composer:v2
extensions: pinpoint_php-https://github.com/pinpoint-apm/pinpoint-c-agent@dev
extensions: pinpoint_php-0.6.0
- run: |
composer install --prefer-dist
vendor/bin/phpunit --configuration PHPUnit.xml --testsuit pinpoint --testdox
Expand Down Expand Up @@ -43,7 +47,7 @@ jobs:
with:
php-version: ${{ matrix.php-versions }}
tools: composer:v2
extensions: pinpoint_php-https://github.com/pinpoint-apm/pinpoint-c-agent@dev, mongodb-mongodb/[email protected]
extensions: pinpoint_php-0.6.0, mongodb-mongodb/[email protected]
- run: |
composer install --prefer-dist
vendor/bin/phpunit --configuration PHPUnit_aop_libraries.xml --testsuit pinpoint --testdox
14 changes: 14 additions & 0 deletions Changes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
## Changes

### - v4.0.1 async invocation API

- rdkafka plugins

### v4.0.0
> https://github.com/pinpoint-apm/pinpoint-php-aop/releases/tag/v4.0.0
* support nikic/php-parser v5.1.0

### - v3.0.8 async invocation API

> https://github.com/pinpoint-apm/pinpoint-php-aop/releases/tag/v3.0.8
* fix a bug in https://github.com/pinpoint-apm/pinpoint-c-agent/issues/672

### v0.3.3

* fix dsn on pdo8
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"require": {
"php": "^7.4 || ^8.0",
"ext-json": "*",
"ext-pinpoint_php": "^0.5.2",
"ext-pinpoint_php": "^0.6.0",
"nikic/php-parser": "^5.0"
},
"autoload": {
Expand All @@ -27,7 +27,7 @@
"require-dev": {
"phpunit/phpunit": "^8",
"phpbench/phpbench": "1.0.0",
"ext-pinpoint_php": "^0.5.2",
"ext-pinpoint_php": "^0.6.0",
"nikic/php-parser": "^5.0",
"predis/predis": "^3.0@alpha",
"guzzlehttp/guzzle": "8.0.x-dev",
Expand Down
11 changes: 10 additions & 1 deletion lib/Pinpoint/Plugins/Common/defines.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
define("PP_HTTP_IO", 49);
define("PP_MESSAGE_QUEUE_URI ", 100);
define("PP_KAFKA_TOPIC", 140);
define("PP_KAFKA_PARTITION", 141);
define("PP_PHP_METHOD", "1501");
define("PP_PHP", "1500");
define("PP_MYSQL", "2101");
Expand All @@ -46,6 +47,7 @@
define("PP_CASSANDRA", "2600");
define("PP_MONGODB", "2651");
define("PP_KAFKA", "8660");
define("PP_KAFKA_STREAMS", "8662");
define("PP_REDIS", "8200");
define("PP_REDIS_REDISSON", "8203");
define("PP_REDIS_REDISSON_INTERNAL", "8204");
Expand Down Expand Up @@ -82,7 +84,7 @@
define("PP_PARENT_HOST", "Ah");
define("PP_NGINX_PROXY", "NP");
define("PP_APACHE_PROXY", "AP");
define("PP_TRANSCATION_ID", "tid");
define("PP_TRANSACTION_ID", "tid");
define("PP_SPAN_ID", "sid");
define("PP_NOT_SAMPLED", "s0");
define("PP_SAMPLED", "s1");
Expand All @@ -91,3 +93,10 @@
# https://github.com/pinpoint-apm/pinpoint-c-agent/issues/534
define("PP_ROUTE_KEY", '__pinpoint__route');
define("UT", 'UT');
define("PP_ASYNC_CALL_ID", "asyId");
define("PP_KAFKA_HEADER_ASYNC_CALL_ID", "_asyId_");
define("PP_KAFKA_HEADER_SEQUENCE_ID", "_sequence_id_");
define("PP_KAFKA_HEADER_TRANSACTION_ID", "_tid_");
define("PP_KAFKA_HEADER_SPAN_ID", "_sid_");
define("PP_KAFKA_HEADER_APP_ID", "_appid_");
define("PP_KAFKA_HEADER_APP_NAME", "_app_");
28 changes: 28 additions & 0 deletions lib/Pinpoint/Plugins/EmptyRequestPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/******************************************************************************
* 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;

class EmptyRequestPlugin extends DefaultRequestPlugin
{
public function __construct()
{
// skip call DefaultRequestPlugin::__construct
require_once __DIR__ . "/SysV2/__init__.php";
}
}
4 changes: 2 additions & 2 deletions lib/Pinpoint/Plugins/PinpointPerRequestPlugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ public function __construct()
require_once __DIR__ . "/SysV2/__init__.php";
}

pinpoint_add_clue(PP_TRANSCATION_ID, $this->tid);
pinpoint_add_clue(PP_TRANSACTION_ID, $this->tid);
pinpoint_add_clue(PP_SPAN_ID, $this->sid);
pinpoint_set_context(PP_TRANSCATION_ID, $this->tid);
pinpoint_set_context(PP_TRANSACTION_ID, $this->tid);
pinpoint_set_context(PP_SPAN_ID, (string) $this->sid);
}

Expand Down
4 changes: 2 additions & 2 deletions lib/Pinpoint/Plugins/Sys/curl/CurlUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static function getPinpointHeader($url)
'Pinpoint-Papptype:1500',
'Pinpoint-Pappname:' . APPLICATION_NAME,
'Pinpoint-Host:' . static::getHostFromURL($url),
'Pinpoint-Traceid:' . pinpoint_get_context(PP_TRANSCATION_ID),
'Pinpoint-Traceid:' . pinpoint_get_context(PP_TRANSACTION_ID),
'Pinpoint-Pspanid:' . pinpoint_get_context(PP_SPAN_ID),
'Pinpoint-Spanid:' . $nsid
];
Expand All @@ -56,7 +56,7 @@ public static function getPPHeader($url)
'Pinpoint-Papptype' => '1500',
'Pinpoint-Pappname' => APPLICATION_NAME,
'Pinpoint-Host' => static::getHostFromURL($url),
'Pinpoint-Traceid' => pinpoint_get_context(PP_TRANSCATION_ID),
'Pinpoint-Traceid' => pinpoint_get_context(PP_TRANSACTION_ID),
'Pinpoint-Pspanid' => pinpoint_get_context(PP_SPAN_ID),
'Pinpoint-Spanid' => $nsid
];
Expand Down
27 changes: 27 additions & 0 deletions lib/Pinpoint/Plugins/SysV2/_rdKafka/__init__.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/******************************************************************************
* Copyright 2024 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\SysV2\_rdKafka;

if (extension_loaded('rdkafka')) {
require_once __DIR__ . "/rdKafka.php";
if (defined("PP_ENABLE_EXPERIMENTAL_FEATURE")) {
require_once __DIR__ . "/rdKafka_consumer.php";
}
}

// author: eeliu
144 changes: 144 additions & 0 deletions lib/Pinpoint/Plugins/SysV2/_rdKafka/rdKafka.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<?php
namespace Pinpoint\Plugins\SysV2\_rdKafka;

/******************************************************************************
* Copyright 2024 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. *
******************************************************************************/

use function Pinpoint\Plugins\pinpoint_get_context;
use function Pinpoint\Plugins\pinpoint_get_sequence_id;
use function Pinpoint\Plugins\{
pinpoint_join_cut,
pinpoint_start_trace,
pinpoint_add_clue,
pinpoint_add_clues,
pinpoint_set_context,
pinpoint_end_trace
};
use Pinpoint\Common\Logger;

const KAFKA_BROKER_LIST = "__kafka_broker_list__";

$config_set_on_before = function ($name, $value) {
if ($name == 'metadata.broker.list') {
// store value into pinpoint context
pinpoint_set_context(KAFKA_BROKER_LIST, $value);
}
};

$RdKafka_add_brokers_on_before = function ($blocker_list) {
if (!empty($blocker_list)) {
pinpoint_set_context(KAFKA_BROKER_LIST, $blocker_list);
}
};

function get_broker_list_plugins($joinable, $on_before)
{
$on_end = function ($ret) {
};
$on_exception = function ($exp) {
};
return [$joinable, $on_before, $on_end, $on_exception];
}

$producev_on_before = function ($partition, $msgflags, $payload, $key = NULL, $headers = [], $timestamp_ms = NULL, $opaque = NULL) {
Logger::Inst()->debug("call on_before");
pinpoint_start_trace();
pinpoint_add_clue(PP_INTERCEPTOR_NAME, "RdKafka\ProducerTopic::producev");
pinpoint_add_clue(PP_SERVER_TYPE, PP_KAFKA);
$topic_pro = pinpoint_get_this();
$topic = "unknown";
if ($topic_pro instanceof \RdKafka\ProducerTopic) {
$topic = $topic_pro->getName();
}
pinpoint_add_clues(PP_KAFKA_TOPIC, $topic);
pinpoint_add_clues(PP_KAFKA_PARTITION, "$partition");
$broker_list_value = pinpoint_get_context(KAFKA_BROKER_LIST);
if ($broker_list_value) {
pinpoint_add_clue(PP_DESTINATION, $broker_list_value);
}
$async_id = mt_rand();
pinpoint_add_clue(PP_ASYNC_CALL_ID, $async_id);
$sequence_id = pinpoint_get_sequence_id();

$headers[PP_KAFKA_HEADER_ASYNC_CALL_ID] = $async_id;
$headers[PP_KAFKA_HEADER_SEQUENCE_ID] = $sequence_id;

$headers[PP_KAFKA_HEADER_TRANSACTION_ID] = pinpoint_get_context(PP_TRANSACTION_ID);
$headers[PP_KAFKA_HEADER_SPAN_ID] = pinpoint_get_context(PP_SPAN_ID);
$headers[PP_KAFKA_HEADER_APP_ID] = APPLICATION_ID;
$headers[PP_KAFKA_HEADER_APP_NAME] = APPLICATION_NAME;


return [$partition, $msgflags, $payload, $key, $headers, $timestamp_ms, $opaque];
};

$produce_on_before = function ($partition, $msgflags, $payload = NULL, $key = NULL, $headers = NULL, $opaque = NULL) {
Logger::Inst()->debug("call on_before");
pinpoint_start_trace();
pinpoint_add_clue(PP_INTERCEPTOR_NAME, "RdKafka\ProducerTopic::produce");
pinpoint_add_clue(PP_SERVER_TYPE, PP_KAFKA);
$topic_pro = pinpoint_get_this();
$topic = "unknown";
if ($topic_pro instanceof \RdKafka\ProducerTopic) {
$topic = $topic_pro->getName();
}
pinpoint_add_clues(PP_KAFKA_TOPIC, $topic);
pinpoint_add_clues(PP_KAFKA_PARTITION, "$partition");
$broker_list_value = pinpoint_get_context(KAFKA_BROKER_LIST);
if ($broker_list_value) {
pinpoint_add_clue(PP_DESTINATION, $broker_list_value);
}
};


$on_end = function ($ret) {
Logger::Inst()->debug("call on_end");
pinpoint_end_trace();
};

$on_exception = function ($exp) {
Logger::Inst()->debug("call on_exception");
};

// https://arnaud.le-blanc.net/php-rdkafka-doc/phpdoc/rdkafka-producertopic.producev.html
pinpoint_join_cut(
["RdKafka\ProducerTopic", "producev"],
$producev_on_before,
$on_end,
$on_exception
);

pinpoint_join_cut(
["RdKafka\ProducerTopic", "produce"],
$produce_on_before,
$on_end,
$on_exception
);

$points = [
get_broker_list_plugins(["RdKafka\Conf", "set"], $config_set_on_before), // https://arnaud.le-blanc.net/php-rdkafka-doc/phpdoc/rdkafka-conf.set.html
get_broker_list_plugins(["RdKafka", "addBrokers"], $RdKafka_add_brokers_on_before) //https://arnaud.le-blanc.net/php-rdkafka-doc/phpdoc/rdkafka.addbrokers.html
];

foreach ($points as $point) {
pinpoint_join_cut(
$point[0],
$point[1],
$point[2],
$point[3]
);
}
// @author eeliu
Loading

0 comments on commit 1bc80a7

Please sign in to comment.