-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.go
147 lines (121 loc) · 3.11 KB
/
manager.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
package tasks
import (
"sort"
"sync"
"github.com/f1shl3gs/gossiping/tasks/targetpb"
"github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"
)
type Collector struct {
logger *zap.Logger
externalLabels map[string]string
mtx sync.RWMutex
tasks map[string]map[uint64]*Task
}
func New(logger *zap.Logger, externalLabels map[string]string) *Collector {
c := &Collector{
logger: logger,
tasks: make(map[string]map[uint64]*Task),
externalLabels: externalLabels,
}
return c
}
func (c *Collector) Describe(descs chan<- *prometheus.Desc) {
descs <- prometheus.NewDesc("dummy", "", nil, nil)
}
func (c *Collector) Collect(metrics chan<- prometheus.Metric) {
c.mtx.RLock()
defer c.mtx.RUnlock()
for _, group := range c.tasks {
for _, task := range group {
task.Collect(metrics)
}
}
}
func (c *Collector) Coordinate(me *targetpb.MeshEntry) {
c.mtx.Lock()
defer c.mtx.Unlock()
taskGroup := c.tasks[me.Name]
if taskGroup == nil {
taskGroup = make(map[uint64]*Task)
c.tasks[me.Name] = taskGroup
}
// add task
idCache := make([]uint64, 0, len(me.Targetgroup.Targets))
for _, addr := range me.Targetgroup.Targets {
m := make(map[string]string, len(me.Targetgroup.Labels)+len(c.externalLabels))
for k, v := range me.Targetgroup.Labels {
m[k] = v
}
for k, v := range c.externalLabels {
m[k] = v
}
taskID := TaskID(addr, m)
idCache = append(idCache, taskID)
_, ok := taskGroup[taskID]
if ok {
continue
}
task, err := newTask(addr, m)
if err != nil {
c.logger.Warn("create new task failed",
zap.String("job", me.Name),
zap.String("addr", addr),
zap.Error(err))
continue
}
go task.Start(c.logger)
taskGroup[taskID] = task
c.logger.Info("add target",
zap.String("job", me.Name),
zap.String("addr", addr))
}
// remote none exist
for taskID, task := range taskGroup {
found := false
for _, id := range idCache {
if id == taskID {
found = true
break
}
}
if found {
continue
}
task.Stop()
delete(taskGroup, taskID)
c.logger.Info("delete target",
zap.String("job", me.Name),
zap.String("addr", task.address))
}
}
// SeparatorByte is a byte that cannot occur in valid UTF-8 sequences and is
// used to separate label names, label values, and other strings from each other
// when calculating their combined hash value (aka signature aka fingerprint).
const SeparatorByte byte = 255
var (
// cache the signature of an empty label set.
emptyLabelSignature = hashNew()
)
func TaskID(addr string, labels map[string]string) uint64 {
if len(labels) == 0 {
return emptyLabelSignature
}
labelNames := make([]string, 0, len(labels))
for labelName := range labels {
labelNames = append(labelNames, labelName)
}
sort.Strings(labelNames)
sum := hashNew()
for _, labelName := range labelNames {
sum = hashAdd(sum, labelName)
sum = hashAddByte(sum, SeparatorByte)
sum = hashAdd(sum, labels[labelName])
sum = hashAddByte(sum, SeparatorByte)
}
sum = hashAdd(sum, "__addr__")
sum = hashAddByte(sum, SeparatorByte)
sum = hashAdd(sum, addr)
sum = hashAddByte(sum, SeparatorByte)
return sum
}