Spring Boot Actuator: Application Monitoring | Sri Lankan Developer Guide

Spring Boot Actuator: Application Monitoring | Sri Lankan Developer Guide

මචංලා, කොහොමද ඉතින්? අද අපි කතා කරමු Spring Boot Actuator ගැන. මේක Spring Boot development කරන අයට නැතුවම බැරි tool එකක් කිව්වොත් වැරදක් නැහැ. ලොකු project වලදී වගේම පොඩි project වලදීත් අපේ application එකේ performance එක, health එක වගේ දේවල් monitor කරන එක ගොඩක් වැදගත්. විශේෂයෙන්ම production environment එකක application එකක් දුවනකොට මේ වගේ monitoring capabilities නැතුව වැඩේ ගොඩ දාගන්න අමාරුයි. අන්න ඒ වගේ තැන්වලදී අපේ ගැලවුම්කාරයා තමයි Spring Boot Actuator කියන්නේ.

අද අපි බලමු Actuator කියන්නේ මොකක්ද, කොහොමද ඒක enable කරගන්නේ, ඒකෙන් අපිට ලැබෙන endpoints මොනවද, සහ ඒවා ප්‍රායෝගිකව භාවිතා කරන්නේ කොහොමද කියලා. ඉතින්, වැඩි කතා නැතුව අපි පටන් ගමු නේද?

Spring Boot Actuator කියන්නේ මොකක්ද?

සරලවම කිව්වොත්, Spring Boot Actuator කියන්නේ Spring Boot applications වලට monitoring සහ management capabilities එකතු කරන feature එකක්. අපේ application එක production එකට deploy කරාම, ඒක ඇත්තටම හරියට වැඩ කරනවද, CPU එක වැඩි වෙලාද, memory usage කොහොමද, requests වලට කොච්චර වෙලා යනවද වගේ දේවල් දැනගන්න ඕන වෙනවා. Actuator මේ හැම තොරතුරක්ම අපිට ලබා දෙනවා, ඒ වගේම application එක run වෙන අතරතුරම සමහර දේවල් configure කරන්නත් පුළුවන්කම දෙනවා.

හිතලා බලන්න, ඔයා microservices ගොඩක් තියෙන project එකක වැඩ කරනවා කියලා. එක microservice එකක පොඩි අවුලක් ආවොත් ඒක ඉක්මනටම හොයාගන්නේ කොහොමද? Manual check කරන එක ලේසි වැඩක් නෙවෙයි නේද? Actuator හරහා expose කරන endpoints භාවිතා කරලා අපිට මේ monitoring process එක automate කරන්න පුළුවන්. DevOps pipelines වලටත් මේ endpoints integrate කරලා continuous monitoring කරන්න පුළුවන්. ඒක තමයි මේකේ ලොකුම වාසිය.

Actuator Enable කරගන්නේ කොහොමද?

Spring Boot Actuator enable කරන එක නම් පුදුම විදිහට ලේසියි. කරන්න තියෙන්නේ එක dependency එකක් project එකට එකතු කරන එක විතරයි. ඔයා Maven use කරනවා නම්, pom.xml එකට මේ dependency එක එකතු කරන්න:

<dependency>
    <groupId>org.springframework.boot</groupId&n>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

Gradle use කරනවා නම්, build.gradle එකට මේ විදිහට එකතු කරන්න:

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
}

dependency එක add කරලා project එක build කරාට පස්සේ, Actuator auto-configure වෙනවා. ඒත් default විදිහට හැම Actuator endpoint එකක්ම expose වෙන්නේ නැහැ. Security reasons නිසා, Spring Boot team එක තීරණය කරලා තියෙනවා /health සහ /info වගේ endpoints විතරක් default expose කරන්න. අපිට වෙනත් endpoints expose කරගන්න ඕන නම්, application.properties හෝ application.yml file එකේ මේ configuration එක කරන්න ඕන:

management.endpoints.web.exposure.include=*

