Environment-Based Configuration
What You'll Learn
You'll learn how to manage different observability settings for development, staging, and production environments using environment variables and feature flags.
Why Configuration Matters Across Environments
Not all environments need the same level of observability. In development, you want verbose logging and full trace capture to debug issues quickly. In production, you need to balance observability with performance and cost, often sampling only a fraction of requests to avoid overwhelming your monitoring system.
Think of it like a car's diagnostics: a mechanic (dev) wants every sensor reading in real-time, but a daily driver (production) only needs the check-engine light and basic dashboard metrics.
Core Implementation Patterns
Environment Variables provide the foundation. Set different values per environment:
import os
# Different sampling rates per environment
TRACE_SAMPLE_RATE = float(os.getenv("TRACE_SAMPLE_RATE", "1.0")) # 100% in dev
LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO") # DEBUG in dev, INFO in prod
ENABLE_COST_TRACKING = os.getenv("ENABLE_COST_TRACKING", "false") == "true"
# Apply environment-aware settings
if os.getenv("ENVIRONMENT") == "production":
TRACE_SAMPLE_RATE = 0.1 # Only 10% of requests
ENABLE_DETAILED_METADATA = False
else:
ENABLE_DETAILED_METADATA = True
Feature Flags allow runtime toggling without redeployment:
FEATURE_FLAGS = {
"enable_arize_tracking": os.getenv("ENABLE_ARIZE") == "true",
"enable_helicone_caching": os.getenv("ENABLE_HELICONE_CACHE") == "true",
"detailed_token_accounting": os.getenv("DETAILED_TOKENS") == "true"
}
This lets you gradually roll out new observability features or quickly disable expensive monitoring during incidents.
Environment-Specific Trade-offs
- Dev: Max verbosity, all custom metadata, 100% sampling
- Staging: Production-like sampling, but more detailed errors
- Production: Cost-optimized sampling, critical metrics only, performance-first
Key Takeaway: Use environment variables and feature flags to tune observability granularity per environment, keeping dev visibility high while optimizing production for cost and performance.