Skip to content

Commit c487740

Browse files
authored
Merge pull request prometheus-community#125 from prometheus-community/superq/bump
Update build
2 parents 8a311b8 + eaf94d3 commit c487740

File tree

10 files changed

+76
-76
lines changed

10 files changed

+76
-76
lines changed

.circleci/config.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ executors:
88
# This must match .promu.yml.
99
golang:
1010
docker:
11-
- image: circleci/golang:1.16
11+
- image: circleci/golang:1.17
1212

1313
jobs:
1414
test:

.golangci.yml

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
linters:
2+
enable:
3+
- golint
24
disable:
35
# Disable soon to deprecated[1] linters that lead to false
46
# positives when build tags disable certain files[2]

.promu.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
go:
22
# This must match .circle/config.yml.
3-
version: 1.16
3+
version: 1.17
44
repository:
55
path: github.com/prometheus-community/json_exporter
66
build:

cmd/main.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ func Run() {
5858
level.Error(logger).Log("msg", "Error loading config", "err", err)
5959
os.Exit(1)
6060
}
61-
configJson, err := json.Marshal(config)
61+
configJSON, err := json.Marshal(config)
6262
if err != nil {
6363
level.Error(logger).Log("msg", "Failed to marshal config to JSON", "err", err)
6464
}
65-
level.Info(logger).Log("msg", "Loaded config file", "config", string(configJson))
65+
level.Info(logger).Log("msg", "Loaded config file", "config", string(configJSON))
6666

