arkaitzgarro / elastic-apm-laravel

Laravel APM agent for Elastic v2 intake API
MIT License
79 stars 17 forks source link

Laravel Octane Support #159

Open alignwebs opened 2 years ago

alignwebs commented 2 years ago

Hi @arkaitzgarro, thanks for the wonderful package! It will be really great if we could use it safely with Laravel Octane.

I have attached a issue that seems to be with Octane:

Long Duration Time due to the Singleton Behavior image

Also since most of the microservices will be going over Octane, so authenticity of the data is also required. Once again its great package for PHP devs looking to get APM. I am not so much experienced as you, else i would have surely raised a PR :)

Thanks

arkaitzgarro commented 2 years ago

@alignwebs I don't have experience with Octane, but we faced a similar problem with workers/jobs, that we handle checking if the application is running in the console via $this->app->runningInConsole().

Do you know if there's a similar helper in Octane that can tell us if the package/application is running via a worker, so we can set the start time on every request?

dstepe commented 2 years ago

I also have not used Octane. Nothing in the docs jumped out as an appropriate indicator to use. It looks like inference from the config or something would be needed. I was thinking a new config in this package would be better, that way the user could explicitly indicate they want "per request start time" or something.

arkaitzgarro commented 2 years ago

I would prefer if this is transparent and it is handled by the package, and not expect from developers to understand that Octane only boots the application once, and specific configuration is needed to make this package work. But I don't see an obvious way to do it.

dstepe commented 2 years ago

The use of Octane could be inferred by looking for the presence of it's configuration (I've never tried that in Laravel, so that's really more a theory). We could flip the behavior and add auto-detection of Octane with the option to disable auto-detection if needed. That auto-detection method could be extended as other use cases arise.

alignwebs commented 2 years ago

Do you know if there's a similar helper in Octane that can tell us if the package/application is running via a worker, so we can set the start time on every request?

When I tried to Log the time value inside the RequestStartTime class, it was giving new values on each request! So technically the class was working fine but I don't know even if it's getting terminated at any point or not.

image

Octane has a way to attach Listeners to Octane Events in config/octane.php. I feel this way we need to make some manual adjustments to make it work.

image

What do you think @arkaitzgarro ?

fmiqbal commented 1 year ago

Stumbled upon this, tried to flush the RequestTime for each request, at least it give me the real latency, although there's Closure span that I dunno where it comes. Also I got warning for route_matching

image image

The sleep/google/facebook duration span is correct but the position is wrong. I also tried to add all the collector to flush, but still the same

image

Laravel 9.42.2 php:8.1-cli-alpine Octane using roadrunner

fmiqbal commented 1 year ago

Update

So I somewhat get it working in testing environment, turns its only the problem of "terminating" hook from this package, because originally "terminating" event is broadcasted in every request, in octane, it doesn't. But, octane do have "RequestTerminated" event which is equivalent of the normal terminating event, so I just hook the send command to that, it works. Also I don't actually need to flush the RequestStartTime.

image

image

But, there seem to be some caveat,

  1. there are some $_SERVER that doesn't available in roadrunner, here's the json comparison of the data sent to apm, it's the cookies and more notably url key that affect the view in Kibana in url section

image

image

  1. I found this anomaly from excessive refresh, and I found it twice on 10 transaction, I probably need more investigation on why this is happen, probably because the multiple worker scenario on octane.

image

Lorrhanl commented 1 year ago

I'm trying to add this package to my app Laravel + Octane + Swoole, but the time is so long and the trace didn't show.

@fmiqbal I tried to add function into the array requestTerminated but don't work for me :/. How did you resolve this? I need to add something in another place? Could you help me?

image

image

image

Update

I added \AG\ElasticApmLaravel\Collectors\RequestStartTime::class into the key flush and the request time it's ok.