cc-metric-store/debug.go

108 lines
2.1 KiB
Go
Raw Permalink Normal View History

2021-09-20 10:29:55 +02:00
package main
import (
"bufio"
"fmt"
2022-03-31 14:17:27 +02:00
"strconv"
2021-09-20 10:29:55 +02:00
)
2022-03-31 14:17:27 +02:00
func (b *buffer) debugDump(buf []byte) []byte {
2021-09-20 10:29:55 +02:00
if b.prev != nil {
2022-03-31 14:17:27 +02:00
buf = b.prev.debugDump(buf)
2021-09-20 10:29:55 +02:00
}
2022-03-31 14:17:27 +02:00
start, len, end := b.start, len(b.data), b.start+b.frequency*int64(len(b.data))
buf = append(buf, `{"start":`...)
buf = strconv.AppendInt(buf, start, 10)
buf = append(buf, `,"len":`...)
buf = strconv.AppendInt(buf, int64(len), 10)
buf = append(buf, `,"end":`...)
buf = strconv.AppendInt(buf, end, 10)
if b.archived {
buf = append(buf, `,"saved":true`...)
}
2021-09-20 10:29:55 +02:00
if b.next != nil {
2022-03-31 14:17:27 +02:00
buf = append(buf, `},`...)
} else {
buf = append(buf, `}`...)
2021-09-20 10:29:55 +02:00
}
2022-03-31 14:17:27 +02:00
return buf
2021-09-20 10:29:55 +02:00
}
2022-03-31 14:17:27 +02:00
func (l *level) debugDump(m *MemoryStore, w *bufio.Writer, lvlname string, buf []byte, depth int) ([]byte, error) {
2021-09-20 10:29:55 +02:00
l.lock.RLock()
defer l.lock.RUnlock()
2022-03-31 14:17:27 +02:00
for i := 0; i < depth; i++ {
buf = append(buf, '\t')
}
buf = append(buf, '"')
buf = append(buf, lvlname...)
buf = append(buf, "\":{\n"...)
depth += 1
objitems := 0
for name, mc := range m.metrics {
if b := l.metrics[mc.offset]; b != nil {
for i := 0; i < depth; i++ {
buf = append(buf, '\t')
}
2021-09-20 10:29:55 +02:00
2022-03-31 14:17:27 +02:00
buf = append(buf, '"')
buf = append(buf, name...)
buf = append(buf, `":[`...)
buf = b.debugDump(buf)
buf = append(buf, "],\n"...)
objitems++
2021-09-20 10:29:55 +02:00
}
}
2022-03-31 14:17:27 +02:00
for name, lvl := range l.children {
_, err := w.Write(buf)
if err != nil {
return nil, err
}
buf = buf[0:0]
buf, err = lvl.debugDump(m, w, name, buf, depth)
if err != nil {
return nil, err
2021-09-20 10:29:55 +02:00
}
2022-03-31 14:17:27 +02:00
buf = append(buf, ',', '\n')
objitems++
}
// remove final `,`:
if objitems > 0 {
buf = append(buf[0:len(buf)-1], '\n')
2021-09-20 10:29:55 +02:00
}
2022-03-31 14:17:27 +02:00
depth -= 1
for i := 0; i < depth; i++ {
buf = append(buf, '\t')
}
buf = append(buf, '}')
return buf, nil
2021-09-20 10:29:55 +02:00
}
2022-03-31 14:17:27 +02:00
func (m *MemoryStore) DebugDump(w *bufio.Writer, selector []string) error {
lvl := m.root.findLevel(selector)
if lvl == nil {
return fmt.Errorf("not found: %#v", selector)
}
buf := make([]byte, 0, 2048)
buf = append(buf, "{"...)
buf, err := lvl.debugDump(m, w, "data", buf, 0)
if err != nil {
return err
}
buf = append(buf, "}\n"...)
if _, err = w.Write(buf); err != nil {
return err
}
2021-09-20 10:29:55 +02:00
return w.Flush()
}