Overview
Distributed tracing allows you to track requests as they flow through your application, providing visibility into performance bottlenecks and request lifecycles.Automatic Instrumentation
The application automatically instruments common operations through the OpenTelemetry Node SDK.Instrumented Operations
Configured insrc/instrumentation.ts:
src/instrumentation.ts:88
Automatic Instrumentation Includes
- HTTP Instrumentation: Tracks all HTTP requests and responses
- PostgreSQL Instrumentation: Traces database queries with SQL comments
- DNS Instrumentation: Tracks DNS lookups
- Network Instrumentation: Low-level network operations
- Undici Instrumentation: Modern HTTP client instrumentation
- Runtime Instrumentation: Node.js runtime metrics
Filtering Traced Requests
The HTTP instrumentation filters out certain requests:- OpenAPI documentation endpoints (
/openapi/*) - Well-known paths (
/.well-known/*) - Static image files (
.png,.jpg,.svg, etc.)
HTTP Request Middleware
The Hono app uses@hono/otel for HTTP instrumentation:
src/app.ts:35
This middleware:
- Creates a span for each HTTP request
- Captures HTTP method, route, and status code
- Automatically handles errors and exceptions
- Propagates trace context to downstream services
Manual Span Creation
You can create custom spans for specific operations using the telemetry utilities.Getting a Tracer
Fromsrc/core/utils/telemetry.ts:
src/core/utils/telemetry.ts:95
Recording Spans
Use therecordSpan utility to wrap async operations:
src/core/utils/telemetry.ts:129
Span Options
name: The name of the span (e.g.,'db.query','api.call')tracer: The tracer instance to useattributes: Key-value pairs to attach to the spanfn: The async function to execute within the spanendWhenDone: Whether to automatically end the span (default:true)
Span Attributes
Spans can include attributes for filtering and analysis.Flattening Nested Objects
UseflattenAttributes or flattenAttributesV2 to convert complex objects:
src/core/utils/telemetry.ts:207
Array Handling
The flattening utilities handle arrays intelligently:- Small arrays (≤5 items): Each item is flattened individually
- Large arrays: Only length and preview (first 3 items) are included
Error Handling in Spans
TherecordSpan utility automatically handles errors:
src/core/utils/telemetry.ts:169
The error handling:
- Records the exception with name, message, and stack trace
- Sets span status to
ERROR - Ends the span automatically
- Re-throws the error for normal handling
Trace Context Propagation
Trace context is automatically propagated through:HTTP Headers
Thetraceparent and tracestate headers are automatically added to outgoing requests:
Async Operations
Using Hono’scontextStorage() middleware, context is preserved across async boundaries:
src/app.ts:42
PostgreSQL Query Tracing
Database queries are automatically traced with enhanced reporting:src/instrumentation.ts:117
This adds SQL comments with trace information:
Noop Tracer
For environments where tracing should be disabled:src/core/utils/telemetry.ts:18
The noop tracer:
- Has zero performance overhead
- Implements the full Tracer API
- Does not create or export spans
Best Practices
1. Use Semantic Naming
Follow OpenTelemetry semantic conventions:2. Add Relevant Attributes
Include attributes that help with debugging:3. Keep Span Granularity Balanced
Don’t create too many or too few spans:4. Use endWhenDone Appropriately
SetendWhenDone: false only when you need manual control:
Viewing Traces in Grafana
Traces are exported to Tempo and viewable in Grafana:- Navigate to
http://localhost:3111 - Go to Explore → Tempo
- Search by:
- Trace ID
- Service name
- Operation name
- Attributes
Configuration
Next Steps
Metrics
Learn about metrics collection
Grafana Setup
Visualize traces in Grafana
