Skip to content

Commit 19c3295

Browse files
fix: reload collectors correctly (#21)
1 parent 7a221ab commit 19c3295

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

cmd/sql_exporter/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func main() {
8080
func reloadCollectors(e sql_exporter.Exporter) func(http.ResponseWriter, *http.Request) {
8181
return func(w http.ResponseWriter, r *http.Request) {
8282
klog.Infof("Reloading the collectors...")
83-
err := e.Config().LoadCollectorFiles()
83+
err := e.Config().ReloadCollectorFiles()
8484
if err != nil {
8585
klog.Errorf("Error reloading collectors - %v", err)
8686
http.Error(w, err.Error(), http.StatusInternalServerError)

config/config.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
6060
}
6161

6262
// Load any externally defined collectors.
63-
if err := c.LoadCollectorFiles(); err != nil {
63+
if err := c.loadCollectorFiles(); err != nil {
6464
return err
6565
}
6666

@@ -99,8 +99,20 @@ func (c *Config) YAML() ([]byte, error) {
9999
return yaml.Marshal(c)
100100
}
101101

102+
// ReloadCollectorFiles blah
103+
func (c *Config) ReloadCollectorFiles() error {
104+
if len(c.Collectors) > 0 {
105+
c.Collectors = c.Collectors[:0]
106+
}
107+
err := c.loadCollectorFiles()
108+
if err != nil {
109+
return err
110+
}
111+
return nil
112+
}
113+
102114
// LoadCollectorFiles resolves all collector file globs to files and loads the collectors they define.
103-
func (c *Config) LoadCollectorFiles() error {
115+
func (c *Config) loadCollectorFiles() error {
104116
baseDir := filepath.Dir(c.configFile)
105117
for _, cfglob := range c.CollectorFiles {
106118
// Resolve relative paths by joining them to the configuration file's directory.
@@ -128,9 +140,6 @@ func (c *Config) LoadCollectorFiles() error {
128140
return err
129141
}
130142

131-
if len(c.Collectors) > 0 {
132-
c.Collectors = c.Collectors[:0]
133-
}
134143
c.Collectors = append(c.Collectors, &cc)
135144
klog.Infof("Loaded collector %q from %s", cc.Name, cf)
136145
}

0 commit comments

Comments
 (0)