PHP Usage

Usage

Transactions

All start with a transaction. A Transaction represents an execution cycle and it can contains one or hundreds of segments. It should be started as soon as possible in your application to better represent the time your script takes to fulfill the request. Typically the transaction should be started in the entry point of your application e.g. index.php

/*
 * A transaction should start as soon as possible, 
 * typically in the "index.php" file of your application.
 */
$pathInfo = explode('?', $_SERVER["REQUEST_URI"]);
$path = array_shift($pathInfo);

$palzin->startTransaction($path);


// Continue with the script...

Set transaction result

image 1

When your application return a response back to the client you can setup the result of the transaction like the HTTP code:

$palzin->currentTransaction()->setResult(200);

Add Context

Each transaction can be enriched with additional information to have a more complete picture of the process execution context.

You can use the addCotext method to attach new tabs to the current transaction:

$palzin->currentTransaction()->addContext('label', ['foo' => 'bar']);

Contextual information will be reported as additional tabs in the transaction view. This code example creates a new tab "Label" in the transaction details as showed in the image below:

Transaction

Make Palzin APM available in your application globally

To be free to add new segments, or interact with the current transaction you need a way to make the Palzin APM instance available everywhere in your application. Use an IoC container like PHP-DI is a common way to distribute internal services inside an application. If your application has one, or implement other similar strategies, you could put the Palzin APM instance in the registry:

$container->set('ultimate', $palzin);

in order to use it later inside your application to add segments:

class UserController extends BaseController
{
    /**
     * @var \Palzin\Palzin
     */
    protected $palzin;
    
    /**
     * UserController constructor.
     */
    public function __construct()
    {
        // Get Palzin APM instance from the container.
        $this->ultimate = $container->get('ultimate');
    }
    
    /**
     * Do something.
     */
    public function action()
    {
        // Monitor a piece of code and return the result back to the client.
        return $this->palzin->addSegment(function () {
            
            return $this->someService->doSomething();
        
        }, 'someService', 'doSomething');
    }
}

Last updated: 1 year ago

Want to get started with Palzin Monitor? We offer a no-strings-attached
15 days trial. No credit card required.

It takes less than a minutes to setup your first monitoring.