Profiling HTTP Sub-Requests using Blackfire

By Romain Neutron, on Dec 20, 2016

Nowadays, relying on HTTP services when handling a PHP request is pretty common. It is actually the paradigm provided by micro-services architectures.

If you’re using this approach, you might have wondered how to profile HTTP sub-requests while profiling the main request. Let’s see how you can make it work with Blackfire.

A note on how Blackfire works

In order to profile a request, Blackfire instruments your code at runtime.

For HTTP requests, Blackfire instrumentation is triggered via the X-Blackfire-Query header. The header value contains the profile configuration (like the reference profile, the number of samples, …) and a signature that identifies the user triggering the profile. You can find more details about this in our FAQ, and in the 24 Days of Blackfire tutorial.

By default, the X-Blackfire-Query header is not transferred from a request to a sub-request, which means that only the main request is actually profiled. But if you are using Guzzle to manage sub-requests, this is easy to fix.

Guzzle 6 Middleware

Guzzle is a PHP HTTP client that makes it easy to send HTTP requests. Integrating Blackfire with Guzzle lets you profile programmatically your websites, HTTP APIs, or Web Services.

The latest version of Blackfire’s PHP SDK provides a Guzzle 6 middleware that enables micro-services profiling. Check the documentation to see how to use it.

Result

The main request and each sub-request will be identified as separate profiles. It means that you will be able to review each request independently, and each sub-request will be accountable for part of its parent’s I/O time.

Limitations

  • Be aware that HTTP sub-requests profiling is not compatible with aggregation. Therefore you must disable aggregation by using the relevant checkbox in the companion or add –samples=1 on the CLI.
  • All HTTP services must be configured with the same Blackfire environment credentials.

Happy micro-services profiling!

Bonus, courtesy of CommitStrip

strip-apidays-nostaligia-english-1

From CommitStrip.

Romain Neutron

Romain is a developer at Blackfire.io. He started programming years ago while he was studying physics. He loved it so much he stopped his studies to be a programmer and contributed to various open source projects. He joined SensioLabs in 2014 to work on Blackfire and discovered a new love in application performance.