chore: refactor plugin structure (#18335)
This commit is contained in:
parent
9127e1a913
commit
07fa1b64f8
127 changed files with 861 additions and 938 deletions
114
plugin/prometheus.go
Normal file
114
plugin/prometheus.go
Normal file
|
|
@ -0,0 +1,114 @@
|
|||
package plugin
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"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"
|
||||
)
|
||||
|
||||
// Prometheus provider
|
||||
type Prometheus struct {
|
||||
log *util.Logger
|
||||
api v1.API
|
||||
query string
|
||||
timeout time.Duration
|
||||
}
|
||||
|
||||
func init() {
|
||||
registry.Add("prometheus", NewPrometheusFromConfig)
|
||||
}
|
||||
|
||||
func NewPrometheusFromConfig(other map[string]interface{}) (Plugin, error) {
|
||||
cc := struct {
|
||||
Uri, Query string
|
||||
Timeout time.Duration
|
||||
}{
|
||||
Timeout: request.Timeout,
|
||||
}
|
||||
|
||||
if err := util.DecodeOther(other, &cc); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
log := util.NewLogger("prometheus")
|
||||
|
||||
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, nil
|
||||
}
|
||||
|
||||
func NewPrometheus(log *util.Logger, client api.Client, query string, timeout time.Duration) *Prometheus {
|
||||
p := &Prometheus{
|
||||
log: log,
|
||||
api: v1.NewAPI(client),
|
||||
query: query,
|
||||
timeout: timeout,
|
||||
}
|
||||
|
||||
return p
|
||||
}
|
||||
|
||||
func (p *Prometheus) Query() (model.Value, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), p.timeout)
|
||||
defer cancel()
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
var _ FloatGetter = (*Prometheus)(nil)
|
||||
|
||||
// FloatGetter expects scalar value from query response as float
|
||||
func (p *Prometheus) FloatGetter() (func() (float64, error), error) {
|
||||
return func() (float64, error) {
|
||||
res, err := p.Query()
|
||||
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
|
||||
}, nil
|
||||
}
|
||||
|
||||
var _ IntGetter = (*Prometheus)(nil)
|
||||
|
||||
// IntGetter expects scalar value from query response as int
|
||||
func (p *Prometheus) IntGetter() (func() (int64, error), error) {
|
||||
g, err := p.FloatGetter()
|
||||
return func() (int64, error) {
|
||||
float, err := g()
|
||||
return int64(math.Round(float)), err
|
||||
}, err
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue