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:
Ben Ye 2022-05-23 09:35:44 -07:00 committed by GitHub
parent 5251391806
commit d43d3ed28e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 11 deletions

View File

@ -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

View File

@ -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) {

View File

@ -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)
}