Skip to content

Commit dc2ab97

Browse files
committed
rewrite complex ifs with switch
1 parent b45e3ed commit dc2ab97

File tree

3 files changed

+16
-17
lines changed

3 files changed

+16
-17
lines changed

internal/reaper/database.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -708,11 +708,12 @@ func FetchMetricsPgpool(ctx context.Context, msg MetricFetchConfig, vme Monitore
708708
retRow[k] = vs
709709
if k == "status" { // was changed from numeric to string at some pgpool version so leave the string
710710
// but also add "status_num" field
711-
if vs == "up" {
711+
switch vs {
712+
case "up":
712713
retRow["status_num"] = 1
713-
} else if vs == "down" {
714+
case "down":
714715
retRow["status_num"] = 0
715-
} else {
716+
default:
716717
i, err := strconv.ParseInt(vs, 10, 64)
717718
if err == nil {
718719
retRow["status_num"] = i

internal/sinks/postgres.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,8 @@ func (pgw *PostgresWriter) flush(msgs []metrics.MeasurementEnvelope) {
295295

296296
rowsBatched++
297297

298-
if pgw.metricSchema == DbStorageSchemaTimescale {
298+
switch pgw.metricSchema {
299+
case DbStorageSchemaTimescale:
299300
// set min/max timestamps to check/create partitions
300301
bounds, ok := pgPartBounds[msg.MetricName]
301302
if !ok || (ok && epochTime.Before(bounds.StartTime)) {
@@ -306,7 +307,7 @@ func (pgw *PostgresWriter) flush(msgs []metrics.MeasurementEnvelope) {
306307
bounds.EndTime = epochTime
307308
pgPartBounds[msg.MetricName] = bounds
308309
}
309-
} else if pgw.metricSchema == DbStorageSchemaPostgres {
310+
case DbStorageSchemaPostgres:
310311
_, ok := pgPartBoundsDbName[msg.MetricName]
311312
if !ok {
312313
pgPartBoundsDbName[msg.MetricName] = make(map[string]ExistingPartitionInfo)
@@ -320,6 +321,8 @@ func (pgw *PostgresWriter) flush(msgs []metrics.MeasurementEnvelope) {
320321
bounds.EndTime = epochTime
321322
pgPartBoundsDbName[msg.MetricName][msg.DBName] = bounds
322323
}
324+
default:
325+
logger.Fatal("unknown storage schema...")
323326
}
324327
}
325328
}

internal/sinks/prometheus.go

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"net"
77
"net/http"
8-
"reflect"
98
"slices"
109
"strconv"
1110
"strings"
@@ -230,21 +229,17 @@ func (promw *PrometheusWriter) MetricStoreMessageToPromMetrics(msg metrics.Measu
230229
tag := k[4:]
231230
labels[tag] = fmt.Sprintf("%v", v)
232231
} else {
233-
dataType := reflect.TypeOf(v).String()
234-
if dataType == "float64" || dataType == "float32" || dataType == "int64" || dataType == "int32" || dataType == "int" {
232+
switch t := v.(type) {
233+
case int, int32, int64, float32, float64:
235234
f, err := strconv.ParseFloat(fmt.Sprintf("%v", v), 64)
236235
if err != nil {
237-
logger.Debugf("Skipping scraping column %s of [%s:%s]: %v", k, msg.DBName, msg.MetricName, err)
236+
logger.Debugf("skipping scraping column %s of [%s:%s]: %v", k, msg.DBName, msg.MetricName, err)
238237
}
239238
fields[k] = f
240-
} else if dataType == "bool" {
241-
if v.(bool) {
242-
fields[k] = 1
243-
} else {
244-
fields[k] = 0
245-
}
246-
} else {
247-
logger.Debugf("Skipping scraping column %s of [%s:%s], unsupported datatype: %s", k, msg.DBName, msg.MetricName, dataType)
239+
case bool:
240+
fields[k] = map[bool]float64{true: 1, false: 0}[t]
241+
default:
242+
logger.Debugf("skipping scraping column %s of [%s:%s], unsupported datatype: %v", k, msg.DBName, msg.MetricName, t)
248243
continue
249244
}
250245
}

0 commit comments

Comments
 (0)