Prometheus: align with other providers
This commit is contained in:
parent
a9d4b03a70
commit
69666f2262
1 changed files with 26 additions and 17 deletions
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue