Skip to main content

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 loading

Client

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 loading

Best Practices

  1. Don’t modify behavior: Instrumentation is read-only
  2. Handle errors gracefully: Instrumentation errors are swallowed
  3. Keep it lightweight: Avoid heavy computation
  4. Use appropriate scope: Route vs request level
  5. 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