2020-08-04 05:50:52 +00:00
|
|
|
// Copyright 2020 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2020-10-03 00:03:56 +00:00
|
|
|
package exporter
|
2020-08-04 05:50:52 +00:00
|
|
|
|
|
|
|
import (
|
2021-01-24 03:51:34 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2020-08-04 05:50:52 +00:00
|
|
|
|
2021-10-03 11:30:18 +00:00
|
|
|
"github.com/go-kit/log"
|
|
|
|
"github.com/go-kit/log/level"
|
2020-08-04 05:50:52 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2021-01-24 03:51:34 +00:00
|
|
|
"k8s.io/client-go/util/jsonpath"
|
2020-08-04 05:50:52 +00:00
|
|
|
)
|
|
|
|
|
2021-10-05 11:21:42 +00:00
|
|
|
type JSONMetricCollector struct {
|
|
|
|
JSONMetrics []JSONMetric
|
2020-08-18 23:56:31 +00:00
|
|
|
Data []byte
|
|
|
|
Logger log.Logger
|
2020-08-04 05:50:52 +00:00
|
|
|
}
|
|
|
|
|
2021-10-05 11:21:42 +00:00
|
|
|
type JSONMetric struct {
|
2020-08-18 23:56:31 +00:00
|
|
|
Desc *prometheus.Desc
|
2021-10-05 11:21:42 +00:00
|
|
|
KeyJSONPath string
|
|
|
|
ValueJSONPath string
|
|
|
|
LabelsJSONPaths []string
|
2020-08-18 23:56:31 +00:00
|
|
|
}
|
2020-08-04 05:50:52 +00:00
|
|
|
|
2021-10-05 11:21:42 +00:00
|
|
|
func (mc JSONMetricCollector) Describe(ch chan<- *prometheus.Desc) {
|
|
|
|
for _, m := range mc.JSONMetrics {
|
2020-08-18 23:56:31 +00:00
|
|
|
ch <- m.Desc
|
|
|
|
}
|
|
|
|
}
|
2020-08-04 05:50:52 +00:00
|
|
|
|
2021-10-05 11:21:42 +00:00
|
|
|
func (mc JSONMetricCollector) Collect(ch chan<- prometheus.Metric) {
|
|
|
|
for _, m := range mc.JSONMetrics {
|
|
|
|
if m.ValueJSONPath == "" { // ScrapeType is 'value'
|
|
|
|
value, err := extractValue(mc.Logger, mc.Data, m.KeyJSONPath, false)
|
2020-08-04 05:50:52 +00:00
|
|
|
if err != nil {
|
2021-10-05 11:21:42 +00:00
|
|
|
level.Error(mc.Logger).Log("msg", "Failed to extract value for metric", "path", m.KeyJSONPath, "err", err, "metric", m.Desc)
|
2020-08-04 05:50:52 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-01-24 03:51:34 +00:00
|
|
|
if floatValue, err := SanitizeValue(value); err == nil {
|
|
|
|
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
m.Desc,
|
|
|
|
prometheus.UntypedValue,
|
|
|
|
floatValue,
|
2021-10-05 11:21:42 +00:00
|
|
|
extractLabels(mc.Logger, mc.Data, m.LabelsJSONPaths)...,
|
2021-01-24 03:51:34 +00:00
|
|
|
)
|
|
|
|
} else {
|
2021-10-05 11:21:42 +00:00
|
|
|
level.Error(mc.Logger).Log("msg", "Failed to convert extracted value to float64", "path", m.KeyJSONPath, "value", value, "err", err, "metric", m.Desc)
|
2020-08-04 05:50:52 +00:00
|
|
|
continue
|
|
|
|
}
|
2021-01-24 03:51:34 +00:00
|
|
|
} else { // ScrapeType is 'object'
|
2021-10-05 11:21:42 +00:00
|
|
|
values, err := extractValue(mc.Logger, mc.Data, m.KeyJSONPath, true)
|
2020-08-04 05:50:52 +00:00
|
|
|
if err != nil {
|
2021-10-03 11:30:18 +00:00
|
|
|
level.Error(mc.Logger).Log("msg", "Failed to extract json objects for metric", "err", err, "metric", m.Desc)
|
2020-08-04 05:50:52 +00:00
|
|
|
continue
|
|
|
|
}
|
2021-01-24 03:51:34 +00:00
|
|
|
|
|
|
|
var jsonData []interface{}
|
|
|
|
if err := json.Unmarshal([]byte(values), &jsonData); err == nil {
|
|
|
|
for _, data := range jsonData {
|
|
|
|
jdata, err := json.Marshal(data)
|
2020-08-04 05:50:52 +00:00
|
|
|
if err != nil {
|
2021-10-05 11:21:42 +00:00
|
|
|
level.Error(mc.Logger).Log("msg", "Failed to marshal data to json", "path", m.ValueJSONPath, "err", err, "metric", m.Desc, "data", data)
|
2021-01-24 03:51:34 +00:00
|
|
|
continue
|
|
|
|
}
|
2021-10-05 11:21:42 +00:00
|
|
|
value, err := extractValue(mc.Logger, jdata, m.ValueJSONPath, false)
|
2021-01-24 03:51:34 +00:00
|
|
|
if err != nil {
|
2021-10-05 11:21:42 +00:00
|
|
|
level.Error(mc.Logger).Log("msg", "Failed to extract value for metric", "path", m.ValueJSONPath, "err", err, "metric", m.Desc)
|
2020-08-04 05:50:52 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-01-24 03:51:34 +00:00
|
|
|
if floatValue, err := SanitizeValue(value); err == nil {
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
m.Desc,
|
|
|
|
prometheus.UntypedValue,
|
|
|
|
floatValue,
|
2021-10-05 11:21:42 +00:00
|
|
|
extractLabels(mc.Logger, jdata, m.LabelsJSONPaths)...,
|
2021-01-24 03:51:34 +00:00
|
|
|
)
|
|
|
|
} else {
|
2021-10-05 11:21:42 +00:00
|
|
|
level.Error(mc.Logger).Log("msg", "Failed to convert extracted value to float64", "path", m.ValueJSONPath, "value", value, "err", err, "metric", m.Desc)
|
2021-01-24 03:51:34 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-08-04 05:50:52 +00:00
|
|
|
}
|
2021-01-24 03:51:34 +00:00
|
|
|
} else {
|
2021-10-03 11:30:18 +00:00
|
|
|
level.Error(mc.Logger).Log("msg", "Failed to convert extracted objects to json", "err", err, "metric", m.Desc)
|
2021-01-24 03:51:34 +00:00
|
|
|
continue
|
2020-08-04 05:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-24 03:51:34 +00:00
|
|
|
// Returns the last matching value at the given json path
|
|
|
|
func extractValue(logger log.Logger, data []byte, path string, enableJSONOutput bool) (string, error) {
|
|
|
|
var jsonData interface{}
|
|
|
|
buf := new(bytes.Buffer)
|
2020-08-04 05:50:52 +00:00
|
|
|
|
2021-01-24 03:51:34 +00:00
|
|
|
j := jsonpath.New("jp")
|
|
|
|
if enableJSONOutput {
|
|
|
|
j.EnableJSONOutput(true)
|
2020-08-04 05:50:52 +00:00
|
|
|
}
|
|
|
|
|
2021-01-24 03:51:34 +00:00
|
|
|
if err := json.Unmarshal(data, &jsonData); err != nil {
|
2021-10-03 11:30:18 +00:00
|
|
|
level.Error(logger).Log("msg", "Failed to unmarshal data to json", "err", err, "data", data)
|
2021-01-24 03:51:34 +00:00
|
|
|
return "", err
|
2020-08-04 05:50:52 +00:00
|
|
|
}
|
|
|
|
|
2021-01-24 03:51:34 +00:00
|
|
|
if err := j.Parse(path); err != nil {
|
2021-10-03 11:30:18 +00:00
|
|
|
level.Error(logger).Log("msg", "Failed to parse jsonpath", "err", err, "path", path, "data", data)
|
2021-01-24 03:51:34 +00:00
|
|
|
return "", err
|
2020-08-04 05:50:52 +00:00
|
|
|
}
|
|
|
|
|
2021-01-24 03:51:34 +00:00
|
|
|
if err := j.Execute(buf, jsonData); err != nil {
|
2021-10-03 11:30:18 +00:00
|
|
|
level.Error(logger).Log("msg", "Failed to execute jsonpath", "err", err, "path", path, "data", data)
|
2021-01-24 03:51:34 +00:00
|
|
|
return "", err
|
2020-08-04 05:50:52 +00:00
|
|
|
}
|
|
|
|
|
2021-01-24 03:51:34 +00:00
|
|
|
// Since we are finally going to extract only float64, unquote if necessary
|
|
|
|
if res, err := jsonpath.UnquoteExtend(buf.String()); err == nil {
|
|
|
|
return res, nil
|
2020-08-04 05:50:52 +00:00
|
|
|
}
|
|
|
|
|
2021-01-24 03:51:34 +00:00
|
|
|
return buf.String(), nil
|
2020-08-04 05:50:52 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 23:56:31 +00:00
|
|
|
// Returns the list of labels created from the list of provided json paths
|
2021-01-24 03:51:34 +00:00
|
|
|
func extractLabels(logger log.Logger, data []byte, paths []string) []string {
|
2020-08-18 23:56:31 +00:00
|
|
|
labels := make([]string, len(paths))
|
|
|
|
for i, path := range paths {
|
2021-01-24 03:51:34 +00:00
|
|
|
if result, err := extractValue(logger, data, path, false); err == nil {
|
|
|
|
labels[i] = result
|
2020-08-04 05:50:52 +00:00
|
|
|
} else {
|
2021-10-03 11:30:18 +00:00
|
|
|
level.Error(logger).Log("msg", "Failed to extract label value", "err", err, "path", path, "data", data)
|
2020-08-04 05:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return labels
|
|
|
|
}
|