-
-
Notifications
You must be signed in to change notification settings - Fork 66
/
reload.go
101 lines (88 loc) · 3.04 KB
/
reload.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
package sql_exporter
import (
"errors"
"log/slog"
cfg "github.com/burningalchemist/sql_exporter/config"
)
// Reload function is used to reload the exporter configuration without restarting the exporter
func Reload(e Exporter, configFile *string) error {
slog.Warn("Reloading collectors has started...")
slog.Warn("Connections will not be changed upon the restart of the exporter")
configNext, err := cfg.Load(*configFile)
if err != nil {
slog.Error("Error reading config file", "error", err)
return err
}
configCurrent := e.Config()
// Clear current collectors and replace with new ones
if len(configCurrent.Collectors) > 0 {
configCurrent.Collectors = configCurrent.Collectors[:0]
}
configCurrent.Collectors = configNext.Collectors
slog.Debug("Total collector size change", "from", len(configCurrent.Collectors), "to", len(configNext.Collectors))
// Reload targets
switch {
case configCurrent.Target != nil && configNext.Target != nil:
if err = reloadTarget(e, configNext, configCurrent); err != nil {
return err
}
case len(configCurrent.Jobs) > 0 && len(configNext.Jobs) > 0:
if err = reloadJobs(e, configNext, configCurrent); err != nil {
return err
}
case configCurrent.Target != nil && len(configNext.Jobs) > 0:
case len(configCurrent.Jobs) > 0 && configNext.Target != nil:
return errors.New("changing scrape mode is not allowed. Please restart the exporter")
default:
slog.Warn("No target or jobs have been found - nothing to reload")
}
return nil
}
func reloadTarget(e Exporter, nc, cc *cfg.Config) error {
slog.Warn("Recreating target...")
// We want to preserve DSN from the previous config revision to avoid any connection changes
nc.Target.DSN = cc.Target.DSN
// Apply the new target configuration
cc.Target = nc.Target
// Recreate the target object
target, err := NewTarget("", cc.Target.Name, "", string(cc.Target.DSN),
cc.Target.Collectors(), nil, cc.Globals, cc.Target.EnablePing)
if err != nil {
slog.Error("Error recreating a target", "error", err)
return err
}
// Populate the target list
e.UpdateTarget([]Target{target})
slog.Warn("Collectors have been successfully updated for the target")
return nil
}
func reloadJobs(e Exporter, nc, cc *cfg.Config) error {
slog.Warn("Recreating jobs...")
// We want to preserve `static_configs`` from the previous config revision to avoid any connection changes
for _, currentJob := range cc.Jobs {
for _, newJob := range nc.Jobs {
if newJob.Name == currentJob.Name {
newJob.StaticConfigs = currentJob.StaticConfigs
}
}
}
cc.Jobs = nc.Jobs
var updateErr error
targets := make([]Target, 0, len(cc.Jobs))
for _, jobConfigItem := range cc.Jobs {
job, err := NewJob(jobConfigItem, cc.Globals)
if err != nil {
updateErr = err
break
}
targets = append(targets, job.Targets()...)
slog.Debug("Recreated Job", "name", jobConfigItem.Name)
}
if updateErr != nil {
slog.Error("Error recreating jobs", "error", updateErr)
return updateErr
}
e.UpdateTarget(targets)
slog.Warn("Collectors have been successfully updated for the jobs")
return nil
}