PHP code example of middleware / agent-apm-php

1. Go to this page and download the library: Download middleware/agent-apm-php 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/ */

    

middleware / agent-apm-php example snippets


   
   leware\AgentApmPhp\MwTracker;
   
   $tracker = new MwTracker('<PROJECT-NAME>', '<SERVICE-NAME>');
   $tracker->preTrack();
   $tracker->registerHook('<CLASS-NAME-1>', '<FUNCTION-NAME-1>', [
       'custom.attr1' => 'value1',
       'custom.attr2' => 'value2',
   ]);
   $tracker->registerHook('<CLASS-NAME-2>', '<FUNCTION-NAME-2>');
   
   $tracker->info("this is info log.");
   
   // ----
   // Your code goes here.
   // ----
   
   $tracker->postTrack();
   


iddleware\AgentApmPhp\MwTracker;

$tracker = new MwTracker('DemoProject', 'PrintService');
$tracker->preTrack();
$tracker->registerHook('DemoClass', 'runCode', [
    'code.column' => '12',
    'net.host.name' => 'localhost',
    'db.name' => 'users',
    'custom.attr1' => 'value1',
]);
$tracker->registerHook('DoThings', 'printString');

$tracker->info("this is info log.");

class DoThings {
    public static function printString($str): void {
        // sleep(1);
        global $tracker;
        $tracker->warn("this is warning log, but from inner function.");
        
        echo $str . PHP_EOL;
    }
}

class DemoClass {
    public static function runCode(): void {
        DoThings::printString('Hello World!');
    }
}

DemoClass::runCode();

$tracker->postTrack();