Skip to content

Commit

Permalink
adding gauges and tests for memberlist.nodes.<state>
Browse files Browse the repository at this point in the history
  • Loading branch information
jmurret committed Aug 29, 2022
1 parent 832dddd commit ed85e1e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
30 changes: 30 additions & 0 deletions net.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ const (
maxPushPullRequests = 128 // Maximum number of concurrent push/pull requests
)

const (
nodeStateAlive = "alive"
nodeStateDead = "dead"
nodeStateLeft = "left"
nodeStateSuspect = "suspect"
)

// ping request sent directly to node
type ping struct {
SeqNo uint32
Expand Down Expand Up @@ -1007,6 +1014,29 @@ func (m *Memberlist) sendLocalState(conn net.Conn, join bool, streamLabel string
}
m.nodeLock.RUnlock()

nodeStateCounts := make(map[string]int)
nodeStateCounts[nodeStateAlive] = 0
nodeStateCounts[nodeStateLeft] = 0
nodeStateCounts[nodeStateDead] = 0
nodeStateCounts[nodeStateSuspect] = 0

for _, n := range localNodes {
switch n.State {
case StateAlive:
nodeStateCounts[nodeStateAlive]++
case StateDead:
nodeStateCounts[nodeStateDead]++
case StateSuspect:
nodeStateCounts[nodeStateSuspect]++
case StateLeft:
nodeStateCounts[nodeStateLeft]++
}
}

for nodeState, cnt := range nodeStateCounts {
metrics.SetGaugeWithLabels([]string{"memberlist", "nodes", nodeState}, float32(cnt), m.metricLabels)
}

// Get the delegate state
var userData []byte
if m.config.Delegate != nil {
Expand Down
20 changes: 12 additions & 8 deletions state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2317,14 +2317,11 @@ func TestMemberlist_PushPull(t *testing.T) {
failf("expected 2 messages from pushPull")
}

intv := getIntervalMetrics(t, sink)

gaugeName := "consul.usage.test.memberlist.size.local"
actualGauge := intv.Gauges[gaugeName]

if actualGauge.Value == 0 {
t.Fatalf("memberlist.size.local gauge not emitted")
}
verifyGaugeExists(t, "consul.usage.test.memberlist.size.local", sink)
verifyGaugeExists(t, "consul.usage.test.memberlist.nodes.alive", sink)
verifyGaugeExists(t, "consul.usage.test.memberlist.nodes.suspect", sink)
verifyGaugeExists(t, "consul.usage.test.memberlist.nodes.left", sink)
verifyGaugeExists(t, "consul.usage.test.memberlist.nodes.dead", sink)
})
}

Expand Down Expand Up @@ -2467,3 +2464,10 @@ func getIntervalMetrics(t *testing.T, sink *metrics.InmemSink) *metrics.Interval
intv := intervals[0]
return intv
}

func verifyGaugeExists(t *testing.T, name string, sink *metrics.InmemSink) {
interval := getIntervalMetrics(t, sink)
if _, ok := interval.Gauges[name]; !ok {
t.Fatalf("%s gauge not emmited", name)
}
}

0 comments on commit ed85e1e

Please sign in to comment.