HttplugBundle icon indicating copy to clipboard operation
HttplugBundle copied to clipboard

Profiler does not show response if Cache plugin is used

Open ostrolucky opened this issue 7 years ago • 6 comments

Q A
Bug? yes
New Feature? no
Version 1.8.1

Without using cache plugin:

Now do this

            plugins:
                - 'httplug.plugin.content_length'
                - 'httplug.plugin.redirect'
+               - 'httplug.plugin.cache'

Result: screenshot from 2018-01-06 19-50-09

I'm aware this is because Cache plugin does not pass response over to other middlewares when cache is hit, but this is major usability restriction of Profiler. I would like to not wait for response while developing, but still see responses pulled out of Cache plugin. There should be a workaround in place

ostrolucky avatar Jan 06 '18 19:01 ostrolucky

i am surprised why we then see the request at all, but not deep enough into the profiling to tell. is there a reason why we don't treat a cached response the same as a response from the server? i think the plugins that come before the cache in the chain should be executed the same way regardless of cache hit or miss.

or do we record the request when it enters the chain, but the response when its received from the server, before processing it in the plugin chain? then indeed, we might add a check when the response leaves the chain, and record it there if it was not yet recorded, and add some kind of warning that this is a cached response because no "real" request has been sent. detecting cache hits in the debug toolbar sounds useful anyways.

dbu avatar Jan 07 '18 09:01 dbu

is this still relevant? has it meanwhile been solved, or is it important to have this?

dbu avatar Aug 19 '22 15:08 dbu

Yes

ostrolucky avatar Aug 19 '22 15:08 ostrolucky

I've did some debugging today on this topic and here are my findings:

  • Response body was actually recorded, but user has to toggle plugin stack, that's where they can see it image
  • Reason why "main" panel doesn't show it, is because CachePlugin understandably doesn't call $next($request) plugin, which would trigger calling \Http\HttplugBundle\Collector\ProfileClient. Job of this client is to fill in clientResponse used here https://github.com/php-http/HttplugBundle/blob/5ed4b8a69fafe57834f26e3d57d009fe0d67c6c6/src/Resources/views/stack.html.twig#L45-L45

One of the hook points we could use to solve this is to register Cache Listener, so that we record the response from within it. This would probably involve non-trivial refactoring of ProfilerClient where we extract recording functionality outside of the class (we cannot call the public method as it would call wrapped HTTP client).

Another idea of mine was in case clientResponse twig variable is empty, fall back to iterating through profiles and get body from first one that gives back response. Perhaps we could even get rid of ProfilerClient this way and move extra functionality it was providing (eg. time metrics) into ProfilePlugin

ostrolucky avatar Nov 12 '22 16:11 ostrolucky

thanks a lot for the analysis, @ostrolucky - great job!

Another idea of mine was in case clientResponse twig variable is empty, fall back to iterating through profiles and get body from first one that gives back response.

can we be sure that the first profile that has something is the right one? we should not display something arbitrarily, or in special cases show things from a previous request, or some such problem.

Perhaps we could even get rid of ProfilerClient this way and move extra functionality it was providing (eg. time metrics) into ProfilePlugin

that would be quite neat if that works.

before we do large refactorings, could we fix the situation by having ProfileClient detect when the $next was never called and attach the necessary information in that special case? working with the cache would not work if somebody for some reason implements e.g. their own cache plugin that works differently and does not do the events.

dbu avatar Nov 14 '22 08:11 dbu

ProfilePlugin can do it perhaps, or Collector. ProfileClient not, because it is not activated in this case.

ostrolucky avatar Nov 19 '22 11:11 ostrolucky