Observability Options
2 minute read
Observability Options
These options are used with WithObservability():
app.WithObservability(
app.WithLogging(logging.WithJSONHandler()),
app.WithMetrics(),
app.WithTracing(tracing.WithOTLP("localhost:4317")),
)
Component Options
WithLogging
func WithLogging(opts ...logging.Option) ObservabilityOption
Enables structured logging with slog. Service name/version automatically injected.
WithMetrics
func WithMetrics(opts ...metrics.Option) ObservabilityOption
Enables metrics collection (Prometheus by default). Service name/version automatically injected.
WithTracing
func WithTracing(opts ...tracing.Option) ObservabilityOption
Enables distributed tracing. Service name/version automatically injected.
Metrics Server Options
WithMetricsOnMainRouter
func WithMetricsOnMainRouter(path string) ObservabilityOption
Mounts metrics endpoint on the main HTTP server (default: separate server).
WithMetricsSeparateServer
func WithMetricsSeparateServer(addr, path string) ObservabilityOption
Configures separate metrics server address and path.
Default: :9090/metrics
Path Filtering
WithExcludePaths
func WithExcludePaths(paths ...string) ObservabilityOption
Excludes exact paths from observability.
WithExcludePrefixes
func WithExcludePrefixes(prefixes ...string) ObservabilityOption
Excludes path prefixes from observability.
WithExcludePatterns
func WithExcludePatterns(patterns ...string) ObservabilityOption
Excludes paths matching regex patterns from observability.
WithoutDefaultExclusions
func WithoutDefaultExclusions() ObservabilityOption
Disables default path exclusions (/health*, /metrics, /debug/*).
Access Logging
WithAccessLogging
func WithAccessLogging(enabled bool) ObservabilityOption
Enables or disables access logging.
Default: true
WithLogOnlyErrors
func WithLogOnlyErrors() ObservabilityOption
Logs only errors and slow requests (reduces log volume).
Default: false in development, true in production
In production, this is automatically enabled to reduce log volume. Normal successful requests are not logged, but errors (status >= 400) and slow requests are always logged.
WithSlowThreshold
func WithSlowThreshold(d time.Duration) ObservabilityOption
Marks requests as slow if they exceed this duration.
Default: 1s
Example
app.WithObservability(
// Components
app.WithLogging(logging.WithJSONHandler()),
app.WithMetrics(metrics.WithPrometheus(":9090", "/metrics")),
app.WithTracing(tracing.WithOTLP("localhost:4317")),
// Path filtering
app.WithExcludePaths("/healthz", "/readyz"),
app.WithExcludePrefixes("/internal/"),
// Access logging
app.WithLogOnlyErrors(),
app.WithSlowThreshold(500 * time.Millisecond),
)
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.