Skip to content

Commit

Permalink
Support rounding big decimal metric values.
Browse files Browse the repository at this point in the history
This temporarily fixes the API error: undefined method `new' for BigDecimal:Class.
  • Loading branch information
wi1dcard committed Sep 6, 2021
1 parent bb98237 commit 10033fc
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
21 changes: 14 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ import (
)

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

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

func main() {
flag.Parse()
if lvl, err := log.ParseLevel(*logLevel); err != nil {
log.Fatal(err)
} else {
log.SetLevel(lvl)
}

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

metrics := queryPrometheus(backfill)
metrics := queryPrometheus(backfill, *metricValueRounding)
chunkedMetrics := chunkMetrics(metrics)

for _, m := range chunkedMetrics {
Expand Down
8 changes: 6 additions & 2 deletions metrics.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package main

import (
"encoding/json"
)

type statuspageMetricPoint struct {
Timestamp int64 `json:"timestamp"`
Value float64 `json:"value"`
Timestamp int64 `json:"timestamp"`
Value json.Number `json:"value"`
}

type statuspageMetrics map[string][]statuspageMetricPoint
Expand Down
15 changes: 8 additions & 7 deletions prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"encoding/json"
"fmt"
"math"
"time"
Expand All @@ -13,7 +14,7 @@ import (
"github.com/prometheus/common/model"
)

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

if backfill == nil {
metricPoints, warnings, err = queryInstant(api, query, ctxlog)
metricPoints, warnings, err = queryInstant(api, query, decimal, ctxlog)
} else {
metricPoints, warnings, err = queryRange(api, query, backfill, ctxlog)
metricPoints, warnings, err = queryRange(api, query, decimal, backfill, ctxlog)
}

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

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

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

func queryRange(api prometheus.API, query string, backfill *time.Duration, logger *log.Entry) ([]statuspageMetricPoint, prometheus.Warnings, error) {
func queryRange(api prometheus.API, query string, decimal uint, backfill *time.Duration, logger *log.Entry) ([]statuspageMetricPoint, prometheus.Warnings, error) {
now := time.Now()
start := now.Add(-*backfill)
var (
Expand Down Expand Up @@ -132,7 +133,7 @@ func queryRange(api prometheus.API, query string, backfill *time.Duration, logge
}
metricPoints = append(metricPoints, statuspageMetricPoint{
Timestamp: int64(v.Timestamp / 1000),
Value: float64(v.Value),
Value: json.Number(fmt.Sprintf("%.*f", decimal, v.Value)),
})
}

Expand Down

0 comments on commit 10033fc

Please sign in to comment.