Update querying

Signed-off-by: Katyanna Moura <amelie.kn@gmail.com>
This commit is contained in:
Katyanna Moura
2023-10-24 11:54:43 +02:00
parent b237f5a9d2
commit 77a04c49b6

View File

@ -5,7 +5,7 @@ import (
"fmt"
"time"
"github.com/influxdata/influxdb-client-go"
influxdb "github.com/influxdata/influxdb-client-go"
autoscalingv2 "k8s.io/api/autoscaling/v2"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@ -92,14 +92,11 @@ func NewInfluxDBCollector(hpa *autoscalingv2.HorizontalPodAutoscaler, address st
if v, ok := config.Config[influxDBOrgKey]; ok {
org = v
}
influxDbClient, err := influxdb.New(address, token)
if err != nil {
return nil, err
}
influxDbClient := influxdb.NewClient(address, token)
collector.address = address
collector.token = token
collector.org = org
collector.influxDBClient = influxDbClient
collector.influxDBClient = &influxDbClient
return collector, nil
}
@ -111,7 +108,9 @@ type queryResult struct {
// getValue returns the first result gathered from an InfluxDB instance.
func (c *InfluxDBCollector) getValue() (resource.Quantity, error) {
res, err := c.influxDBClient.QueryCSV(context.Background(), c.query, c.org)
client := *c.influxDBClient
queryAPI := client.QueryAPI(c.org)
res, err := queryAPI.Query(context.Background(), c.query)
if err != nil {
return resource.Quantity{}, err
}
@ -119,12 +118,9 @@ func (c *InfluxDBCollector) getValue() (resource.Quantity, error) {
// Keeping just the first result.
if res.Next() {
qr := queryResult{}
if err := res.Unmarshal(&qr); err != nil {
return resource.Quantity{}, fmt.Errorf("error in unmarshaling query result: %v", err)
}
return *resource.NewMilliQuantity(int64(qr.MetricValue*1000), resource.DecimalSI), nil
}
if err := res.Err; err != nil {
if err := res.Err(); err != nil {
return resource.Quantity{}, fmt.Errorf("error in query result: %v", err)
}
return resource.Quantity{}, fmt.Errorf("empty result returned")