Overview
Effect has built-in support for structured logging, distributed tracing, and metrics. These observability features help you understand what your application is doing in production, debug issues, and monitor performance. For exporting telemetry data:- Use the lightweight Otlp modules from
effect/unstable/observabilityfor new projects - Use @effect/opentelemetry when integrating with existing OpenTelemetry setups
Structured Logging
Basic Logging
Effect provides log levels similar to other logging libraries:Structured Metadata
Add structured metadata to your logs:Customizing Logging
JSON Logger
Emit one JSON line per log entry for production:Log Level Filtering
Raise the minimum level to skip debug/info logs:File Logger
Write logs to a file:Custom Logger
Define a custom logger for app-specific formatting and routing:Environment-Based Logger
Switch loggers based on environment:Distributed Tracing
What is Tracing?
Distributed tracing tracks requests as they flow through your system. Each operation creates a “span” that records:- Operation name and duration
- Parent-child relationships
- Custom attributes
- Error information
Adding Spans
Add spans to track operation duration:OTLP Tracing Setup
Configure OpenTelemetry Protocol (OTLP) tracing export:Complete Tracing Example
Here’s a complete service with tracing:Using the Observability Layer
Provide the observability layer at the top level:Span Annotations
Add custom attributes to spans:Tracing Best Practices
- Add spans at service boundaries: Track calls between services
- Use meaningful span names: Name spans after the operation (e.g., “Database.query”, “API.fetch”)
- Add relevant attributes: Include IDs, types, and other context
- Use Effect.fn with names: Automatically creates spans with good names
- Annotate errors: Errors are automatically captured in spans
- Use log spans for timing:
Effect.withLogSpanadds duration metadata to logs
Observability Backends
Effect’s OTLP exporters work with any OpenTelemetry-compatible backend:- Jaeger: Open-source distributed tracing
- Zipkin: Distributed tracing system
- Honeycomb: Observability platform
- Datadog: Application monitoring
- New Relic: Full-stack observability
- Grafana Tempo: Distributed tracing backend
- AWS X-Ray: Distributed tracing for AWS
Local Development Setup
Run a local OpenTelemetry collector:http://localhost:16686 to view traces.
Combining Logging and Tracing
Logs and traces work together:- A parent span “parent-operation”
- A child span “expensive-operation”
- Logs with
service=apiandoperation=expensivemetadata - Duration metadata for the parent operation