Sentinel Common
Shared types, utilities, and infrastructure for all Sentinel components.
Overview
The sentinel-common crate provides the foundational building blocks used across the Sentinel platform:
- Type-safe identifiers - Compile-time safety for IDs and scopes
- Error handling - Comprehensive error types with HTTP mapping
- Resource limits - Hard bounds for predictable behavior
- Observability - Prometheus metrics and structured logging
- Circuit breakers - Failure isolation patterns
- Registries - Thread-safe component storage with hot reload
- Budget tracking - Token budgets and cost attribution for inference
Modules
| Module | Description |
|---|---|
ids |
Type-safe identifiers (RouteId, UpstreamId, AgentId) and hierarchical scoping |
types |
Common types (HttpMethod, TlsVersion, LoadBalancingAlgorithm, etc.) |
errors |
Error types with HTTP status mapping and client-safe messages |
limits |
Resource limits and rate limiting infrastructure |
observability |
Prometheus metrics and tracing initialization |
circuit_breaker |
Circuit breaker state machine |
registry |
Generic thread-safe component registry |
scoped_registry |
Scope-aware hierarchical registry |
scoped_metrics |
Namespace/service-scoped metrics |
budget |
Token budgets and cost attribution for inference |
inference |
Inference health check configurations |
Quick Start
use ;
// Initialize tracing
init_tracing;
// Create metrics collector
let metrics = new;
// Use type-safe identifiers
let route_id = new;
let scope = Service ;
// Resolve names through scope chain
let qualified = new;
println!; // "production:payments:backend"
Documentation
Detailed documentation is available in the docs/ directory:
- Identifiers & Scoping - Type-safe IDs and hierarchical scopes
- Types Reference - Common type definitions
- Error Handling - Error types and HTTP mapping
- Limits & Rate Limiting - Resource bounds
- Observability - Metrics and logging
- Patterns - Circuit breakers and registries
Hierarchical Scoping
Sentinel supports hierarchical configuration with scope-based resolution:
Global
└── Namespace (e.g., "production")
└── Service (e.g., "payments")
Names resolve through the scope chain (most specific wins):
use ;
let registry: = new;
// Insert at different scopes
registry.insert;
registry.insert;
registry.insert;
// Resolve from service scope
let scope = Service ;
// Finds "production:payments:timeout" first
let config = registry.resolve;
Error Handling
All errors map to appropriate HTTP status codes:
use ;
// In handler
match result
Resource Limits
Hard bounds prevent resource exhaustion:
use Limits;
// Production defaults
let limits = for_production;
// Check before processing
if body.len > limits.max_body_size_bytes
Observability
Prometheus metrics with automatic registration:
use RequestMetrics;
let metrics = new;
// Record request
metrics.record_request;
// Track active requests
metrics.inc_active_requests;
// ... process ...
metrics.dec_active_requests;
// Circuit breaker state
metrics.set_circuit_breaker_state;
Circuit Breakers
Failure isolation with automatic recovery:
use ;
let config = CircuitBreakerConfig ;
let breaker = new;
// Check before calling
if !breaker.is_closed
// Record result
match upstream_call.await
Token Budgets
For inference routes with usage limits:
use ;
let config = TokenBudgetConfig ;
// Check budget before processing
match budget_tracker.check
Design Principles
- Type Safety - Distinct ID types prevent accidental confusion
- Fail-Safe Defaults - Secure, bounded defaults everywhere
- Observability First - Metrics for every significant operation
- Lock-Free Hot Paths - Atomic operations where possible
- Graceful Degradation - Clear failure modes with fallbacks
Usage by Other Crates
| Crate | Uses |
|---|---|
config |
Types, Limits, Error handling, ID types |
proxy |
All modules - core runtime infrastructure |
agent-protocol |
Error types, ID types |
Minimum Rust Version
Rust 1.92.0 or later (Edition 2021)