Task 4-2: Gift Aggregation Metrics
Phase: 4 Priority: Medium Module:
batchDepends on: Không có Reference: docs/BountyHunter-Backend/details/feature-livestream/SPEC.md
Background
GiftAggregationJob không có observability. Không biết số lượng gifts được process, throughput, hay khi nào backlog xảy ra.
Tasks
DI Note:
GiftAggregationJobcần inject: -RedisCounterService(hoặc tương đương) để gọiscanKeys()vàgetAndSet()-MeterRegistry(Micrometer) nếu thêm metrics — inject qua constructor:private final MeterRegistry meterRegistry;- Đảm bảospring-boot-starter-actuatorvàmicrometer-corecó trongbatch/pom.xmlnếu dùng Micrometer
File: batch/job/gift_aggregation/GiftAggregationJob.java
-
[ ] Log tổng số streamers và tổng bcoin mỗi batch run:
@Scheduled(fixedDelayString = "${jobs.gift-aggregation.fixedDelay:5000}") public void processAggregatedGifts() { Set<String> keys = redisCounterService.scanKeys(PENDING_GIFT_KEY_PREFIX + "*"); int processedCount = 0; long totalValue = 0; for (String key : keys) { Long value = redisCounterService.getAndSet(key, 0L); if (value != null && value > 0) { // send to queue processedCount++; totalValue += value; } } if (processedCount > 0) { LOGGER.info("[GIFT_AGGREGATION] batch_run: streamers={}, total_bcoin={}", processedCount, totalValue); } // Alert nếu backlog quá lớn if (keys.size() > 100) { LOGGER.warn("[GIFT_AGGREGATION] High Redis key count: {} - possible backlog", keys.size()); } } -
[ ] Bổ sung Micrometer metric (optional, nếu
micrometer-coređã có trong pom):// Inject: private final MeterRegistry meterRegistry; meterRegistry.counter("gift_aggregation.streamers_processed").increment(processedCount); meterRegistry.counter("gift_aggregation.total_bcoin").increment(totalValue); -
[ ] Verify log format chứa đủ thông tin cho task-3-2 tuning:
streamers=,total_bcoin=, key count
Verification / Acceptance Criteria
- [ ] Log
[GIFT_AGGREGATION] batch_run: streamers=X, total_bcoin=Yxuất hiện mỗi batch run khi có data - [ ] Khi
processedCount == 0→ không log INFO (tránh spam logs khi idle) - [ ] Khi
keys.size() > 100→ log WARN[GIFT_AGGREGATION] High Redis key count - [ ] Nếu có Micrometer: metric
gift_aggregation.streamers_processedvàgift_aggregation.total_bcoinxuất hiện trong/actuator/metrics - [ ] Metrics đủ để task-3-2 quyết định tuning interval
Files to Modify
batch/src/main/java/com/figpop/batch/job/gift_aggregation/GiftAggregationJob.java