Overview
Theexpvar middleware exposes Goβs expvar metrics including runtime stats, memory usage, and custom variables.
Use it when you need:
- Go runtime metrics
- Memory usage monitoring
- Custom variable export
Installation
Quick Start
Examples
Basic Setup
Custom Variables
With Auth Protection
Default Variables
API Reference
Functions
Response Format
Technical Details
Standard Library Integration
The middleware wraps Goβs standardexpvar package:
- Uses
expvar.Handler()to serve the variables endpoint - Exports all published variables as JSON
- Includes built-in
cmdlineandmemstatsvariables
Path Matching
- Only intercepts requests to the configured path (default:
/debug/vars) - All other requests pass through to the next handler
- Efficient path comparison using string equality
Helper Functions
The package provides convenience wrappers for common expvar types:NewInt(name): Creates and publishes an expvar.IntNewFloat(name): Creates and publishes an expvar.FloatNewString(name): Creates and publishes an expvar.StringNewMap(name): Creates and publishes an expvar.Map
Thread Safety
All expvar types are safe for concurrent use across goroutines.Best Practices
- Protect endpoint in production
- Use for debugging and monitoring
- Combine with custom metrics
- Donβt expose sensitive data
Testing
The middleware includes comprehensive test coverage. Key test cases:Related Middlewares
- prometheus - Prometheus metrics
- pprof - Profiling
- healthcheck - Health checks