Skip to content

Commit

Permalink
Sleuth and Zipkin are replaced with Micrometer. Grafana, Prometheus, …
Browse files Browse the repository at this point in the history
…and Zipkin changes are implemented
  • Loading branch information
musab.bozkurt committed Feb 4, 2024
1 parent 1271891 commit a3aaa87
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 55 deletions.
60 changes: 45 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,58 @@
</ol>
</details>

-------

### Summary

#### live-data-service is providing CRUD operations to process live events

-------

### Prerequisites

- `Java 21+` needs to be installed `export JAVA_HOME=$(/usr/libexec/java_home -v 21)`
- `Maven` needs to be installed
- `Docker` needs to be installed
- Install any Java IDE (Eclipse, STS, Intellij etc..) and ensure you are able to launch
- Clone or checkout the project from version control system (git) and follow below steps
- Install any Java IDE (`Eclipse`, `STS`, `Intellij` etc..) and ensure you are able to launch
- Clone or checkout the project from version control system (`git`) and follow below steps

-------

### Tech_Stack

- `Java 21+`
- `Java 21`
- `Spring Boot 3`
- `H2 Database` (Default values are provided below)
- `Default H2 Database Url`: http://localhost:8080/h2-console
- `username`: `sa`
- `password`: `sa`
- `url`: `jdbc:h2:mem:MB_TEST;DB_CLOSE_DELAY=-1`
- `Default H2 Database url`: http://localhost:8080/h2-console
- `Swagger` - For API Local Testing
- `Default Swagger url`: http://localhost:8080/swagger-ui/index.html
- `Swagger`: http://localhost:8080/swagger-ui/index.html
- `Flyway` for database migration
- Centralize exception handling by `ControllerAdvice`
- `Mapstruct` to map different type of objects to each other
- `Zipkin` and `Sleuth` dependencies were added to track the logs easily
- `docker-compose.yml` contains `Grafana` and `Prometheus` to track metrics, `Kafka` for event-driven architecture
- `Actuator url`: http://localhost:8080/actuator/prometheus
- `Prometheus url`: http://localhost:9090/graph
- `Grafana url`: http://localhost:3000/login
- `username`: `admin`
- `password`: `admin`
- `Kafka UI url`: http://localhost:9091/
- `Micrometer` dependencies were added to track the logs easily
- `docker-compose.yml` contains `Grafana`, `Prometheus` and `Zipkin` to track metrics, `Kafka` for event-driven
architecture
- `Actuator`: http://localhost:8080/actuator/
- `Kafka UI`: http://localhost:9091/
- `Grafana`
- Login Credentials
- Url: http://localhost:3000/
- Email or username: `admin`
- Password: `admin`
- Add `Prometheus` Datasource
- Url: http://host.docker.internal:9090
- Add Dashboards
- https://grafana.com/grafana/dashboards/4701-jvm-micrometer/
- https://grafana.com/grafana/dashboards/5373-micrometer-spring-throughput/
- `Prometheus`
- `Actuator`: http://localhost:8080/actuator/prometheus
- `Prometheus`: http://localhost:9090/graph
- `Zipkin UI`: http://localhost:9411

-------

### How_To_Run_And_Test_Application

Expand All @@ -58,6 +77,8 @@
4 - mvn spring-boot:run
```

-------

### How_To_Run_And_Test_Dockerfile

- Please follow the following steps, if you want to build and run Dockerfile
Expand All @@ -70,12 +91,14 @@
5 - docker run -p 8080:8080 mb/live-data-service
```

-------

### How_To_Run_And_Test_Docker_Compose

- Please follow the following steps, if you want to build and run `docker-compose.yml`
- Remove `live-data-service` service comment in `services` section in `docker-compose.yml`
- `Docker` -> `Preferences` -> `Resources` -> `File sharing` -> click add button and select `prometheus` folder under
the `/src/main/resources` -> `Apply & Restart`
the `/src/main/resources` or just add `/etc/prometheus` path -> `Apply & Restart`
- ![img.png](img.png)

```
Expand All @@ -84,3 +107,10 @@
3 - docker build -t mb/live-data-service .
4 - docker-compose up -d
```

-------

### References

