Skip to content

Commit 51c169d

Browse files
tsgandrewkroh
authored andcommitted
Snapshot expvar maps for periodic logging (#1969)
Part of #1931, this adds logic to descend into maps. This is useful especially to support the metricbeat fetches stats. Locking should be safe as the expvar.Map Do function takes a read lock on the map. Note that expvars of type Func are still not included, which is why the memstats are not logged.
1 parent 4ae89f2 commit 51c169d

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

libbeat/logp/logp.go

+17-3
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,28 @@ func getLogLevel(config *Logging) (Priority, error) {
157157
return level, nil
158158
}
159159

160-
// snapshotExpvars iterates through all the defined expvars, and for the top
161-
// level vars that are integers it snapshots the name and value in a separate
162-
// map.
160+
// snapshotMap recursively walks expvar Maps and records their integer expvars
161+
// in a separate flat map.
162+
func snapshotMap(varsMap map[string]int64, path string, mp *expvar.Map) {
163+
mp.Do(func(kv expvar.KeyValue) {
164+
switch kv.Value.(type) {
165+
case *expvar.Int:
166+
varsMap[path+"."+kv.Key], _ = strconv.ParseInt(kv.Value.String(), 10, 64)
167+
case *expvar.Map:
168+
snapshotMap(varsMap, path+"."+kv.Key, kv.Value.(*expvar.Map))
169+
}
170+
})
171+
}
172+
173+
// snapshotExpvars iterates through all the defined expvars, and for the vars
174+
// that are integers it snapshots the name and value in a separate (flat) map.
163175
func snapshotExpvars(varsMap map[string]int64) {
164176
expvar.Do(func(kv expvar.KeyValue) {
165177
switch kv.Value.(type) {
166178
case *expvar.Int:
167179
varsMap[kv.Key], _ = strconv.ParseInt(kv.Value.String(), 10, 64)
180+
case *expvar.Map:
181+
snapshotMap(varsMap, kv.Key, kv.Value.(*expvar.Map))
168182
}
169183
})
170184
}

libbeat/logp/logp_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,21 @@ func TestSnapshotExpvars(t *testing.T) {
1919
assert.Equal(t, vals["test"], int64(42))
2020
}
2121

22+
func TestSnapshotExpvarsMap(t *testing.T) {
23+
test := expvar.NewMap("testMap")
24+
test.Add("hello", 42)
25+
26+
map2 := new(expvar.Map).Init()
27+
map2.Add("test", 5)
28+
test.Set("map2", map2)
29+
30+
vals := map[string]int64{}
31+
snapshotExpvars(vals)
32+
33+
assert.Equal(t, vals["testMap.hello"], int64(42))
34+
assert.Equal(t, vals["testMap.map2.test"], int64(5))
35+
}
36+
2237
func TestBuildMetricsOutput(t *testing.T) {
2338
test := expvar.NewInt("testLog")
2439
test.Add(1)

0 commit comments

Comments
 (0)