Skip to content

Task 1-1: ActiveMQ Metrics Integration

Phase: 1 - Observability Priority: High Module: batch Depends on: Không có Reference: docs/BountyHunter-Backend/details/feature-batch-async-processing/SPEC.md

Background

Không có visibility vào ActiveMQ queue depth, processing rate, consumer count. Khi queue backlog xảy ra, không có alert.

Tasks

1. Enable JMS metrics trong Spring Actuator

File: batch/src/main/resources/application-common.yaml

management:
  endpoints:
    web:
      exposure:
        include: health,metrics,hikaricp,jms
  metrics:
    enable:
      jms: true

2. Tạo BatchQueueMonitorService

File mới: batch/service/BatchQueueMonitorService.java

DI Note: JmsTemplate and MeterRegistry are injected via @RequiredArgsConstructor (Lombok). Ensure the following imports are present: - org.springframework.jms.core.JmsTemplate - io.micrometer.core.instrument.MeterRegistry - io.micrometer.core.instrument.Tags - org.springframework.scheduling.annotation.Scheduled

@Component
@RequiredArgsConstructor
@Slf4j
public class BatchQueueMonitorService {

    private final JmsTemplate jmsTemplate;
    private final MeterRegistry meterRegistry;

    private static final String[] CRITICAL_QUEUES = {
        "queue-prize-allocation-PRIZE",
        "queue-prize-allocation-PRIZE_FOR_STREAMER",
        "queue-invitation",
        "livestream-viewer-gift-firebase-update"
    };

    @Scheduled(fixedDelay = 30000)
    public void recordQueueDepths() {
        for (String queue : CRITICAL_QUEUES) {
            try {
                Long depth = getQueueDepth(queue);
                meterRegistry.gauge("jms.queue.depth", Tags.of("queue", queue), depth);
            } catch (Exception e) {
                log.error("[QUEUE_MONITOR] Failed to get depth for queue={}", queue, e);
            }
        }
    }

    private Long getQueueDepth(String queueName) {
        return jmsTemplate.browse(queueName, (session, browser) -> {
            long count = 0;
            while (browser.getEnumeration().hasMoreElements()) { count++; }
            return count;
        });
    }
}

Verification / Acceptance Criteria

  • [ ] Spring Actuator exposes /actuator/metrics/jms.queue.depth endpoint and returns valid data
  • [ ] BatchQueueMonitorService bean is instantiated at startup with no injection errors
  • [ ] Every queue name in CRITICAL_QUEUES appears as a queue tag in the Micrometer gauge after one scheduler tick (30s)
  • [ ] Logs show [QUEUE_MONITOR] entries for each queue; any failure logs an error without crashing the scheduler
  • [ ] /actuator/metrics response includes jms and hikaricp keys (confirming application-common.yaml change took effect)

Files to Modify / Create

  • batch/src/main/resources/application-common.yaml
  • batch/src/main/java/com/figpop/batch/service/BatchQueueMonitorService.java (new)