Skip to content

Commit

Permalink
reset consul_autopilot_healthy to NaN on leadership state
Browse files Browse the repository at this point in the history
Signed-off-by: FFMMM <[email protected]>
  • Loading branch information
FFMMM committed Oct 6, 2021
1 parent eeccf9c commit f45d85f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
3 changes: 3 additions & 0 deletions .changelog/11231.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
telemetry: fixes a bug with Prometheus consul_autopilot_healthy metric where 0 is reported instead of NaN on servers.
```
6 changes: 6 additions & 0 deletions agent/consul/autopilot.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ func (d *AutopilotDelegate) NotifyState(state *autopilot.State) {
} else {
metrics.SetGauge([]string{"autopilot", "healthy"}, 0)
}
} else {

// if we are not a leader, emit NaN per
// https://www.consul.io/docs/agent/telemetry#autopilot
metrics.SetGauge([]string{"autopilot", "healthy"}, float32(math.NaN()))

}
}

Expand Down
9 changes: 4 additions & 5 deletions agent/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import (
"testing"
)

// TestMetrics_ConsulAutopilotHealthy_Prometheus adds testing around
// TestHTTPHandlers_AgentMetrics_ConsulAutopilotHealthy_Prometheus adds testing around
// the published autopilot metrics on https://www.consul.io/docs/agent/telemetry#autopilot
func TestMetrics_ConsulAutopilotHealthy_Prometheus(t *testing.T) {
func TestHTTPHandlers_AgentMetrics_ConsulAutopilotHealthy_Prometheus(t *testing.T) {
checkForShortTesting(t)
t.Parallel()

// configure agent to emit Prometheus metrics
hcl :=`
hcl := `
telemetry = {
prometheus_retention_time = "5s",
disable_hostname = true
Expand All @@ -24,7 +24,6 @@ func TestMetrics_ConsulAutopilotHealthy_Prometheus(t *testing.T) {
a := StartTestAgent(t, TestAgent{HCL: hcl, NoWaitForStartup: true})
defer a.Shutdown()


req, err := http.NewRequest("GET", "/v1/agent/metrics?format=prometheus", nil)
if err != nil {
t.Fatalf("Failed to generate new http request. err: %v", err)
Expand All @@ -49,4 +48,4 @@ func checkForShortTesting(t *testing.T) {
if testing.Short() {
t.Skip("too slow for testing.Short")
}
}
}
6 changes: 2 additions & 4 deletions agent/testagent.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"crypto/x509"
"fmt"
"github.com/armon/go-metrics"
"io"
"math/rand"
"net"
Expand All @@ -17,6 +16,7 @@ import (
"text/template"
"time"

metrics "github.com/armon/go-metrics"
"github.com/hashicorp/go-hclog"
uuid "github.com/hashicorp/go-uuid"

Expand Down Expand Up @@ -83,8 +83,7 @@ type TestAgent struct {
// It is valid after Start().
*Agent

// NoWaitForStartup, if false, will wait for the agent to be up or for leader
// election to have happened before finishing the start process for an Agent
// NoWaitForStartup, if false, will start the anti entropy sync and waitForUp() (see *waitForUp()).
NoWaitForStartup bool
}

Expand Down Expand Up @@ -232,7 +231,6 @@ func (a *TestAgent) Start(t *testing.T) error {
// Start the anti-entropy syncer
a.Agent.StartSync()


if err := a.waitForUp(); err != nil {
a.Shutdown()
a.Agent = nil
Expand Down

0 comments on commit f45d85f

Please sign in to comment.