forked from stellar/go
/
logging_change_reader.go
69 lines (57 loc) · 1.38 KB
/
logging_change_reader.go
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
60
61
62
63
64
65
66
67
68
69
package ingest
import (
"runtime"
"github.com/stellar/go/ingest"
)
func bToMb(b uint64) uint64 {
return b / 1024 / 1024
}
func getMemStats() (uint64, uint64) {
var m runtime.MemStats
runtime.ReadMemStats(&m)
return bToMb(m.HeapAlloc), bToMb(m.HeapSys)
}
type loggingChangeReader struct {
ingest.ChangeReader
profile bool
entryCount int
// how often should the logger report
frequency int
source string
sequence uint32
}
func newloggingChangeReader(
reader ingest.ChangeReader,
source string,
sequence uint32,
every int,
profile bool,
) *loggingChangeReader {
return &loggingChangeReader{
ChangeReader: reader,
frequency: every,
profile: profile,
source: source,
sequence: sequence,
}
}
// Read returns a new ledger entry change on each call, returning io.EOF when the stream ends.
func (lcr *loggingChangeReader) Read() (ingest.Change, error) {
change, err := lcr.ChangeReader.Read()
if err == nil {
lcr.entryCount++
if lcr.entryCount%lcr.frequency == 0 {
logger := log.WithField("numEntries", lcr.entryCount).
WithField("source", lcr.source).
WithField("sequence", lcr.sequence)
if lcr.profile {
curHeap, sysHeap := getMemStats()
logger = logger.
WithField("currentHeapSizeMB", curHeap).
WithField("systemHeapSizeMB", sysHeap)
}
logger.Info("Processing ledger entry changes")
}
}
return change, err
}