2010-12-03 05:34:57 +01:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2011-05-20 02:18:15 +02:00
|
|
|
// Package expvar provides a standardized interface to public variables, such
|
|
|
|
// as operation counters in servers. It exposes these variables via HTTP at
|
|
|
|
// /debug/vars in JSON format.
|
2010-12-03 05:34:57 +01:00
|
|
|
//
|
|
|
|
// Operations to set or modify these public variables are atomic.
|
|
|
|
//
|
|
|
|
// In addition to adding the HTTP handler, this package registers the
|
|
|
|
// following variables:
|
|
|
|
//
|
|
|
|
// cmdline os.Args
|
|
|
|
// memstats runtime.Memstats
|
|
|
|
//
|
|
|
|
// The package is sometimes only imported for the side effect of
|
|
|
|
// registering its HTTP handler and the above variables. To use it
|
2012-02-09 09:19:58 +01:00
|
|
|
// this way, link this package into your program:
|
2010-12-03 05:34:57 +01:00
|
|
|
// import _ "expvar"
|
|
|
|
//
|
|
|
|
package expvar
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2011-12-07 02:11:29 +01:00
|
|
|
"encoding/json"
|
2010-12-03 05:34:57 +01:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2011-12-07 02:11:29 +01:00
|
|
|
"net/http"
|
2010-12-03 05:34:57 +01:00
|
|
|
"os"
|
|
|
|
"runtime"
|
2014-06-07 00:37:27 +02:00
|
|
|
"sort"
|
2010-12-03 05:34:57 +01:00
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Var is an abstract type for all exported variables.
|
|
|
|
type Var interface {
|
|
|
|
String() string
|
|
|
|
}
|
|
|
|
|
2011-01-21 19:19:03 +01:00
|
|
|
// Int is a 64-bit integer variable that satisfies the Var interface.
|
2010-12-03 05:34:57 +01:00
|
|
|
type Int struct {
|
2012-03-30 23:27:11 +02:00
|
|
|
mu sync.RWMutex
|
2014-06-07 00:37:27 +02:00
|
|
|
i int64
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2012-03-30 23:27:11 +02:00
|
|
|
func (v *Int) String() string {
|
|
|
|
v.mu.RLock()
|
|
|
|
defer v.mu.RUnlock()
|
|
|
|
return strconv.FormatInt(v.i, 10)
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
func (v *Int) Add(delta int64) {
|
|
|
|
v.mu.Lock()
|
|
|
|
defer v.mu.Unlock()
|
|
|
|
v.i += delta
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Int) Set(value int64) {
|
|
|
|
v.mu.Lock()
|
|
|
|
defer v.mu.Unlock()
|
|
|
|
v.i = value
|
|
|
|
}
|
|
|
|
|
2011-01-21 19:19:03 +01:00
|
|
|
// Float is a 64-bit float variable that satisfies the Var interface.
|
|
|
|
type Float struct {
|
2012-03-30 23:27:11 +02:00
|
|
|
mu sync.RWMutex
|
2014-06-07 00:37:27 +02:00
|
|
|
f float64
|
2011-01-21 19:19:03 +01:00
|
|
|
}
|
|
|
|
|
2012-03-30 23:27:11 +02:00
|
|
|
func (v *Float) String() string {
|
|
|
|
v.mu.RLock()
|
|
|
|
defer v.mu.RUnlock()
|
|
|
|
return strconv.FormatFloat(v.f, 'g', -1, 64)
|
|
|
|
}
|
2011-01-21 19:19:03 +01:00
|
|
|
|
|
|
|
// Add adds delta to v.
|
|
|
|
func (v *Float) Add(delta float64) {
|
|
|
|
v.mu.Lock()
|
|
|
|
defer v.mu.Unlock()
|
|
|
|
v.f += delta
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set sets v to value.
|
|
|
|
func (v *Float) Set(value float64) {
|
|
|
|
v.mu.Lock()
|
|
|
|
defer v.mu.Unlock()
|
|
|
|
v.f = value
|
|
|
|
}
|
|
|
|
|
|
|
|
// Map is a string-to-Var map variable that satisfies the Var interface.
|
2010-12-03 05:34:57 +01:00
|
|
|
type Map struct {
|
2014-06-07 00:37:27 +02:00
|
|
|
mu sync.RWMutex
|
|
|
|
m map[string]Var
|
|
|
|
keys []string // sorted
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// KeyValue represents a single entry in a Map.
|
|
|
|
type KeyValue struct {
|
|
|
|
Key string
|
|
|
|
Value Var
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Map) String() string {
|
2012-02-09 09:19:58 +01:00
|
|
|
v.mu.RLock()
|
|
|
|
defer v.mu.RUnlock()
|
2012-03-30 23:27:11 +02:00
|
|
|
var b bytes.Buffer
|
|
|
|
fmt.Fprintf(&b, "{")
|
2010-12-03 05:34:57 +01:00
|
|
|
first := true
|
2014-07-19 10:53:52 +02:00
|
|
|
v.doLocked(func(kv KeyValue) {
|
2010-12-03 05:34:57 +01:00
|
|
|
if !first {
|
2012-03-30 23:27:11 +02:00
|
|
|
fmt.Fprintf(&b, ", ")
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2014-07-19 10:53:52 +02:00
|
|
|
fmt.Fprintf(&b, "%q: %v", kv.Key, kv.Value)
|
2010-12-03 05:34:57 +01:00
|
|
|
first = false
|
2014-06-07 00:37:27 +02:00
|
|
|
})
|
2012-03-30 23:27:11 +02:00
|
|
|
fmt.Fprintf(&b, "}")
|
2010-12-03 05:34:57 +01:00
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Map) Init() *Map {
|
|
|
|
v.m = make(map[string]Var)
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2014-06-07 00:37:27 +02:00
|
|
|
// updateKeys updates the sorted list of keys in v.keys.
|
|
|
|
// must be called with v.mu held.
|
|
|
|
func (v *Map) updateKeys() {
|
|
|
|
if len(v.m) == len(v.keys) {
|
|
|
|
// No new key.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
v.keys = v.keys[:0]
|
|
|
|
for k := range v.m {
|
|
|
|
v.keys = append(v.keys, k)
|
|
|
|
}
|
|
|
|
sort.Strings(v.keys)
|
|
|
|
}
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
func (v *Map) Get(key string) Var {
|
2012-02-09 09:19:58 +01:00
|
|
|
v.mu.RLock()
|
|
|
|
defer v.mu.RUnlock()
|
2010-12-03 05:34:57 +01:00
|
|
|
return v.m[key]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Map) Set(key string, av Var) {
|
|
|
|
v.mu.Lock()
|
|
|
|
defer v.mu.Unlock()
|
|
|
|
v.m[key] = av
|
2014-06-07 00:37:27 +02:00
|
|
|
v.updateKeys()
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Map) Add(key string, delta int64) {
|
2012-02-09 09:19:58 +01:00
|
|
|
v.mu.RLock()
|
2010-12-03 05:34:57 +01:00
|
|
|
av, ok := v.m[key]
|
2012-02-09 09:19:58 +01:00
|
|
|
v.mu.RUnlock()
|
2010-12-03 05:34:57 +01:00
|
|
|
if !ok {
|
2012-02-09 09:19:58 +01:00
|
|
|
// check again under the write lock
|
|
|
|
v.mu.Lock()
|
2014-06-07 00:37:27 +02:00
|
|
|
av, ok = v.m[key]
|
|
|
|
if !ok {
|
2012-02-09 09:19:58 +01:00
|
|
|
av = new(Int)
|
|
|
|
v.m[key] = av
|
2014-06-07 00:37:27 +02:00
|
|
|
v.updateKeys()
|
2012-02-09 09:19:58 +01:00
|
|
|
}
|
|
|
|
v.mu.Unlock()
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add to Int; ignore otherwise.
|
|
|
|
if iv, ok := av.(*Int); ok {
|
|
|
|
iv.Add(delta)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-21 19:19:03 +01:00
|
|
|
// AddFloat adds delta to the *Float value stored under the given map key.
|
|
|
|
func (v *Map) AddFloat(key string, delta float64) {
|
2012-02-09 09:19:58 +01:00
|
|
|
v.mu.RLock()
|
2011-01-21 19:19:03 +01:00
|
|
|
av, ok := v.m[key]
|
2012-02-09 09:19:58 +01:00
|
|
|
v.mu.RUnlock()
|
2011-01-21 19:19:03 +01:00
|
|
|
if !ok {
|
2012-02-09 09:19:58 +01:00
|
|
|
// check again under the write lock
|
|
|
|
v.mu.Lock()
|
2014-06-07 00:37:27 +02:00
|
|
|
av, ok = v.m[key]
|
|
|
|
if !ok {
|
2012-02-09 09:19:58 +01:00
|
|
|
av = new(Float)
|
|
|
|
v.m[key] = av
|
2014-06-07 00:37:27 +02:00
|
|
|
v.updateKeys()
|
2012-02-09 09:19:58 +01:00
|
|
|
}
|
|
|
|
v.mu.Unlock()
|
2011-01-21 19:19:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add to Float; ignore otherwise.
|
|
|
|
if iv, ok := av.(*Float); ok {
|
|
|
|
iv.Add(delta)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-09 09:19:58 +01:00
|
|
|
// Do calls f for each entry in the map.
|
|
|
|
// The map is locked during the iteration,
|
|
|
|
// but existing entries may be concurrently updated.
|
|
|
|
func (v *Map) Do(f func(KeyValue)) {
|
|
|
|
v.mu.RLock()
|
|
|
|
defer v.mu.RUnlock()
|
2014-07-19 10:53:52 +02:00
|
|
|
v.doLocked(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
// doLocked calls f for each entry in the map.
|
|
|
|
// v.mu must be held for reads.
|
|
|
|
func (v *Map) doLocked(f func(KeyValue)) {
|
2014-06-07 00:37:27 +02:00
|
|
|
for _, k := range v.keys {
|
|
|
|
f(KeyValue{k, v.m[k]})
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// String is a string variable, and satisfies the Var interface.
|
|
|
|
type String struct {
|
2012-03-30 23:27:11 +02:00
|
|
|
mu sync.RWMutex
|
2014-06-07 00:37:27 +02:00
|
|
|
s string
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2012-03-30 23:27:11 +02:00
|
|
|
func (v *String) String() string {
|
|
|
|
v.mu.RLock()
|
|
|
|
defer v.mu.RUnlock()
|
|
|
|
return strconv.Quote(v.s)
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2012-03-30 23:27:11 +02:00
|
|
|
func (v *String) Set(value string) {
|
|
|
|
v.mu.Lock()
|
|
|
|
defer v.mu.Unlock()
|
|
|
|
v.s = value
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2011-05-20 02:18:15 +02:00
|
|
|
// Func implements Var by calling the function
|
|
|
|
// and formatting the returned value using JSON.
|
|
|
|
type Func func() interface{}
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2011-05-20 02:18:15 +02:00
|
|
|
func (f Func) String() string {
|
|
|
|
v, _ := json.Marshal(f())
|
|
|
|
return string(v)
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
// All published variables.
|
2012-02-09 09:19:58 +01:00
|
|
|
var (
|
2014-06-07 00:37:27 +02:00
|
|
|
mutex sync.RWMutex
|
|
|
|
vars = make(map[string]Var)
|
|
|
|
varKeys []string // sorted
|
2012-02-09 09:19:58 +01:00
|
|
|
)
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2012-01-12 02:31:45 +01:00
|
|
|
// Publish declares a named exported variable. This should be called from a
|
2010-12-03 05:34:57 +01:00
|
|
|
// package's init function when it creates its Vars. If the name is already
|
|
|
|
// registered then this will log.Panic.
|
|
|
|
func Publish(name string, v Var) {
|
|
|
|
mutex.Lock()
|
|
|
|
defer mutex.Unlock()
|
|
|
|
if _, existing := vars[name]; existing {
|
|
|
|
log.Panicln("Reuse of exported var name:", name)
|
|
|
|
}
|
|
|
|
vars[name] = v
|
2014-06-07 00:37:27 +02:00
|
|
|
varKeys = append(varKeys, name)
|
|
|
|
sort.Strings(varKeys)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get retrieves a named exported variable.
|
|
|
|
func Get(name string) Var {
|
2012-02-09 09:19:58 +01:00
|
|
|
mutex.RLock()
|
|
|
|
defer mutex.RUnlock()
|
2010-12-03 05:34:57 +01:00
|
|
|
return vars[name]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convenience functions for creating new exported variables.
|
|
|
|
|
|
|
|
func NewInt(name string) *Int {
|
|
|
|
v := new(Int)
|
|
|
|
Publish(name, v)
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2011-01-21 19:19:03 +01:00
|
|
|
func NewFloat(name string) *Float {
|
|
|
|
v := new(Float)
|
|
|
|
Publish(name, v)
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
func NewMap(name string) *Map {
|
|
|
|
v := new(Map).Init()
|
|
|
|
Publish(name, v)
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewString(name string) *String {
|
|
|
|
v := new(String)
|
|
|
|
Publish(name, v)
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2012-02-09 09:19:58 +01:00
|
|
|
// Do calls f for each exported variable.
|
|
|
|
// The global variable map is locked during the iteration,
|
|
|
|
// but existing entries may be concurrently updated.
|
|
|
|
func Do(f func(KeyValue)) {
|
|
|
|
mutex.RLock()
|
|
|
|
defer mutex.RUnlock()
|
2014-06-07 00:37:27 +02:00
|
|
|
for _, k := range varKeys {
|
|
|
|
f(KeyValue{k, vars[k]})
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func expvarHandler(w http.ResponseWriter, r *http.Request) {
|
2011-03-25 00:46:17 +01:00
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
2010-12-03 05:34:57 +01:00
|
|
|
fmt.Fprintf(w, "{\n")
|
|
|
|
first := true
|
2012-02-09 09:19:58 +01:00
|
|
|
Do(func(kv KeyValue) {
|
2010-12-03 05:34:57 +01:00
|
|
|
if !first {
|
|
|
|
fmt.Fprintf(w, ",\n")
|
|
|
|
}
|
|
|
|
first = false
|
2012-02-09 09:19:58 +01:00
|
|
|
fmt.Fprintf(w, "%q: %s", kv.Key, kv.Value)
|
|
|
|
})
|
2010-12-03 05:34:57 +01:00
|
|
|
fmt.Fprintf(w, "\n}\n")
|
|
|
|
}
|
|
|
|
|
2011-05-20 02:18:15 +02:00
|
|
|
func cmdline() interface{} {
|
|
|
|
return os.Args
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2011-05-20 02:18:15 +02:00
|
|
|
func memstats() interface{} {
|
2012-02-09 09:19:58 +01:00
|
|
|
stats := new(runtime.MemStats)
|
|
|
|
runtime.ReadMemStats(stats)
|
|
|
|
return *stats
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2012-02-09 09:19:58 +01:00
|
|
|
http.HandleFunc("/debug/vars", expvarHandler)
|
2011-05-20 02:18:15 +02:00
|
|
|
Publish("cmdline", Func(cmdline))
|
|
|
|
Publish("memstats", Func(memstats))
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|