Skip to content

Commit 10033fc

Browse files
committed
Support rounding big decimal metric values.
This temporarily fixes the API error: undefined method `new' for BigDecimal:Class.
1 parent bb98237 commit 10033fc

File tree

3 files changed

+28
-16
lines changed

3 files changed

+28
-16
lines changed

main.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ import (
1818
)
1919

2020
var (
21-
prometheusURL = flag.String("prom", "http://localhost:9090", "URL of Prometheus server")
22-
statusPageAPIKey = flag.String("apikey", "", "Statuspage API key")
23-
statusPageID = flag.String("pageid", "", "Statuspage page ID")
24-
queryConfigFile = flag.String("config", "queries.yaml", "Query config file")
25-
metricInterval = flag.Duration("interval", 30*time.Second, "Metric push interval")
26-
backfillDuration = flag.String("backfill", "", "Backfill the data points in, for example, 5d")
21+
prometheusURL = flag.String("prom", "http://localhost:9090", "URL of Prometheus server")
22+
statusPageAPIKey = flag.String("apikey", "", "Statuspage API key")
23+
statusPageID = flag.String("pageid", "", "Statuspage page ID")
24+
queryConfigFile = flag.String("config", "queries.yaml", "Query config file")
25+
metricInterval = flag.Duration("interval", 30*time.Second, "Metric push interval")
26+
metricValueRounding = flag.Uint("rounding", 6, "Round metric values to specific decimal places")
27+
backfillDuration = flag.String("backfill", "", "Backfill the data points in, for example, 5d")
28+
logLevel = flag.String("log-level", "info", "Log level accepted by Logrus, for example, \"error\", \"warn\", \"info\", \"debug\", ...")
2729

2830
httpClient = &http.Client{
2931
Timeout: 30 * time.Second,
@@ -34,6 +36,11 @@ var (
3436

3537
func main() {
3638
flag.Parse()
39+
if lvl, err := log.ParseLevel(*logLevel); err != nil {
40+
log.Fatal(err)
41+
} else {
42+
log.SetLevel(lvl)
43+
}
3744

3845
qcd, err := ioutil.ReadFile(*queryConfigFile)
3946
if err != nil {
@@ -66,7 +73,7 @@ func main() {
6673
func queryAndPush(backfill *time.Duration) {
6774
log.Infof("Started to query and pushing metrics")
6875

69-
metrics := queryPrometheus(backfill)
76+
metrics := queryPrometheus(backfill, *metricValueRounding)
7077
chunkedMetrics := chunkMetrics(metrics)
7178

7279
for _, m := range chunkedMetrics {

metrics.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package main
22

3+
import (
4+
"encoding/json"
5+
)
6+
37
type statuspageMetricPoint struct {
4-
Timestamp int64 `json:"timestamp"`
5-
Value float64 `json:"value"`
8+
Timestamp int64 `json:"timestamp"`
9+
Value json.Number `json:"value"`
610
}
711

812
type statuspageMetrics map[string][]statuspageMetricPoint

prometheus.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"context"
5+
"encoding/json"
56
"fmt"
67
"math"
78
"time"
@@ -13,7 +14,7 @@ import (
1314
"github.com/prometheus/common/model"
1415
)
1516

16-
func queryPrometheus(backfill *time.Duration) statuspageMetrics {
17+
func queryPrometheus(backfill *time.Duration, decimal uint) statuspageMetrics {
1718
client, err := api.NewClient(api.Config{Address: *prometheusURL})
1819
if err != nil {
1920
log.Fatalf("Couldn't create Prometheus client: %s", err)
@@ -34,9 +35,9 @@ func queryPrometheus(backfill *time.Duration) statuspageMetrics {
3435
)
3536

3637
if backfill == nil {
37-
metricPoints, warnings, err = queryInstant(api, query, ctxlog)
38+
metricPoints, warnings, err = queryInstant(api, query, decimal, ctxlog)
3839
} else {
39-
metricPoints, warnings, err = queryRange(api, query, backfill, ctxlog)
40+
metricPoints, warnings, err = queryRange(api, query, decimal, backfill, ctxlog)
4041
}
4142

4243
for _, w := range warnings {
@@ -54,7 +55,7 @@ func queryPrometheus(backfill *time.Duration) statuspageMetrics {
5455
return metrics
5556
}
5657

57-
func queryInstant(api prometheus.API, query string, logger *log.Entry) ([]statuspageMetricPoint, prometheus.Warnings, error) {
58+
func queryInstant(api prometheus.API, query string, decimal uint, logger *log.Entry) ([]statuspageMetricPoint, prometheus.Warnings, error) {
5859
now := time.Now()
5960
response, warnings, err := api.Query(context.Background(), query, now)
6061

@@ -81,12 +82,12 @@ func queryInstant(api prometheus.API, query string, logger *log.Entry) ([]status
8182
return []statuspageMetricPoint{
8283
{
8384
Timestamp: int64(vec[0].Timestamp / 1000),
84-
Value: float64(value),
85+
Value: json.Number(fmt.Sprintf("%.*f", decimal, value)),
8586
},
8687
}, warnings, nil
8788
}
8889

89-
func queryRange(api prometheus.API, query string, backfill *time.Duration, logger *log.Entry) ([]statuspageMetricPoint, prometheus.Warnings, error) {
90+
func queryRange(api prometheus.API, query string, decimal uint, backfill *time.Duration, logger *log.Entry) ([]statuspageMetricPoint, prometheus.Warnings, error) {
9091
now := time.Now()
9192
start := now.Add(-*backfill)
9293
var (
@@ -132,7 +133,7 @@ func queryRange(api prometheus.API, query string, backfill *time.Duration, logge
132133
}
133134
metricPoints = append(metricPoints, statuspageMetricPoint{
134135
Timestamp: int64(v.Timestamp / 1000),
135-
Value: float64(v.Value),
136+
Value: json.Number(fmt.Sprintf("%.*f", decimal, v.Value)),
136137
})
137138
}
138139

0 commit comments

Comments
 (0)