PHP code example of cosmastech / laravel-statsd-adapter

1. Go to this page and download the library: Download cosmastech/laravel-statsd-adapter 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/ */

    

cosmastech / laravel-statsd-adapter example snippets


return [
    'default' => env("STATSD_ADAPTER_DEFAULT", "datadog"),

    "default_tags" => [
        "app_version" => "1.0.2",
    ],
    'channels' => [
        "datadog" => [
            "adapter" => "datadog",
            "host" => env("DD_AGENT_HOST"),
            "port" => env("DD_DOGSTATSD_PORT"),
            "socket_path" => null,
            "datadog_host" => null,
            "decimal_precision" => null,
            "global_tags" => [],
            "metric_prefix" => null,
            "disable_telemetry" => null,
        ],
    ],
];

use Cosmastech\LaravelStatsDAdapter\Stats;

// Increment a counter
Stats::increment('page.views');

// Record a gauge
Stats::gauge('user.login', 1);

// Record a timing (in ms)
Stats::timing('response.time', 320);

use Cosmastech\StatsDClientAdapter\Adapters\StatsDClientAdapter;
use App\Models\User;
use App\Models\Post;

class DeleteAllUserPostsAction
{
    public function __construct(private readonly StatsDClientAdapter $statsClient)
    {
    }
    
    public function __invoke(User $user): void
    {
        $user->posts->each(function(Post $post) use ($user) {
            $post->delete();
            $this->statsClient->decrement("posts", 1.0, ["user_id" => $user->id], 1);
        });
    }
}

Stats::increment('page.views', tags: ['url' => '/home']);

function makeSomeApiCall()
{
    return \Http::get("https://packagist.org/packages/list.json?vendor=cosmastech");
}

$apiResponseToDoSomethingWith = Stats::time(makeSomeApiCall(...), "api-request");

\DB::listen(function ($query) {
    Stats::increment('database.queries');
    Stats::timing('database.query_time', $query->time);
});

Stats::channel('memory')->increment('custom.metric');

$role = auth()->role; // Let's assume the User model has a property named `role`
Stats::increment("user.{$role}.login");
shell
php artisan vendor:publish --provider="Cosmastech\LaravelStatsDAdapter\StatsDAdapterServiceProvider"
shell
composer