|
|
@@ -2,6 +2,7 @@ package prometheus
|
|
|
|
|
|
import (
|
|
|
"context"
|
|
|
+ "encoding/json"
|
|
|
"fmt"
|
|
|
"strings"
|
|
|
|
|
|
@@ -75,5 +76,70 @@ func QueryPrometheus(
|
|
|
queryParams,
|
|
|
)
|
|
|
|
|
|
- return resp.DoRaw(context.TODO())
|
|
|
+ rawQuery, err := resp.DoRaw(context.TODO())
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ return parseQuery(rawQuery, opts.Metric)
|
|
|
+}
|
|
|
+
|
|
|
+type promRawQuery struct {
|
|
|
+ Data struct {
|
|
|
+ Result []struct {
|
|
|
+ Metric struct {
|
|
|
+ Pod string `json:"pod,omitempty"`
|
|
|
+ } `json:"metric,omitempty"`
|
|
|
+
|
|
|
+ Values [][]interface{} `json:"values"`
|
|
|
+ } `json:"result"`
|
|
|
+ } `json:"data"`
|
|
|
+}
|
|
|
+
|
|
|
+type promParsedSingletonQueryResult struct {
|
|
|
+ Date interface{} `json:"date,omitempty"`
|
|
|
+ CPU interface{} `json:"cpu,omitempty"`
|
|
|
+ Memory interface{} `json:"memory,omitempty"`
|
|
|
+}
|
|
|
+
|
|
|
+type promParsedSingletonQuery struct {
|
|
|
+ Pod string `json:"pod,omitempty"`
|
|
|
+ Results []promParsedSingletonQueryResult `json:"results"`
|
|
|
+}
|
|
|
+
|
|
|
+func parseQuery(rawQuery []byte, metric string) ([]byte, error) {
|
|
|
+ rawQueryObj := &promRawQuery{}
|
|
|
+
|
|
|
+ json.Unmarshal(rawQuery, rawQueryObj)
|
|
|
+
|
|
|
+ res := make([]*promParsedSingletonQuery, 0)
|
|
|
+
|
|
|
+ for _, result := range rawQueryObj.Data.Result {
|
|
|
+ singleton := &promParsedSingletonQuery{
|
|
|
+ Pod: result.Metric.Pod,
|
|
|
+ }
|
|
|
+
|
|
|
+ singletonResults := make([]promParsedSingletonQueryResult, 0)
|
|
|
+
|
|
|
+ for _, values := range result.Values {
|
|
|
+ singletonResult := &promParsedSingletonQueryResult{
|
|
|
+ Date: values[0],
|
|
|
+ }
|
|
|
+
|
|
|
+ if metric == "cpu" {
|
|
|
+ singletonResult.CPU = values[1]
|
|
|
+ } else if metric == "memory" {
|
|
|
+ singletonResult.Memory = values[1]
|
|
|
+ }
|
|
|
+
|
|
|
+ singletonResults = append(singletonResults, *singletonResult)
|
|
|
+ }
|
|
|
+
|
|
|
+ singleton.Results = singletonResults
|
|
|
+
|
|
|
+ res = append(res, singleton)
|
|
|
+ }
|
|
|
+
|
|
|
+ return json.Marshal(res)
|
|
|
}
|