Skip to content

Commit 1a5679d

Browse files
authored
Merge pull request prometheus-community#37 from davdr/fix/boolean
Fix bool support
2 parents da5d78d + c58897c commit 1a5679d

File tree

4 files changed

+19
-0
lines changed

4 files changed

+19
-0
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,19 @@ $ cat example/data.json
2020
{
2121
"id": "id-A",
2222
"count": 1,
23+
"some_boolean": true,
2324
"state": "ACTIVE"
2425
},
2526
{
2627
"id": "id-B",
2728
"count": 2,
29+
"some_boolean": true,
2830
"state": "INACTIVE"
2931
},
3032
{
3133
"id": "id-C",
3234
"count": 3,
35+
"some_boolean": false,
3336
"state": "ACTIVE"
3437
},
3538
]
@@ -50,20 +53,25 @@ $ cat example/config.yml
5053
values:
5154
active: 1 # static value
5255
count: $.count # dynamic value
56+
boolean: $.some_boolean
5357

5458
$ python -m SimpleHTTPServer 8000 &
5559
Serving HTTP on 0.0.0.0 port 8000 ...
5660

5761
$ ./json_exporter http://localhost:8000/example/data.json example/config.yml &
5862
INFO[2016-02-08T22:44:38+09:00] metric registered;name:<example_global_value>
63+
INFO[2016-02-08T22:44:38+09:00] metric registered;name:<example_value_boolean>
5964
INFO[2016-02-08T22:44:38+09:00] metric registered;name:<example_value_active>
6065
INFO[2016-02-08T22:44:38+09:00] metric registered;name:<example_value_count>
6166
127.0.0.1 - - [08/Feb/2016 22:44:38] "GET /example/data.json HTTP/1.1" 200 -
6267

68+
6369
$ curl http://localhost:7979/metrics | grep ^example
6470
example_global_value{environment="beta"} 1234
6571
example_value_active{environment="beta",id="id-A"} 1
6672
example_value_active{environment="beta",id="id-C"} 1
73+
example_value_boolean{environment="beta",id="id-A"} 1
74+
example_value_boolean{environment="beta",id="id-C"} 0
6775
example_value_count{environment="beta",id="id-A"} 1
6876
example_value_count{environment="beta",id="id-C"} 3
6977
```

example/config.yml

+1
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@
1212
values:
1313
active: 1 # static value
1414
count: $.count # dynamic value
15+
boolean: $.some_boolean

example/data.json

+3
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,19 @@
44
{
55
"id": "id-A",
66
"count": 1,
7+
"some_boolean": true,
78
"state": "ACTIVE"
89
},
910
{
1011
"id": "id-B",
1112
"count": 2,
13+
"some_boolean": true,
1214
"state": "INACTIVE"
1315
},
1416
{
1517
"id": "id-C",
1618
"count": 3,
19+
"some_boolean": false,
1720
"state": "ACTIVE"
1821
},
1922
]

jsonexporter/scraper.go

+7
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ func (obsc *ObjectScraper) Scrape(data []byte, reg *harness.MetricRegistry) erro
209209
}
210210

211211
var value float64
212+
var boolValue bool
212213
switch firstResult.Type {
213214
case jsonpath.JsonNumber:
214215
value, err = obsc.parseValue(firstResult.Value)
@@ -217,6 +218,12 @@ func (obsc *ObjectScraper) Scrape(data []byte, reg *harness.MetricRegistry) erro
217218
value, err = obsc.parseValue(firstResult.Value[1 : len(firstResult.Value)-1])
218219
case jsonpath.JsonNull:
219220
value = math.NaN()
221+
case jsonpath.JsonBool:
222+
if boolValue, err = strconv.ParseBool(string(firstResult.Value)); boolValue {
223+
value = 1.0
224+
} else {
225+
value = 0.0
226+
}
220227
default:
221228
log.Warnf("skipping not numerical result;path:<%v>,value:<%s>",
222229
obsc.valueJsonPath, result.Value)

0 commit comments

Comments
 (0)