Skip to content

Commit

Permalink
Merge pull request #1 from gopaytech/feat/up-down-metric
Browse files Browse the repository at this point in the history
feat: add new metric to mark is patroni service is up or down
  • Loading branch information
ecojuntak authored Dec 15, 2022
2 parents 4a65198 + c500908 commit 9ff7d6a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ test:
.PHONY: fmt
fmt:
$(GOFMT) $(PKGS)

.PHONY: run
run:
.build/${GOOS}-${GOARCH}/patroni_exporter --patroni.host="http://localhost" --patroni.port=8008
13 changes: 10 additions & 3 deletions collector/patroni_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import (

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"

"github.com/gopaytech/patroni_exporter/client"

"github.com/prometheus/client_golang/prometheus"
)

Expand All @@ -25,6 +23,7 @@ type patroniCollector struct {
stateDesc *prometheus.Desc
roleDesc *prometheus.Desc
staticDesc *prometheus.Desc
upDesc *prometheus.Desc
logger log.Logger
client client.PatroniClient
}
Expand All @@ -45,10 +44,16 @@ func createPatroniCollectorFactory(client client.PatroniClient, logger log.Logge
"The collection of static value as reported by Patroni",
[]string{"version"},
nil)
upDesc := prometheus.NewDesc(
prometheus.BuildFQName(namespace, "node", "up"),
"The current status of Patroni service",
[]string{},
nil)
return &patroniCollector{
stateDesc: stateDesc,
roleDesc: roleDesc,
staticDesc: staticDesc,
upDesc: upDesc,
logger: logger,
client: client,
}
Expand All @@ -62,7 +67,8 @@ func (p *patroniCollector) Describe(ch chan<- *prometheus.Desc) {
func (p *patroniCollector) Collect(ch chan<- prometheus.Metric) {
patroniResponse, err := p.client.GetMetrics()
if err != nil {
level.Error(p.logger).Log("msg", "Unable to get metrics from Patroni", "err", fmt.Sprintf("errornya: %v", err))
level.Error(p.logger).Log("msg", "Unable to get metrics from Patroni", "err", fmt.Sprintf("error: %v", err))
ch <- prometheus.MustNewConstMetric(p.upDesc, prometheus.GaugeValue, 0)
return
}
for _, possibleState := range possiblePatroniState {
Expand All @@ -81,4 +87,5 @@ func (p *patroniCollector) Collect(ch chan<- prometheus.Metric) {
ch <- prometheus.MustNewConstMetric(p.roleDesc, prometheus.GaugeValue, stateValue, possibleRole, patroniResponse.Patroni.Scope)
}
ch <- prometheus.MustNewConstMetric(p.staticDesc, prometheus.GaugeValue, 1.0, patroniResponse.Patroni.Version)
ch <- prometheus.MustNewConstMetric(p.upDesc, prometheus.GaugeValue, 1)
}

0 comments on commit 9ff7d6a

Please sign in to comment.