- [Metrics Made Easy Via Spring Actuator, Docker, Prometheus, and Grafana](https://www.youtube.com/watch?v=Utv7MWgNTvI)
- https://prometheus.io/docs/prometheus/latest/installation/#volumes-bind-mount
7 changes: 7 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ services:
- "9090:9090"
volumes:
- ./src/main/resources/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
command: --web.enable-lifecycle --config.file=/etc/prometheus/prometheus.yml
container_name: prometheus

grafana:
Expand Down Expand Up @@ -64,6 +65,12 @@ services:
- KAFKA_CLUSTERS_0_BOOTSTRAPSERVERS=kafka:9093
- KAFKA_CLUSTERS_0_ZOOKEEPER=localhost:2181

zipkin:
image: openzipkin/zipkin
container_name: zipkin
ports:
- "9411:9411"

volumes:
zookeeper_data:
driver: local
Expand Down
34 changes: 13 additions & 21 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,8 @@

<properties>
<java.version>21</java.version>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<org.projectlombok.version>1.18.30</org.projectlombok.version>
<org.mapstruct.version>1.5.5.Final</org.mapstruct.version>
<spring.cloud.starter.sleuth.version>3.1.10</spring.cloud.starter.sleuth.version>
<spring.cloud.sleuth.zipkin.version>3.1.10</spring.cloud.sleuth.zipkin.version>
<dockerfile-maven-plugin.version>1.4.13</dockerfile-maven-plugin.version>
<registry>${env.CI_REGISTRY}/${env.CI_REGION}/${project.artifactId}</registry>
</properties>
Expand Down Expand Up @@ -84,34 +80,32 @@
<version>${org.mapstruct.version}</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-sleuth -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
<version>${spring.cloud.starter.sleuth.version}</version>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<scope>runtime</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-sleuth-zipkin -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-zipkin</artifactId>
<version>${spring.cloud.sleuth.zipkin.version}</version>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-core</artifactId>
</dependency>

<!--OpenTelemetry Tracer: Bridges the Micrometer Observation API to OpenTelemetry-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing-bridge-otel</artifactId>
</dependency>

<!--Reporter/Zipkin with OpenTelemetry: Reports traces to Zipkin-->
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<scope>runtime</scope>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-exporter-zipkin</artifactId>
</dependency>

<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-core</artifactId>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
Expand Down Expand Up @@ -140,8 +134,6 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.12.1</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
Expand Down
16 changes: 4 additions & 12 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,6 @@ spring:
settings:
web-allow-others: true

zipkin:
base-url: ${ZIPKIN_BASE_URL:http://localhost:9411}
enabled: false
sleuth:
sampler:
probability: 1.0
scheduled:
enabled: false
async:
enabled: false

kafka:
consumer:
bootstrap-servers: localhost:9092
Expand All @@ -63,13 +52,16 @@ spring:

docker:
compose:
enabled: false
enabled: true

management:
endpoints:
web:
exposure:
include: health, info, metrics, loggers, prometheus
tracing:
sampling:
probability: 1.0

swagger:
documentation:
Expand Down
12 changes: 7 additions & 5 deletions src/main/resources/prometheus/prometheus.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#Global configurations
# Global configurations
global:
scrape_interval: 5s # Set the scrape interval to every 5 seconds.
evaluation_interval: 5s # Evaluate rules every 5 seconds.
scrape_interval: 5s # Set the scrape interval to every 5 seconds. Default is every 1 minute.
evaluation_interval: 5s # Evaluate rules every 5 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).

scrape_configs:
- job_name: 'live-data-app'
- job_name: 'live-data-service'
metrics_path: '/actuator/prometheus'
scrape_interval: 5s
static_configs:
- targets: [ 'live-data-service:8080' ]
- targets: [ 'host.docker.internal:8080' ] # 8080 is live-data-service port.
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ void shouldGetScoreBoardByIdThrowException_WhenScoreBoardNotFound() throws Excep

mockMvc.perform(get("/score-boards/{id}", id))
.andExpect(status().isNotFound())
.andExpect(result -> Assertions.assertTrue(result.getResolvedException() instanceof BaseException))
.andExpect(result -> Assertions.assertInstanceOf(BaseException.class, result.getResolvedException()))
.andDo(print());
}

Expand Down Expand Up @@ -154,7 +154,7 @@ void shouldUpdateScoreBoardByIdThrowException_WhenScoreBoardNotFound() throws Ex
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(apiScoreBoardUpdateRequest)))
.andExpect(status().isNotFound())
.andExpect(result -> Assertions.assertTrue(result.getResolvedException() instanceof BaseException))
.andExpect(result -> Assertions.assertInstanceOf(BaseException.class, result.getResolvedException()))
.andDo(print());
}

Expand Down

0 comments on commit a3aaa87

Please sign in to comment.