PHP code example of ivoglent / elastic-apm-php-agent

1. Go to this page and download the library: Download ivoglent/elastic-apm-php-agent library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ivoglent / elastic-apm-php-agent example snippets




$config = [
    'name' => 'Service Name',
    'secretToken' => 'APM api token',
    'transport' => [
        'host' => 'APM server host',
        'config' => [
            'base_uri' => 'base URL to APM server',
        ],
    ],
    'framework' => [
        'name' => 'Framework Name',
        'version' => 'Framework version',
    ]
];
$contexts = [
    'user' => [
        'id' => 'USER_ID',
        'username' => 'USER_NAME',
        'email' => 'EMAIL'
    ]
];
$agent = new \PhilKra\Agent($config, $contexts);


$error = $agent->factory()->newError(new \Exception());
$error->setTransaction($transaction);
$error->setParentId($transaction->getId());
$agent->register($error);

//Create APM agent
$agent = new \PhilKra\Agent($config, $contexts);

//Create new transaction
$transaction = $agent->factory()->newTransaction($transactionName, $transactionType);
//Start current transaction
$transaction->start();

//Create new span
$span = $agent->factory()->newSpan($spanName, $spanType, $spanAction);
$span->setTransaction($transaction);
$span->setParentId($transaction->getId());

//Start span
$span->start();

//Add sql context to span
$context = new \PhilKra\Traces\SpanContexts\SpanContext();
$context->statement = 'SQL Query String';
$trace->addContext('db', $context);


//Stop span
$span->stop();

//Register span to agent
$agent->register($span);


//Stop transaction
$transaction->stop();

//Register transaction to agent
$agent->register($transaction);

//Send all data to APM server
$agent->send();

'active' => PHP_SAPI !== 'cli'

'active' => PHP_SAPI !== 'cli' && env('ELASTIC_APM_ACTIVE', false)
bash
php composer.phar