Skip to content

Commit d43d3ed

Browse files
authored
support custom valuetype like counter, gauge or untyped (prometheus-community#145)
* support custom valuetype like counter, gauge or untyped Signed-off-by: Ben Ye <ben.ye@bytedance.com>
1 parent 5251391 commit d43d3ed

File tree

3 files changed

+37
-11
lines changed

3 files changed

+37
-11
lines changed

config/config.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,28 @@ import (
2222

2323
// Metric contains values that define a metric
2424
type Metric struct {
25-
Name string
26-
Path string
27-
Labels map[string]string
28-
Type MetricType
29-
Help string
30-
Values map[string]string
25+
Name string
26+
Path string
27+
Labels map[string]string
28+
Type ScrapeType
29+
ValueType ValueType
30+
Help string
31+
Values map[string]string
3132
}
3233

33-
type MetricType string
34+
type ScrapeType string
3435

3536
const (
36-
ValueScrape MetricType = "value" // default
37-
ObjectScrape MetricType = "object"
37+
ValueScrape ScrapeType = "value" // default
38+
ObjectScrape ScrapeType = "object"
39+
)
40+
41+
type ValueType string
42+
43+
const (
44+
ValueTypeGauge ValueType = "gauge"
45+
ValueTypeCounter ValueType = "counter"
46+
ValueTypeUntyped ValueType = "untyped"
3847
)
3948

4049
// Config contains metrics and headers defining a configuration
@@ -69,6 +78,9 @@ func LoadConfig(configPath string) (Config, error) {
6978
if config.Metrics[i].Help == "" {
7079
config.Metrics[i].Help = config.Metrics[i].Name
7180
}
81+
if config.Metrics[i].ValueType == "" {
82+
config.Metrics[i].ValueType = ValueTypeUntyped
83+
}
7284
}
7385

7486
return config, nil

exporter/collector.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@ type JSONMetricCollector struct {
3232

3333
type JSONMetric struct {
3434
Desc *prometheus.Desc
35-
Type config.MetricType
35+
Type config.ScrapeType
3636
KeyJSONPath string
3737
ValueJSONPath string
3838
LabelsJSONPaths []string
39+
ValueType prometheus.ValueType
3940
}
4041

4142
func (mc JSONMetricCollector) Describe(ch chan<- *prometheus.Desc) {

exporter/util.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,19 @@ func SanitizeValue(s string) (float64, error) {
6363
}
6464

6565
func CreateMetricsList(c config.Config) ([]JSONMetric, error) {
66-
var metrics []JSONMetric
66+
var (
67+
metrics []JSONMetric
68+
valueType prometheus.ValueType
69+
)
6770
for _, metric := range c.Metrics {
71+
switch metric.ValueType {
72+
case config.ValueTypeGauge:
73+
valueType = prometheus.GaugeValue
74+
case config.ValueTypeCounter:
75+
valueType = prometheus.CounterValue
76+
default:
77+
valueType = prometheus.UntypedValue
78+
}
6879
switch metric.Type {
6980
case config.ValueScrape:
7081
var variableLabels, variableLabelsValues []string
@@ -82,6 +93,7 @@ func CreateMetricsList(c config.Config) ([]JSONMetric, error) {
8293
),
8394
KeyJSONPath: metric.Path,
8495
LabelsJSONPaths: variableLabelsValues,
96+
ValueType: valueType,
8597
}
8698
metrics = append(metrics, jsonMetric)
8799
case config.ObjectScrape:
@@ -103,6 +115,7 @@ func CreateMetricsList(c config.Config) ([]JSONMetric, error) {
103115
KeyJSONPath: metric.Path,
104116
ValueJSONPath: valuePath,
105117
LabelsJSONPaths: variableLabelsValues,
118+
ValueType: valueType,
106119
}
107120
metrics = append(metrics, jsonMetric)
108121
}

0 commit comments

Comments
 (0)