1
0
mirror of https://github.com/prometheus/mysqld_exporter.git synced 2026-10-23 08:48:59 +03:00

config: report failed reloads in config_last_reload_successful

ReloadConfig deferred observeReload(err), but err resolved to the package-level variable while the function body assigned a local err, so a failed reload still set mysqld_exporter_config_last_reload_successful to 1 and updated the success timestamp. Use a named return value so the deferred call sees the real error, and add a test.

Signed-off-by: Shamim Shahraeini <sh.shahraeini@gmail.com>
This commit is contained in:
Shamim Shahraeini
2026-10-21 17:14:32 +03:30
parent 22eda977f4
commit 56b1e7baf2
2 changed files with 30 additions and 1 deletions

View File

@@ -334,7 +334,7 @@ func (ch *AuthConfigHandler) GetConfig() *AuthConfig {
return ch.Config
}
func (ch *AuthConfigHandler) ReloadConfig(filename string, mysqldAddress string, mysqldUser string, tlsInsecureSkipVerify bool, logger *slog.Logger) error {
func (ch *AuthConfigHandler) ReloadConfig(filename string, mysqldAddress string, mysqldUser string, tlsInsecureSkipVerify bool, logger *slog.Logger) (err error) {
var host, port string
defer func() {
ch.observeReload(err)

View File

@@ -21,6 +21,7 @@ import (
"github.com/go-sql-driver/mysql"
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
"github.com/prometheus/common/promslog"
"github.com/smartystreets/goconvey/convey"
)
@@ -451,3 +452,31 @@ func TestFormDSNWithCustomTls(t *testing.T) {
})
})
}
func TestReloadConfigReportsFailure(t *testing.T) {
ch, err := NewAuthConfigHandler(prometheus.NewRegistry())
if err != nil {
t.Fatal(err)
}
reloadSuccess := func() float64 {
m := &dto.Metric{}
if err := ch.configReloadSuccess.Write(m); err != nil {
t.Fatal(err)
}
return m.GetGauge().GetValue()
}
t.Setenv("MYSQLD_EXPORTER_PASSWORD", "secret")
if err := ch.ReloadConfig("", "localhost:3306", "user", true, promslog.NewNopLogger()); err != nil {
t.Fatalf("valid reload failed: %v", err)
}
if got := reloadSuccess(); got != 1 {
t.Fatalf("after a successful reload: got %v, want 1", got)
}
if err := ch.ReloadConfig("", "invalid-address", "user", true, promslog.NewNopLogger()); err == nil {
t.Fatal("expected an error for an address without a port")
}
if got := reloadSuccess(); got != 0 {
t.Fatalf("after a failed reload: got %v, want 0", got)
}
}