Moleculer’s Laboratory — All-in-one microservices dashboard

Roni Cruz
Moleculer
Published in
8 min readNov 8, 2021

The development of a microservices-based application involves multiple moving parts. It’s a process of designing/creating complex interactions between several services. As the application grows in complexity it becomes hard to create a mental picture of service relations and their interactions, which inevitably slows the development and the debugging. A tool that can provide a graphical overview of the system and track the interactions between the services can greatly alleviate the cognitive load of the developer and, therefore, result in a significant speed-up during the application development.

Moleculer’s Lab is a dashboard designed to solve the abovementioned issues during the development of a Moleculer-based application. The dashboard includes a global architecture overview, information about system load, service calls, events, and much more.

In this article, you will see how to create a microservice application developed in the Node.js environment, based on the Moleculer framework, and also how to integrate Moleculer’s Lab into the project and have a complete dashboard for our microservices.

What is Moleculer?

Moleculer is an open-source fast & flexible microservices framework, licensed under MIT. It contains most of all important microservices features (service registry, auto-discovery, load balancing, circuit-breaker, bulkhead, fallback response…etc).¹

What is Moleculer’s Laboratory?

Moleculer’s Laboratory is a developer tool for Moleculer-based microservices projects. It is still only available for Beta Testers. To join beta testing, fill out this form

Hands-on

Let’s use an example to demonstrate the power of Moleculer’s Lab.

System overview

This example is a fictitious travel control system responsible for validating a trip by checking the customer’s body temperature and if the customer’s name is registered in a suspect list. The following services will be implemented:

  • api: The API gateway, responsible for receiving all requests and redirecting to the corresponding internal service.
  • passengers: Service responsible for receiving passenger's name and checking their temperature. If the temperature is greater than or equal to 38 degrees, an event fever.detected is emitted warning that fever was detected and doctors will be awaited.
    Then, the passenger’s name will be consulted in another service that has a list of suspects. If the passenger is a suspect, he will be arrested, otherwise, his trip will be allowed.
  • doctors: Service responsible for listening to events fever.detected and providing appropriate care.
  • suspects: A service responsible for providing a list of known suspects.
  • lab: A service that will be the agent of the Moleculer’s Lab.
Moleculer’s Lab Demo

Implementation

1. First, install Moleculer CLI.

npm i moleculer-cli -g

2. Create a new project based on moleculer-demo template.

moleculer init project moleculer-demo

The CLI is going to ask you some questions regarding the template configuration. To make things simple, just follow these options::

Template Config

3. Go to the new folder created and open it with your favorite editor. Now, let’s review the template and add the services that are necessary to implement our travel control system.

  • Create suspects.service.js service in the services folder and copy the following code:
suspects.service.js
  • Create doctors.service.js service in the services folder and copy the following code:
doctors.service.js
  • Create passengers.service.js service in the services folder and copy the following code:
passengers.service.js

4. Install Lab Agent service with:

npm i @moleculer/lab --save

5. Create lab.service.js service in the services folder and copy the following code:

lab.service.js

6. Create a .env file with your credentials:

LAB_TOKEN=labdemo
LAB_APIKEY=

1. Protect the access from others (if you expose the Agent service to the internet). It can be a static string, or if it’s not set, a random token will be generated and printed to the console. Env var: LAB_TOKEN
2 .
The API key is sent to users by e-mail. Env var: LAB_APIKEY. ²

7. To load the environment variables from the .env file, install dotenv package and change the package.json to include the option --env in dev script.

npm install dotenv
package.json

8. You can delete the greeter and products services from the services folder. They are included in the template by default and are not necessary for this example. After that, your file structure should look like this:

File Structure

9. Adjust the moleculer.config.js file

moleculer.config.js

The lab package contains a logger, metrics reporter, and tracing exporter modules that collect and aggregate the internal state changes and events of the system.

  • Logger. Moleculer supports multiple loggers. In addition to the console logger, let’s add the Laboratory logger. Configure the logger to look like this:
logger
  • Metrics. Export metrics to Moleculer’s Lab.
metrics
  • Tracing. Export tracings to Moleculer’s Lab.
tracing

10. Start the application.

npm run dev

To check if API is ok, enter http://localhost:5000/api/suspects/ in the browser. Seven suspects will be listed. Great!

List of suspects

Moleculer’s Lab in action

After initializing the application, the Console logger will display a message saying that the Lab is online. To open the laboratory, follow the link printed in the log.

The link is displayed on logs

For security purposes, the GUI will ask you to confirm the token. In our case is labdemo, but can be any string created by you to protect your lab. Finally, press the CONNECT button.

Token created in LAB_TOKEN env var

1. Overview

After the connection is established, you’ll see the initial tab providing a global view of the system. The dashboard includes the total node count, services, actions, events, and some general system stats.

Overview
  • Top metrics
Click to follow to other tabs
  • Node Status
Is possible to click on the status of a node to show more details. In this first tab, we can see the services created in this node.
The second tab shows usage statistics for this node.
The third tab shows system information. The top button shows a complete definition related to this node.
  • Service Status
Is possible to click on the status of service to show more details. In this first tab, we can see the actions created in this service.
The second tab shows usage statistics for this service.
The third tab shows the main service settings. The top button shows a complete definition related to this service.
  • Action Status
Is possible to click on the status of the action to show more details. In this first tab, we can see the main parameters and we can even call this action.
The second tab shows usage statistics for this action.
The top button shows a complete definition related to this action.
  • Event Status
Is possible to click on the status of an event to show more details. In this first tab, we can see the main parameters and we can even emit this event.
The second tab shows usage statistics for this event. And the top button shows other details.

2. Nodes

The Nodes tab provides an overview of the deployed nodes, it can be a simple list or a graph.

List of Nodes
Graphical representation of nodes is available

3. Services

The Services tab lists the services currently available. Clicking on a service will provide service-related information and statistics. The Lab marks as “non-redundant” services running on a single instance, i.e., services without a replica.

List of services
Call a request and see the communication between services

4. Actions

Actions tab allows interacting with the actions exposed by the services.

List of actions

To call an action (e.g.,passengers.check) simply click on it. Fill in the parameters and press the CALL button.

Action passengers.check has been called
Actions suspects.list sorted by name has been called. Some advanced options are available.

5. Events

The Events tab provides the list of event listeners.

List of events

To emit an event, click on an event listener (e.g. fever.detected), fill payload, and press EMIT button.

Emit an event

6. Tracing

The Tracing tab shows the action calls and their spans.

The “span” is the primary building block of a distributed trace, representing an individual unit of work done in a distributed system.³

List of tracing
Span— By clicking on a specific trace, you can see the details of each span.

7. Logs

The Log tab shows the system logs.

In the logs, it is possible to see that the event was received by the doctors.

Conclusion

As demonstrated in this article, Moleculer’s Lab is a tool that can improve developer’s productivity, allowing them to quickly build and deploy moleculer-based microservices applications.

You can see the contents of this project in this repository: https://github.com/brasiqui/labdemo

If you need another demo to better understand the basis of the Moleculer, follow this article.⁴

Here you can find the complete documentation about Moleculer. If you have any questions please come chat with us at Discord. ⁵

--

--