Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Value to healthz metric, avoid ignoring server when error #257

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions collector/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,30 @@ func TestConnz(t *testing.T) {
verifyCollector(CoreSystem, url, "connz", cases, t)
}

func TestHealthz(t *testing.T) {
s := pet.RunServer()
defer s.Shutdown()

url := fmt.Sprintf("http://localhost:%d", pet.MonitorPort)
// see if we get the same stats as the original monitor testing code.
// just for our monitoring_port

cases := map[string]float64{
"gnatsd_healthz_status": 0,
}

verifyCollector(CoreSystem, url, "healthz", cases, t)

// test after server shutdown
s.Shutdown()

cases = map[string]float64{
"gnatsd_healthz_status": 1,
}

verifyCollector(CoreSystem, url, "healthz", cases, t)
}

func TestNoServer(t *testing.T) {
url := fmt.Sprintf("http://localhost:%d", pet.MonitorPort)

Expand Down
15 changes: 10 additions & 5 deletions collector/healthz.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func newHealthzCollector(system, endpoint string, servers []*CollectedServer) pr
status: prometheus.NewDesc(
prometheus.BuildFQName(system, endpoint, "status"),
"status",
[]string{"server_id"},
[]string{"server_id", "value"},
nil,
),
}
Expand All @@ -69,16 +69,21 @@ func (nc *healthzCollector) Collect(ch chan<- prometheus.Metric) {
for _, server := range nc.servers {
var health Healthz
if err := getMetricURL(nc.httpClient, server.URL, &health); err != nil {
Debugf("ignoring server %s: %v", server.ID, err)
continue
Debugf("error collecting server %s: %v", server.ID, err)
health.Error = err.Error()
}

var status float64 = 1
var (
status float64 = 1
value = health.Error
)

if health.Status == "ok" {
status = 0
value = health.Status
}

ch <- prometheus.MustNewConstMetric(nc.status, prometheus.GaugeValue, status, server.ID)
ch <- prometheus.MustNewConstMetric(nc.status, prometheus.GaugeValue, status, server.ID, value)
}
}

Expand Down