-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtarget.go
188 lines (159 loc) · 4.05 KB
/
target.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
package blackbox
/*
Copyright 2020 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import (
"bytes"
"sort"
"strings"
"text/template"
"github.com/golang/glog"
)
type Target struct {
Module string
Destination string
Name string
ScrapeInterval int
}
type Targets struct {
Targets []Target
JobName string
BlackboxHostPort string
ScrapeInterval int
}
type TargetOption func(t *Target)
func (ts *Targets) Add(m *Module, d, n string, os ...*Option) Target {
t := Target{
Module: m.Name,
Destination: d,
Name: n,
}
t.applyOptions(os...)
ts.Targets = append(ts.Targets, t)
return t
}
func (t *Target) applyOptions(os ...*Option) {
for _, o := range os {
if o.TargetOption == nil {
continue
}
o.TargetOption(t)
}
}
func ScrapeInterval(si int) *Option {
return &Option{
TargetOption: func(t *Target) {
t.ScrapeInterval = si
},
}
}
var header = `global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_configs:
`
var scCfgTmpl = `{{ range . }}- job_name: '{{ .JobName }}_{{ .ScrapeInterval }}'
scrape_interval: {{ .ScrapeInterval }}s
metrics_path: /probe
static_configs:
- targets:{{ range .Targets }}
- {{.Module}}|{{.Destination}}|{{.Name}}{{end}}
relabel_configs:
- source_labels: [__address__]
regex: (.+)\|(.+)\|(.+)
target_label: __param_target
replacement: ${2}
- source_labels: [__address__]
regex: (.+)\|(.+)\|(.+)
target_label: __param_module
replacement: ${1}
- source_labels: [__address__]
regex: (.+)\|(.+)\|(.+)
target_label: module
replacement: ${1}
- source_labels: [__address__]
regex: (.+)\|(.+)\|(.+)
target_label: name
replacement: ${3}
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: {{.BlackboxHostPort}}
{{ end }}
`
func (ts *Targets) Marshal() []byte {
var b bytes.Buffer
b.WriteString(header)
b.Write(ts.marshal())
return b.Bytes()
}
func (ts *Targets) MarshalSC() []byte {
return ts.marshal()
}
func (ts *Targets) byScrapeInterval() map[int][]Target {
out := make(map[int][]Target)
for _, t := range ts.Targets {
si := t.ScrapeInterval
if si == 0 {
si = ts.ScrapeInterval
}
out[si] = append(out[si], t)
}
return out
}
var tmpl = template.Must(template.New("targets").Parse(scCfgTmpl))
func trimScheme(s string) string {
return strings.TrimPrefix(strings.TrimPrefix(s, "https://"), "http://")
}
func (ts *Targets) sort() {
sort.SliceStable(ts.Targets, func(i, j int) bool {
ii := trimScheme(ts.Targets[i].Destination) + ts.Targets[i].Name + ts.Targets[i].Module
jj := trimScheme(ts.Targets[j].Destination) + ts.Targets[j].Name + ts.Targets[j].Module
return strings.Compare(ii, jj) == -1
})
}
func (ts *Targets) marshal() []byte {
ts.sort()
tsis := ts.byScrapeInterval()
var b bytes.Buffer
type tmplD struct {
JobName string
ScrapeInterval int
Targets []Target
BlackboxHostPort string
}
var cfgs []*tmplD
for si, tsi := range tsis {
d := &tmplD{
JobName: ts.JobName,
BlackboxHostPort: ts.BlackboxHostPort,
ScrapeInterval: si,
Targets: tsi,
}
cfgs = append(cfgs, d)
}
sort.SliceStable(cfgs, func(i, j int) bool {
if cfgs[i].ScrapeInterval < cfgs[j].ScrapeInterval {
return true
}
if cfgs[i].ScrapeInterval > cfgs[j].ScrapeInterval {
return false
}
// If the ScrapeIntervals are equal, sort by JobName.
return strings.Compare(cfgs[i].JobName, cfgs[j].JobName) == -1
})
err := tmpl.Execute(&b, cfgs)
if err != nil {
glog.Fatal(err)
}
return b.Bytes()
}