Skip to content

[DRAFT] feat(edge): strengthen edge reporter with metrics, backoff, structured logging - #2

Closed
tanmayxchoudhary wants to merge 1 commit into
mainfrom
bounty/edge-reporter
Closed

[DRAFT] feat(edge): strengthen edge reporter with metrics, backoff, structured logging#2
tanmayxchoudhary wants to merge 1 commit into
mainfrom
bounty/edge-reporter

Conversation

@tanmayxchoudhary

Copy link
Copy Markdown
Owner

Summary

Implements the Edge Reporter Module from Clawland-AI#24 with a differentiated approach focusing on observability, reliability, and test coverage.

What was implemented

pkg/edge/config.go

  • Added StatusInterval, MaxRetries, RetryBaseDelay, EventQueueSize, EventWorkers fields for fine-grained reporter control
  • Updated DefaultConfig() with sensible defaults

pkg/edge/metrics.go (NEW)

  • In-process Prometheus-friendly metrics collector via expvar (exported as edgereporter)
  • Counters: registrations, heartbeats, status reports, events, deliveries (ok/failed), retries, dropped events
  • Gauges: queue depth, last delivery status
  • Latency ring buffer for recent delivery latency tracking

pkg/edge/reporter.go (Rewritten)

  • Exponential backoff with jitter (±50% jitter, doubles each attempt, capped at 30s)
  • Structured logging via pkg/logger with component prefix and key-value fields
  • Context-aware HTTP requests with timeout propagation
  • Separate heartbeat loop (with initial registration + heartbeat on start)
  • Separate status loop with configurable interval
  • Async event queue with bounded worker pool and non-blocking QueueEvent()
  • Graceful drain on shutdown (waits for in-flight events)
  • Idempotent Stop() via atomicBool
  • Deterministic clock injection for testable timestamps via WithClock() option
  • Test hooks via WithHeartbeatHook() / WithStatusHook()
  • Backward-compatible StartHeartbeat() alias
  • All public methods thread-safe with sync.RWMutex for GeneStatsProvider

pkg/gene/selector.go

  • Added deterministic tie-breaker (confidence → verified_by) when scores are equal

How this differs from the 4 existing competing PRs

Feature PR Clawland-AI#20 PR Clawland-AI#25 PR Clawland-AI#28 PR Clawland-AI#31 This PR
Prometheus/expvar metrics
Exponential backoff with jitter ❌ (linear retry)
Structured logging (pkg/logger) ❌ (log.Print)
Context-aware HTTP
Async event queue with worker pool
Separate heartbeat + status loops
Deterministic clock injection
Test hooks for observability
Race-condition tests (-race clean)
Timeout/retry tests
Concurrent access tests
Response body on failure
Bearer auth + X-Clawland-Node-ID
Endpoint normalization
Gene stats in heartbeat/status

Test Results

$ go test -race -count=1 ./pkg/edge/
ok  github.com/sipeed/picoclaw/pkg/edge  2.954s

$ go test -race -count=1 ./pkg/gene/
ok  github.com/sipeed/picoclaw/pkg/gene  1.119s

$ go test ./...
All packages pass (edge, gene, logger)

$ go vet ./...
All clean

$ go test -race ./pkg/edge/
29 tests, 0 races

Files Changed

  • pkg/edge/config.go — extended config with new reporter fields
  • pkg/edge/metrics.goNEW expvar-based metrics collector
  • pkg/edge/reporter.go — full rewrite with differentiated features
  • pkg/edge/reporter_test.goNEW 29 tests covering all paths
  • pkg/gene/selector.go — deterministic tie-breaker fix

Closes Clawland-AI#24

…d logging, and comprehensive tests

Differentiators vs competing PRs (Clawland-AI#20, Clawland-AI#25, Clawland-AI#28, Clawland-AI#31):
- In-process Prometheus-friendly metrics (expvar) tracking deliveries, failures, retries, queue depth
- Exponential backoff with ±50% jitter for failed deliveries
- Structured logging via pkg/logger (component, fields, levels)
- Context-aware HTTP with timeout propagation
- Separate heartbeat and status reporting loops
- Async event queue with bounded worker pool and graceful drain
- Race-condition-tested concurrency safety (-race clean)
- Deterministic clock injection for testable timestamps
- Full httptest coverage: registration, heartbeat, status, events, queueing,
  auth headers, X-Clawland-Node-ID, endpoint normalization, failure details,
  retries, timeouts, idempotent stop, concurrent access, metrics verification
- Also fixes pkg/gene/selector.go tie-breaker for deterministic ordering

Closes Clawland-AI#24
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.

bounty: edge reporter module

1 participant