Obtaining the application’s operational metrics gives us a better understanding of its actual status. By connecting these metrics to monitoring systems such as prometheus, zabbix, etc., the application can be continuously checked and any abnormalities can be alerted and handled in a timely manner.

Pull and Push

There are two ways to interface with monitoring systems, one is Pull and the other is Push.

In the case of Prometheus, for example, the application exposes an HTTP interface for Prometheus to periodically grab metrics through, which is called Pull, while Push is when the application actively pushes metrics to PushGateway.

The Go standard library has a package called expvar, whose name is a combination of exp and var, meaning exported variable.

expvar provides a standardized interface to public variables and exposes them in Json format via HTTP, making it ideal for interfacing with monitoring systems using Pull.

Using the expvar library

expvar is a standard library, meaning we don’t want additional dependencies, and it also provides some out-of-the-box metrics. Let’s learn how to use the library.

When the expvar library is imported (import "expvar"), the following init functions will be called automatically.

1
2
3
4
5
func init() {
 http.HandleFunc("/debug/vars", expvarHandler)
 Publish("cmdline", Func(cmdline))
 Publish("memstats", Func(memstats))
}

This function registers us with an HTTP service for the /debug/vars path, accessing which will give us the metrics in Json format.

Therefore, we also need to call ListenAndServe to bind the port and start serving HTTP requests.

1
http.ListenAndServe(":8080", nil)

The complete code is as follows.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
package main

import (
 _ "expvar"
 "net/http"
)

func main() {
 http.ListenAndServe(":8080", nil)
}

After running the program up and requesting it via curl, the following results are obtained.

1
2
3
4
5
$ curl localhost:8080/debug/vars
{
"cmdline": ["/var/folders/xk/gn46n46d503dsztbc6_9qb2h0000gn/T/go-build1657217338/b001/exe/main"],
"memstats": {"Alloc":278880,"TotalAlloc":278880,"Sys":8735760,"Lookups":0,"Mallocs":1169,"Frees":87,"HeapAlloc":278880,"HeapSys":3866624,"HeapIdle":2949120,"HeapInuse":917504,"HeapReleased":2899968,"HeapObjects":1082,"StackInuse":327680,"StackSys":327680,"MSpanInuse":28696,"MSpanSys":32640,"MCacheInuse":9600,"MCacheSys":15600,"BuckHashSys":3875,"GCSys":3826448,"OtherSys":662893,"NextGC":4194304,"LastGC":0,"PauseTotalNs":0,"PauseNs":[0,0,0,0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0],"NumGC":0,"NumForcedGC":0,"GCCPUFraction":0,"EnableGC":true,"DebugGC":false,"BySize":[{"Size":0,"Mallocs":0,"Frees":0},{"Size":8,"Mallocs":41,"Frees":0},{"Size":16,"Mallocs":496,"Frees":0},{"Size":24,"Mallocs":63,"Frees":0},{"Size":32,"Mallocs":28,"Frees":0},{"Size":48,"Mallocs":134,"Frees":0},{"Size":64,"Mallocs":50,"Frees":0},{"Size":80,"Mallocs":17,"Frees":0},{"Size":96,"Mallocs":17,"Frees":0},{"Size":112,"Mallocs":6,"Frees":0},{"Size":128,"Mallocs":9,"Frees":0},{"Size":144,"Mallocs":9,"Frees":0},{"Size":160,"Mallocs":18,"Frees":0},{"Size":176,"Mallocs":6,"Frees":0},{"Size":192,"Mallocs":0,"Frees":0},{"Size":208,"Mallocs":37,"Frees":0},{"Size":224,"Mallocs":6,"Frees":0},{"Size":240,"Mallocs":0,"Frees":0},{"Size":256,"Mallocs":12,"Frees":0},{"Size":288,"Mallocs":7,"Frees":0},{"Size":320,"Mallocs":2,"Frees":0},{"Size":352,"Mallocs":13,"Frees":0},{"Size":384,"Mallocs":1,"Frees":0},{"Size":416,"Mallocs":30,"Frees":0},{"Size":448,"Mallocs":1,"Frees":0},{"Size":480,"Mallocs":2,"Frees":0},{"Size":512,"Mallocs":0,"Frees":0},{"Size":576,"Mallocs":5,"Frees":0},{"Size":640,"Mallocs":5,"Frees":0},{"Size":704,"Mallocs":3,"Frees":0},{"Size":768,"Mallocs":0,"Frees":0},{"Size":896,"Mallocs":6,"Frees":0},{"Size":1024,"Mallocs":8,"Frees":0},{"Size":1152,"Mallocs":10,"Frees":0},{"Size":1280,"Mallocs":3,"Frees":0},{"Size":1408,"Mallocs":1,"Frees":0},{"Size":1536,"Mallocs":0,"Frees":0},{"Size":1792,"Mallocs":7,"Frees":0},{"Size":2048,"Mallocs":2,"Frees":0},{"Size":2304,"Mallocs":3,"Frees":0},{"Size":2688,"Mallocs":2,"Frees":0},{"Size":3072,"Mallocs":0,"Frees":0},{"Size":3200,"Mallocs":0,"Frees":0},{"Size":3456,"Mallocs":0,"Frees":0},{"Size":4096,"Mallocs":8,"Frees":0},{"Size":4864,"Mallocs":1,"Frees":0},{"Size":5376,"Mallocs":1,"Frees":0},{"Size":6144,"Mallocs":2,"Frees":0},{"Size":6528,"Mallocs":0,"Frees":0},{"Size":6784,"Mallocs":0,"Frees":0},{"Size":6912,"Mallocs":0,"Frees":0},{"Size":8192,"Mallocs":2,"Frees":0},{"Size":9472,"Mallocs":8,"Frees":0},{"Size":9728,"Mallocs":0,"Frees":0},{"Size":10240,"Mallocs":0,"Frees":0},{"Size":10880,"Mallocs":0,"Frees":0},{"Size":12288,"Mallocs":0,"Frees":0},{"Size":13568,"Mallocs":0,"Frees":0},{"Size":14336,"Mallocs":0,"Frees":0},{"Size":16384,"Mallocs":0,"Frees":0},{"Size":18432,"Mallocs":0,"Frees":0}]}
}

As you can see, expvar already provides two indicators by default, namely program execution commands (os.Args) and runtime memory allocation (runtime.Memstats) information.

Highlights of the expvar library

The most important things in expvar are the Publish function and the Var interface.

1
func Publish(name string, v Var) {}

The Publish function takes two arguments in its signature, name is the name of the indicator we specify. For example, in the Publish("cmdline", Func(cmdline)) code line under the init function of expvar above, where cmdline is the indicator name and Func(cmdline) is an expvar.Func variable that implements the Var interface.

Var interface, which defines only a String method. Note that the method must return a valid Json string.

1
2
3
4
5
6
7
// Var is an abstract type for all exported variables.
type Var interface {
 // String returns a valid JSON value for the variable.
 // Types with String methods that do not return valid JSON
 // (such as time.Time) must not be used as a Var.
 String() string
}

For ease of use, five exported variable types are provided in the expvar library, all of which implement the Var interface.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
type Int struct {
 i int64
}

type Float struct {
 f uint64
}

type Map struct {
 m      sync.Map // map[string]Var
 keysMu sync.RWMutex
 keys   []string // sorted
}

type String struct {
 s atomic.Value // string
}

type Func func() any

We can create the first four types of variables and register the indicators by calling the expvar.NewXXX function respectively.

1
2
3
4
intVar = expvar.NewInt("metricName")
floatVar = expvar.NewFloat("metricName")
mapVar = expvar.NewMap("metricName")
stringVar = expvar.NewString("metricName")

For example, the expvar.NewInt function calls the Publish method internally to bind the indicator name to a variable of type expvar.Int.

