1 Star 0 Fork 0

sqos/beats

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
snapshot.go 1.71 KB
一键复制 编辑 原始数据 按行查看 历史
package monitoring
import "strings"
// FlatSnapshot represents a flatten snapshot of all metrics.
// Names in the tree will be joined with `.` .
type FlatSnapshot struct {
Bools map[string]bool
Ints map[string]int64
Floats map[string]float64
Strings map[string]string
}
type snapshotVisitor struct {
snapshot FlatSnapshot
level []string
}
// CollectFlatSnapshot collects a flattened snapshot of
// a metrics tree start with the given registry.
func CollectFlatSnapshot(r *Registry, mode Mode, expvar bool) FlatSnapshot {
vs := newSnapshotVisitor()
r.Visit(mode, vs)
if expvar {
VisitExpvars(vs)
}
return vs.snapshot
}
func MakeFlatSnapshot() FlatSnapshot {
return FlatSnapshot{
Bools: map[string]bool{},
Ints: map[string]int64{},
Floats: map[string]float64{},
Strings: map[string]string{},
}
}
func newSnapshotVisitor() *snapshotVisitor {
return &snapshotVisitor{snapshot: MakeFlatSnapshot()}
}
func (vs *snapshotVisitor) OnRegistryStart() {}
func (vs *snapshotVisitor) OnRegistryFinished() {
if len(vs.level) > 0 {
vs.dropName()
}
}
func (vs *snapshotVisitor) OnKey(name string) {
vs.level = append(vs.level, name)
}
func (vs *snapshotVisitor) getName() string {
defer vs.dropName()
if len(vs.level) == 1 {
return vs.level[0]
}
return strings.Join(vs.level, ".")
}
func (vs *snapshotVisitor) dropName() {
vs.level = vs.level[:len(vs.level)-1]
}
func (vs *snapshotVisitor) OnString(s string) {
vs.snapshot.Strings[vs.getName()] = s
}
func (vs *snapshotVisitor) OnBool(b bool) {
vs.snapshot.Bools[vs.getName()] = b
}
func (vs *snapshotVisitor) OnInt(i int64) {
vs.snapshot.Ints[vs.getName()] = i
}
func (vs *snapshotVisitor) OnFloat(f float64) {
vs.snapshot.Floats[vs.getName()] = f
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/sqos/beats.git
git@gitee.com:sqos/beats.git
sqos
beats
beats
v5.6.13

搜索帮助

0d507c66 1850385 C8b1a773 1850385