Blackfire PHP SDK

repository·master·Indexed 41 days ago

https://github.com/blackfireio/php-sdk

A toolkit for integrating Blackfire profiling capabilities into PHP applications. It includes a client for interacting with the Blackfire API, a PHP Probe implementation for environments where the standard extension cannot be installed, and a CLI proxy tool for inspecting HTTP traffic between profiled servers and Blackfire servers.

Tokens
6.6K
Snippets
21
Records
34
Agent score
99%

What's inside Blackfire PHP SDK

  1. Use the Blackfire PHP Probe as a fallback or for XHProf

    master

    The SDK provides a PHP Probe implementation that can be used as a fallback when the official Blackfire PHP extension cannot be installed.

    Warning: This should only be used in specific scenarios:

    • You have XHProf installed but cannot install the Blackfire PHP extension (e.g., on older PHP versions like 5.2).
    • You want a fallback mechanism where manual instrumentation is converted to no-ops if the Blackfire PHP extension is missing on certain machines.

    For detailed implementation details, refer to the Blackfire blog regarding XHProf users.

  2. Migrate from single-scenario builds to multi-scenario builds (v1.14.0)

    master

    Starting in version 1.14.0, a build can contain multiple scenarios. This changed the way builds are structured and initialized.

    Deprecated items:

    • Class \Blackfire\Build (replaced by \Blackfire\Build\Scenario)
    • Methods createBuild, endBuild, and addJobInBuild (replaced by startScenario, closeScenario, and addJobInScenario)

    New items:

    • Classes \Blackfire\Build\Build and \Blackfire\Build\Scenario
    • Methods startBuild, closeBuild, startScenario, closeScenario, and addJobInScenario

    Migration Example:

    From the legacy single-scenario approach:

    $build = $blackfire->createBuild('Blackfire dev', array('title' => 'Legacy build'));
    $config = new \Blackfire\Profile\Configuration();
    $config->setBuild($build);
    $probe = $blackfire->createProbe($config);
    // ... profile code ...
    $blackfire->endProbe($probe);
    $report = $blackfire->endBuild($build);

    To the new multi-scenario approach:

    $build = $blackfire->startBuild('Blackfire dev', array('title' => 'My build'));
    $scenario = $blackfire->startScenario($build, array('title' => 'Test documentation'));
    $config = new \Blackfire\Profile\Configuration();
    $config->setScenario($scenario);
    $probe = $blackfire->createProbe($config);
    // ... profile code ...
    $blackfire->endProbe($probe);
    $report = $blackfire->closeScenario($scenario);
    $blackfire->closeBuild($build);
    <?php
    
    require_once __DIR__.'/vendor/autoload.php';
    
    $blackfire = new /Blackfire/Client();
    
    // create a build
    $build = $blackfire->startBuild('Blackfire dev', array(
        'title' => 'My build',
        'trigger_name' => 'PHP',
    ));
    
    // create a scenario
    $scenario = $blackfire->startScenario($build, array(
        'title' => 'Test documentation',
        'external_id' => 'c:my-scenario',
        'external_parent_id' => 'b:my-scenario',
    ));
    
    // create a configuration
    $config = new /Blackfire/Profile/Configuration();
    $config->setScenario($scenario);
    
    // create as many profiles as you need
    $probe = $blackfire->createProbe($config);
    
    // some PHP code you want to profile
    echo strlen('Hello !');
    
    $blackfire->endProbe($probe);
    
    // end the build and fetch the report
    $report = $blackfire->closeScenario($scenario);
    
    $blackfire->closeBuild($build);
  3. Inspect traffic using the Blackfire Proxy

    master

    If you need to inspect the traffic sent between your profiled servers and Blackfire's servers, you can use the proxy script included in this repository.

    Refer to the instructions located in ./bin/blackfire-io-proxy.php to set up and use the proxy.

  4. Replace deprecated Scenario and Job methods (v2.3.0)

    master

    In version 2.3.0, the following changes were introduced:

    • Blackfire\Build::incScenario and Blackfire\Scenario::incJob are deprecated without replacement.
    • Blackfire\Client::getScenarioReport is deprecated. Use Blackfire\Client::getBuildReport instead.
    • Blackfire\Client::addJobInScenario is deprecated. To achieve the same result, set the scenario with Blackfire\Profile\Configuration::setScenario and then call Blackfire\Client::endProbe.
  5. Update Report methods and return types (v1.16.0)

    master

    In version 1.16.0:

    • Use getScenarioReport($scenarioUuid) instead of the deprecated getReport($scenarioUuid). Alternatively, use getBuildReport($buildUuid) for a full Build Report.
    • The return type of closeBuild() changed from void to Blackfire\Report.
  6. Replace deprecated Reference methods (v1.18.0)

    master

    In version 1.18.0, the following methods and class are deprecated:

    • Blackfire\LoopClient::promoteReferenceSignal
    • Blackfire\LoopClient::attachReference
    • Blackfire\Profile\Configuration::getReference
    • Blackfire\Profile\Configuration::setReference
    • Blackfire\Profile\Configuration::isNewReference
    • Blackfire\Profile\Configuration::setAsReference
    • Blackfire\Exception\ReferenceNotFoundException
  7. Migrate from Build feature to Blackfire Player

    master

    As of version 2.6.0, the entire Build feature is deprecated and scheduled for removal in version 3.0. Users should migrate to Blackfire Player using the --report flag.

    Deprecated classes include:

    • Blackfire\Build\Build
    • Blackfire\Build\Scenario
    • Blackfire\Build\BuildHelper
    • Various PHPUnit, Laravel, and Behat bridge extensions and traits related to builds.

    Deprecated methods include:

    • Blackfire\Client::startBuild()
    • Blackfire\Client::closeBuild()
    • Blackfire\Client::startScenario()
    • Blackfire\Client::closeScenario()
    • Blackfire\Client::getBuildReport()
    • Blackfire\Client::addStep()
    • Blackfire\Profile\Configuration::setScenario()
    • Blackfire\Profile\Configuration::setBuildUuid()
    • Blackfire\Profile\Request::getStoreUrl()
    • Blackfire\LoopClient::generateBuilds()
  8. How to obtain a Blackfire Profile Request

    master

    A Blackfire\Profile\Request object represents the HTTP request required to trigger a Blackfire profile.

    Important: You should never instantiate this class directly. Instead, use the Blackfire\Client to create requests. The Client handles the complex signing process, metadata attachment, and YAML configuration encoding required by the Blackfire API.

  9. Create and manage a Blackfire probe

    master

    A probe allows you to instrument a specific execution flow to collect performance data.

    1. Create a probe: Use createProbe() which returns a Blackfire\Probe instance. You can optionally pass a Blackfire\Profile\Configuration to customize the profile (e.g., setting a title).
    2. End a probe: Use endProbe(Probe $probe) to stop the instrumentation. This method returns a Blackfire\Profile object, which represents the collected data.

    Note: createProbe enables the probe by default unless the second argument is set to false.

    use Blackfire\Client;
    use Blackfire\Profile\Configuration as ProfileConfiguration;
    
    $client = new Client();
    
    // 1. Create the probe
    $config = new ProfileConfiguration();
    $config->setTitle('My Custom Profile');
    $probe = $client->createProbe($config);
    
    // ... run your code to be profiled ...
    
    // 2. End the probe and get the profile
    $profile = $client->endProbe($probe);
  10. Inspect traffic with the Blackfire IO Proxy

    master

    The blackfire-io-proxy.php script is a CLI tool used to inspect the HTTP traffic between profiled servers and Blackfire's servers. It acts as a middleman, logging requests and responses to standard output.

    Setup Instructions

    To use the proxy effectively, you must reconfigure your Blackfire settings to route traffic through it:

    1. Agent Configuration: Set the collector setting of your Blackfire agent to http://127.0.0.1:8383.
    2. CLI Tool Configuration: Set the endpoint setting of your Blackfire CLI tool to http://127.0.0.1:8383.
    3. Run the Proxy: Start this script in a dedicated terminal window to view the intercepted traffic.

    Usage

    Run the script from the command line. It accepts two optional positional arguments:

    1. backendEndpoint: The destination server (defaults to https://blackfire.io).
    2. upstreamHost: The local address the proxy should listen on (defaults to 127.0.0.1:8383).

    Traffic Inspection Features

    • Visual Indicators: The proxy uses color-coded prefixes in the output:
      • -> (Red): Outgoing traffic (from the client/agent towards Blackfire).
      • <- (Green): Incoming traffic (from Blackfire towards the client/agent).
    • Data Decoding:
      • JSON: Automatically pretty-prints JSON payloads.
      • Compressed Data: If the zlib extension is enabled, the proxy will automatically inflate and display compressed data. If zlib is missing, it will notify you that data is compressed and suggest enabling the extension.
    # Basic usage (listening on default 127.0.0.1:8383)
    php bin/blackfire-io-proxy.php
    
    # Custom backend and custom listening port
    php bin/blackfire-io-proxy.php https://custom-blackfire-endpoint.com 127.0.0.1:9000
  11. Initialize the Blackfire Client

    master

    The Blackfire\Client is the primary entry point for interacting with the Blackfire API. You can initialize it with an optional Blackfire\ClientConfiguration object to customize settings like credentials and endpoints. If no configuration is provided, a default ClientConfiguration is used.

    use Blackfire\Client;
    use Blackfire\ClientConfiguration;
    
    $config = new ClientConfiguration();
    // Configure $config here...
    
    $client = new Client($config);