Skip to content

Commit

Permalink
Merge pull request #152 from prometheus-community/superq/refactor_types
Browse files Browse the repository at this point in the history
Use explicit metric type switching
  • Loading branch information
SuperQ committed May 23, 2022
2 parents bcb4232 + 6de63fe commit 5251391
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
11 changes: 9 additions & 2 deletions exporter/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus-community/json_exporter/config"
"github.com/prometheus/client_golang/prometheus"
"k8s.io/client-go/util/jsonpath"
)
Expand All @@ -31,6 +32,7 @@ type JSONMetricCollector struct {

type JSONMetric struct {
Desc *prometheus.Desc
Type config.MetricType
KeyJSONPath string
ValueJSONPath string
LabelsJSONPaths []string
Expand All @@ -44,7 +46,8 @@ func (mc JSONMetricCollector) Describe(ch chan<- *prometheus.Desc) {

func (mc JSONMetricCollector) Collect(ch chan<- prometheus.Metric) {
for _, m := range mc.JSONMetrics {
if m.ValueJSONPath == "" { // ScrapeType is 'value'
switch m.Type {
case config.ValueScrape:
value, err := extractValue(mc.Logger, mc.Data, m.KeyJSONPath, false)
if err != nil {
level.Error(mc.Logger).Log("msg", "Failed to extract value for metric", "path", m.KeyJSONPath, "err", err, "metric", m.Desc)
Expand All @@ -63,7 +66,8 @@ func (mc JSONMetricCollector) Collect(ch chan<- prometheus.Metric) {
level.Error(mc.Logger).Log("msg", "Failed to convert extracted value to float64", "path", m.KeyJSONPath, "value", value, "err", err, "metric", m.Desc)
continue
}
} else { // ScrapeType is 'object'

case config.ObjectScrape:
values, err := extractValue(mc.Logger, mc.Data, m.KeyJSONPath, true)
if err != nil {
level.Error(mc.Logger).Log("msg", "Failed to extract json objects for metric", "err", err, "metric", m.Desc)
Expand Down Expand Up @@ -100,6 +104,9 @@ func (mc JSONMetricCollector) Collect(ch chan<- prometheus.Metric) {
level.Error(mc.Logger).Log("msg", "Failed to convert extracted objects to json", "err", err, "metric", m.Desc)
continue
}
default:
level.Error(mc.Logger).Log("msg", "Unknown scrape config type", "type", m.Type, "metric", m.Desc)
continue
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions exporter/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func CreateMetricsList(c config.Config) ([]JSONMetric, error) {
variableLabelsValues = append(variableLabelsValues, v)
}
jsonMetric := JSONMetric{
Type: config.ValueScrape,
Desc: prometheus.NewDesc(
metric.Name,
metric.Help,
Expand All @@ -92,6 +93,7 @@ func CreateMetricsList(c config.Config) ([]JSONMetric, error) {
variableLabelsValues = append(variableLabelsValues, v)
}
jsonMetric := JSONMetric{
Type: config.ObjectScrape,
Desc: prometheus.NewDesc(
name,
metric.Help,
Expand Down

0 comments on commit 5251391

Please sign in to comment.