From 69666f226249c9a1c5265e7afc8cefc06aa9f1a9 Mon Sep 17 00:00:00 2001 From: andig Date: Sun, 5 Jun 2022 12:23:07 +0200 Subject: [PATCH] Prometheus: align with other providers --- provider/prometheus.go | 43 +++++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/provider/prometheus.go b/provider/prometheus.go index b524aa18a..e1c1c8ff6 100644 --- a/provider/prometheus.go +++ b/provider/prometheus.go @@ -3,23 +3,23 @@ package provider import ( "context" "fmt" - "github.com/evcc-io/evcc/util/request" "math" "time" + "github.com/evcc-io/evcc/util" + "github.com/evcc-io/evcc/util/request" + "github.com/evcc-io/evcc/util/transport" + "github.com/prometheus/client_golang/api" v1 "github.com/prometheus/client_golang/api/prometheus/v1" "github.com/prometheus/common/model" - - "github.com/evcc-io/evcc/util" - "github.com/prometheus/client_golang/api" ) // Prometheus provider type Prometheus struct { - log *util.Logger - clientV1Api v1.API - query string - timeout time.Duration + log *util.Logger + api v1.API + query string + timeout time.Duration } func init() { @@ -28,8 +28,7 @@ func init() { func NewPrometheusFromConfig(other map[string]interface{}) (IntProvider, error) { cc := struct { - api.Config `mapstructure:",squash"` - Query string + Uri, Query string Timeout time.Duration }{ Timeout: request.Timeout, @@ -41,22 +40,27 @@ func NewPrometheusFromConfig(other map[string]interface{}) (IntProvider, error) log := util.NewLogger("prometheus") - client, err := api.NewClient(cc.Config) + config := api.Config{ + Address: cc.Uri, + RoundTripper: transport.Default(), + } + + client, err := api.NewClient(config) if err != nil { return nil, err } p := NewPrometheus(log, client, cc.Query, cc.Timeout) - return p, err + return p, nil } func NewPrometheus(log *util.Logger, client api.Client, query string, timeout time.Duration) *Prometheus { p := &Prometheus{ - log: log, - clientV1Api: v1.NewAPI(client), - query: query, - timeout: timeout, + log: log, + api: v1.NewAPI(client), + query: query, + timeout: timeout, } return p @@ -65,14 +69,17 @@ func NewPrometheus(log *util.Logger, client api.Client, query string, timeout ti func (p *Prometheus) Query() (model.Value, error) { ctx, cancel := context.WithTimeout(context.Background(), p.timeout) defer cancel() - res, warn, err := p.clientV1Api.Query(ctx, p.query, time.Now()) + + res, warn, err := p.api.Query(ctx, p.query, time.Now()) if err != nil { return nil, err } + p.log.TRACE.Printf("query %q: %+v", p.query, res) if len(warn) > 0 { p.log.WARN.Printf("query %q returned warnings: %v", p.query, warn) } + return res, nil } @@ -85,9 +92,11 @@ func (p *Prometheus) FloatGetter() func() (float64, error) { if err != nil { return 0, err } + if res.Type() != model.ValScalar { return 0, fmt.Errorf("query returned value of type %q, expected %q, consider wrapping query in scalar()", res.Type().String(), model.ValScalar.String()) } + scalarVal := res.(*model.Scalar) return float64(scalarVal.Value), nil }