Instrumentation API
The instrumentation API allows you to wrap route handlers and operations for logging, performance tracing, and error reporting.Overview
Instrumentation provides:- Request-level tracing: Measure entire request duration
- Route-level instrumentation: Track individual loaders/actions
- Non-invasive observation: Cannot modify runtime behavior
- Flexible composition: Combine multiple instrumentations
Basic Server Instrumentation
Route Instrumentation
Instrument individual route handlers:Client Instrumentation
Error Handling
Instrumentation handlers never throw - they return error status:OpenTelemetry Integration
Integrate with OpenTelemetry for distributed tracing:Performance Monitoring
Track performance metrics:Custom Middleware
Implement middleware-like patterns:Composition
Combine multiple instrumentations:Conditional Instrumentation
Enable instrumentation conditionally:Data Mode
Instrument data mode applications:Available Handlers
Server
handler.request: Entire request lifecycle route.loader: Route loader execution route.action: Route action execution route.middleware: Middleware execution route.lazy: Lazy route loadingClient
router.initialize: Router initialization router.navigate: Navigation operations router.fetch: Fetcher operations route.loader: Route loader execution route.action: Route action execution route.middleware: Client middleware execution route.lazy: Lazy route loadingBest Practices
- Don’t modify behavior: Instrumentation is read-only
- Handle errors gracefully: Instrumentation errors are swallowed
- Keep it lightweight: Avoid heavy computation
- Use appropriate scope: Route vs request level
- Compose instrumentations: Separate concerns
Limitations
- Cannot modify request/response
- Cannot access handler arguments
- Cannot change data returned
- Errors are caught and logged
- Handler still runs if instrumentation throws