Skip to content

Task 3-1: Metrics per Queue - Processing Time & Success Rate

Phase: 3 - Observability Priority: Medium Depends on: Không có Reference: docs/BountyHunter-Backend/details/feature-prize-allocation/SPEC.md

Background

Không có metrics về processing time per queue, success rate, hay redelivery count. Cần để so sánh performance giữa các queue types và detect degradation.

Tasks

DI Note: Tất cả 9 listeners cần inject MeterRegistry (Micrometer) qua constructor (@RequiredArgsConstructor). Đảm bảo spring-boot-starter-actuatormicrometer-core có trong batch/pom.xml. data.getActionType().name() — verify actionType field là enum với .name() method; nếu có thể null, dùng Optional.ofNullable(data.getActionType()).map(Enum::name).orElse("UNKNOWN").

  • [ ] Thêm timer wrapper vào tất cả 9 listeners:

    // Inject: private final MeterRegistry meterRegistry;
    @JmsListener(destination = "${queue.prize-allocation.prize}", ...)
    public void onMessage(String messageJson) {
        PrizeAllocationData data = convertMessage(messageJson, PrizeAllocationData.class);
        Timer.Sample sample = Timer.start(meterRegistry);
        String status = "success";
        try {
            // existing logic
        } catch (Exception e) {
            status = "error";
            LOGGER.error("[PRIZE_ALLOC] Processing failed correlationId={}", data.getCorrelationId(), e);
            throw e;  // re-throw để JMS trigger redelivery policy
        } finally {
            sample.stop(meterRegistry.timer("prize_allocation.processing_time",
                "queue", "PRIZE",  // thay đổi per listener
                "status", status,
                "action", data.getActionType() != null ? data.getActionType().name() : "UNKNOWN"
            ));
        }
    }
    

  • [ ] Record redelivery count (header cần spring-jms):

    @JmsListener(...)
    public void onMessage(
        String messageJson,
        @Header(value = "JMSXDeliveryCount", required = false) Integer deliveryCount
    ) {
        if (deliveryCount != null && deliveryCount > 1) {
            meterRegistry.counter("prize_allocation.redelivery",
                "queue", "PRIZE"
            ).increment();
            LOGGER.warn("[PRIZE_ALLOC] Message redelivery count={} correlationId={}",
                deliveryCount, data.getCorrelationId());
        }
    }
    

Verification / Acceptance Criteria

  • [ ] Metric prize_allocation.processing_time với tags queue, status, action xuất hiện trong /actuator/metrics
  • [ ] Metric prize_allocation.redelivery tăng khi message bị redeliver
  • [ ] Tất cả 9 queues có metrics riêng (khác nhau ở tag queue)
  • [ ] Exception path: status=error được record, exception được re-throw (không nuốt)
  • [ ] Timer overhead: < 1ms per message (không ảnh hưởng throughput)

Files to Modify

  • 9 listener files trong batch/jms/prize_allocation/