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

import "github.com/go-mizu/mizu/middlewares/expvar"

Quick Start

app := mizu.New()

// Expose expvar endpoint
app.Get("/debug/vars", expvar.Handler())

Examples

Basic Setup

app.Get("/debug/vars", expvar.Handler())

Custom Variables

import "expvar"

// Publish custom variable
var requests = expvar.NewInt("requests")

app.Use(func(next mizu.Handler) mizu.Handler {
    return func(c *mizu.Ctx) error {
        requests.Add(1)
        return next(c)
    }
})

app.Get("/debug/vars", expvar.Handler())

With Auth Protection

debug := app.Group("/debug")
debug.Use(basicauth.New(adminCredentials))
debug.Get("/vars", expvar.Handler())

Default Variables

VariableDescription
cmdlineCommand line arguments
memstatsMemory statistics

API Reference

Functions

// Handler returns expvar HTTP handler
func Handler() mizu.Handler

Response Format

{
    "cmdline": ["./myapp"],
    "memstats": {
        "Alloc": 1234567,
        "TotalAlloc": 9876543,
        "Sys": 12345678,
        ...
    },
    "requests": 42
}

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:
Test CaseDescriptionExpected Behavior
TestNew/expvar_endpointRequest to /debug/varsReturns 200 OK with expvar JSON
TestNew/non-expvar_pathRequest to other pathsPasses through to next handler
TestWithOptions_CustomPathCustom expvar pathReturns expvar data at custom path
TestHelpers/NewIntInteger counter helperCreates and increments counter
TestHelpers/NewFloatFloat gauge helperCreates and sets float value
TestHelpers/NewStringString variable helperCreates and sets string value
TestHelpers/NewMapMap variable helperCreates map with key-value pairs
TestHelpers/GetGet published variableReturns published variable by name
TestHelpers/DoIterate all variablesIterates over all expvars
TestJSONJSON export functionReturns valid JSON object