1
2
3
4
5
func NewInt(name string) *Int {
 v := new(Int)
 Publish(name, v)
 return v
}

And the expvar.Func type is actually designed to allow us to customize the export type.

For example, let’s say we want to expose the following defined structure.

1
2
3
4
5
type MyStruct struct {
 Field1 string
 Field2 int
 Field3 float64
}

First you need to create a data generation function. It is used to export the data in this function each time the HTTP service path is called.

1
2
3
4
5
6
7
func MyStructData() interface{} {
 return MyStruct{
  Field1: "Gopher",
  Field2: 22,
  Field3: 19.99,
 }
}

Finally, just register the indicator name with the Publish function.

1
expvar.Publish("metricName", expvar.Func(MyStructData))

Complete Example

Below, we give a complete example covering the five exported variable types.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main

import (
 "expvar"
 "github.com/shirou/gopsutil/v3/host"
 "github.com/shirou/gopsutil/v3/load"
 "github.com/shirou/gopsutil/v3/mem"
 "net/http"
 "time"
)

type Load struct {
 Load1  float64
 Load5  float64
 Load15 float64
}

func AllLoadAvg() interface{} {
 return Load{
  Load1:  LoadAvg(1),
  Load5:  LoadAvg(5),
  Load15: LoadAvg(15),
 }
}

func LoadAvg(loadNumber int) float64 {
 avg, _ := load.Avg()
 switch loadNumber {
 case 5:
  return avg.Load5
 case 15:
  return avg.Load15
 default:
  return avg.Load1
 }
}

func main() {
 var (
  aliveOfSeconds = expvar.NewInt("aliveOfSeconds")
  hostID         = expvar.NewString("hostID")
  lastLoad       = expvar.NewFloat("lastLoad")
  virtualMemory  = expvar.NewMap("virtualMemory")
 )
 expvar.Publish("allLoadAvg", expvar.Func(AllLoadAvg))
 h, _ := host.HostID()
 hostID.Set(h)

 go http.ListenAndServe(":8080", nil)

 for {
  aliveOfSeconds.Add(1)
  vm, _ := mem.VirtualMemory()
  lastLoad.Set(LoadAvg(1))
  virtualMemory.Add("active", int64(vm.Active))
  virtualMemory.Add("buffer", int64(vm.Buffers))
  time.Sleep(1 * time.Second)
 }
}

In the above example, we get some system information through the gopsutil library and show how to export this information through the various variable types in expvar.

