1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // The expvar package provides a standardized interface to public variables,
6 // such as operation counters in servers. It exposes these variables via
7 // HTTP at /debug/vars in JSON format.
9 // Operations to set or modify these public variables are atomic.
11 // In addition to adding the HTTP handler, this package registers the
12 // following variables:
15 // memstats runtime.Memstats
17 // The package is sometimes only imported for the side effect of
18 // registering its HTTP handler and the above variables. To use it
19 // this way, simply link this package into your program:
36 // Var is an abstract type for all exported variables.
41 // Int is a 64-bit integer variable, and satisfies the Var interface.
47 func (v
*Int
) String() string { return strconv
.Itoa64(v
.i
) }
49 func (v
*Int
) Add(delta
int64) {
55 func (v
*Int
) Set(value
int64) {
61 // Map is a string-to-Var map variable, and satisfies the Var interface.
67 // KeyValue represents a single entry in a Map.
68 type KeyValue
struct {
73 func (v
*Map
) String() string {
76 b
:= new(bytes
.Buffer
)
79 for key
, val
:= range v
.m
{
83 fmt
.Fprintf(b
, "\"%s\": %v", key
, val
.String())
90 func (v
*Map
) Init() *Map
{
91 v
.m
= make(map[string]Var
)
95 func (v
*Map
) Get(key
string) Var
{
101 func (v
*Map
) Set(key
string, av Var
) {
107 func (v
*Map
) Add(key
string, delta
int64) {
116 // Add to Int; ignore otherwise.
117 if iv
, ok
:= av
.(*Int
); ok
{
122 // TODO(rsc): Make sure map access in separate thread is safe.
123 func (v
*Map
) iterate(c
chan<- KeyValue
) {
124 for k
, v
:= range v
.m
{
130 func (v
*Map
) Iter() <-chan KeyValue
{
131 c
:= make(chan KeyValue
)
136 // String is a string variable, and satisfies the Var interface.
141 func (v
*String
) String() string { return strconv
.Quote(v
.s
) }
143 func (v
*String
) Set(value
string) { v
.s
= value
}
145 // IntFunc wraps a func() int64 to create a value that satisfies the Var interface.
146 // The function will be called each time the Var is evaluated.
147 type IntFunc
func() int64
149 func (v IntFunc
) String() string { return strconv
.Itoa64(v()) }
151 // StringFunc wraps a func() string to create value that satisfies the Var interface.
152 // The function will be called each time the Var is evaluated.
153 type StringFunc
func() string
155 func (f StringFunc
) String() string { return f() }
158 // All published variables.
159 var vars
map[string]Var
= make(map[string]Var
)
162 // Publish declares an named exported variable. This should be called from a
163 // package's init function when it creates its Vars. If the name is already
164 // registered then this will log.Panic.
165 func Publish(name
string, v Var
) {
168 if _
, existing
:= vars
[name
]; existing
{
169 log
.Panicln("Reuse of exported var name:", name
)
174 // Get retrieves a named exported variable.
175 func Get(name
string) Var
{
179 // RemoveAll removes all exported variables.
180 // This is for tests; don't call this on a real server.
184 vars
= make(map[string]Var
)
187 // Convenience functions for creating new exported variables.
189 func NewInt(name
string) *Int
{
195 func NewMap(name
string) *Map
{
201 func NewString(name
string) *String
{
207 // TODO(rsc): Make sure map access in separate thread is safe.
208 func iterate(c
chan<- KeyValue
) {
209 for k
, v
:= range vars
{
215 func Iter() <-chan KeyValue
{
216 c
:= make(chan KeyValue
)
221 func expvarHandler(w http
.ResponseWriter
, r
*http
.Request
) {
222 w
.SetHeader("content-type", "application/json; charset=utf-8")
223 fmt
.Fprintf(w
, "{\n")
225 for name
, value
:= range vars
{
227 fmt
.Fprintf(w
, ",\n")
230 fmt
.Fprintf(w
, "%q: %s", name
, value
)
232 fmt
.Fprintf(w
, "\n}\n")
235 func memstats() string {
236 b
, _
:= json
.MarshalIndent(&runtime
.MemStats
, "", "\t")
240 func cmdline() string {
241 b
, _
:= json
.Marshal(os
.Args
)
246 http
.Handle("/debug/vars", http
.HandlerFunc(expvarHandler
))
247 Publish("cmdline", StringFunc(cmdline
))
248 Publish("memstats", StringFunc(memstats
))