-
-
Notifications
You must be signed in to change notification settings - Fork 67
/
job.go
61 lines (52 loc) · 1.52 KB
/
job.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
package sql_exporter
import (
"fmt"
"github.com/burningalchemist/sql_exporter/config"
"github.com/burningalchemist/sql_exporter/errors"
"github.com/prometheus/client_golang/prometheus"
)
// Job is a collection of targets with the same collectors applied.
type Job interface {
Targets() []Target
}
// job implements Job. It wraps the corresponding JobConfig and a set of Targets.
type job struct {
config *config.JobConfig
targets []Target
logContext string
}
// NewJob returns a new Job with the given configuration.
func NewJob(jc *config.JobConfig, gc *config.GlobalConfig) (Job, errors.WithContext) {
j := job{
config: jc,
targets: make([]Target, 0, 10),
logContext: fmt.Sprintf(`job=%s`, jc.Name),
}
if jc.EnablePing == nil {
jc.EnablePing = &config.EnablePing
}
for _, sc := range jc.StaticConfigs {
for tname, dsn := range sc.Targets {
constLabels := prometheus.Labels{
"job": jc.Name,
config.TargetLabel: tname,
}
for name, value := range sc.Labels {
// Shouldn't happen as there are sanity checks in config, but check nonetheless.
if _, found := constLabels[name]; found {
return nil, errors.Errorf(j.logContext, "duplicate label %q", name)
}
constLabels[name] = value
}
t, err := NewTarget(j.logContext, tname, jc.Name, string(dsn), jc.Collectors(), constLabels, gc, jc.EnablePing)
if err != nil {
return nil, err
}
j.targets = append(j.targets, t)
}
}
return &j, nil
}
func (j *job) Targets() []Target {
return j.targets
}