-
Notifications
You must be signed in to change notification settings - Fork 3
Application metric
Somkiat Puisungnoen edited this page Nov 18, 2022
·
11 revisions
- Actuator
- Micrometer + Prometheus
<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>
- Healthcheck
- Info
- Prometheus
management.info.env.enabled=true
info.app.name=My demo service
info.app.description=My demo service description
info.app.version=1.0.0
management.endpoints.web.exposure.include=health,info,prometheus
- Check result in URL = http://localhost:8080/actuator
{
"_links": {
"self": {
"href": "http://localhost:8080/actuator",
"templated": false
},
"health-path": {
"href": "http://localhost:8080/actuator/health/{*path}",
"templated": true
},
"health": {
"href": "http://localhost:8080/actuator/health",
"templated": false
},
"info": {
"href": "http://localhost:8080/actuator/info",
"templated": false
},
"prometheus": {
"href": "http://localhost:8080/actuator/prometheus",
"templated": false
}
}
}
DemoMetricController.java
@RestController
public class DemoMetricController {
@Autowired
private MeterRegistry meterRegistry;
@GetMapping("/count/{message}")
public ResponseEntity<String> count(@PathVariable String message) {
if("success".equals(message)) {
generateMetric("success");
return new ResponseEntity<>("Success", HttpStatus.OK);
} else if("notfound".equals(message)) {
generateMetric("notfound");
return new ResponseEntity<>("Not found", HttpStatus.NOT_FOUND);
}
generateMetric("error");
return new ResponseEntity<>("Error", HttpStatus.INTERNAL_SERVER_ERROR);
}
private void generateMetric(String success) {
Counter counter = Counter
.builder("hello-springboot")
.description("Success case")
.tag("result", success)
.register(meterRegistry);
counter.increment();
}
}
Try to call API
- http://localhost:8080/count/success
- http://localhost:8080/count/notfound
- http://localhost:8080/count/error
Config file prometheus.yml
scrape_configs:
- job_name: 'demo-service'
scrape_interval: 5s
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ['localhost:8080']
Start prometheus server
- Access to server with URL=http://localhost:9090
Start server
$./prometheus --web.enable-lifecycle
Reload
$curl -i -XPOST localhost:9090/-/reload