Contact Us

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

Mobile App | June 29, 2022

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
5use AppJobsExampleJob;
7use InspectorLaravelFacadesInspector;
12 * Register any application services.
22 * Bootstrap any application services.
28 Inspector::beforeFlush(function ($inspector) {
31 // You can get the desired service_name by config, env, etc.
32 ->hostname = config(‘app.service_name’)
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:

7 ‘service_name’ => env(‘INSPECTOR_SERVICE_NAME’, ‘rest_api’),

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.

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.

This content was originally published here.