@@ -91,6 +91,207 @@ Application expose các endpoints sau:
9191- ` /actuator/env ` - Environment properties
9292- ` /actuator/loggers ` - Log levels configuration
9393
94+ ### Spring Boot Management Configuration
95+
96+ Cấu hình chi tiết trong ` src/main/resources/application.yml ` :
97+
98+ #### 1. Endpoints Configuration
99+
100+ ``` yaml
101+ management :
102+ endpoints :
103+ web :
104+ exposure :
105+ include : health,info,prometheus,metrics,env,loggers
106+ base-path : /actuator
107+ ` ` `
108+
109+ **` exposure.include`**: Danh sách endpoints được expose qua HTTP
110+ - ` health` - Kiểm tra sức khỏe app (database, disk space, etc.)
111+ - ` info` - Thông tin về application
112+ - ` prometheus` - Metrics theo Prometheus format
113+ - ` metrics` - Tất cả metrics có sẵn
114+ - ` env` - Environment variables và properties
115+ - ` loggers` - Xem và thay đổi log levels động
116+
117+ **Mặc định**: Spring Boot chỉ expose `health` và `info`
118+
119+ **Lưu ý bảo mật**: Không expose tất cả endpoints ở production. Chỉ expose những gì cần thiết và xem xét thêm authentication.
120+
121+ **`base-path`**: Prefix cho tất cả actuator endpoints (default: `/actuator`)
122+
123+ # ### 2. Health Endpoint
124+
125+ ` ` ` yaml
126+ endpoint:
127+ health:
128+ show-details: always
129+ probes:
130+ enabled: true
131+ ` ` `
132+
133+ **`show-details`**: Mức độ chi tiết hiển thị
134+ - ` never` - Chỉ hiện status (UP/DOWN)
135+ - ` when-authorized` - Hiện chi tiết khi user có quyền (khuyến nghị cho production)
136+ - ` always` - Luôn hiện chi tiết (dùng cho development)
137+
138+ **`probes.enabled`**: Kích hoạt Kubernetes liveness/readiness probes
139+ - ` /actuator/health/liveness` - Check app còn sống không (restart nếu DOWN)
140+ - ` /actuator/health/readiness` - Check app sẵn sàng nhận request (ngừng route traffic nếu DOWN)
141+
142+ **Ví dụ output với `show-details: always`**:
143+ ` ` ` json
144+ {
145+ "status": "UP",
146+ "components": {
147+ "db": {
148+ "status": "UP",
149+ "details": {
150+ "database": "PostgreSQL",
151+ "validationQuery": "isValid()"
152+ }
153+ },
154+ "diskSpace": {
155+ "status": "UP",
156+ "details": {
157+ "total": 500000000000,
158+ "free": 250000000000
159+ }
160+ }
161+ }
162+ }
163+ ` ` `
164+
165+ # ### 3. Prometheus Configuration
166+
167+ ` ` ` yaml
168+ endpoint:
169+ prometheus:
170+ access: unrestricted
171+ prometheus:
172+ metrics:
173+ export:
174+ enabled: true
175+ ` ` `
176+
177+ **`access: unrestricted`**: Cho phép Alloy/Prometheus scrape metrics không cần authentication
178+ - ` restricted` - Cần authentication
179+ - ` unrestricted` - Không cần authentication (dùng cho internal network)
180+
181+ **`export.enabled`**: Kích hoạt export metrics theo Prometheus format
182+
183+ **Output tại `/actuator/prometheus`**:
184+ ```
185+ # HELP jvm_memory_used_bytes The amount of used memory
186+ # TYPE jvm_memory_used_bytes gauge
187+ jvm_memory_used_bytes{area="heap",id="PS Eden Space"} 1.25829120E8
188+
189+ # HELP http_server_requests_seconds
190+ # TYPE http_server_requests_seconds summary
191+ http_server_requests_seconds_count{method="GET",uri="/api/users"} 42.0
192+ http_server_requests_seconds_sum{method="GET",uri="/api/users"} 2.1
193+ ```
194+
195+ #### 4. Metrics Configuration
196+
197+ ```yaml
198+ metrics:
199+ tags:
200+ application: ${spring.application.name}
201+ distribution:
202+ percentiles-histogram:
203+ http.server.requests: true
204+ ```
205+
206+ ** ` tags.application ` ** : Thêm tag ` application=playground ` vào TẤT CẢ metrics
207+ - Lợi ích: Dễ filter metrics trong Grafana khi có nhiều applications
208+ - Ví dụ: ` jvm_memory_used_bytes{application="playground",area="heap"} `
209+
210+ ** ` percentiles-histogram ` ** : Generate histogram buckets cho HTTP requests
211+ - Kích hoạt metric: ` http_server_requests_seconds_bucket `
212+ - Cho phép tính percentiles (p50, p95, p99) trong Prometheus/Grafana
213+
214+ ** Query Grafana để tính p95 latency** :
215+ ``` promql
216+ histogram_quantile(0.95,
217+ sum(rate(http_server_requests_seconds_bucket[5m])) by (le, uri)
218+ )
219+ ```
220+
221+ #### 5. Distributed Tracing
222+
223+ ``` yaml
224+ tracing :
225+ sampling :
226+ probability : 1.0
227+ zipkin :
228+ tracing :
229+ endpoint : http://localhost:9411/api/v2/spans
230+ ` ` `
231+
232+ **` sampling.probability`**: Tỷ lệ sample traces (0.0 đến 1.0)
233+ - ` 1.0` = Sample 100% requests (development/testing)
234+ - ` 0.1` = Sample 10% requests (production - giảm overhead)
235+ - ` 0.01` = Sample 1% requests (high traffic production)
236+
237+ **Lưu ý**: 100% sampling có thể gây overhead cao ở production!
238+
239+ **`zipkin.endpoint`**: URL của Zipkin server để gửi traces
240+
241+ **Tracing Flow**:
242+ ```
243+ Request → Generate traceId/spanId → Add to logs → Send to Zipkin
244+ ```
245+
246+ **Trong logs (với pattern tracing)**:
247+ ```
248+ 2025-11-25T10:00:00.000+07:00 INFO [ playground,abc123,def456] ... Processing request
249+ ```
250+ - `abc123` = traceId (track request qua nhiều services)
251+ - `def456` = spanId (track operations trong request)
252+
253+ #### 6. Logging Pattern
254+
255+ ```yaml
256+ logging:
257+ pattern:
258+ level: "%5p [${spring.application.name:},%X{traceId:-},%X{spanId:-}]"
259+ ```
260+
261+ ** Format** : ` LEVEL [app-name,traceId,spanId] `
262+
263+ ** Ví dụ output** :
264+ ```
265+ 2025-11-25T10:00:00.000 DEBUG [playground,648ae8bc12345,648ae8bc12345] 87402 --- [main] c.k.s.p.UserService : Finding user
266+ ```
267+
268+ ** So sánh với mặc định** :
269+ - Mặc định: ` DEBUG 87402 --- [main] c.k.s.p.UserService : Finding user `
270+ - Với tracing: ` DEBUG [playground,648ae8bc12345,648ae8bc12345] 87402 --- [main] c.k.s.p.UserService : Finding user `
271+
272+ ** Lợi ích** :
273+ - Trace request qua nhiều microservices
274+ - Correlate logs từ nhiều services cùng một request
275+ - Dễ dàng filter logs theo traceId trong log aggregation tools
276+
277+ #### Production Best Practices
278+
279+ Cho production environment, nên điều chỉnh:
280+
281+ ``` yaml
282+ management :
283+ endpoints :
284+ web :
285+ exposure :
286+ include : health,prometheus,metrics # Chỉ expose cần thiết
287+ endpoint :
288+ health :
289+ show-details : when-authorized # Bảo mật thông tin
290+ tracing :
291+ sampling :
292+ probability : 0.1 # Giảm overhead, chỉ sample 10%
293+ ` ` `
294+
94295### Grafana Alloy
95296
96297Alloy scrape metrics từ Spring Boot application mỗi 15 giây và forward đến Mimir.
0 commit comments