Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
Add leader status Prometheus metric
Browse files Browse the repository at this point in the history
In HA mode, this metric will expose which of the connector had the
leader status and write privileges at specific times.
  • Loading branch information
antekresic committed Apr 21, 2020
1 parent 4c666a6 commit 8bc0164
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions cmd/timescale-prometheus/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ const (
)

var (
leaderGauge = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "current_leader",
Help: "Shows current election leader status",
},
)
receivedSamples = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "received_samples_total",
Expand Down Expand Up @@ -111,6 +117,7 @@ var (
)

func init() {
prometheus.MustRegister(leaderGauge)
prometheus.MustRegister(receivedSamples)
prometheus.MustRegister(sentSamples)
prometheus.MustRegister(failedSamples)
Expand Down Expand Up @@ -217,14 +224,17 @@ func initElector(cfg *config) *util.Elector {
func migrate(cfg *pgclient.Config) {
shouldWrite, err := isWriter()
if err != nil {
leaderGauge.Set(0)
log.Error("msg", "IsLeader check failed", "err", err)
return
}
if !shouldWrite {
leaderGauge.Set(0)
log.Debug("msg", fmt.Sprintf("Election id %v: Instance is not a leader. Won't update", elector.ID()))
return
}

leaderGauge.Set(1)
dbStd, err := sql.Open("pgx", cfg.GetConnectionStr())
if err != nil {
log.Error(err)
Expand Down Expand Up @@ -259,14 +269,17 @@ func write(writer pgmodel.DBInserter) http.Handler {

shouldWrite, err := isWriter()
if err != nil {
leaderGauge.Set(0)
log.Error("msg", "IsLeader check failed", "err", err)
return
}
if !shouldWrite {
leaderGauge.Set(0)
log.Debug("msg", fmt.Sprintf("Election id %v: Instance is not a leader. Can't write data", elector.ID()))
return
}

leaderGauge.Set(1)
reqBuf, err := snappy.Decode(nil, compressed)
if err != nil {
log.Error("msg", "Decode error", "err", err.Error())
Expand Down

0 comments on commit 8bc0164

Please sign in to comment.