Skip to content

Commit

Permalink
etcdserver: add is_leader prometheus metric that is 1 on the leader.
Browse files Browse the repository at this point in the history
Before this change, we had now way to find a leader using /metrics
endpoint. This commit adds a metric to do that.
  • Loading branch information
mborsz committed Apr 19, 2018
1 parent e5c9483 commit 46bc966
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
7 changes: 7 additions & 0 deletions etcdserver/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ var (
Name: "has_leader",
Help: "Whether or not a leader exists. 1 is existence, 0 is not.",
})
isLeader = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "etcd",
Subsystem: "server",
Name: "is_leader",
Help: "Whether or not this member is a leader. 1 if is, 0 otherwise.",
})
leaderChanges = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "etcd",
Subsystem: "server",
Expand Down Expand Up @@ -77,6 +83,7 @@ var (

func init() {
prometheus.MustRegister(hasLeader)
prometheus.MustRegister(isLeader)
prometheus.MustRegister(leaderChanges)
prometheus.MustRegister(proposalsCommitted)
prometheus.MustRegister(proposalsApplied)
Expand Down
5 changes: 5 additions & 0 deletions etcdserver/raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ func (r *raftNode) start(rh *raftReadyHandler) {

rh.updateLead(rd.SoftState.Lead)
islead = rd.RaftState == raft.StateLeader
if islead {
isLeader.Set(1)
} else {
isLeader.Set(0)
}
rh.updateLeadership(newLeader)
r.td.Reset()
}
Expand Down

0 comments on commit 46bc966

Please sign in to comment.