6767
if *configCheck {
6868
os.Exit(0)
@@ -93,7 +93,7 @@ func probeHandler(w http.ResponseWriter, r *http.Request, logger log.Logger, con
9393
level.Error(logger).Log("msg", "Failed to create metrics list from config", "err", err)
9494
}
9595

96-
jsonMetricCollector := exporter.JsonMetricCollector{JsonMetrics: metrics}
96+
jsonMetricCollector := exporter.JSONMetricCollector{JSONMetrics: metrics}
9797
jsonMetricCollector.Logger = logger
9898

9999
target := r.URL.Query().Get("target")
@@ -102,8 +102,8 @@ func probeHandler(w http.ResponseWriter, r *http.Request, logger log.Logger, con
102102
return
103103
}
104104

105-
fetcher := exporter.NewJsonFetcher(ctx, logger, config, r.URL.Query())
106-
data, err := fetcher.FetchJson(target)
105+
fetcher := exporter.NewJSONFetcher(ctx, logger, config, r.URL.Query())
106+
data, err := fetcher.FetchJSON(target)
107107
if err != nil {
108108
http.Error(w, "Failed to fetch JSON response. TARGET: "+target+", ERROR: "+err.Error(), http.StatusServiceUnavailable)
109109
return

cmd/main_test.go

+16-16
Original file line numberDiff line numberDiff line change
@@ -224,29 +224,29 @@ func TestHTTPHeaders(t *testing.T) {
224224
// Test is the body template is correctly rendered
225225
func TestBodyPostTemplate(t *testing.T) {
226226
bodyTests := []struct {
227-
Body config.ConfigBody
227+
Body config.Body
228228
ShouldSucceed bool
229229
Result string
230230
}{
231231
{
232-
Body: config.ConfigBody{Content: "something static like pi, 3.14"},
232+
Body: config.Body{Content: "something static like pi, 3.14"},
233233
ShouldSucceed: true,
234234
},
235235
{
236-
Body: config.ConfigBody{Content: "arbitrary dynamic value pass: {{ randInt 12 30 }}", Templatize: false},
236+
Body: config.Body{Content: "arbitrary dynamic value pass: {{ randInt 12 30 }}", Templatize: false},
237237
ShouldSucceed: true,
238238
},
239239
{
240-
Body: config.ConfigBody{Content: "arbitrary dynamic value fail: {{ randInt 12 30 }}", Templatize: true},
240+
Body: config.Body{Content: "arbitrary dynamic value fail: {{ randInt 12 30 }}", Templatize: true},
241241
ShouldSucceed: false,
242242
},
243243
{
244-
Body: config.ConfigBody{Content: "templatized mutated value: {{ upper `hello` }} is now all caps", Templatize: true},
244+
Body: config.Body{Content: "templatized mutated value: {{ upper `hello` }} is now all caps", Templatize: true},
245245
Result: "templatized mutated value: HELLO is now all caps",
246246
ShouldSucceed: true,
247247
},
248248
{
249-
Body: config.ConfigBody{Content: "value should be {{ lower `All Small` | trunc 3 }}", Templatize: true},
249+
Body: config.Body{Content: "value should be {{ lower `All Small` | trunc 3 }}", Templatize: true},
250250
Result: "value should be all",
251251
ShouldSucceed: true,
252252
},
@@ -283,58 +283,58 @@ func TestBodyPostTemplate(t *testing.T) {
283283
// Test is the query parameters are correctly replaced in the provided body template
284284
func TestBodyPostQuery(t *testing.T) {
285285
bodyTests := []struct {
286-
Body config.ConfigBody
286+
Body config.Body
287287
ShouldSucceed bool
288288
Result string
289289
QueryParams map[string]string
290290
}{
291291
{
292-
Body: config.ConfigBody{Content: "pi has {{ .piValue | first }} value", Templatize: true},
292+
Body: config.Body{Content: "pi has {{ .piValue | first }} value", Templatize: true},
293293
ShouldSucceed: true,
294294
Result: "pi has 3.14 value",
295295
QueryParams: map[string]string{"piValue": "3.14"},
296296
},
297297
{
298-
Body: config.ConfigBody{Content: `{ "pi": "{{ .piValue | first }}" }`, Templatize: true},
298+
Body: config.Body{Content: `{ "pi": "{{ .piValue | first }}" }`, Templatize: true},
299299
ShouldSucceed: true,
300300
Result: `{ "pi": "3.14" }`,
301301
QueryParams: map[string]string{"piValue": "3.14"},
302302
},
303303
{
304-
Body: config.ConfigBody{Content: "pi has {{ .anotherQuery | first }} value", Templatize: true},
304+
Body: config.Body{Content: "pi has {{ .anotherQuery | first }} value", Templatize: true},
305305
ShouldSucceed: true,
306306
Result: "pi has very high value",
307307
QueryParams: map[string]string{"piValue": "3.14", "anotherQuery": "very high"},
308308
},
309309
{
310-
Body: config.ConfigBody{Content: "pi has {{ .piValue }} value", Templatize: true},
310+
Body: config.Body{Content: "pi has {{ .piValue }} value", Templatize: true},
311311
ShouldSucceed: false,
312312
QueryParams: map[string]string{"piValue": "3.14", "anotherQuery": "dummy value"},
313313
},
314314
{
315-
Body: config.ConfigBody{Content: "pi has {{ .piValue }} value", Templatize: true},
315+
Body: config.Body{Content: "pi has {{ .piValue }} value", Templatize: true},
316316
ShouldSucceed: true,
317317
Result: "pi has [3.14] value",
318318
QueryParams: map[string]string{"piValue": "3.14", "anotherQuery": "dummy value"},
319319
},
320320
{
321-
Body: config.ConfigBody{Content: "value of {{ upper `pi` | repeat 3 }} is {{ .anotherQuery | first }}", Templatize: true},
321+
Body: config.Body{Content: "value of {{ upper `pi` | repeat 3 }} is {{ .anotherQuery | first }}", Templatize: true},
322322
ShouldSucceed: true,
323323
Result: "value of PIPIPI is dummy value",
324324
QueryParams: map[string]string{"piValue": "3.14", "anotherQuery": "dummy value"},
325325
},
326326
{
327-
Body: config.ConfigBody{Content: "pi has {{ .piValue }} value", Templatize: true},
327+
Body: config.Body{Content: "pi has {{ .piValue }} value", Templatize: true},
328328
ShouldSucceed: true,
329329
Result: "pi has [] value",
330330
},
331331
{
332-
Body: config.ConfigBody{Content: "pi has {{ .piValue | first }} value", Templatize: true},
332+
Body: config.Body{Content: "pi has {{ .piValue | first }} value", Templatize: true},
333333
ShouldSucceed: true,
334334
Result: "pi has <no value> value",
335335
},
336336
{
337-
Body: config.ConfigBody{Content: "value of pi is 3.14", Templatize: true},
337+
Body: config.Body{Content: "value of pi is 3.14", Templatize: true},
338338
ShouldSucceed: true,
339339
},
340340
}

config/config.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ type Config struct {
4242
Headers map[string]string `yaml:"headers,omitempty"`
4343
Metrics []Metric `yaml:"metrics"`
4444
HTTPClientConfig pconfig.HTTPClientConfig `yaml:"http_client_config,omitempty"`
45-
Body ConfigBody `yaml:"body,omitempty"`
45+
Body Body `yaml:"body,omitempty"`
4646
}
4747

48-
type ConfigBody struct {
48+
type Body struct {
4949
Content string `yaml:"content"`
5050
Templatize bool `yaml:"templatize,omitempty"`
5151
}

exporter/collector.go

+21-21
Original file line numberDiff line numberDiff line change
@@ -23,31 +23,31 @@ import (
2323
"k8s.io/client-go/util/jsonpath"
2424
)
2525

26-
type JsonMetricCollector struct {
27-
JsonMetrics []JsonMetric
26+
type JSONMetricCollector struct {
27+
JSONMetrics []JSONMetric
2828
Data []byte
2929
Logger log.Logger
3030
}
3131

32-
type JsonMetric struct {
32+
type JSONMetric struct {
3333
Desc *prometheus.Desc
34-
KeyJsonPath string
35-
ValueJsonPath string
36-
LabelsJsonPaths []string
34+
KeyJSONPath string
35+
ValueJSONPath string
36+
LabelsJSONPaths []string
3737
}
3838

39-
func (mc JsonMetricCollector) Describe(ch chan<- *prometheus.Desc) {
40-
for _, m := range mc.JsonMetrics {
39+
func (mc JSONMetricCollector) Describe(ch chan<- *prometheus.Desc) {
40+
for _, m := range mc.JSONMetrics {
4141
ch <- m.Desc
4242
}
4343
}
4444

45-
func (mc JsonMetricCollector) Collect(ch chan<- prometheus.Metric) {
46-
for _, m := range mc.JsonMetrics {
47-
if m.ValueJsonPath == "" { // ScrapeType is 'value'
48-
value, err := extractValue(mc.Logger, mc.Data, m.KeyJsonPath, false)
45+
func (mc JSONMetricCollector) Collect(ch chan<- prometheus.Metric) {
46+
for _, m := range mc.JSONMetrics {
47+
if m.ValueJSONPath == "" { // ScrapeType is 'value'
48+
value, err := extractValue(mc.Logger, mc.Data, m.KeyJSONPath, false)
4949
if err != nil {
50-
level.Error(mc.Logger).Log("msg", "Failed to extract value for metric", "path", m.KeyJsonPath, "err", err, "metric", m.Desc)
50+
level.Error(mc.Logger).Log("msg", "Failed to extract value for metric", "path", m.KeyJSONPath, "err", err, "metric", m.Desc)
5151
continue
5252
}
5353

@@ -57,14 +57,14 @@ func (mc JsonMetricCollector) Collect(ch chan<- prometheus.Metric) {
5757
m.Desc,
5858
prometheus.UntypedValue,
5959
floatValue,
60-
extractLabels(mc.Logger, mc.Data, m.LabelsJsonPaths)...,
60+
extractLabels(mc.Logger, mc.Data, m.LabelsJSONPaths)...,
6161
)
6262
} else {
63-
level.Error(mc.Logger).Log("msg", "Failed to convert extracted value to float64", "path", m.KeyJsonPath, "value", value, "err", err, "metric", m.Desc)
63+
level.Error(mc.Logger).Log("msg", "Failed to convert extracted value to float64", "path", m.KeyJSONPath, "value", value, "err", err, "metric", m.Desc)
6464
continue
6565
}
6666
} else { // ScrapeType is 'object'
67-
values, err := extractValue(mc.Logger, mc.Data, m.KeyJsonPath, true)
67+
values, err := extractValue(mc.Logger, mc.Data, m.KeyJSONPath, true)
6868
if err != nil {
6969
level.Error(mc.Logger).Log("msg", "Failed to extract json objects for metric", "err", err, "metric", m.Desc)
7070
continue
@@ -75,12 +75,12 @@ func (mc JsonMetricCollector) Collect(ch chan<- prometheus.Metric) {
7575
for _, data := range jsonData {
7676
jdata, err := json.Marshal(data)
7777
if err != nil {
78-
level.Error(mc.Logger).Log("msg", "Failed to marshal data to json", "path", m.ValueJsonPath, "err", err, "metric", m.Desc, "data", data)
78+
level.Error(mc.Logger).Log("msg", "Failed to marshal data to json", "path", m.ValueJSONPath, "err", err, "metric", m.Desc, "data", data)
7979
continue
8080
}
81-
value, err := extractValue(mc.Logger, jdata, m.ValueJsonPath, false)
81+
value, err := extractValue(mc.Logger, jdata, m.ValueJSONPath, false)
8282
if err != nil {
83-
level.Error(mc.Logger).Log("msg", "Failed to extract value for metric", "path", m.ValueJsonPath, "err", err, "metric", m.Desc)
83+
level.Error(mc.Logger).Log("msg", "Failed to extract value for metric", "path", m.ValueJSONPath, "err", err, "metric", m.Desc)
8484
continue
8585
}
8686

@@ -89,10 +89,10 @@ func (mc JsonMetricCollector) Collect(ch chan<- prometheus.Metric) {
8989
m.Desc,
9090
prometheus.UntypedValue,
9191
floatValue,
92-
extractLabels(mc.Logger, jdata, m.LabelsJsonPaths)...,
92+
extractLabels(mc.Logger, jdata, m.LabelsJSONPaths)...,
9393
)
9494
} else {
95-
level.Error(mc.Logger).Log("msg", "Failed to convert extracted value to float64", "path", m.ValueJsonPath, "value", value, "err", err, "metric", m.Desc)
95+
level.Error(mc.Logger).Log("msg", "Failed to convert extracted value to float64", "path", m.ValueJSONPath, "value", value, "err", err, "metric", m.Desc)
9696
continue
9797
}
9898
}

exporter/util.go

+19-21
Original file line numberDiff line numberDiff line change
@@ -39,33 +39,31 @@ func MakeMetricName(parts ...string) string {
3939
}
4040

4141
func SanitizeValue(s string) (float64, error) {
42+
var err error
4243
var value float64
4344
var resultErr string
4445

45-
if value, err := strconv.ParseFloat(s, 64); err == nil {
46+
if value, err = strconv.ParseFloat(s, 64); err == nil {
4647
return value, nil
47-
} else {
48-
resultErr = fmt.Sprintf("%s", err)
4948
}
49+
resultErr = fmt.Sprintf("%s", err)
5050

5151
if boolValue, err := strconv.ParseBool(s); err == nil {
5252
if boolValue {
5353
return 1.0, nil
54-
} else {
55-
return 0.0, nil
5654
}
57-
} else {
58-
resultErr = resultErr + "; " + fmt.Sprintf("%s", err)
55+
return 0.0, nil
5956
}
57+
resultErr = resultErr + "; " + fmt.Sprintf("%s", err)
6058

6159
if s == "<nil>" {
6260
return math.NaN(), nil
6361
}
6462
return value, fmt.Errorf(resultErr)
6563
}
6664

67-
func CreateMetricsList(c config.Config) ([]JsonMetric, error) {
68-
var metrics []JsonMetric
65+
func CreateMetricsList(c config.Config) ([]JSONMetric, error) {
66+
var metrics []JSONMetric
6967
for _, metric := range c.Metrics {
7068
switch metric.Type {
7169
case config.ValueScrape:
@@ -74,15 +72,15 @@ func CreateMetricsList(c config.Config) ([]JsonMetric, error) {
7472
variableLabels = append(variableLabels, k)
7573
variableLabelsValues = append(variableLabelsValues, v)
7674
}
77-
jsonMetric := JsonMetric{
75+
jsonMetric := JSONMetric{
7876
Desc: prometheus.NewDesc(
7977
metric.Name,
8078
metric.Help,
8179
variableLabels,
8280
nil,
8381
),
84-
KeyJsonPath: metric.Path,
85-
LabelsJsonPaths: variableLabelsValues,
82+
KeyJSONPath: metric.Path,
83+
LabelsJSONPaths: variableLabelsValues,
8684
}
8785
metrics = append(metrics, jsonMetric)
8886
case config.ObjectScrape:
@@ -93,16 +91,16 @@ func CreateMetricsList(c config.Config) ([]JsonMetric, error) {
9391
variableLabels = append(variableLabels, k)
9492
variableLabelsValues = append(variableLabelsValues, v)
9593
}
96-
jsonMetric := JsonMetric{
94+
jsonMetric := JSONMetric{
9795
Desc: prometheus.NewDesc(
9896
name,
9997
metric.Help,
10098
variableLabels,
10199
nil,
102100
),
103-
KeyJsonPath: metric.Path,
104-
ValueJsonPath: valuePath,
105-
LabelsJsonPaths: variableLabelsValues,
101+
KeyJSONPath: metric.Path,
102+
ValueJSONPath: valuePath,
103+
LabelsJSONPaths: variableLabelsValues,
106104
}
107105
metrics = append(metrics, jsonMetric)
108106
}
@@ -113,17 +111,17 @@ func CreateMetricsList(c config.Config) ([]JsonMetric, error) {
113111
return metrics, nil
114112
}
115113

116-
type jsonFetcher struct {
114+
type JSONFetcher struct {
117115
config config.Config
118116
ctx context.Context
119117
logger log.Logger
120118
method string
121119
body io.Reader
122120
}
123121

124-
func NewJsonFetcher(ctx context.Context, logger log.Logger, c config.Config, tplValues url.Values) *jsonFetcher {
122+
func NewJSONFetcher(ctx context.Context, logger log.Logger, c config.Config, tplValues url.Values) *JSONFetcher {
125123
method, body := renderBody(logger, c.Body, tplValues)
126-
return &jsonFetcher{
124+
return &JSONFetcher{
127125
config: c,
128126
ctx: ctx,
129127
logger: logger,
@@ -132,7 +130,7 @@ func NewJsonFetcher(ctx context.Context, logger log.Logger, c config.Config, tpl
132130
}
133131
}
134132

135-
func (f *jsonFetcher) FetchJson(endpoint string) ([]byte, error) {
133+
func (f *JSONFetcher) FetchJSON(endpoint string) ([]byte, error) {
136134
httpClientConfig := f.config.HTTPClientConfig
137135
client, err := pconfig.NewClientFromConfig(httpClientConfig, "fetch_json", pconfig.WithKeepAlivesDisabled(), pconfig.WithHTTP2Disabled())
138136
if err != nil {
@@ -181,7 +179,7 @@ func (f *jsonFetcher) FetchJson(endpoint string) ([]byte, error) {
181179
// Use the configured template to render the body if enabled
182180
// Do not treat template errors as fatal, on such errors just log them
183181
// and continue with static body content
184-
func renderBody(logger log.Logger, body config.ConfigBody, tplValues url.Values) (method string, br io.Reader) {
182+
func renderBody(logger log.Logger, body config.Body, tplValues url.Values) (method string, br io.Reader) {
185183
method = "POST"
186184
if body.Content == "" {
187185
return "GET", nil

0 commit comments

Comments
 (0)