PHP code example of slick / telemetry

1. Go to this page and download the library: Download slick/telemetry 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/ */

    

slick / telemetry example snippets




namespace App;

use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Slick\Telemetry\TelemetryClient;

$logger = new Logger('my-app');
$logger->pushHandler(
    new StreamHandler('path/to/your.log', Logger::INFO)
);

$telemetryClient = new TelemetryClient($logger);



$telemetryClient->trackMetric("Open processes", 345); // measurement
$telemetryClient->tracMetric("Memory", 89.43, 8, 60.3, 94.323, 5) // Pre-aggregated metric

// adding more context to the measurement
$telemetryClient->trackMetric("Open processes", 345, context: ["region" => "north"]);




// A SQL example of a dependency entry
$telemetryClient->trackDependency(
    "Query user tasks list",
    "SQL"
    "select * from tasks where user_id = :uid",
    1643808614,
    2.33793258,
    true,
    [
        "database" => "Local MySQL DB",
        "rows" => 23,
        "uid" => 212
    ]
);


// Handled exception
try {
    // faulty code
} catch (\Exception $e) {
    $telemetryClient->trackException($e);
}

// Unhandled exception
function exception_handler($exception) {
  $telemetryClient->trackException($e);
}

set_exception_handler('exception_handler');
throw new Exception('Uncaught Exception');



namespace App;
 
use Slick\Event\Domain\AbstractEvent;
use Slick\Event\Event as SlickEvent;

/**
 * Event class example
 */
class TaskWasFinished extends AbstractEvent implements SlickEvent, \JsonSerializable
{
    public function __construct(private int $taskId)
    {
        parent::__construct();
    }
    
    public function jsonSerializa()
    {
        return ['taskId' => $this->taskId];
    }
}

// track the event
$telemetryClient->trackEvent(new TaskWasFinished(34));



$telemetryClient->trackRequest(
    'create user',
    '/users',
    1643887671,
    201,
    276.4379,
    $_REQUEST
);


// front-controller script
$trackableRequest = $telemetryClient->beginRequest('create user', '/users', $_REQUEST);

// front-controller execution code

$telemetryClient->endRequest($trackableRequest, 201);



$telemetryClient->notice(
    "User {name}, was suspended because {reason}.",
    ["name" => "John Doe", "he had too many login attempts"]
);

// Resulting message will be:
// User John Doe, was suspended because he had too many login attempts.



// Override LogLevel::INFO with LogLevel::WARNING in a metric register
$telemeteryClient->trackMetric('free space', 20, context: ['level' => LogLevel::WARNING]);