-
Notifications
You must be signed in to change notification settings - Fork 544
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dragonboat: added Prometheus health metrics
- Loading branch information
Showing
12 changed files
with
391 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
// Copyright 2017-2019 Lei Ni ([email protected]) and other Dragonboat authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package dragonboat | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"sync/atomic" | ||
|
||
"github.com/VictoriaMetrics/metrics" | ||
"github.com/lni/dragonboat/v3/internal/server" | ||
"github.com/lni/dragonboat/v3/raftio" | ||
) | ||
|
||
// WriteHealthMetrics writes all health metrics in Prometheus format to the | ||
// specified writer. This function is typically called by the metrics http | ||
// handler. | ||
func WriteHealthMetrics(w io.Writer) { | ||
metrics.WritePrometheus(w, false) | ||
} | ||
|
||
type raftEventListener struct { | ||
clusterID uint64 | ||
nodeID uint64 | ||
leaderID *uint64 | ||
metrics bool | ||
isLeader *metrics.Gauge | ||
campaignLaunched *metrics.Counter | ||
campaignSkipped *metrics.Counter | ||
snapshotRejected *metrics.Counter | ||
replicationRejected *metrics.Counter | ||
proposalDropped *metrics.Counter | ||
readIndexDropped *metrics.Counter | ||
userListener raftio.IRaftEventListener | ||
} | ||
|
||
func newRaftEventListener(clusterID uint64, nodeID uint64, | ||
leaderID *uint64, useMetrics bool, | ||
userListener raftio.IRaftEventListener) *raftEventListener { | ||
el := &raftEventListener{ | ||
clusterID: clusterID, | ||
nodeID: nodeID, | ||
leaderID: leaderID, | ||
metrics: useMetrics, | ||
userListener: userListener, | ||
} | ||
if useMetrics { | ||
label := fmt.Sprintf(`{clusterid="%d",nodeid="%d"}`, clusterID, nodeID) | ||
name := fmt.Sprintf(`campaign_launched%s`, label) | ||
campaignLaunched := metrics.GetOrCreateCounter(name) | ||
name = fmt.Sprintf(`campaign_skipped%s`, label) | ||
campaignSkipped := metrics.GetOrCreateCounter(name) | ||
name = fmt.Sprintf(`snapshot_rejected%s`, label) | ||
snapshotRejected := metrics.GetOrCreateCounter(name) | ||
name = fmt.Sprintf(`replication_rejected%s`, label) | ||
replicationRejected := metrics.GetOrCreateCounter(name) | ||
name = fmt.Sprintf(`proposal_dropped%s`, label) | ||
proposalDropped := metrics.GetOrCreateCounter(name) | ||
name = fmt.Sprintf(`read_index_dropped%s`, label) | ||
readIndexDropped := metrics.GetOrCreateCounter(name) | ||
name = fmt.Sprintf(`is_leader%s`, label) | ||
isLeader := metrics.GetOrCreateGauge(name, func() float64 { | ||
if atomic.LoadUint64(leaderID) == nodeID { | ||
return 1.0 | ||
} | ||
return 0.0 | ||
}) | ||
|
||
el.isLeader = isLeader | ||
el.campaignLaunched = campaignLaunched | ||
el.campaignSkipped = campaignSkipped | ||
el.snapshotRejected = snapshotRejected | ||
el.replicationRejected = replicationRejected | ||
el.proposalDropped = proposalDropped | ||
el.readIndexDropped = readIndexDropped | ||
} | ||
return el | ||
} | ||
|
||
func (e *raftEventListener) LeaderUpdated(info server.LeaderInfo) { | ||
atomic.StoreUint64(e.leaderID, info.LeaderID) | ||
if e.userListener != nil { | ||
ui := raftio.LeaderInfo{ | ||
ClusterID: info.ClusterID, | ||
NodeID: info.NodeID, | ||
Term: info.Term, | ||
LeaderID: info.LeaderID, | ||
} | ||
go e.userListener.LeaderUpdated(ui) | ||
} | ||
} | ||
|
||
func (e *raftEventListener) CampaignLaunched(info server.CampaignInfo) { | ||
if e.metrics { | ||
e.campaignLaunched.Add(1) | ||
} | ||
} | ||
|
||
func (e *raftEventListener) CampaignSkipped(info server.CampaignInfo) { | ||
if e.metrics { | ||
e.campaignSkipped.Add(1) | ||
} | ||
} | ||
|
||
func (e *raftEventListener) SnapshotRejected(info server.SnapshotInfo) { | ||
if e.metrics { | ||
e.snapshotRejected.Add(1) | ||
} | ||
} | ||
|
||
func (e *raftEventListener) ReplicationRejected(info server.ReplicationInfo) { | ||
if e.metrics { | ||
e.replicationRejected.Add(1) | ||
} | ||
} | ||
|
||
func (e *raftEventListener) ProposalDropped(info server.ProposalInfo) { | ||
if e.metrics { | ||
e.proposalDropped.Add(len(info.Entries)) | ||
} | ||
} | ||
|
||
func (e *raftEventListener) ReadIndexDropped(info server.ReadIndexInfo) { | ||
if e.metrics { | ||
e.readIndexDropped.Add(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
module github.com/lni/dragonboat/v3 | ||
|
||
require github.com/golang/protobuf v1.2.0 | ||
require ( | ||
github.com/VictoriaMetrics/metrics v1.5.0 | ||
github.com/golang/protobuf v1.2.0 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,8 @@ | ||
github.com/VictoriaMetrics/metrics v1.5.0 h1:WvQqPn+z9pR1U7J58CgaGiWrN8phNGSpr2xUSxJnfpE= | ||
github.com/VictoriaMetrics/metrics v1.5.0/go.mod h1:QZAL5yLaXvhSPeib0ahluGo9VK0HXDZHovKaKlpuWvs= | ||
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM= | ||
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= | ||
github.com/valyala/fastrand v1.0.0 h1:LUKT9aKer2dVQNUi3waewTbKV+7H17kvWFNKs2ObdkI= | ||
github.com/valyala/fastrand v1.0.0/go.mod h1:HWqCzkrkg6QXT8V2EXWvXCoow7vLwOFN002oeRzjapQ= | ||
github.com/valyala/histogram v1.0.1 h1:FzA7n2Tz/wKRMejgu3PV1vw3htAklTjjuoI6z3d4KDg= | ||
github.com/valyala/histogram v1.0.1/go.mod h1:lQy0xA4wUz2+IUnf97SivorsJIp8FxsnRd6x25q7Mto= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.