Using curl to request localhost:8080/debug/vars, the result is as follows.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
$ curl localhost:8080/debug/vars
{
"aliveOfSeconds": 1,
"allLoadAvg": {"Load1":1.69580078125,"Load5":1.97412109375,"Load15":1.90283203125},
"cmdline": ["/var/folders/xk/gn46n46d503dsztbc6_9qb2h0000gn/T/go-build3566019824/b001/exe/main"],
"hostID": "7a1a74f2-30fc-5bc1-b439-6b7aef22e58d",
"lastLoad": 1.69580078125,
"memstats": {"Alloc":256208,"TotalAlloc":256208,"Sys":8735760,"Lookups":0,"Mallocs":1089,"Frees":48,"HeapAlloc":256208,"HeapSys":3866624,"HeapIdle":2891776,"HeapInuse":974848,"HeapReleased":2859008,"HeapObjects":1041,"StackInuse":327680,"StackSys":327680,"MSpanInuse":19992,"MSpanSys":32640,"MCacheInuse":9600,"MCacheSys":15600,"BuckHashSys":3905,"GCSys":3851120,"OtherSys":638191,"NextGC":4194304,"LastGC":0,"PauseTotalNs":0,"PauseNs":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"PauseEnd":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"NumGC":0,"NumForcedGC":0,"GCCPUFraction":0,"EnableGC":true,"DebugGC":false,"BySize":[{"Size":0,"Mallocs":0,"Frees":0},{"Size":8,"Mallocs":35,"Frees":0},{"Size":16,"Mallocs":415,"Frees":0},{"Size":24,"Mallocs":71,"Frees":0},{"Size":32,"Mallocs":37,"Frees":0},{"Size":48,"Mallocs":141,"Frees":0},{"Size":64,"Mallocs":52,"Frees":0},{"Size":80,"Mallocs":20,"Frees":0},{"Size":96,"Mallocs":23,"Frees":0},{"Size":112,"Mallocs":14,"Frees":0},{"Size":128,"Mallocs":7,"Frees":0},{"Size":144,"Mallocs":7,"Frees":0},{"Size":160,"Mallocs":18,"Frees":0},{"Size":176,"Mallocs":6,"Frees":0},{"Size":192,"Mallocs":1,"Frees":0},{"Size":208,"Mallocs":42,"Frees":0},{"Size":224,"Mallocs":3,"Frees":0},{"Size":240,"Mallocs":0,"Frees":0},{"Size":256,"Mallocs":9,"Frees":0},{"Size":288,"Mallocs":8,"Frees":0},{"Size":320,"Mallocs":5,"Frees":0},{"Size":352,"Mallocs":13,"Frees":0},{"Size":384,"Mallocs":3,"Frees":0},{"Size":416,"Mallocs":33,"Frees":0},{"Size":448,"Mallocs":0,"Frees":0},{"Size":480,"Mallocs":2,"Frees":0},{"Size":512,"Mallocs":1,"Frees":0},{"Size":576,"Mallocs":4,"Frees":0},{"Size":640,"Mallocs":8,"Frees":0},{"Size":704,"Mallocs":3,"Frees":0},{"Size":768,"Mallocs":1,"Frees":0},{"Size":896,"Mallocs":6,"Frees":0},{"Size":1024,"Mallocs":8,"Frees":0},{"Size":1152,"Mallocs":9,"Frees":0},{"Size":1280,"Mallocs":3,"Frees":0},{"Size":1408,"Mallocs":1,"Frees":0},{"Size":1536,"Mallocs":1,"Frees":0},{"Size":1792,"Mallocs":9,"Frees":0},{"Size":2048,"Mallocs":1,"Frees":0},{"Size":2304,"Mallocs":2,"Frees":0},{"Size":2688,"Mallocs":2,"Frees":0},{"Size":3072,"Mallocs":0,"Frees":0},{"Size":3200,"Mallocs":1,"Frees":0},{"Size":3456,"Mallocs":0,"Frees":0},{"Size":4096,"Mallocs":5,"Frees":0},{"Size":4864,"Mallocs":0,"Frees":0},{"Size":5376,"Mallocs":1,"Frees":0},{"Size":6144,"Mallocs":1,"Frees":0},{"Size":6528,"Mallocs":0,"Frees":0},{"Size":6784,"Mallocs":0,"Frees":0},{"Size":6912,"Mallocs":0,"Frees":0},{"Size":8192,"Mallocs":1,"Frees":0},{"Size":9472,"Mallocs":8,"Frees":0},{"Size":9728,"Mallocs":0,"Frees":0},{"Size":10240,"Mallocs":0,"Frees":0},{"Size":10880,"Mallocs":0,"Frees":0},{"Size":12288,"Mallocs":0,"Frees":0},{"Size":13568,"Mallocs":0,"Frees":0},{"Size":14336,"Mallocs":0,"Frees":0},{"Size":16384,"Mallocs":0,"Frees":0},{"Size":18432,"Mallocs":0,"Frees":0}]},
"virtualMemory": {"active": 1957449728, "buffer": 0}
}

Summary

The standard library expvar provides a standardized interface to the public variables that need to be exported and is relatively simple to use.

Several base types defined inside the expvar package are given concurrency-safe methods of operation. We don’t need to implement them all over again, they are ready to use out of the box.

However, the number of imports of this library is less than 10,000 for the public projects counted on https://go.dev/.

expvar