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"
|
2024-10-18 02:26:10 +00:00
|
|
|
"log/slog"
|
2022-08-28 11:48:35 +00:00
|
|
|
"time"
|
2020-08-04 05:50:52 +00:00
|
|
|
|
2022-05-22 13:32:36 +00:00
|
|
|
"github.com/prometheus-community/json_exporter/config"
|
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
|
2024-10-18 02:26:10 +00:00
|
|
|
Logger *slog.Logger
|
2020-08-04 05:50:52 +00:00
|
|
|
}
|
|
|
|
|
2021-10-05 11:21:42 +00:00
|
|
|
type JSONMetric struct {
|
2022-08-28 11:48:35 +00:00
|
|
|
Desc *prometheus.Desc
|
|
|
|
Type config.ScrapeType
|
|
|
|
KeyJSONPath string
|
|
|
|
ValueJSONPath string
|
|
|
|
LabelsJSONPaths []string
|
|
|
|
ValueType prometheus.ValueType
|
|
|
|
EpochTimestampJSONPath 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 {
|
2022-05-22 13:32:36 +00:00
|
|
|
switch m.Type {
|
|
|
|
case config.ValueScrape:
|
2021-10-05 11:21:42 +00:00
|
|
|
value, err := extractValue(mc.Logger, mc.Data, m.KeyJSONPath, false)
|
2020-08-04 05:50:52 +00:00
|
|
|
if err != nil {
|
2024-10-18 02:26:10 +00:00
|
|
|
mc.Logger.Error("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 {
|
2022-08-28 11:48:35 +00:00
|
|
|
metric := prometheus.MustNewConstMetric(
|
2021-01-24 03:51:34 +00:00
|
|
|
m.Desc,
|
2022-07-01 07:20:07 +00:00
|
|
|
m.ValueType,
|
2021-01-24 03:51:34 +00:00
|
|
|
floatValue,
|
2021-10-05 11:21:42 +00:00
|
|
|
extractLabels(mc.Logger, mc.Data, m.LabelsJSONPaths)...,
|
2021-01-24 03:51:34 +00:00
|
|
|
)
|
2022-08-28 11:48:35 +00:00
|
|
|
ch <- timestampMetric(mc.Logger, m, mc.Data, metric)
|
2021-01-24 03:51:34 +00:00
|
|
|
} else {
|
2024-10-18 02:26:10 +00:00
|
|
|
mc.Logger.Error("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
|
|
|
|
}
|
2022-05-22 13:32:36 +00:00
|
|
|
|
|
|
|
case config.ObjectScrape:
|
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 {
|
2024-10-18 02:26:10 +00:00
|
|
|
mc.Logger.Error("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 {
|
2024-10-18 02:26:10 +00:00
|
|
|
mc.Logger.Error("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 {
|
2024-10-18 02:26:10 +00:00
|
|
|
mc.Logger.Error("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 {
|
2022-08-28 11:48:35 +00:00
|
|
|
metric := prometheus.MustNewConstMetric(
|
2021-01-24 03:51:34 +00:00
|
|
|
m.Desc,
|
2022-07-01 07:20:07 +00:00
|
|
|
m.ValueType,
|
2021-01-24 03:51:34 +00:00
|
|
|
floatValue,
|
2021-10-05 11:21:42 +00:00
|
|
|
extractLabels(mc.Logger, jdata, m.LabelsJSONPaths)...,
|
2021-01-24 03:51:34 +00:00
|
|
|
)
|
2022-08-28 11:48:35 +00:00
|
|
|
ch <- timestampMetric(mc.Logger, m, jdata, metric)
|
2021-01-24 03:51:34 +00:00
|
|
|
} else {
|
2024-10-18 02:26:10 +00:00
|
|
|
mc.Logger.Error("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 {
|
2024-10-18 02:26:10 +00:00
|
|
|
mc.Logger.Error("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
|
|
|
}
|
2022-05-22 13:32:36 +00:00
|
|
|
default:
|
2024-10-18 02:26:10 +00:00
|
|
|
mc.Logger.Error("Unknown scrape config type", "type", m.Type, "metric", m.Desc)
|
2022-05-22 13:32:36 +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
|
2024-10-18 02:26:10 +00:00
|
|
|
func extractValue(logger *slog.Logger, data []byte, path string, enableJSONOutput bool) (string, error) {
|
2021-01-24 03:51:34 +00:00
|
|
|
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 {
|
2024-10-18 02:26:10 +00:00
|
|
|
logger.Error("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 {
|
2024-10-18 02:26:10 +00:00
|
|
|
logger.Error("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 {
|
2024-10-18 02:26:10 +00:00
|
|
|
logger.Error("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
|
2024-10-18 02:26:10 +00:00
|
|
|
func extractLabels(logger *slog.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 {
|
2024-10-18 02:26:10 +00:00
|
|
|
logger.Error("Failed to extract label value", "err", err, "path", path, "data", data)
|
2020-08-04 05:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return labels
|
|
|
|
}
|
2022-08-28 11:48:35 +00:00
|
|
|
|
2024-10-18 02:26:10 +00:00
|
|
|
func timestampMetric(logger *slog.Logger, m JSONMetric, data []byte, pm prometheus.Metric) prometheus.Metric {
|
2022-08-28 11:48:35 +00:00
|
|
|
if m.EpochTimestampJSONPath == "" {
|
|
|
|
return pm
|
|
|
|
}
|
|
|
|
ts, err := extractValue(logger, data, m.EpochTimestampJSONPath, false)
|
|
|
|
if err != nil {
|
2024-10-18 02:26:10 +00:00
|
|
|
logger.Error("Failed to extract timestamp for metric", "path", m.KeyJSONPath, "err", err, "metric", m.Desc)
|
2022-08-28 11:48:35 +00:00
|
|
|
return pm
|
|
|
|
}
|
|
|
|
epochTime, err := SanitizeIntValue(ts)
|
|
|
|
if err != nil {
|
2024-10-18 02:26:10 +00:00
|
|
|
logger.Error("Failed to parse timestamp for metric", "path", m.KeyJSONPath, "err", err, "metric", m.Desc)
|
2022-08-28 11:48:35 +00:00
|
|
|
return pm
|
|
|
|
}
|
|
|
|
timestamp := time.UnixMilli(epochTime)
|
|
|
|
return prometheus.NewMetricWithTimestamp(timestamp, pm)
|
|
|
|
}
|