json_exporter/internal/util.go
rustyclock c869516e98
Refactor the complete code
* Make the working of this exporter similar to that of the blackbox_exporter to allow probing multiple targets.
* Add functionality to add headers to the request
* Update the example config to use `headers` as well as the `metrics` keys in alignment with the new code
* Add default header 'Accept: application/json'

Signed-off-by: rustyclock <rustyclock@protonmail.com>
2020-08-04 15:21:24 +09:00

103 lines
2.9 KiB
Go

// 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.
package internal
import (
"fmt"
"math"
"strconv"
"strings"
"github.com/kawamuray/jsonpath"
"github.com/prometheus-community/json_exporter/config"
"github.com/prometheus/client_golang/prometheus"
)
func MakeMetricName(parts ...string) string {
return strings.Join(parts, "_")
}
func SanitizeValue(v *jsonpath.Result) (float64, error) {
var value float64
var boolValue bool
var err error
switch v.Type {
case jsonpath.JsonNumber:
value, err = parseValue(v.Value)
case jsonpath.JsonString:
// If it is a string, lets pull off the quotes and attempt to parse it as a number
value, err = parseValue(v.Value[1 : len(v.Value)-1])
case jsonpath.JsonNull:
value = math.NaN()
case jsonpath.JsonBool:
if boolValue, err = strconv.ParseBool(string(v.Value)); boolValue {
value = 1.0
} else {
value = 0.0
}
default:
value, err = parseValue(v.Value)
}
if err != nil {
// Should never happen.
return -1.0, err
}
return value, err
}
func parseValue(bytes []byte) (float64, error) {
value, err := strconv.ParseFloat(string(bytes), 64)
if err != nil {
return -1.0, fmt.Errorf("failed to parse value as float;value:<%s>", bytes)
}
return value, nil
}
func CreateMetricsList(r *prometheus.Registry, c config.Config) ([]JsonGaugeCollector, error) {
var metrics []JsonGaugeCollector
for _, metric := range c.Metrics {
switch metric.Type {
case config.ValueScrape:
jsonCollector := JsonGaugeCollector{
GaugeVec: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: metric.Name,
Help: metric.Help,
}, metric.LabelNames()),
KeyJsonPath: metric.Path,
LabelsJsonPath: metric.Labels,
}
r.MustRegister(jsonCollector)
metrics = append(metrics, jsonCollector)
case config.ObjectScrape:
for subName, valuePath := range metric.Values {
name := MakeMetricName(metric.Name, subName)
jsonCollector := JsonGaugeCollector{
GaugeVec: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: name,
Help: name,
}, metric.LabelNames()),
KeyJsonPath: metric.Path,
ValueJsonPath: valuePath,
LabelsJsonPath: metric.Labels,
}
r.MustRegister(jsonCollector)
metrics = append(metrics, jsonCollector)
}
default:
return nil, fmt.Errorf("Unknown metric type: '%s', for metric: '%s'", metric.Type, metric.Name)
}
}
return metrics, nil
}