Skip to content

Commit 05eb8d0

Browse files
committed
Task 14 : Integrate Prometheus and Grafana
1 parent 7078365 commit 05eb8d0

File tree

5 files changed

+143
-0
lines changed

5 files changed

+143
-0
lines changed

Dockerfile

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# ---------- Stage 1: Build ----------
2+
FROM maven:3.9.11-amazoncorretto-25 AS build
3+
4+
# Work dir
5+
WORKDIR /build
6+
7+
# Copy Maven descriptor + wrapper dir first to leverage layer caching
8+
COPY pom.xml .
9+
COPY .mvn .mvn
10+
11+
# Copy sources and build
12+
COPY src src
13+
RUN mvn -q clean package -DskipTests
14+
15+
# ---------- Stage 2: Runtime ----------
16+
FROM amazoncorretto:25
17+
18+
# Work dir
19+
WORKDIR /app
20+
21+
# Copy the built JAR
22+
COPY --from=build /build/target/*.jar cryptoexchangeapi.jar
23+
24+
# Service port
25+
EXPOSE 1927
26+
27+
# Run with preview features enabled (Java 25)
28+
ENTRYPOINT ["java", "--enable-preview", "-jar", "cryptoexchangeapi.jar"]
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
global:
2+
scrape_interval: 120s # By default, scrape targets every 15 seconds.
3+
evaluation_interval: 120s # By default, scrape targets every 15 seconds.
4+
5+
# A scrape configuration containing exactly one endpoint to scrape:
6+
# Here it's Prometheus itself.
7+
scrape_configs:
8+
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
9+
- job_name: 'prometheus'
10+
# Override the global default and scrape targets from this job every 5 seconds.
11+
scrape_interval: 5s
12+
# metrics_path defaults to '/metrics'
13+
# scheme defaults to 'http'.
14+
static_configs:
15+
- targets: ['localhost:9090']
16+
17+
- job_name: 'Spring Boot Application input'
18+
metrics_path: '/actuator/prometheus'
19+
scrape_interval: 2s
20+
static_configs:
21+
- targets: [ 'cryptoexchangeapi:1927' ]
22+
labels:
23+
application: 'Crypto Exchange Api'

docker-compose.yml

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
services:
2+
3+
mongodb:
4+
image: "mongo:latest"
5+
container_name: mongo-container
6+
restart: always
7+
ports:
8+
- "27017:27017"
9+
volumes:
10+
- mongodb_data_container:/data/db
11+
networks:
12+
- cryptoexchangeapinetwork
13+
14+
15+
cryptoexchangeapi:
16+
image: 'cryptoexchangeapi:latest'
17+
build:
18+
context: .
19+
dockerfile: Dockerfile
20+
container_name: cryptoexchangeapi
21+
restart: on-failure
22+
env_file:
23+
- .env # Use the .env file for environment variables
24+
ports:
25+
- "1927:1927"
26+
environment:
27+
- server.port=1927
28+
- spring.data.mongodb.host=mongodb
29+
- spring.data.mongodb.port=${MONGO_DB_PORT}
30+
- spring.data.mongodb.database=${MONGO_DB_NAME}
31+
depends_on:
32+
- mongodb
33+
networks:
34+
- cryptoexchangeapinetwork
35+
36+
prometheus:
37+
image: prom/prometheus:latest
38+
container_name: prometheus
39+
restart: unless-stopped
40+
ports:
41+
- "9090:9090"
42+
volumes:
43+
- ./data/prometheus/config:/etc/prometheus/
44+
command:
45+
- '--config.file=/etc/prometheus/prometheus.yml'
46+
networks:
47+
- cryptoexchangeapinetwork
48+
49+
grafana:
50+
image: "grafana/grafana-oss:latest"
51+
pull_policy: always
52+
container_name: grafana
53+
restart: unless-stopped
54+
ports:
55+
- "3000:3000"
56+
volumes:
57+
- ./data/grafana:/var/lib/grafana
58+
environment:
59+
- GF_SECURITY_ADMIN_PASSWORD=admin
60+
- GF_SERVER_DOMAIN=localhost
61+
networks:
62+
- cryptoexchangeapinetwork
63+
64+
volumes:
65+
mongodb_data_container:
66+
67+
networks:
68+
cryptoexchangeapinetwork:

pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,17 @@
129129
<artifactId>spring-boot-starter-validation</artifactId>
130130
</dependency>
131131

132+
<dependency>
133+
<groupId>org.springframework.boot</groupId>
134+
<artifactId>spring-boot-starter-actuator</artifactId>
135+
</dependency>
136+
137+
<dependency>
138+
<groupId>io.micrometer</groupId>
139+
<artifactId>micrometer-registry-prometheus</artifactId>
140+
<scope>runtime</scope>
141+
</dependency>
142+
132143
</dependencies>
133144

134145
<build>

src/main/resources/application.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,16 @@ logging:
5353
level:
5454
org.springframework.cache: TRACE
5555
org.springframework.cache.interceptor: TRACE
56+
57+
# Prometheus
58+
management:
59+
endpoints:
60+
web:
61+
exposure:
62+
include:
63+
- "*"
64+
- prometheus
65+
prometheus:
66+
metrics:
67+
export:
68+
enabled: true

0 commit comments

Comments
 (0)