opencensus-php icon indicating copy to clipboard operation
opencensus-php copied to clipboard

How to implement in a ReactPHP app?

Open acasademont opened this issue 6 years ago • 5 comments

Hi! Seems like the library is heavily geared towards the classic PHP single-request mode. How should I instrument my code for a ReactPHP use case? A single PHP process handles multiple requests.

Thanks!

acasademont avatar Oct 21 '18 19:10 acasademont

Would you mind to share your example code to have a clearer picture on what you try to instrument?

Den søn. 21. okt. 2018, 21:42 skrev Albert Casademont < [email protected]>:

Hi! Seems like the library is heavily geared towards the classic PHP single-request mode. How should I instrument my code for a ReactPHP use case? A single PHP process handles multiple requests.

Thanks!

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/census-instrumentation/opencensus-php/issues/214, or mute the thread https://github.com/notifications/unsubscribe-auth/AC7sAr2FzyG0zzEi3aetM0eVVrAmmWsZks5unM4ygaJpZM4XyobZ .

jcchavezs avatar Oct 22 '18 16:10 jcchavezs

Sure, this is taken from ReactPHP's website, for example

$loop = React\EventLoop\Factory::create();

$server = new React\Http\Server(function (Psr\Http\Message\ServerRequestInterface $request) {
    //Lots of calls and work here
    return new React\Http\Response(
        200,
        array('Content-Type' => 'text/plain'),
        "Hello World!\n"
    );
});

$socket = new React\Socket\Server(8080, $loop);
$server->listen($socket);

echo "Server running at http://127.0.0.1:8080\n";

$loop->run();

This is script runs indefinitely, serving an undefined number of requests.

acasademont avatar Oct 23 '18 14:10 acasademont

Is there any way to pass some sort of context downstream from the point where we recieve the request to the implementation for example? A global scope might not be possible as this is a concurrency model.

Den tir. 23. okt. 2018, 16:30 skrev Albert Casademont < [email protected]>:

Sure, this is taken from ReactPHP's website, for example

$loop = React\EventLoop\Factory::create();$server = new React\Http\Server(function (Psr\Http\Message\ServerRequestInterface $request) { //Lots of calls and work here return new React\Http\Response( 200, array('Content-Type' => 'text/plain'), "Hello World!\n" );});$socket = new React\Socket\Server(8080, $loop);$server->listen($socket);echo "Server running at http://127.0.0.1:8080\n";$loop->run();

This is script runs indefinitely, serving an undefined number of requests.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/census-instrumentation/opencensus-php/issues/214#issuecomment-432269794, or mute the thread https://github.com/notifications/unsubscribe-auth/AC7sAoUnU6xHR90mhdAwU_y5cHOn1MtXks5unygKgaJpZM4XyobZ .

jcchavezs avatar Oct 31 '18 15:10 jcchavezs

@jcchavezs yes exactly. We're right now using PHP-PM which limits concurrency for the ReactPHP loops but in theory they could be running in parallel. ReactPHP has the concept of Middlwares that let's you manipulate the request object and pass some kind of context.

https://reactphp.org/http/#middleware

And in theory that ReactPHP process is never killed in the sense of a traditional php request-response, so having stuff in destructors is not recommended, it might take a long time until they are executed.

acasademont avatar Nov 07 '18 08:11 acasademont

Unfortunately the current implementation of OpenCensus Tracing expects a standard isolated PHP Request lifecycle and is thus incompatible with event driven PHP solutions like ReactPHP.

Given enough demand this might change in the future. OpenCensus Stats should actually work quite nicely as it allows for both implicit as well as explicit scope handling.

basvanbeek avatar Jan 15 '19 12:01 basvanbeek