forked from grafana-cold-storage/metrictank
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotifier.go
More file actions
95 lines (82 loc) · 2.05 KB
/
Copy pathnotifier.go
File metadata and controls
95 lines (82 loc) · 2.05 KB
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
package mdata
import (
"encoding/json"
"github.com/raintank/worldping-api/pkg/log"
)
var (
notifierHandlers []NotifierHandler
persistMessageBatch *PersistMessageBatch
)
type NotifierHandler interface {
Send(SavedChunk)
}
//PersistMessage format version
const PersistMessageBatchV1 = 1
type PersistMessage struct {
Instance string `json:"instance"`
Key string `json:"key"`
T0 uint32 `json:"t0"`
}
type PersistMessageBatch struct {
Instance string `json:"instance"`
SavedChunks []SavedChunk `json:"saved_chunks"`
}
type SavedChunk struct {
Key string `json:"key"`
T0 uint32 `json:"t0"`
}
func SendPersistMessage(key string, t0 uint32) {
sc := SavedChunk{Key: key, T0: t0}
for _, h := range notifierHandlers {
h.Send(sc)
}
}
func InitPersistNotifier(handlers ...NotifierHandler) {
notifierHandlers = handlers
}
type Notifier struct {
Instance string
Metrics Metrics
CreateMissingMetrics bool
}
func (cl Notifier) Handle(data []byte) {
version := uint8(data[0])
if version == uint8(PersistMessageBatchV1) {
// new batch format.
batch := PersistMessageBatch{}
err := json.Unmarshal(data[1:], &batch)
if err != nil {
log.Error(3, "failed to unmarsh batch message. skipping.", err)
return
}
if batch.Instance == cl.Instance {
log.Debug("CLU skipping batch message we generated.")
return
}
for _, c := range batch.SavedChunks {
if agg, ok := cl.Metrics.Get(c.Key); ok {
agg.(*AggMetric).SyncChunkSaveState(c.T0)
}
}
} else {
// assume the old format.
ms := PersistMessage{}
err := json.Unmarshal(data, &ms)
if err != nil {
log.Error(3, "skipping message. %s", err)
return
}
if ms.Instance == cl.Instance {
log.Debug("CLU skipping message we generated. %s - %s:%d", ms.Instance, ms.Key, ms.T0)
return
}
// get metric
if cl.CreateMissingMetrics {
agg := cl.Metrics.GetOrCreate(ms.Key)
agg.(*AggMetric).SyncChunkSaveState(ms.T0)
} else if agg, ok := cl.Metrics.Get(ms.Key); ok {
agg.(*AggMetric).SyncChunkSaveState(ms.T0)
}
}
return
}