Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Snapshot expvar maps for periodic logging #1969

Merged
merged 1 commit into from
Jul 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions libbeat/logp/logp.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,28 @@ func getLogLevel(config *Logging) (Priority, error) {
return level, nil
}

// snapshotExpvars iterates through all the defined expvars, and for the top
// level vars that are integers it snapshots the name and value in a separate
// map.
// snapshotMap recursively walks expvar Maps and records their integer expvars
// in a separate flat map.
func snapshotMap(varsMap map[string]int64, path string, mp *expvar.Map) {
mp.Do(func(kv expvar.KeyValue) {
switch kv.Value.(type) {
case *expvar.Int:
varsMap[path+"."+kv.Key], _ = strconv.ParseInt(kv.Value.String(), 10, 64)
case *expvar.Map:
snapshotMap(varsMap, path+"."+kv.Key, kv.Value.(*expvar.Map))
}
})
}

// snapshotExpvars iterates through all the defined expvars, and for the vars
// that are integers it snapshots the name and value in a separate (flat) map.
func snapshotExpvars(varsMap map[string]int64) {
expvar.Do(func(kv expvar.KeyValue) {
switch kv.Value.(type) {
case *expvar.Int:
varsMap[kv.Key], _ = strconv.ParseInt(kv.Value.String(), 10, 64)
case *expvar.Map:
snapshotMap(varsMap, kv.Key, kv.Value.(*expvar.Map))
}
})
}
Expand Down
15 changes: 15 additions & 0 deletions libbeat/logp/logp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@ func TestSnapshotExpvars(t *testing.T) {
assert.Equal(t, vals["test"], int64(42))
}

func TestSnapshotExpvarsMap(t *testing.T) {
test := expvar.NewMap("testMap")
test.Add("hello", 42)

map2 := new(expvar.Map).Init()
map2.Add("test", 5)
test.Set("map2", map2)

vals := map[string]int64{}
snapshotExpvars(vals)

assert.Equal(t, vals["testMap.hello"], int64(42))
assert.Equal(t, vals["testMap.map2.test"], int64(5))
}

func TestBuildMetricsOutput(t *testing.T) {
test := expvar.NewInt("testLog")
test.Add(1)
Expand Down