This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
/
storagestats.go
85 lines (76 loc) · 3.26 KB
/
storagestats.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
package models
import (
"strconv"
"sync/atomic"
"github.com/grafana/metrictank/mdata/cache"
opentracing "github.com/opentracing/opentracing-go"
traceLog "github.com/opentracing/opentracing-go/log"
)
//go:generate msgp
type StorageStats struct {
CacheMiss uint32 `json:"executeplan.cache-miss.count"`
CacheHitPartial uint32 `json:"executeplan.cache-hit-partial.count"`
CacheHit uint32 `json:"executeplan.cache-hit.count"`
ChunksFromTank uint32 `json:"executeplan.chunks-from-tank.count"`
ChunksFromCache uint32 `json:"executeplan.chunks-from-cache.count"`
ChunksFromStore uint32 `json:"executeplan.chunks-from-store.count"`
}
func (ss *StorageStats) IncCacheResult(t cache.ResultType) {
switch t {
case cache.Hit:
atomic.AddUint32(&ss.CacheHit, 1)
case cache.HitPartial:
atomic.AddUint32(&ss.CacheHitPartial, 1)
case cache.Miss:
atomic.AddUint32(&ss.CacheMiss, 1)
}
}
func (ss *StorageStats) IncChunksFromTank(n uint32) {
atomic.AddUint32(&ss.ChunksFromTank, n)
}
func (ss *StorageStats) IncChunksFromCache(n uint32) {
atomic.AddUint32(&ss.ChunksFromCache, n)
}
func (ss *StorageStats) IncChunksFromStore(n uint32) {
atomic.AddUint32(&ss.ChunksFromStore, n)
}
// Add adds a to ss.
func (ss *StorageStats) Add(a *StorageStats) {
atomic.AddUint32(&ss.CacheHit, atomic.LoadUint32(&a.CacheHit))
atomic.AddUint32(&ss.CacheHitPartial, atomic.LoadUint32(&a.CacheHitPartial))
atomic.AddUint32(&ss.CacheMiss, atomic.LoadUint32(&a.CacheMiss))
atomic.AddUint32(&ss.ChunksFromTank, atomic.LoadUint32(&a.ChunksFromTank))
atomic.AddUint32(&ss.ChunksFromCache, atomic.LoadUint32(&a.ChunksFromCache))
atomic.AddUint32(&ss.ChunksFromStore, atomic.LoadUint32(&a.ChunksFromStore))
}
func (ss *StorageStats) MarshalJSONFast(b []byte) ([]byte, error) {
b = append(b, '{')
b, _ = ss.MarshalJSONFastRaw(b)
b = append(b, '}')
return b, nil
}
func (ss *StorageStats) MarshalJSONFastRaw(b []byte) ([]byte, error) {
b = append(b, `"executeplan.cache-miss.count":`...)
b = strconv.AppendUint(b, uint64(atomic.LoadUint32(&ss.CacheMiss)), 10)
b = append(b, `,"executeplan.cache-hit-partial.count":`...)
b = strconv.AppendUint(b, uint64(atomic.LoadUint32(&ss.CacheHitPartial)), 10)
b = append(b, `,"executeplan.cache-hit.count":`...)
b = strconv.AppendUint(b, uint64(atomic.LoadUint32(&ss.CacheHit)), 10)
b = append(b, `,"executeplan.chunks-from-tank.count":`...)
b = strconv.AppendUint(b, uint64(atomic.LoadUint32(&ss.ChunksFromTank)), 10)
b = append(b, `,"executeplan.chunks-from-cache.count":`...)
b = strconv.AppendUint(b, uint64(atomic.LoadUint32(&ss.ChunksFromCache)), 10)
b = append(b, `,"executeplan.chunks-from-store.count":`...)
b = strconv.AppendUint(b, uint64(atomic.LoadUint32(&ss.ChunksFromStore)), 10)
return b, nil
}
func (ss *StorageStats) Trace(span opentracing.Span) {
span.LogFields(
traceLog.Int32("cache-miss", int32(atomic.LoadUint32(&ss.CacheMiss))),
traceLog.Int32("cache-hit-partial", int32(atomic.LoadUint32(&ss.CacheHitPartial))),
traceLog.Int32("cache-hit", int32(atomic.LoadUint32(&ss.CacheHit))),
traceLog.Int32("chunks-from-tank", int32(atomic.LoadUint32(&ss.ChunksFromTank))),
traceLog.Int32("chunks-from-cache", int32(atomic.LoadUint32(&ss.ChunksFromCache))),
traceLog.Int32("chunks-from-store", int32(atomic.LoadUint32(&ss.ChunksFromStore))),
)
}