mirror of
https://github.com/prometheus-community/json_exporter
synced 2024-12-26 08:22:10 +00:00
support custom valuetype like counter, gauge or untyped (#145)
* support custom valuetype like counter, gauge or untyped Signed-off-by: Ben Ye <ben.ye@bytedance.com>
This commit is contained in:
parent
5251391806
commit
d43d3ed28e
@ -22,19 +22,28 @@ import (
|
||||
|
||||
// Metric contains values that define a metric
|
||||
type Metric struct {
|
||||
Name string
|
||||
Path string
|
||||
Labels map[string]string
|
||||
Type MetricType
|
||||
Help string
|
||||
Values map[string]string
|
||||
Name string
|
||||
Path string
|
||||
Labels map[string]string
|
||||
Type ScrapeType
|
||||
ValueType ValueType
|
||||
Help string
|
||||
Values map[string]string
|
||||
}
|
||||
|
||||
type MetricType string
|
||||
type ScrapeType string
|
||||
|
||||
const (
|
||||
ValueScrape MetricType = "value" // default
|
||||
ObjectScrape MetricType = "object"
|
||||
ValueScrape ScrapeType = "value" // default
|
||||
ObjectScrape ScrapeType = "object"
|
||||
)
|
||||
|
||||
type ValueType string
|
||||
|
||||
const (
|
||||
ValueTypeGauge ValueType = "gauge"
|
||||
ValueTypeCounter ValueType = "counter"
|
||||
ValueTypeUntyped ValueType = "untyped"
|
||||
)
|
||||
|
||||
// Config contains metrics and headers defining a configuration
|
||||
@ -69,6 +78,9 @@ func LoadConfig(configPath string) (Config, error) {
|
||||
if config.Metrics[i].Help == "" {
|
||||
config.Metrics[i].Help = config.Metrics[i].Name
|
||||
}
|
||||
if config.Metrics[i].ValueType == "" {
|
||||
config.Metrics[i].ValueType = ValueTypeUntyped
|
||||
}
|
||||
}
|
||||
|
||||
return config, nil
|
||||
|
@ -32,10 +32,11 @@ type JSONMetricCollector struct {
|
||||
|
||||
type JSONMetric struct {
|
||||
Desc *prometheus.Desc
|
||||
Type config.MetricType
|
||||
Type config.ScrapeType
|
||||
KeyJSONPath string
|
||||
ValueJSONPath string
|
||||
LabelsJSONPaths []string
|
||||
ValueType prometheus.ValueType
|
||||
}
|
||||
|
||||
func (mc JSONMetricCollector) Describe(ch chan<- *prometheus.Desc) {
|
||||
|
@ -63,8 +63,19 @@ func SanitizeValue(s string) (float64, error) {
|
||||
}
|
||||
|
||||
func CreateMetricsList(c config.Config) ([]JSONMetric, error) {
|
||||
var metrics []JSONMetric
|
||||
var (
|
||||
metrics []JSONMetric
|
||||
valueType prometheus.ValueType
|
||||
)
|
||||
for _, metric := range c.Metrics {
|
||||
switch metric.ValueType {
|
||||
case config.ValueTypeGauge:
|
||||
valueType = prometheus.GaugeValue
|
||||
case config.ValueTypeCounter:
|
||||
valueType = prometheus.CounterValue
|
||||
default:
|
||||
valueType = prometheus.UntypedValue
|
||||
}
|
||||
switch metric.Type {
|
||||
case config.ValueScrape:
|
||||
var variableLabels, variableLabelsValues []string
|
||||
@ -82,6 +93,7 @@ func CreateMetricsList(c config.Config) ([]JSONMetric, error) {
|
||||
),
|
||||
KeyJSONPath: metric.Path,
|
||||
LabelsJSONPaths: variableLabelsValues,
|
||||
ValueType: valueType,
|
||||
}
|
||||
metrics = append(metrics, jsonMetric)
|
||||
case config.ObjectScrape:
|
||||
@ -103,6 +115,7 @@ func CreateMetricsList(c config.Config) ([]JSONMetric, error) {
|
||||
KeyJSONPath: metric.Path,
|
||||
ValueJSONPath: valuePath,
|
||||
LabelsJSONPaths: variableLabelsValues,
|
||||
ValueType: valueType,
|
||||
}
|
||||
metrics = append(metrics, jsonMetric)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user