-
Notifications
You must be signed in to change notification settings - Fork 17
/
http-range.go
120 lines (105 loc) · 2.52 KB
/
http-range.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
package main
import (
"io"
"path/filepath"
"strings"
"time"
"github.com/rpcpool/yellowstone-faithful/metrics"
"k8s.io/klog/v2"
)
type ReaderAtCloser interface {
io.ReaderAt
io.Closer
}
type readCloserWrapper struct {
rac ReaderAtCloser
isRemote bool
isSplitCar bool
name string
size int64
}
func (r *readCloserWrapper) Size() int64 {
return r.size
}
// when reading print a dot
func (r *readCloserWrapper) ReadAt(p []byte, off int64) (n int, err error) {
startedAt := time.Now()
defer func() {
took := time.Since(startedAt)
var isIndex, isCar bool
// Metrics want to always report
// if has suffix .index, then it's an index file
if strings.HasSuffix(r.name, ".index") {
isIndex = true
}
// if has suffix .car, then it's a car file
if strings.HasSuffix(r.name, ".car") || r.isSplitCar {
isCar = true
}
if isCar {
carName := filepath.Base(r.name)
metrics.CarLookupHistogram.WithLabelValues(
carName,
boolToString(r.isRemote),
boolToString(r.isSplitCar),
).Observe(float64(took.Seconds()))
}
if isIndex {
// get the index name, which is the part before the .index suffix, after the last .
indexName := strings.TrimSuffix(r.name, ".index")
// split the index name by . and get the last part
byDot := strings.Split(indexName, ".")
if len(byDot) > 0 {
indexName = byDot[len(byDot)-1]
}
metrics.IndexLookupHistogram.WithLabelValues(
indexName,
boolToString(r.isRemote),
boolToString(r.isSplitCar),
).Observe(float64(took.Seconds()))
}
if klog.V(5).Enabled() {
// Very verbose logging:
var icon string
if r.isRemote {
// add internet icon
icon = "🌐 "
} else {
// add disk icon
icon = "💾 "
}
prefix := icon + "[READ-UNKNOWN]"
if isIndex {
prefix = icon + azureBG("[READ-INDEX]")
}
// if has suffix .car, then it's a car file
if isCar {
if r.isSplitCar {
prefix = icon + azureBG("[READ-SPLIT-CAR]")
} else {
prefix = icon + purpleBG("[READ-CAR]")
}
}
klog.V(5).Infof(prefix+" %s:%d+%d (%s)\n", (r.name), off, len(p), took)
}
}()
return r.rac.ReadAt(p, off)
}
func purpleBG(s string) string {
// blue bg, black fg
return "\033[48;5;4m\033[38;5;0m" + s + "\033[0m"
}
func azureBG(s string) string {
// azure bg, black fg
return "\033[48;5;6m\033[38;5;0m" + s + "\033[0m"
}
// when closing print a newline
func (r *readCloserWrapper) Close() error {
return r.rac.Close()
}
func boolToString(b bool) string {
if b {
return "true"
}
return "false"
}