Skip to content

Commit c28dcbb

Browse files
committed
refactor metric
1 parent 45e6ba6 commit c28dcbb

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

server/metric.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,18 +77,23 @@ func (pm *PrometheusMetric) Set() {
7777
metricM["host"][p.Host]++
7878
}
7979
for k, v := range metricM {
80+
// todo: define a new type for metrics and contain name, protmetheus metric type. So, we won't have to
81+
// do manuel if-else check here.
8082
if k == "db" {
81-
// todo: DRY
82-
for i, j := range v {
83-
pm.dbCount.With(prometheus.Labels{"db": i}).Set(float64(j))
84-
}
83+
setGaugeVec(k, v, pm.dbCount)
8584
} else {
8685
if k == "host" {
87-
// todo: DRY
88-
for i, j := range v {
89-
pm.hostConn.With(prometheus.Labels{"host": i}).Set(float64(j))
90-
}
86+
setGaugeVec(k, v, pm.hostConn)
9187
}
9288
}
9389
}
9490
}
91+
92+
func setGaugeVec(name string, m map[string]int, gv *prometheus.GaugeVec) {
93+
for i, j := range m {
94+
if i == "" {
95+
i = "UNKNOWN"
96+
}
97+
gv.With(prometheus.Labels{name: i}).Set(float64(j))
98+
}
99+
}

0 commit comments

Comments
 (0)