මේකෙන් කියන්නේ * (all) endpoints expose කරන්න කියලා. එහෙමත් නැත්නම්, අපිට ඕන කරන specific endpoints list එකක් දෙන්නත් පුළුවන්. උදාහරණයක් විදිහට, management.endpoints.web.exposure.include=health,metrics,info වගේ.

ජනප්‍රිය Actuator Endpoints ටිකක්

Actuator එකෙන් අපිට ලැබෙන endpoints ගොඩක් තියෙනවා. ඒ හැම එකක්ම application එකේ වෙනස් පැත්තක් ගැන තොරතුරු දෙනවා. අපි වැඩිපුරම use කරන endpoints ටිකක් බලමු:

  • /actuator/health: මේක තමයි හැමදාම use කරන endpoint එක. Application එක UPDOWN ද කියලා මේකෙන් කියනවා. Database connections, disk space, messaging queues වගේ dependencies වල health status එකත් මේකෙන් බලාගන්න පුළුවන්. production environment එකක load balancers සහ container orchestration systems (Kubernetes වගේ) මේ endpoint එක use කරලා application instances වල health check කරනවා.
  • /actuator/info: මේකෙන් application එක ගැන custom information පෙන්නන්න පුළුවන්. Build details, Git commit IDs, version numbers වගේ දේවල් application.properties එකේ configure කරලා මේකෙන් expose කරන්න පුළුවන්. info.app.name=My Spring App වගේ information එකතු කරලා බලාගන්න පුළුවන්.
  • /actuator/metrics: මේකනම් developers ලට වගේම operations teams වලටත් වටිනම එකක්. Application එකේ සියලුම metrics මෙතනින් බලාගන්න පුළුවන්. JVM metrics (heap memory, non-heap memory, garbage collection), CPU usage, HTTP request counts, response times වගේ දේවල් මේකට අයිතියි. specific metric එකක් බලන්න /actuator/metrics/{metricName} විදිහට call කරන්න පුළුවන්. උදාහරණයක් විදිහට /actuator/metrics/jvm.memory.used.
  • /actuator/loggers: Application එකේ logging levels manage කරන්න මේකෙන් පුළුවන්. Production එකේදී සමහර වෙලාවට run වෙන වෙලාවෙම logging level එක debug එකට මාරු කරන්න ඕන වෙනවා. මේ endpoint එකෙන් ඒක කරන්න පුළුවන්.
  • /actuator/beans: Spring IoC container එකේ තියෙන හැම bean එකක්ම මේකෙන් බලාගන්න පුළුවන්. Dependencies resolve වෙන හැටි, bean scopes වගේ දේවල් troubleshoot කරන්න මේකෙන් උදව් වෙනවා.
  • /actuator/env: Application එකේ environment properties බලාගන්න මේකෙන් පුළුවන්. Active profiles, system properties, environment variables, command-line arguments වගේ දේවල් මෙතනින් display වෙනවා. sensitive information disclose නොවෙන්න Spring Boot එකෙන් automatic filtering එකක් කරනවා.

