Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use explicit metric type switching #152

Merged
merged 1 commit into from
May 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:
SuperQ marked this conversation as resolved.
Show resolved Hide resolved
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