Skip to content

Commit

Permalink
feat: add metrics when config file and certificate config file changes
Browse files Browse the repository at this point in the history
  • Loading branch information
fgouteroux committed Dec 11, 2024
1 parent f729846 commit 7e143e4
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
6 changes: 6 additions & 0 deletions certstore/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
cert "github.com/fgouteroux/acme_manager/certificate"
"github.com/fgouteroux/acme_manager/cmd"
"github.com/fgouteroux/acme_manager/config"
"github.com/fgouteroux/acme_manager/metrics"
"github.com/fgouteroux/acme_manager/ring"
"github.com/fgouteroux/acme_manager/storage/vault"

Expand Down Expand Up @@ -54,6 +55,7 @@ func WatchConfigFileChanges(logger log.Logger, interval time.Duration, configPat
err = Setup(logger, cfg)
if err != nil {
_ = level.Error(logger).Log("msg", fmt.Sprintf("Ignoring issuer changes in file %s because of error", configPath), "err", err)
metrics.SetConfigError(1)
continue
}

Expand All @@ -63,6 +65,7 @@ func WatchConfigFileChanges(logger log.Logger, interval time.Duration, configPat
continue
}
config.GlobalConfig = cfg
metrics.IncConfigReload()
}
}
}
Expand All @@ -79,12 +82,14 @@ func WatchCertificateFileChanges(logger log.Logger, interval time.Duration, conf
newConfigBytes, err := os.ReadFile(filepath.Clean(configPath))
if err != nil {
_ = level.Error(logger).Log("msg", fmt.Sprintf("Unable to read file %s", configPath), "err", err)
metrics.SetCertificateConfigError(1)
continue
}
var cfg cert.Config
err = yaml.Unmarshal(newConfigBytes, &cfg)
if err != nil {
_ = level.Error(logger).Log("msg", fmt.Sprintf("Ignoring file changes %s because of error", configPath), "err", err)
metrics.SetCertificateConfigError(1)
continue
}

Expand Down Expand Up @@ -165,6 +170,7 @@ func WatchCertificateFileChanges(logger log.Logger, interval time.Duration, conf
localCache.Set(AmRingKey, certInfo)
AmStore.PutKVRing(AmRingKey, certInfo)
certConfig = cfg
metrics.IncCertificateConfigReload()
} else {
_ = level.Error(logger).Log("err", err)
}
Expand Down
52 changes: 52 additions & 0 deletions metrics/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,38 @@ var deleteFailedVaultSecret = prometheus.NewCounterVec(
[]string{},
)

var certificateConfigReload = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "acme_manager_certificate_config_reload",
Help: "Number of certificate config file reload",
},
[]string{},
)

var certificateConfigError = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "acme_manager_certificate_config_error",
Help: "1 if there was an error opening or reading the certificate config file, 0 otherwise",
},
[]string{},
)

var configReload = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "acme_manager_config_reload",
Help: "Number of config file reload",
},
[]string{},
)

var configError = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "acme_manager_config_error",
Help: "1 if there was an error opening or reading the config file, 0 otherwise",
},
[]string{},
)

func SetManagedCertificate(issuer string, value float64) {
managedCertificate.WithLabelValues(issuer).Set(value)
}
Expand Down Expand Up @@ -178,6 +210,22 @@ func IncDeleteFailedVaultSecret() {
deleteFailedVaultSecret.WithLabelValues().Inc()
}

func IncCertificateConfigReload() {
certificateConfigReload.WithLabelValues().Inc()
}

func SetCertificateConfigError(value float64) {
certificateConfigError.WithLabelValues().Set(value)
}

func IncConfigReload() {
configReload.WithLabelValues().Inc()
}

func SetConfigError(value float64) {
configError.WithLabelValues().Set(value)
}

func init() {
collectors := []prometheus.Collector{
managedCertificate,
Expand All @@ -194,6 +242,10 @@ func init() {
getFailedVaultSecret,
putFailedVaultSecret,
deleteFailedVaultSecret,
certificateConfigReload,
certificateConfigError,
configReload,
configError,
}

for _, collector := range collectors {
Expand Down

0 comments on commit 7e143e4

Please sign in to comment.