ප්‍රායෝගිකව බලමු! (Let's See Practically!)

හරි, දැන් අපි මේක ප්‍රායෝගිකව කරලා බලමු. ඔයාට දැනටමත් Spring Boot project එකක් නැත්නම්, Spring Initializr (start.spring.io) එකට ගිහින් අලුත් project එකක් හදාගන්න පුළුවන්. Dependencies විදිහට Spring Web සහ Spring Boot Actuator තෝරගන්න.

  1. Project එකක් හදාගන්න (නැත්නම් තියෙන එකක් use කරන්න):ඔයාගේ pom.xml එකේ spring-boot-starter-actuator dependency එක තියෙනවද කියලා check කරන්න. නැත්නම් කලින් කිව්ව විදිහට එකතු කරන්න.
  2. Application එක Run කරන්න:දැන් ඔයාගේ Spring Boot application එක run කරන්න. IDE එකකින් (IntelliJ IDEA, VS Code) හෝ command line එකෙන් mvn spring-boot:run (Maven) / gradle bootRun (Gradle) use කරලා run කරන්න පුළුවන්.
  3. Actuator Endpoints Explore කරන්න:Application එක run වුනාට පස්සේ, ඔයාගේ browser එකේ හෝ curl වගේ tool එකක් use කරලා මේ URLs වලට ගිහින් බලන්න:මේ endpoint response ඔක්කොම JSON format එකෙන් තමයි එන්නේ. ඒ නිසා programming languages වලින් parse කරලා වෙනත් monitoring tools වලට integrate කරන එකත් ලේසියි.
    • http://localhost:8080/actuator: මේකෙන් expose කරලා තියෙන හැම Actuator endpoint එකක්ම link විදිහට පෙන්නනවා.
    • http://localhost:8080/actuator/health: Application එකේ health status එක බලාගන්න. මේකෙන් UP හෝ DOWN වගේ status එකක් සහ details (database status, disk space) පෙන්නනවා.
    • http://localhost:8080/actuator/info: ඔයා application.properties එකේ info.* කියලා add කරපු details මෙතනින් පෙන්නයි.
    • http://localhost:8080/actuator/metrics: Available metrics list එකක් බලාගන්න.
    • http://localhost:8080/actuator/metrics/jvm.memory.used: JVM memory usage එක බලාගන්න.
    • http://localhost:8080/actuator/metrics/http.server.requests: HTTP requests metrics බලාගන්න. මේක වැඩ කරනවද බලන්න, කලින් හදපු http://localhost:8080/hello URL එකට දෙතුන් පාරක් ගිහින් ආයෙත් http://localhost:8080/actuator/metrics/http.server.requests බලන්න. request count එක වැඩි වෙලා තියෙයි.

සරල REST Controller එකක් හදන්න (optional):මේක අත්‍යවශ්‍ය නැති වුනත්, metrics වලට HTTP requests එන හැටි බලන්න Controller එකක් හදාගමු. HelloController.java වගේ file එකක් හදලා මේ code එක දාන්න:

package com.example.actuatordemo;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @GetMapping("/hello")
    public String hello() {
        return "Hello from Spring Boot Actuator Demo!";
    }
}

application.properties configure කරන්න:src/main/resources/application.properties file එකට මේ line එක එකතු කරන්න, හැම endpoint එකක්ම expose වෙන්න:

management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

show-details=always එකතු කලේ /health endpoint එකෙන් වැඩිපුර details පෙන්නන්න.

අවසාන වශයෙන්

ඉතින් මචංලා, Spring Boot Actuator කියන්නේ අපේ Spring Boot applications නිරීක්ෂණය කරන්න තියෙන සුපිරිම tool එකක්. ඒකෙන් application එකේ health එක, performance එක සහ වෙනත් වැදගත් operational data ඉක්මනින්ම දැනගන්න පුළුවන්. Production environment එකකදී application එකක් maintain කරන එකට මේක අනිවාර්යයෙන්ම අවශ්‍ය වෙනවා. විශේෂයෙන්ම microservices architecture එකකදී Actuator වල වටිනාකම තවත් වැඩි වෙනවා.

මතක තියාගන්න, Actuator endpoints expose කරනකොට security ගැනත් හිතන්න ඕන. හැම endpoint එකක්ම publicly expose කරන එක හොඳ දෙයක් නෙවෙයි. අවශ්‍ය endpoints විතරක් expose කරන්න, ඒ වගේම authentication සහ authorization layers එකතු කරන්නත් පුළුවන්.

ඔයා මේ article එකෙන් අලුත් දෙයක් ඉගෙන ගත්තා කියලා මම හිතනවා. අනිවාර්යයෙන්ම ඔයාගේ project එකකට Spring Boot Actuator එකතු කරලා මේ endpoints explore කරලා බලන්න. මොනවා හරි ප්‍රශ්න තියෙනවා නම්, පහළින් comment එකක් දාගෙන යන්න. ඒ වගේම ඔයා Actuator use කරලා තියෙන experience එකත් අපිට කියන්න. තවත් මේ වගේ article එකකින් හමුවෙමු!