Skip to content

Commit dea9bd2

Browse files
authored
Enhance support for Swoole. (#71)
1 parent 0bb3399 commit dea9bd2

11 files changed

Lines changed: 471 additions & 36 deletions

File tree

Cargo.lock

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,8 @@ SkyWalking PHP Agent requires SkyWalking 8.4+ and PHP 7.2+
3636
* [x] [php-amqplib](https://github.com/php-amqplib/php-amqplib) for Message Queuing Producer
3737

3838
* Swoole Ecosystem
39-
* [ ] [Coroutine\Http\Client](https://wiki.swoole.com/#/coroutine_client/http_client)
40-
* [ ] [Coroutine\MySQL](https://wiki.swoole.com/#/coroutine_client/mysql)
41-
* [ ] [Swoole\Coroutine\Http\Client](https://wiki.swoole.com/#/coroutine_client/http_client)
42-
* [ ] [Coroutine\Redis](https://wiki.swoole.com/#/coroutine_client/redis)
4339

44-
*The components of the PHP-FPM ecosystem can also be used in Swoole.*
40+
*The components of the PHP-FPM ecosystem can also be used in Swoole, wether the flag `SWOOLE_HOOK_ALL` is enabled or not.*
4541

4642
## Contact Us
4743

dist-material/LICENSE

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -578,11 +578,11 @@ MulanPSL-2.0 licenses
578578
The following components are provided under the MulanPSL-2.0 License. See project link for details.
579579
The text of each license is also included in licenses/LICENSE-[project].txt.
580580

581-
https://crates.io/crates/phper/0.11.0 0.11.0 MulanPSL-2.0
582-
https://crates.io/crates/phper-alloc/0.11.0 0.11.0 MulanPSL-2.0
583-
https://crates.io/crates/phper-build/0.11.0 0.11.0 MulanPSL-2.0
584-
https://crates.io/crates/phper-macros/0.11.0 0.11.0 MulanPSL-2.0
585-
https://crates.io/crates/phper-sys/0.11.0 0.11.0 MulanPSL-2.0
581+
https://crates.io/crates/phper/0.11.1 0.11.1 MulanPSL-2.0
582+
https://crates.io/crates/phper-alloc/0.11.1 0.11.1 MulanPSL-2.0
583+
https://crates.io/crates/phper-build/0.11.1 0.11.1 MulanPSL-2.0
584+
https://crates.io/crates/phper-macros/0.11.1 0.11.1 MulanPSL-2.0
585+
https://crates.io/crates/phper-sys/0.11.1 0.11.1 MulanPSL-2.0
586586

587587
========================================================================
588588
Unlicense licenses

src/execute.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
1515

16-
use crate::{plugin::select_plugin, request::IS_SWOOLE, util::catch_unwind_result};
16+
use crate::{
17+
plugin::select_plugin,
18+
request::{HACK_SWOOLE_ON_REQUEST_FUNCTION_NAME, IS_SWOOLE},
19+
util::catch_unwind_result,
20+
};
1721
use anyhow::{bail, Context};
1822
use phper::{
1923
objects::ZObj,
@@ -100,6 +104,11 @@ unsafe extern "C" fn execute_internal(
100104
}
101105
};
102106

107+
if function_name == HACK_SWOOLE_ON_REQUEST_FUNCTION_NAME {
108+
ori_execute_internal(Some(execute_data), Some(return_value));
109+
return;
110+
}
111+
103112
let plugin = select_plugin(class_name.as_deref(), &function_name);
104113
let plugin = match plugin {
105114
Some(plugin) => plugin,
@@ -297,7 +306,7 @@ fn infer_request_id(execute_data: &mut ExecuteData) -> Option<i64> {
297306
};
298307
let func_name = prev_execute_data.func().get_function_name();
299308
if !func_name
300-
.map(|s| s == b"skywalking_hack_swoole_on_request_please_do_not_use")
309+
.map(|s| s == &HACK_SWOOLE_ON_REQUEST_FUNCTION_NAME.as_bytes())
301310
.unwrap_or_default()
302311
{
303312
prev_execute_data_ptr = unsafe { (*prev_execute_data_ptr).prev_execute_data };

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ mod worker;
3131

3232
use phper::{ini::Policy, modules::Module, php_get_module};
3333

34+
use crate::request::HACK_SWOOLE_ON_REQUEST_FUNCTION_NAME;
3435
pub use errors::{Error, Result};
3536

3637
/// Enable agent and report or not.
@@ -156,7 +157,7 @@ pub fn get_module() -> Module {
156157
// The function is used by swoole plugin, to surround the callback of on
157158
// request.
158159
module.add_function(
159-
"skywalking_hack_swoole_on_request_please_do_not_use",
160+
HACK_SWOOLE_ON_REQUEST_FUNCTION_NAME,
160161
request::skywalking_hack_swoole_on_request,
161162
);
162163

src/plugin/plugin_swoole.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
use crate::{
1717
execute::{get_this_mut, validate_num_args, AfterExecuteHook, BeforeExecuteHook, Noop},
1818
plugin::Plugin,
19-
request::{IS_SWOOLE, ORI_SWOOLE_ON_REQUEST, SWOOLE_RESPONSE_STATUS_MAP},
19+
request::{
20+
HACK_SWOOLE_ON_REQUEST_FUNCTION_NAME, IS_SWOOLE, ORI_SWOOLE_ON_REQUEST,
21+
SWOOLE_RESPONSE_STATUS_MAP,
22+
},
2023
};
2124
use phper::{strings::ZString, values::ZVal};
2225
use std::{mem::replace, sync::atomic::Ordering};
@@ -61,9 +64,7 @@ impl SwooleServerPlugin {
6164
let closure = execute_data.get_mut_parameter(1);
6265
let ori_closure = replace(
6366
closure,
64-
ZVal::from(ZString::new(
65-
"skywalking_hack_swoole_on_request_please_do_not_use",
66-
)),
67+
ZVal::from(ZString::new(HACK_SWOOLE_ON_REQUEST_FUNCTION_NAME)),
6768
);
6869

6970
ORI_SWOOLE_ON_REQUEST.store(

src/request.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ use once_cell::sync::Lazy;
2525
use phper::{arrays::ZArr, eg, pg, sg, sys, values::ZVal};
2626
use skywalking::trace::{propagation::decoder::decode_propagation, tracer};
2727
use std::{
28-
convert::Infallible,
2928
panic::AssertUnwindSafe,
3029
ptr::null_mut,
3130
sync::atomic::{AtomicBool, AtomicPtr, Ordering},
@@ -146,6 +145,9 @@ fn get_page_request_server<'a>() -> anyhow::Result<&'a ZArr> {
146145
}
147146
}
148147

148+
pub const HACK_SWOOLE_ON_REQUEST_FUNCTION_NAME: &str =
149+
"skywalking_hack_swoole_on_request_please_do_not_use";
150+
149151
/// Hold the response fd and status code kvs, because I dont't found that
150152
/// response has the status field, so I hook the response.status method, maybe
151153
/// there is a better way?
@@ -157,11 +159,11 @@ pub static IS_SWOOLE: AtomicBool = AtomicBool::new(false);
157159

158160
/// The function is used by swoole plugin, to surround the callback of on
159161
/// request.
160-
pub fn skywalking_hack_swoole_on_request(args: &mut [ZVal]) -> Result<(), Infallible> {
162+
pub fn skywalking_hack_swoole_on_request(args: &mut [ZVal]) -> phper::Result<ZVal> {
161163
let f = ORI_SWOOLE_ON_REQUEST.load(Ordering::Relaxed);
162164
if f.is_null() {
163165
error!("Origin swoole on request handler is null");
164-
return Ok(());
166+
return Ok(ZVal::from(()));
165167
}
166168
let f = unsafe { ZVal::from_mut_ptr(f) };
167169

@@ -170,7 +172,8 @@ pub fn skywalking_hack_swoole_on_request(args: &mut [ZVal]) -> Result<(), Infall
170172
error!(mode = "swoole", ?err, "request init failed");
171173
}
172174

173-
if let Err(err) = f.call(&mut *args) {
175+
let return_value = f.call(&mut *args);
176+
if let Err(err) = &return_value {
174177
error!(
175178
mode = "swoole",
176179
?err,
@@ -186,7 +189,7 @@ pub fn skywalking_hack_swoole_on_request(args: &mut [ZVal]) -> Result<(), Infall
186189
}
187190
}
188191

189-
Ok(())
192+
return_value
190193
}
191194

192195
fn request_init_for_swoole(request: &mut ZVal) -> crate::Result<()> {

tests/common/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ pub const PROXY_SERVER_2_ADDRESS: &str = "127.0.0.1:9012";
5151
pub const FPM_SERVER_1_ADDRESS: &str = "127.0.0.1:9001";
5252
pub const FPM_SERVER_2_ADDRESS: &str = "127.0.0.1:9002";
5353
pub const SWOOLE_SERVER_1_ADDRESS: &str = "127.0.0.1:9501";
54-
#[allow(dead_code)]
5554
pub const SWOOLE_SERVER_2_ADDRESS: &str = "127.0.0.1:9502";
5655
pub const COLLECTOR_GRPC_ADDRESS: &str = "127.0.0.1:19876";
5756
pub const COLLECTOR_HTTP_ADDRESS: &str = "127.0.0.1:12800";

0 commit comments

Comments
 (0)