Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/danilopolani/filament-memory-tracker

Track the memory usage of workers and Laravel queues and display them in Filament Admin Dashboard
https://github.com/danilopolani/filament-memory-tracker

filament laravel php

Last synced: 3 months ago
JSON representation

Track the memory usage of workers and Laravel queues and display them in Filament Admin Dashboard

Awesome Lists containing this project

README

        

# Filament Memory Tracker

[![Latest Stable Version](http://poser.pugx.org/danilopolani/filament-memory-tracker/v)](https://packagist.org/packages/danilopolani/filament-memory-tracker)
[![Total Downloads](http://poser.pugx.org/danilopolani/filament-memory-tracker/downloads)](https://packagist.org/packages/danilopolani/filament-memory-tracker)

Track the memory usage of your workers and display them in Filament.

Filament Worker Memory widget preview

> If you're using Filament v1, please navigate the [1.x branch](https://github.com/danilopolani/filament-memory-tracker/tree/1.x).

## Installation

Install the package via composer:

```bash
composer require danilopolani/filament-memory-tracker
```

Then publish the assets and the config of the package:

```bash
php artisan vendor:publish --tag=filament-memory-tracker-assets
php artisan vendor:publish --tag=filament-memory-tracker-config
```

> If you're upgrading from v1 to v2 please note that the namespace changed from `\DaniloPolani\` to `\DaniloPolani\`.

### Upgrade
When upgrading be sure to republish the assets:

```bash
php artisan vendor:publish --tag=filament-memory-tracker-assets --force
```

## Configuration

There are a few notable configuration options for the package.

Key | Type | Description
------------ | ------------- | -------------
`cache_store` | String | Define the cache store used to track memory usage. By default it will be your `CACHE_DRIVER` env value.
`trackers` | Array | A list of trackers names to be displayed in the dashboard. They must be the same used in your `MemoryTracker()` instance. See **Usage** below to discover more.
`date_format` | String | The [DateTime format](https://www.php.net/manual/en/datetime.format.php) to display dates.

## Usage

In your Worker create a new `MemoryTracker` instance and then ping the `track()` method every time you want. There's an example with [ReactPHP Event Loop](https://reactphp.org/event-loop/).

ℹ️ | The `$realUsage` flag is the same as [memory_get_usage()](https://www.php.net/manual/en/function.memory-get-usage.php).
:---: | :---

```php
memoryTracker = new MemoryTracker('Worker');
}

/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
// Ping every 5minutes
Loop::addPeriodicTimer(60 * 5, function () {
$this->memoryTracker->track(bool $realUsage = false);
});

return 0;
}
}
```

Then don't forget to add your tracker name inside the configuration too:

```php
[
'Worker',
],

];
```

### Track restarts

You can track the latest Worker restart date and memory usage as well! If you're working on a custom Worker, you should intercept the exit signals and then call the `$memoryTracker->trackRestart()` method. Otherwise you can use the Trait provided by the package to achieve that:

1. Include `DaniloPolani\FilamentMemoryTracker\Concerns\TracksRestart` inside your class;
2. Call `$this->trackRestartMemory(MemoryTracker $memoryTrackerInstance)` inside your constructor.

⚠️ | The `TracksRestart` requires the extension **`pcntl`** to be enabled.
:---: | :---

```php
memoryTracker = new MemoryTracker('Worker');
$this->trackRestartMemory($this->memoryTracker);
}

// ...
}
```

### Laravel Queue

You can track [Laravel Queue](laravel.com/docs/8.x/queues) too by listening to some specific events in a provider, for example your `AppServiceProvider`.

```php
track();
});

// Track restarts
Event::listen(WorkerStopping::class, function () use ($memoryTracker) {
$memoryTracker->trackRestart();
});
}
}
```

### Additional notes

- The widget will refresh every 5s automatically;
- By default the widget will be shown full-width if there's more than 1 tracker; otherwise, the widget will be a single block:

Memory Tracker widget single block

## APIs

These are the available methods of the `MemoryTracker` class:

Key | Description
------------ | -------------
`track(): void` | Track the current memory usage for the worker.
`trackRestart(bool $resetPeak = true): void` | Track a restart. If `$resetPeak` is true, the memory peak will be purged as well.
`getHistory(): array` | Get the worker's history of memory usage.
`getPeak(): array\|null` | Get the worker's memory peak. Returns `null` if no peak found.
`getLatestRestart(): array\|null` | Get the worker's latest restart data. Returns `null` if no restart found.
`purge(): void` | Purge all the data of the current worker.
`purgeHistory(): void` | Purge the track history only of the current worker.
`purgePeak(): void` | Purge the memory peak of the current worker.
`purgeRestart(): void` | Purge the latest restart data of the current worker.

## Contributing

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

### Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

## Credits

- [Danilo Polani](https://github.com/danilopolani)
- [All Contributors](../../contributors)

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

## Laravel Package Boilerplate

This package was generated using the [Laravel Package Boilerplate](https://laravelpackageboilerplate.com).