Skip to content

feat(observability): implement injectable Token-API decorators and circuit breaker (#1644) - #2157

Open
SurbhiAgarwal1 wants to merge 3 commits into
LFDT-Panurus:mainfrom
SurbhiAgarwal1:feature/1644-token-api-observability
Open

feat(observability): implement injectable Token-API decorators and circuit breaker (#1644)#2157
SurbhiAgarwal1 wants to merge 3 commits into
LFDT-Panurus:mainfrom
SurbhiAgarwal1:feature/1644-token-api-observability

Conversation

@SurbhiAgarwal1

Copy link
Copy Markdown
Contributor

Fixes #1644

Summary

This PR implements a clean, injectable decorator architecture for Token-API entry points (driver.WalletService, driver.OwnerWallet, driver.IssuerWallet) incorporating Prometheus-compatible metrics and circuit breaker back-pressure protection, following @adecaro's recommendation to use gowrap generator templates.

Proposed Changes

1. Circuit Breaker (token/services/observability/circuitbreaker.go)

  • Implements thread-safe state machine (StateClosed, StateOpen, StateHalfOpen).
  • Configurable consecutive error limit and cooldown timeout.
  • Fast-fails with back-pressure error ErrCircuitOpen when open to limit blast radius during DoS attacks.

2. Metrics Instrumentation (token/services/observability/metrics.go)

  • Integrated with metrics.Provider.
  • Exposes requests_total (Counter), errors_total (Counter), request_duration_seconds (Histogram), and inflight_requests (Gauge).

3. Injectable Decorators (token/services/observability/decorator.go)

  • NewWalletServiceDecorator, NewOwnerWalletDecorator, NewIssuerWalletDecorator wrap services non-intrusively without modifying underlying concrete driver logic.

4. GoWrap Template (token/services/observability/templates/gowrap_prometheus_cb.tmpl)

  • Template for automated decorator generation using gowrap.

5. Unit Tests & Race Detection (token/services/observability/observability_test.go)

  • Comprehensive unit test suite covering metrics observation, circuit breaker state transitions, back-pressure rejection, and recovery.
  • Tested and verified cleanly with Go race detector (go test -race).

Verification & Compliance

  • go test -v ./token/services/observability/...
  • go test -v -race ./token/services/observability/...
  • gofmt -l -s ./token/services/observability
  • Error handling complies with codebase guidelines using github.com/hyperledger-labs/fabric-smart-client/pkg/utils/errors.
  • Commit is signed off with DCO (Signed-off-by: Surbhi Agarwal <SurbhiAgarwal1@users.noreply.github.com>).

Signed-off-by: Surbhi Agarwal <SurbhiAgarwal1@users.noreply.github.com>
…rcuit breaker

Implement a clean, injectable decorator architecture for Token-API entry points (WalletService, OwnerWallet, IssuerWallet) using gowrap templates. Includes metrics collection (counter, gauge, histogram) and circuit breaker protection with back-pressure signal (ErrCircuitOpen).

Signed-off-by: Surbhi Agarwal <SurbhiAgarwal1@users.noreply.github.com>
Fix nlreturn blank line formatting in decorator.go and update error assertions to require in observability_test.go per testifylint rules.

Signed-off-by: Surbhi Agarwal <SurbhiAgarwal1@users.noreply.github.com>
@AkramBitar

Copy link
Copy Markdown
Contributor

@SurbhiAgarwal1

Thanks a lot for submitting this PR!
I also want to apologize in advance if it takes me some time to review it. I'll get to it as soon as possible.

Regards,
Akram

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.

Observability, Alerting, and Circuit Breaking [LOW]

2 participants