Skip to content

feat(observability): add circuit-breaker state transition metrics - #1314

Merged
RUKAYAT-CODER merged 2 commits into
rinafcode:mainfrom
nobodieair:fix/issue-1165-emit-circuit-breaker-state-transitions-as
Aug 31, 2026
Merged

feat(observability): add circuit-breaker state transition metrics#1314
RUKAYAT-CODER merged 2 commits into
rinafcode:mainfrom
nobodieair:fix/issue-1165-emit-circuit-breaker-state-transitions-as

Conversation

@nobodieair

Copy link
Copy Markdown
Contributor

Overview

This PR adds observability for circuit-breaker state transitions. The breaker now emits counters whenever it transitions to OPEN, HALF_OPEN, or CLOSED, using the existing metrics registry. This makes breaker open/half-open/close events visible in dashboards and alerts without changing the breaker’s behavior.

Related Issue

Changes

🔁 Circuit-Breaker Transition Metrics

  • [MODIFY] src/utils/circuitBreaker.ts

    • Emit a metric on every state transition:
      • circuit_breaker_opened_total when transitioning to OPEN
      • circuit_breaker_half_opened_total when transitioning to HALF_OPEN
      • circuit_breaker_closed_total when transitioning to CLOSED
    • Emit exactly once per transition; repeated isOpen() / state checks do not double-count.
    • Preserve existing behavior while reporting transition metadata to the metrics layer.
  • [MODIFY] src/lib/monitoring/metrics.ts

    • Add counter definitions for open, half-open, and closed transitions.
    • Export recordCircuitBreakerTransition(previousState, nextState) to centralize metric emission.
    • Follow existing naming and registry conventions.
  • [ADD] src/utils/__tests__/circuitBreaker.test.ts and src/lib/__tests__/circuitBreakerMetrics.test.ts

    • Verify each state transition increments the correct counter once.
    • Verify no-op transitions and repeated state checks do not produce duplicates.
    • Verify existing circuit-breaker tests still pass.

Verification Results

npm test -- src/utils/__tests__/circuitBreaker.test.ts src/lib/__tests__/circuitBreakerMetrics.test.ts
✅ 16/16 passed

Manual verification:
✅ Open transition emitted exactly once
✅ Half-open transition emitted exactly once
✅ Closed transition emitted exactly once
✅ Repeated state checks produce no extra metrics
Acceptance Criteria Status
Implemented across the listed files circuitBreaker.ts and metrics.ts updated
Unit/integration tests added or updated and passing ✅ 16/16 tests pass
No regression; follows project coding standards ✅ Metrics are additive; breaker state-machine behavior unchanged

Closes #1165

@drips-wave

drips-wave Bot commented Aug 31, 2026

Copy link
Copy Markdown

@nobodieair Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@RUKAYAT-CODER

Copy link
Copy Markdown
Contributor

Thank you for contributing to the project.

@RUKAYAT-CODER
RUKAYAT-CODER merged commit 838fe4e into rinafcode:main Aug 31, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Emit circuit-breaker state transitions as metrics

2 participants