Skip to content

Commit 545a003

Browse files
author
Chris Parsons
committed
Added some extra metrics checks, and a stored procedure to the database.
1 parent cb20036 commit 545a003

File tree

11 files changed

+61
-32
lines changed

11 files changed

+61
-32
lines changed

README.MD

+7-4
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,27 @@ set. This can be executed with `.\gradlew flywayMigrate` and will update the dat
3232
## Docker ##
3333
The docker-compose file contains docker containers for MySQL, Prometheus, AlertManager and Grafana.
3434

35-
### MySQL ###
35+
## MySQL ##
3636
MySQL is booted up in a container as the backend database for the application. Flyway should be used to populate the data, then
3737
the application can fetch the data from the database. Flyway connects using a JDBC connection, but the Spring Boot application
3838
uses R2DBC to connect for asynchronous purposes. The mydb database can then be connected to locally using the 3306 port.
3939

40-
### Prometheus ###
40+
## Prometheus ##
4141
Prometheus collects metrics from the service. It uses the service at `user-api-service:9092` and the yml [here](docker/prometheus/prometheus.yml).
4242

43+
![prometheus.png](src/main/resources/prometheus.png)
4344

44-
### Alert Manager ###
45+
## Alert Manager ##
4546
Alert Manager isn't set up to do anything, but would normally evaluate Prometheus rules with any alerts being routed to the receiver
4647
as set in the [alertmanager](docker/alertmanager/alertmanager.yml) yml file.
4748

48-
### Grafana ###
49+
## Grafana ##
4950
Grafana uses a default dashboard to display data for the springboot service. We can set up Prometheus as a data source, then import
5051
the dashboard from Grafana from https://grafana.com/grafana/dashboards/11378. This then shows the metrics churned out from the
5152
service in real time.
5253

54+
![grafana.png](src/main/resources/grafana.png)
55+
5356
## Helm ##
5457
Helm files can be created using `helm create userdataapi`. This includes templates and helpers as well as the default value
5558
file. Deployment strategies (ie for rolling upgrades) can be set in the [deployment](helm/userdataapi/templates/deployment.yaml) yaml.

docker/docker-compose.yml

+16-16
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@ services:
22

33
# Applications
44

5-
user-api-service:
6-
depends_on:
7-
usermysql:
8-
condition: service_healthy
9-
prometheus:
10-
condition: service_started
11-
grafana:
12-
condition: service_started
13-
alertmanager:
14-
condition: service_started
15-
image: "user-data-api-docker-image"
16-
container_name: "user-data-api-docker-image"
17-
ports:
18-
- "9092:9092"
19-
environment:
20-
- SPRING_R2DBC_URL=r2dbc:mysql://usermysql:3306/mydb
5+
# user-api-service:
6+
# depends_on:
7+
# usermysql:
8+
# condition: service_healthy
9+
# prometheus:
10+
# condition: service_started
11+
# grafana:
12+
# condition: service_started
13+
# alertmanager:
14+
# condition: service_started
15+
# image: "user-data-api-docker-image"
16+
# container_name: "user-data-api-docker-image"
17+
# ports:
18+
# - "9092:9092"
19+
# environment:
20+
# - SPRING_R2DBC_URL=r2dbc:mysql://usermysql:3306/mydb
2121

2222
# Backing Services
2323

docker/prometheus/prometheus.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ rule_files:
88
scrape_configs:
99
- job_name: 'user-service'
1010
scrape_interval: 2s
11-
metrics_path: '/actuator/prometheus'
11+
metrics_path: '/metrics'
1212
static_configs:
1313
- targets: [ 'user-api-service:9092' ]
1414
- job_name: 'prometheus'

src/main/java/com/chrisp1985/UserDataAPI/controller/UserController.java

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.chrisp1985.UserDataAPI.data.User;
44
import com.chrisp1985.UserDataAPI.service.UserDataService;
5+
import lombok.extern.slf4j.Slf4j;
56
import org.springframework.beans.factory.annotation.Autowired;
67
import org.springframework.http.MediaType;
78
import org.springframework.http.ResponseEntity;
@@ -13,6 +14,7 @@
1314

1415
@RestController
1516
@RequestMapping("/api/v1/user")
17+
@Slf4j
1618
public class UserController {
1719

1820
private final UserDataService userDataService;
@@ -29,6 +31,7 @@ public ResponseEntity<Mono<User>> getUserJpa(@PathVariable String entryId) {
2931

3032
@GetMapping(value = "/r2dbc")
3133
public ResponseEntity<Flux<User>> getUsersR2jdbc() {
34+
log.info("User request made.");
3235
return ResponseEntity.ok(userDataService.getAllUsers());
3336
}
3437

src/main/java/com/chrisp1985/UserDataAPI/data/User.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.chrisp1985.UserDataAPI.data;
22

33
import jakarta.persistence.Entity;
4+
import jakarta.persistence.GeneratedValue;
5+
import jakarta.persistence.GenerationType;
6+
import jakarta.persistence.Id;
47
import lombok.AllArgsConstructor;
58
import lombok.Builder;
69
import lombok.Data;
@@ -15,7 +18,8 @@
1518
@NoArgsConstructor
1619
@Entity
1720
public class User {
18-
@NotNull
21+
@Id
22+
@GeneratedValue(strategy = GenerationType.IDENTITY)
1923
Integer id;
2024

2125
@NotBlank

src/main/resources/application-dev.yml

+10-5
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,16 @@ spring:
2828
management:
2929
endpoints:
3030
web:
31-
exposure:
32-
include: health, info, prometheus
33-
info:
34-
env:
35-
enabled: true
31+
exposure.include: "health,prometheus"
32+
base-path: /
33+
path-mapping.prometheus: "metrics"
34+
jmx.exposure.exclude: "*"
35+
metrics:
36+
enable.jvm: true
37+
distribution:
38+
percentiles.http.server.requests: 0.5, 0.9, 0.95, 0.99, 0.999
39+
slo:
40+
"[http.server.requests]": "10ms,50ms,100ms,200ms,500ms,1s,5s,10s,30s"
3641

3742
logging:
3843
level:

src/main/resources/application-prod.yml

+10-5
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,16 @@ spring:
2828
management:
2929
endpoints:
3030
web:
31-
exposure:
32-
include: health, info, prometheus
33-
info:
34-
env:
35-
enabled: true
31+
exposure.include: "health,prometheus"
32+
base-path: /
33+
path-mapping.prometheus: "metrics"
34+
jmx.exposure.exclude: "*"
35+
metrics:
36+
enable.jvm: true
37+
distribution:
38+
percentiles.http.server.requests: 0.5, 0.9, 0.95, 0.99, 0.999
39+
slo:
40+
"[http.server.requests]": "10ms,50ms,100ms,200ms,500ms,1s,5s,10s,30s"
3641

3742
logging:
3843
level:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
DELIMITER //
2+
3+
CREATE PROCEDURE GetAllUsers()
4+
BEGIN
5+
SELECT * FROM user;
6+
END //
7+
8+
DELIMITER ;

src/main/resources/grafana.png

163 KB
Loading

src/main/resources/prometheus.png

14.1 KB
Loading

src/test/java/com/chrisp1985/UserDataAPI/UserDataServiceTests.java

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.junit.runner.RunWith;
99
import org.springframework.beans.factory.annotation.Autowired;
1010
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
11+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
1112
import org.springframework.boot.test.mock.mockito.MockBean;
1213
import org.springframework.cache.Cache;
1314
import org.springframework.cache.CacheManager;

0 commit comments

Comments
 (0)