Spring Boot Metrics: Micrometer සහ Actuator වලින් App Performance බලමු! - SC Guide

Spring Boot Metrics: ඇප් එකේ හද ගැස්ම දැනගනිමු!
මචං, ඔයා Spring Boot වලින් Application එකක් හදලා Production දැම්මට පස්සේ, ඔයාට කොහොමද දැනගන්නේ ඒක හොඳට දුවනවද, නැත්නම් අඩුපාඩු තියෙනවද කියලා? නිකම්ම "ඇප් එක දුවනවා" කියලා කියන එකට වඩා, "මේකේ CPU Usage මෙච්චරයි, Memory මෙච්චරයි, API Calls මෙච්චරයි, Response Time මෙච්චරයි" වගේ විස්තර දැනගන්න එක මොන තරම් වටිනවද?
අන්න ඒකට තමයි Metrics කියන Concepts එක එන්නේ. හරියට කාර් එකක Dashboard එක වගේ, අපේ Application එකේ අභ්යන්තර තොරතුරු අපිට පෙන්වන්නේ මේ Metrics වලින්. Spring Boot මේ වැඩේ ලේසි කරන්න Micrometer සහ Actuator කියන powerful Tools දෙකක් අපිට දීලා තියෙනවා. අද අපි බලමු මේවා භාවිතා කරලා කොහොමද අපේ Application වල Performance හරියටම Track කරන්නේ කියලා, ඒ වගේම අපිට ඕන විදිහට Custom Metrics හදාගන්නේ කොහොමද කියලා.
ඇයි අපිට Metrics ඕන? (Why do we need Metrics?)
මචං, හිතන්න ඔයා ඇප් එකක් දැම්මා Production එකට. Users ලා පාවිච්චි කරන්න පටන් ගත්තා. දැන් ඔයාට දැනගන්න ඕන, ඇප් එක කොච්චර හොඳට වැඩ කරනවද? Users ලා මොන APIs ද වැඩිපුරම පාවිච්චි කරන්නේ? මොන API එකද Slow වෙන්නේ? මොන වෙලාවටද Users ලා වැඩිපුරම එන්නේ? මේ වගේ ප්රශ්න වලට උත්තර දෙන්න Metrics නැතුව බැහැ.
Metrics වල ප්රධානම වාසි ටිකක් කිව්වොත්:
- Early Issue Detection: ප්රශ්න ඇතිවෙන්න කලින්ම හඳුනාගන්න පුළුවන්. උදාහරණයක් විදිහට, Memory Usage එක එකපාරටම වැඩි වෙනවා නම්, ඒක Memory Leak එකක් වෙන්න පුළුවන්.
- Performance Monitoring: APIs වල Response Times, Throughput, Error Rates වගේ දේවල් බලලා Application එකේ Performance එක හොඳටම තේරුම් ගන්න පුළුවන්.
- Capacity Planning: எதிர்கාලේදී කොච්චර Users ලා වැඩි වුණොත් අපේ System එකට එයාලව Handle කරන්න පුළුවන්ද කියලා තීරණය කරන්න Metrics උදව් වෙනවා.
- User Experience Improvement: Users ලාට හොඳ Service එකක් දෙන්න නම්, අපි System එකේ අඩුපාඩු දැනගෙන ඒවා නිවැරදි කරන්න ඕන. Metrics ඒකට හොඳම Guide එක.
- Resource Optimization: System resources (CPU, Memory, Disk) කොහොමද භාවිතා වෙන්නේ කියලා බලලා, අනවශ්ය වියදම් අඩුකරගන්න පුළුවන්.
නිකම්ම දුවන ඇප් එකක් නෙමෙයි, හරියටම වැඩ කරන ඇප් එකක් හදන්න නම් Metrics අත්යවශ්යයි.
Spring Boot Metrics Ecosystem එක (The Spring Boot Metrics Ecosystem)
Spring Boot, Metrics Manage කරන්න ලස්සන Ecosystem එකක් අපිට දීලා තියෙනවා. ඒකේ ප්රධාන Players ලා දෙන්නා තමයි Micrometer සහ Spring Boot Actuator.
Micrometer: Metrics වලට Common Interface එකක්
හිතන්න, ඔයාගේ System එකෙන් Metrics එකතු කරලා, ඒක Prometheus, Grafana, New Relic, Datadog වගේ Monitoring System එකකට යවන්න ඕන කියලා. හැම එකටම වෙන වෙනම Code ලියන්න ගියොත් වැඩේ එපා වෙනවා නේද? අන්න ඒකට තමයි Micrometer කියන "Metrics Facade" එක තියෙන්නේ.
Micrometer කියන්නේ Language-agnostic, Vendor-neutral "Metrics Facade" එකක්. ඒක අපිට Metering API එකක් දෙනවා. අපි ඒ API එක පාවිච්චි කරලා Metrics හදනවා. ඊට පස්සේ Micrometer ට පුළුවන් ඒ Metrics ටික, අපිට ඕන Monitoring System එකකට (Prometheus, InfluxDB, CloudWatch, ආදී) හරවලා දෙන්න. මේකෙන් වෙන්නේ, අපි අපේ Application එකේ Metrics Code එක Monitoring System එකකින් Independent වෙන එක.
Micrometer සපයන ප්රධාන Metric Types ටිකක් මෙන්න:
- Counter: එකක් එකතු වෙන විදිහට Count කරනවා (e.g., API calls, error counts).
- Gauge: වර්තමාන අගයක් පෙන්නනවා (e.g., current active users, queue size).
- Timer: යම්කිසි ක්රියාවක Duration එක Record කරනවා (e.g., API response times, DB query times).
- DistributionSummary: අගයන් සමූහයක Distribution එක Record කරනවා (e.g., request sizes).
Spring Boot Actuator: Production-Ready Features
Actuator කියන්නේ Spring Boot වල තියෙන ලොකුම වාසියක්. ඒක අපේ Application එකට Production-ready Features ගොඩක් එකතු කරනවා. Health checks, Info endpoints, Environment details වගේම Metrics expose කරන්නත් Actuator දායක වෙනවා.
Actuator, Micrometer එක්ක එකට වැඩ කරනවා. අපි Micrometer පාවිච්චි කරලා හදන Metrics ටික, Actuator වල Metrics Endpoint (/actuator/metrics
) එකෙන් Expose කරන්නේ Actuator තමයි. ඒ වගේම, අපිට ඕන නම් Prometheus වගේ Registry එකක් පාවිච්චි කරනවා නම්, Actuator ඒ Registry එකට හරියන Format එකට Metrics Expose කරන්නත් Support කරනවා (/actuator/prometheus
).
දැන් ඔයාලට තේරෙනවා ඇති, මේ දෙක එකට එකතු වෙලා වැඩ කරන හැටි. Micrometer වලින් Metrics හදනවා, Actuator වලින් ඒක එළියට දෙනවා. Super!
Custom Metrics හදමු: API Performance Track කරමු!
දැන් අපි කෙලින්ම වැඩේට බහිමු! අපි හදමු අපේ Spring Boot Application එකේ APIs වල Call Count එකයි, Response Time එකයි Track කරන්න.
පළවෙනියටම Dependencies ටික
ඔයාගේ pom.xml
එකට මේ Dependencies ටික එකතු කරගන්න ඕන. spring-boot-starter-actuator
තමයි Actuator Features දෙන්නේ, micrometer-registry-prometheus
එකෙන් Micrometer Metrics, Prometheus Format එකට Expose කරන්න පුළුවන් වෙනවා.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<scope>runtime</scope>
</dependency>
<!-- Other dependencies -->
</dependencies>
MeterRegistry Bean එක Auto-wire කරගනිමු
අපිට Custom Metrics හදන්න නම්, Micrometer වල MeterRegistry
කියන Interface එකේ Instance එකක් ඕන වෙනවා. Spring Boot, Actuator Add කලාම මේක Auto-Configure කරනවා. අපිට කරන්න තියෙන්නේ, අපිට ඕන Class එකකට මේක Auto-wire කරගන්න එක විතරයි.
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.stereotype.Service;
@Service
public class ApiMetricService {
private final MeterRegistry meterRegistry;
public ApiMetricService(MeterRegistry meterRegistry) {
this.meterRegistry = meterRegistry;
}
// Metrics logic will go here
}
Counter එකක් හදමු: API Call Counts Track කරන්න
අපි Counter එකක් හදලා, අපේ API එකට එන හැම Request එකක්ම Count කරමු. මේකෙන් අපිට දැනගන්න පුළුවන්, මොන API එකටද වැඩිපුරම Hits එන්නේ කියලා.
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.stereotype.Service;
@Service
public class ApiMetricService {
private final MeterRegistry meterRegistry;
// Define a Counter
private final Counter helloApiCallCounter;
public ApiMetricService(MeterRegistry meterRegistry) {
this.meterRegistry = meterRegistry;
// Initialize the Counter
// Name: api.calls.total, Description: Total number of API calls, Tags: endpoint=hello
this.helloApiCallCounter = Counter.builder("api.calls.total")
.description("Total number of API calls")
.tag("endpoint", "/api/hello") // You can add tags for filtering and grouping
.register(meterRegistry);
}
public void incrementHelloApiCall() {
helloApiCallCounter.increment();
}
}
මේ tag
එක කියන්නේ Metrics වලට ලොකු Power එකක් දෙන Feature එකක්. අපිට එකම Metric Name එකට (api.calls.total
) වෙනස් Tags (endpoint=/api/hello
, endpoint=/api/products
) පාවිච්චි කරලා, විවිධ Context වල Metrics Track කරන්න පුළුවන්. මේවා Monitoring System වල Graph බලද්දී Filters විදිහට පාවිච්චි කරන්න පුළුවන්.
Timer එකක් හදමු: API Response Times Track කරන්න
API එකක Response Time එක Track කරන එක හරිම වැදගත්. ඒකට අපි Timer එකක් පාවිච්චි කරනවා. Timer එකෙන් Request එකක් ඇවිත්, Response එකක් යනකල් ගතවන කාලය Record කරනවා.
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Timer;
import org.springframework.stereotype.Service;
import java.time.Duration;
@Service
public class ApiMetricService {
private final MeterRegistry meterRegistry;
private final Counter helloApiCallCounter;
private final Timer helloApiTimer;
public ApiMetricService(MeterRegistry meterRegistry) {
this.meterRegistry = meterRegistry;
this.helloApiCallCounter = Counter.builder("api.calls.total")
.description("Total number of API calls")
.tag("endpoint", "/api/hello")
.register(meterRegistry);
// Initialize the Timer
// Name: api.response.time, Description: Response time of API calls, Tags: endpoint=hello
this.helloApiTimer = Timer.builder("api.response.time")
.description("Response time for /api/hello endpoint")
.tag("endpoint", "/api/hello")
.publishPercentiles(0.5, 0.95, 0.99) // Publish 50th, 95th, 99th percentiles
.publishPercentileHistogram(true) // Publish a histogram for detailed distribution
.minimumExpectedValue(Duration.ofMillis(1))
.maximumExpectedValue(Duration.ofSeconds(10))
.register(meterRegistry);
}
public void incrementHelloApiCall() {
helloApiCallCounter.increment();
}
// Method to record the execution time of an operation
public <T> T recordHelloApiDuration(java.util.function.Supplier<T> supplier) {
return helloApiTimer.record(supplier);
}
// Another option for recording, if you have the duration already
public void recordHelloApiDuration(long durationNanos) {
helloApiTimer.record(durationNanos, java.util.concurrent.TimeUnit.NANOSECONDS);
}
}
publishPercentiles
සහ publishPercentileHistogram
කියන ඒවා Metrics Visualization වලට හරිම වැදගත්. මේකෙන් අපිට පුළුවන් Request වලින් 95% ක් කොච්චර වෙලාවකින් ඉවර වෙනවද වගේ දේවල් බලාගන්න. ඒක Average Response Time එකකට වඩා හොඳ Insight එකක් දෙනවා.
Controller එකක් ඇතුලේ Metrics පාවිච්චි කරමු
දැන් අපි බලමු මේ හදාගත්තු Metrics ටික Controller එකක් ඇතුලේ පාවිච්චි කරන්නේ කොහොමද කියලා.
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
@RestController
@RequestMapping("/api")
public class HelloController {
private final ApiMetricService apiMetricService;
public HelloController(ApiMetricService apiMetricService) {
this.apiMetricService = apiMetricService;
}
@GetMapping("/hello")
public String hello() throws InterruptedException {
// Increment the counter for this API call
apiMetricService.incrementHelloApiCall();
// Simulate some processing time and record it with the timer
return apiMetricService.recordHelloApiDuration(() -> {
try {
Thread.sleep(Math.round(Math.random() * 500) + 100); // Simulate 100-600ms delay
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
return "Hello, Micrometer Metrics with Spring Boot!";
});
}
@GetMapping("/products")
public String getProducts() {
// You can create a separate counter/timer for this endpoint if needed
// Or use tags to differentiate them under the same metric name
return "List of products (metrics not fully implemented for this endpoint in example)";
}
}
මේ Thread.sleep()
එක දැම්මේ API එකක Response Time එකක් Simulation කරන්න. Real World එකේදී මේක Database Calls, External Service Calls වගේ දේවල් වෙන්න පුළුවන්.
application.properties එකේ Configuration ටික
Actuator Metrics Expose කරන්න නම්, අපි application.properties
එකේ පොඩි Setting එකක් දාන්න ඕන.
# Expose all Actuator endpoints via web
management.endpoints.web.exposure.include=*
# If you only want metrics and health, you can specify them:
# management.endpoints.web.exposure.include=health,metrics,prometheus
management.endpoints.web.exposure.include=*
කියන්නේ හැම Actuator Endpoint එකක්ම Web එකට Expose කරන්න කියලා. Production Environment එකකදී මේක පරිස්සමෙන් පාවිච්චි කරන්න ඕන, Security Reasons නිසා. අවශ්ය Endpoints ටික විතරක් Expose කරන එක තමයි හොඳ.
Metrics බලමු (Viewing the Metrics)
දැන් ඔයාගේ Spring Boot Application එක Run කරලා, Browser එකේ මේ URLs ටික Open කරලා බලන්න.
- පළවෙනියටම
http://localhost:8080/api/hello
කියන URL එකට Request කිහිපයක් දාන්න. (Refresh කරලා නැත්නම් Postman එකෙන් Request කිහිපයක් දාන්න). - ඊට පස්සේ
http://localhost:8080/actuator/metrics
කියන URL එකට යන්න. මේකෙන් අපේ Application එකේ Available හැම Metric එකක්ම බලාගන්න පුළුවන්. - ඔයාට ඕන නම් අපේ Custom Metric එක විතරක් බලන්න,
http://localhost:8080/actuator/metrics/api.calls.total
සහhttp://localhost:8080/actuator/metrics/api.response.time
කියන URLs වලට යන්න. ඒකෙන් ඒ Metric එකේ Details බලාගන්න පුළුවන්. - Prometheus Registry එක Add කරලා තියෙන නිසා,
http://localhost:8080/actuator/prometheus
කියන URL එකෙන් Prometheus Scrape කරන්න පුළුවන් Format එකට Metrics බලාගන්න පුළුවන්.
Prometheus සහ Grafana එක්ක වැඩේ (Optional, but highly recommended)
Real World එකේදී, මේ Metrics ටික නිකම්ම JSON වලින් බලනවාට වඩා, Prometheus වගේ Monitoring System එකකට Scrape කරලා, Grafana වගේ Visualization Tool එකකින් Graph විදිහට බලන එක තමයි කරන්නේ. ඒකෙන් මේ Data ටික analyse කරන එක හරිම ලේසියි.
Prometheus Setup කරලා, ඒකේ Configuration එකට ඔයාගේ Spring Boot App එකේ /actuator/prometheus
Endpoint එක Add කලාම, Prometheus ට පුළුවන් අපේ App එකෙන් Metrics ටික Collect කරන්න. ඊට පස්සේ Grafana වල Dashboard හදලා ඒ Metrics ටික ලස්සනට Graph කරලා බලන්න පුළුවන්. මේකෙන් අපිට Application එකේ Performance trends, bottlenecks, සහ anomalies identify කරන්න පුළුවන්.
Prometheus සහ Grafana Setup කරන එකට වෙනම Post එකක් ඕන වෙයි. ඒත් ඔයාලට මේ දැනුමෙන් පස්සේ තනියම හොයලා බලන්න පුළුවන්.
අවසන් වශයෙන්
දැන් ඔයාලට තේරෙනවා ඇති නේද Spring Boot වල Metrics කියන්නේ මොකක්ද කියලා, ඒ වගේම Micrometer සහ Actuator පාවිච්චි කරලා කොහොමද Custom Metrics හදලා API Performance Track කරන්නේ කියලා. Application එකක් Production එකට දානකොට, Metrics කියන්නේ අත්යවශ්ය දෙයක්. ඒකෙන් ඔයාගේ System එකේ හද ගැස්ම හරියටම දැනගෙන, ඕනම ප්රශ්නයකට කලින්ම ලෑස්ති වෙන්න පුළුවන්.
නිකන් බලන් ඉන්නේ නැතුව, මේවා ඔයාගේ Project එකක ට්රයි කරලා බලන්න. පොඩි Experiment එකක් කරලා බලන්න, ඊට පස්සේ ඔයාට තේරෙයි මේකේ ලොකු වැදගත්කම.
ඔබේ අදහස්, ප්රශ්න, සහ මේ Topic එක ගැන වැඩිදුරටත් දැනගන්න ඕන දේවල් තියෙනවා නම්, පහළින් Comment කරන්න! අපි ඊළඟ Post එකෙන් තවත් නියම Technology Concept එකක් ගැන කතා කරමු! ජය වේවා!