How to monitor your Laravel application by services (not by hostnames)

Sponsor

April 11th, 2022

cover-1649076054.png

Hi, I'm Valerio, software engineer, founder & CTO at Inspector.

I decided to write this post after responding to a support request from a developer who asked me how he can monitor his Laravel application by services and not by hostnames.

The company is working on a backend API for a mobile app. The APIs are running on a set of AWS EC2 instances managed by an Autoscaling Group.

After a thorough investigation into the reason for this request, here is the solution we found.

What is an autoscaling group?

An Autoscaling Group contains a collection of VM instances that are treated as a logical grouping for the purposes of automatic scaling and management of specific component of your system.

In Laravel is quite easy to separate your application in logical components that runs in different servers, like APIs, background Jobs workers, web app, scheduled tasks (cron), etc, so they can scale in and out indipendently based on their specific workload.

Every established cloud provider allwos you to configure Autoscaling Groups, or you can use other technologies like Kubernetes. The result is the same:

Due to the constant turnover of servers to handle the application load dynamically you could see a bit of mess in your monitoring charts. A lot of trendlines turn on and off continuously, one for each underlying server.

Grouping monitoring data by service name

It may be more clear to use a human friendly service name to represent all your servers inside the same Autoscaling Group.

Since transactions in Inspector are grouped by the hostname, we can use the Inspector library to “artificially” change it just before the transaction is sent out of your application to make your dashboard more clear and understandable.

In the boot method of the AppServiceProvider you can use beforeFlush() to add a callback that simply change the hostname of the transaction, assigning a general service name (rest-api, workers, web-app, etc) instead of the original hostname of the server.

1<?php
2 
3namespace App\Providers;
4 
5use App\Jobs\ExampleJob;
6use Illuminate\Support\ServiceProvider;
7use Inspector\Laravel\Facades\Inspector;
8 
9class AppServiceProvider extends ServiceProvider
10{
11 /**
12 * Register any application services.
13 *
14 * @return void
15 */
16 public function register()
17 {
18 //
19 }
20 
21 /**
22 * Bootstrap any application services.
23 *
24 * @return void
25 */
26 public function boot()
27 {
28 Inspector::beforeFlush(function ($inspector) {
29 $inspector->currentTransaction()
30 ->host
31 // You can get the desired service_name by config, env, etc.
32 ->hostname = config('app.service_name')
33 });
34 }
35}

You can control what service name should be used in each part of your system through the deployment process by using environment variables.

The app.service_name configuration property could be setup as below:

1<?php
2 
3// config/app.php file
4 
5return [
6 
7 'service_name' => env('INSPECTOR_SERVICE_NAME', 'rest_api'),
8 
9];

Along the deployment pipeline you can inject a different "INSPECTOR_SERVICE_NAME" for each autoscaling group.

Your dashboard will chage from this:

To this:

I think it looks more clear, and it is what our customer think too 😃!

The number of servers is no longer visible, but you could gather this information from your cloud console.

Use filters to eliminate noise

Thanks to the transactions filtering it's even more easy to identify the most heaviest tasks in each Autoscaling Group, so you can optimize their performance and cut the costs due to the high number of tasks perfomed before the Autoscaling Group need to start a new server.

Conclusion

Thanks to its purely software library Inspector could be the right choice for developers that love to stay focused on coding instead of infrastructure management.

You will never have the need to install things at the server level or make complex configuration in your cloud infrastructure to monitor your application in real-time.

Inspector works with a lightweight software library that you can install in your application like any other dependencies. Try the Laravel package, it's free.

Visit our website for more details

Filed in:

Eric L. Barnes

Eric is the creator of Laravel News and has been covering Laravel since 2012.

Laravel News Partners