becklyn/monitoring
Integrates monitoring features for Becklyn apps: injects an uptime-monitor HTML comment with your project name, optionally embeds TrackJS via Twig, and registers a @monitoring namespace for the Becklyn assets bundle.
Installation:
composer require becklyn/monitoring
The package auto-registers via Laravel’s service provider discovery (no manual config/app.php edits needed).
Basic Usage:
Uptime Monitor: All HTML responses automatically include a comment:
<!-- uptime monitor: $project_name -->
Configure project_name in config/monitoring.php (published via php artisan vendor:publish --tag=monitoring-config).
TrackJS Integration: Add the monitoring script to Twig templates:
{% block javascripts %}
{{- monitoring_embed() }}
{{ parent() }}
{% endblock %}
Requires trackjs.token in config.
First Test Case:
curl -I).monitoring_embed() outputs the TrackJS script when trackjs.token is set.Workflow:
uptime_comment_template:
'uptime_comment_template' => '<!-- uptime: %s (custom: %s) -->',
(Where %s is replaced with $project_name and a custom suffix.)Integration Tip:
project_name matches the monitor’s alias.Workflow:
monitoring_embed() Twig function outputs the TrackJS script only if trackjs.token is configured.defer attribute by default (check the published config to modify).config/monitoring.php for non-production environments:
'trackjs' => [
'token' => env('TRACKJS_TOKEN'),
'enabled' => env('APP_ENV') === 'production',
],
Advanced Use:
monitoring_embed function:
{{- monitoring_embed({'token': 'custom-token', 'async': false}) }}
@monitoring namespace in the Becklyn Assets Bundle (assumed to be a custom asset pipeline).// In your main JS file
require('@monitoring/script');
monitoring_embed() function.Missing Config:
php artisan vendor:publish --tag=monitoring-config
to generate config/monitoring.php. Without this, project_name and trackjs.token will default to null.Environment Variables:
trackjs.token should be set via .env (e.g., TRACKJS_TOKEN=your_token_here). Hardcoding in config/monitoring.php is not recommended.Script Injection Timing:
monitoring_embed() function outputs the script before other JS in the javascripts block. If you need it to load last, reorder the Twig blocks or use a custom template.trackjs.token is set and the config is published.Token Validation:
403 response from trackjs.com.Non-HTML Responses:
Response::macro('withUptimeComment', function ($projectName) {
return $this->header('X-Uptime-Monitor', $projectName);
});
Project Name Sanitization:
project_name is used in the uptime comment. If it contains special characters (e.g., /, :), some monitors may fail to parse it. Sanitize it in the config:
'project_name' => str_replace(['/', ':'], '-', env('APP_NAME')),
Log Output:
storage/logs/laravel.log). Check for:
[Becklyn\Monitoring] Config not published. Using defaults.
or
[Becklyn\Monitoring] TrackJS token not set. Skipping script injection.
Asset Pipeline Conflicts:
@monitoring namespace in the Assets Bundle but the script fails to load, ensure:
@monitoring namespace is correctly mapped in your asset manifest.Custom Scrubbing Logic:
spatie/laravel-activitylog with custom filters.public function handle($request, Closure $next) {
$request->merge(['ip' => Package::scrubIp($request->ip())]);
return $next($request);
}
Custom Uptime Comment:
Becklyn\Monitoring\MonitoringServiceProvider:
public function boot() {
parent::boot();
$this->app['view']->composer('*', function ($view) {
$view->with('uptime_comment', '<!-- custom: ' . config('monitoring.project_name') . ' -->');
});
}
How can I help you explore Laravel packages today?