@@ -2,6 +2,7 @@ package main
22
33import (
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