forked from HdrHistogram/hdrhistogram-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_reader.go
209 lines (182 loc) · 5.55 KB
/
log_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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package hdrhistogram
import (
"bufio"
"io"
"math"
"regexp"
"strconv"
"strings"
)
type HistogramLogReader struct {
log *bufio.Reader
startTimeSec float64
observedStartTime bool
baseTimeSec float64
observedBaseTime bool
// scanner handling state
absolute bool
rangeStartTimeSec float64
rangeEndTimeSec float64
observedMax bool
rangeObservedMax int64
observedMin bool
rangeObservedMin int64
reStartTime *regexp.Regexp
reBaseTime *regexp.Regexp
reHistogramInterval *regexp.Regexp
}
func (hlr *HistogramLogReader) ObservedMin() bool {
return hlr.observedMin
}
func (hlr *HistogramLogReader) ObservedMax() bool {
return hlr.observedMax
}
// Returns the overall observed max limit ( up to the current point ) of the read histograms
func (hlr *HistogramLogReader) RangeObservedMax() int64 {
return hlr.rangeObservedMax
}
// Returns the overall observed min limit ( up to the current point ) of the read histograms
func (hlr *HistogramLogReader) RangeObservedMin() int64 {
return hlr.rangeObservedMin
}
func NewHistogramLogReader(log io.Reader) *HistogramLogReader {
//# "#[StartTime: %f (seconds since epoch), %s]\n"
reStartTime, _ := regexp.Compile(`#\[StartTime: ([\d\.]*)`)
//# "#[BaseTime: %f (seconds since epoch)]\n"
reBaseTime, _ := regexp.Compile(`#\[BaseTime: ([\d\.]*)`)
//# 0.127,1.007,2.769,HISTFAAAAEV42pNpmSz...
//# Tag=A,0.127,1.007,2.769,HISTFAAAAEV42pNpmSz
//# "%f,%f,%f,%s\n"
reHistogramInterval, _ := regexp.Compile(`([\d\.]*),([\d\.]*),([\d\.]*),(.*)`)
//
reader := bufio.NewReader(log)
return &HistogramLogReader{log: reader,
startTimeSec: 0.0,
observedStartTime: false,
baseTimeSec: 0.0,
observedBaseTime: false,
reStartTime: reStartTime,
reBaseTime: reBaseTime,
reHistogramInterval: reHistogramInterval,
rangeObservedMin: math.MaxInt64,
observedMin: false,
rangeObservedMax: math.MinInt64,
observedMax: false,
}
}
func (hlr *HistogramLogReader) NextIntervalHistogram() (histogram *Histogram, err error) {
return hlr.NextIntervalHistogramWithRange(0.0, math.MaxFloat64, true)
}
func (hlr *HistogramLogReader) NextIntervalHistogramWithRange(rangeStartTimeSec, rangeEndTimeSec float64, absolute bool) (histogram *Histogram, err error) {
hlr.rangeStartTimeSec = rangeStartTimeSec
hlr.rangeEndTimeSec = rangeEndTimeSec
hlr.absolute = absolute
return hlr.decodeNextIntervalHistogram()
}
func (hlr *HistogramLogReader) decodeNextIntervalHistogram() (histogram *Histogram, err error) {
var line string
var tag string = ""
var logTimeStampInSec float64
var intervalLengthSec float64
for {
line, err = hlr.log.ReadString('\n')
if err != nil {
if err == io.EOF {
err = nil
break
}
break
}
if line[0] == '#' {
matchRes := hlr.reStartTime.FindStringSubmatch(line)
if len(matchRes) > 0 {
hlr.startTimeSec, err = strconv.ParseFloat(matchRes[1], 64)
if err != nil {
return
}
hlr.observedStartTime = true
continue
}
matchRes = hlr.reBaseTime.FindStringSubmatch(line)
if len(matchRes) > 0 {
hlr.baseTimeSec, err = strconv.ParseFloat(matchRes[1], 64)
if err != nil {
return
}
hlr.observedBaseTime = true
continue
}
continue
}
if strings.HasPrefix(line, "Tag=") {
commaPos := strings.Index(line, ",")
tag = line[4:commaPos]
line = line[commaPos+1:]
}
matchRes := hlr.reHistogramInterval.FindStringSubmatch(line)
if len(matchRes) >= 1 {
// Decode: startTimestamp, intervalLength, maxTime, histogramPayload
// Timestamp is expected to be in seconds
logTimeStampInSec, err = strconv.ParseFloat(matchRes[1], 64)
if err != nil {
return
}
intervalLengthSec, err = strconv.ParseFloat(matchRes[2], 64)
if err != nil {
return
}
cpayload := matchRes[4]
// No explicit start time noted. Use 1st observed time:
if !hlr.observedStartTime {
hlr.startTimeSec = logTimeStampInSec
hlr.observedStartTime = true
}
// No explicit base time noted.
// Deduce from 1st observed time (compared to start time):
if !hlr.observedBaseTime {
// Criteria Note: if log timestamp is more than a year in
// the past (compared to StartTime),
// we assume that timestamps in the log are not absolute
if logTimeStampInSec < (hlr.startTimeSec - (365 * 24 * 3600.0)) {
hlr.baseTimeSec = hlr.startTimeSec
} else {
hlr.baseTimeSec = 0.0
}
hlr.observedBaseTime = true
}
absoluteStartTimeStampSec := logTimeStampInSec + hlr.baseTimeSec
offsetStartTimeStampSec := absoluteStartTimeStampSec + hlr.startTimeSec
// Timestamp length is expect to be in seconds
absoluteEndTimeStampSec := absoluteStartTimeStampSec + intervalLengthSec
var startTimeStampToCheckRangeOn float64
if hlr.absolute {
startTimeStampToCheckRangeOn = absoluteStartTimeStampSec
} else {
startTimeStampToCheckRangeOn = offsetStartTimeStampSec
}
if startTimeStampToCheckRangeOn < hlr.rangeStartTimeSec {
continue
}
if startTimeStampToCheckRangeOn > hlr.rangeEndTimeSec {
return
}
histogram, err = Decode([]byte(cpayload))
if err != nil {
return
}
if histogram.Max() > hlr.rangeObservedMax {
hlr.rangeObservedMax = histogram.Max()
}
if histogram.Min() < hlr.rangeObservedMin {
hlr.rangeObservedMin = histogram.Min()
}
histogram.SetStartTimeMs(int64(absoluteStartTimeStampSec * 1000.0))
histogram.SetEndTimeMs(int64(absoluteEndTimeStampSec * 1000.0))
if tag != "" {
histogram.SetTag(tag)
}
return
}
}
return
}