Skip to main content

Overview

The expvar 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 standard expvar package:
  • Uses expvar.Handler() to serve the variables endpoint
  • Exports all published variables as JSON
  • Includes built-in cmdline and memstats variables

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.Int
  • NewFloat(name): Creates and publishes an expvar.Float
  • NewString(name): Creates and publishes an expvar.String
  • NewMap(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: