Metrics and Alerts
Metrics and Alerts

In this course, we will learn the concepts of microservice and spring framework and focus on Microservice concerns.

Learning Objectives

  • Health Checks
  • Alerts
  • Error Handling
  • Security

Intended Audience

  • Beginner Java developers
  • Java developers interested in learning how to Build and Deploy RESTful Web Services
  • Java Developers who want to develop web applications using the Spring framework
  • Java Developers who want to develop web applications with microservices
  • Java Developers who wish to develop Spring Boot Microservices with Spring Cloud


  • Basic Java knowledge

Hello, dear friends. In this lesson, we will talk about managing metrics in a microservices architecture. We will use the MicroProfile framework. MicroProfile main dependency includes the metrics API. If you don't use the main MicroProfile dependency, you can add metric features to your project using microprofile/metrics/api dependency, like this. When you start the service, you can see metrics URL from the console. 

You can open that link and see the default metrics pulled up by the MicroProfile metrics API. But you need to have a user that has admin privilege because when you try to open the metrics link, it asks you to supply username and password, as you see here. Okay, open the server.xml file of the citizen service. Seek to basic registry, tag, insert a user. I'll insert the user with username admin, password admin. Now, I should give the user admin role. To do this, I will use administrator role tag. Okay, save the changes. As you see here, some metrics start with base and some start with vendor.

There are three types of metrics: base, vendor, and application. Base and vendor metrics are supplied by MicroProfile or the microservice platform. Application metrics are defined by developers. You can see here an example of the application metric. When we declare an application metric, we can use various types of annotations. The most common are counted, gauge, metric, and timed. Counted annotation can be applied to method, constructor, and type. It denotes a counter which counts the invocations of the annotated object. The default unit of counted annotation is metrics unit.none. 

Gauge annotation can be applied to method. It denotes a gauge which samples the value of the annotated object. It doesn't have a default unit. You need to specify a unit when you define it. Metric annotation can be applied to field and parameter. It contains the metadata information when requesting a metric to be injected. Its default unit is metric units.none. Timed annotation can be applied to constructor, method, and field. It denotes a timer, which tracks the duration of the annotated object. Its default unit is metric units.nanoseconds.

All annotations can have the following fields that correspond to the metadata fields. Metadata is this part when you define an annotation. An annotation can have the following fields: name, absolute, description, unit, and scope. Name sets the name of the metric. It's an optional field. If you don't define it explicitly, then the name of the annotated object is used. Absolute is another optional field. If it's true, it uses the given name as the absolute name of the metric. 

If it's false, it prepends the package name and class name before the given name. Its default value is false. Unit is the unit of the metric. You can see predefined units through metric units class. Scope is an optional field. Its default value is application. As I said earlier, we can define our own metrics and store them in specialized databases like Prometheus, Influx etc. Now we will go through a simple example. We define an application metric. Open nin-service project. Here, open class. Just above the generateCitizenNumber function, add accounted annotation, and give it a name. I'll call it ninRequestCount. Save your changes.

Now, use curl to make a request. I forgot to include the user specifications in the nin-service server.xml file. I'll add it quickly. Save your changes. Refresh the page and enter admin as both the username and password. When we get to the bottom of the page, we'll see our new metric. As you can see, it's an application metric with a value of three because we curled the service three times. Okay, my friends. See you in the following lesson. See you later.


About the Author
Learning Paths

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.