fix prometheus status_code metric that was always 0
Also ensure prom namespace is used for the status metric
@@ -228,11 +228,7 @@ func (r *result) criticalIfErr(err error, format string, a ...any) bool {
return true
}
-func (r *result) exitCode() int {
- if checkRenderFormat == "prometheus" {
- return 0
- }
-
+func (r *result) nagiosCode() int {
switch r.Status {
case okCheckStatus:
return 0
@@ -245,6 +241,14 @@ func (r *result) exitCode() int {
}
}
+func (r *result) exitCode() int {
+ if checkRenderFormat == "prometheus" {
+ return 0
+ }
+
+ return r.nagiosCode()
+}
+
func (r *result) Exit() {
os.Exit(r.exitCode())
}
@@ -323,12 +327,12 @@ func (r *result) renderPrometheus() string {
}
status := prometheus.NewGaugeVec(prometheus.GaugeOpts{
- Name: fmt.Sprintf("nats_server_check_%s_status_code", r.Check),
+ Name: prometheus.BuildFQName(opts.PrometheusNamespace, r.Check, "status_code"),
Help: fmt.Sprintf("Nagios compatible status code for %s", r.Check),
}, []string{"item", "status"})
prometheus.MustRegister(status)
- status.WithLabelValues(sname, string(r.Status)).Set(float64(r.exitCode()))
+ status.WithLabelValues(sname, string(r.Status)).Set(float64(r.nagiosCode()))
var buf bytes.Buffer
