Skip to content

Commit 7ab7efc

Browse files
authored
Merge pull request #218 from rustycl0ck/fix-example-doc
Fix example syntax and update documentation
2 parents 2f9b473 + 2754bc1 commit 7ab7efc

File tree

3 files changed

+91
-91
lines changed

3 files changed

+91
-91
lines changed

README.md

+38-71
Original file line numberDiff line numberDiff line change
@@ -3,103 +3,73 @@ json_exporter
33
[![CircleCI](https://circleci.com/gh/prometheus-community/json_exporter.svg?style=svg)](https://circleci.com/gh/prometheus-community/json_exporter)
44

55
A [prometheus](https://prometheus.io/) exporter which scrapes remote JSON by JSONPath.
6-
For checking the JSONPath configuration supported by this exporter please head over [here](https://kubernetes.io/docs/reference/kubectl/jsonpath/).
7-
Checkout the [examples](/examples) directory for sample exporter configuration, prometheus configuration and expected data format.
86

9-
#### :warning: The configuration syntax has changed in version `0.3.x`. If you are migrating from `0.2.x`, then please use the above mentioned JSONPath guide for correct configuration syntax.
7+
- [Supported JSONPath Syntax](https://kubernetes.io/docs/reference/kubectl/jsonpath/)
8+
- [Examples configurations](/examples)
109

1110
## Example Usage
1211

1312
```console
14-
$ cat examples/data.json
15-
{
16-
"counter": 1234,
17-
"values": [
18-
{
19-
"id": "id-A",
20-
"count": 1,
21-
"some_boolean": true,
22-
"state": "ACTIVE"
23-
},
24-
{
25-
"id": "id-B",
26-
"count": 2,
27-
"some_boolean": true,
28-
"state": "INACTIVE"
29-
},
30-
{
31-
"id": "id-C",
32-
"count": 3,
33-
"some_boolean": false,
34-
"state": "ACTIVE"
35-
}
36-
],
37-
"location": "mars"
38-
}
39-
40-
$ cat examples/config.yml
41-
---
42-
modules:
43-
default:
44-
metrics:
45-
- name: example_global_value
46-
path: "{ .counter }"
47-
help: Example of a top-level global value scrape in the json
48-
labels:
49-
environment: beta # static label
50-
location: "planet-{.location}" # dynamic label
51-
52-
- name: example_value
53-
type: object
54-
help: Example of sub-level value scrapes from a json
55-
path: '{.values[?(@.state == "ACTIVE")]}'
56-
labels:
57-
environment: beta # static label
58-
id: '{.id}' # dynamic label
59-
values:
60-
active: 1 # static value
61-
count: '{.count}' # dynamic value
62-
boolean: '{.some_boolean}'
63-
64-
headers:
65-
X-Dummy: my-test-header
13+
## SETUP
6614

15+
$ make build
16+
$ ./json_exporter --config.file examples/config.yml &
6717
$ python3 -m http.server 8000 &
6818
Serving HTTP on :: port 8000 (http://[::]:8000/) ...
6919

70-
$ ./json_exporter --config.file examples/config.yml &
7120

72-
$ curl "http://localhost:7979/probe?module=default&target=http://localhost:8000/examples/data.json" | grep ^example
21+
## TEST with 'default' module
22+
23+
$ curl "http://localhost:7979/probe?module=default&target=http://localhost:8000/examples/data.json"
24+
# HELP example_global_value Example of a top-level global value scrape in the json
25+
# TYPE example_global_value untyped
7326
example_global_value{environment="beta",location="planet-mars"} 1234
27+
# HELP example_timestamped_value_count Example of a timestamped value scrape in the json
28+
# TYPE example_timestamped_value_count untyped
29+
example_timestamped_value_count{environment="beta"} 2
30+
# HELP example_value_active Example of sub-level value scrapes from a json
31+
# TYPE example_value_active untyped
7432
example_value_active{environment="beta",id="id-A"} 1
7533
example_value_active{environment="beta",id="id-C"} 1
34+
# HELP example_value_boolean Example of sub-level value scrapes from a json
35+
# TYPE example_value_boolean untyped
7636
example_value_boolean{environment="beta",id="id-A"} 1
7737
example_value_boolean{environment="beta",id="id-C"} 0
38+
# HELP example_value_count Example of sub-level value scrapes from a json
39+
# TYPE example_value_count untyped
7840
example_value_count{environment="beta",id="id-A"} 1
7941
example_value_count{environment="beta",id="id-C"} 3
8042

81-
# To test through prometheus:
43+
44+
## TEST with a different module for different json file
45+
46+
$ curl "http://localhost:7979/probe?module=animals&target=http://localhost:8000/examples/animal-data.json"
47+
# HELP animal_population Example of top-level lists in a separate module
48+
# TYPE animal_population untyped
49+
animal_population{name="deer",predator="false"} 456
50+
animal_population{name="lion",predator="true"} 123
51+
animal_population{name="pigeon",predator="false"} 789
52+
53+
54+
## TEST through prometheus:
55+
8256
$ docker run --rm -it -p 9090:9090 -v $PWD/examples/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
8357
```
8458
Then head over to http://localhost:9090/graph?g0.range_input=1h&g0.expr=example_value_active&g0.tab=1 or http://localhost:9090/targets to check the scraped metrics or the targets.
8559

8660
## Using custom timestamps
8761

88-
This exporter allows you to use a field of the metric as the (unix/epoch) timestamp for the data as an int64. However, this may lead to unexpected behaviour, as the prometheus implements a [Staleness](https://prometheus.io/docs/prometheus/latest/querying/basics/#staleness) mechanism. Including timestamps in metrics disabled this staleness handling and can make data visible for longer than expected.
89-
90-
## Exposing metrics through HTTPS
62+
This exporter allows you to use a field of the metric as the (unix/epoch) timestamp for the data as an int64. However, this may lead to unexpected behaviour, as the prometheus implements a [Staleness](https://prometheus.io/docs/prometheus/latest/querying/basics/#staleness) mechanism.
9163

92-
TLS configuration supported by this exporter can be found at [exporter-toolkit/web](https://github.com/prometheus/exporter-toolkit/blob/v0.5.1/docs/web-configuration.md)
64+
:warning: Including timestamps in metrics disables the staleness handling and can make data visible for longer than expected.
9365

94-
## Build
66+
## Exposing metrics through HTTPS
9567

96-
```sh
97-
make build
98-
```
68+
TLS configuration supported by this exporter can be found at [exporter-toolkit/web](https://github.com/prometheus/exporter-toolkit/blob/v0.9.0/docs/web-configuration.md)
9969

10070
## Sending body content for HTTP `POST`
10171

102-
If `body` paramater is set in config, it will be sent by the exporter as the body content in the scrape request. The HTTP method will also be set as 'POST' in this case.
72+
If `modules.<module_name>.body` paramater is set in config, it will be sent by the exporter as the body content in the scrape request. The HTTP method will also be set as 'POST' in this case.
10373
```yaml
10474
body:
10575
content: |
@@ -132,9 +102,6 @@ Then `curl "http://exporter:7979/probe?target=http://scrape_target:8080/test/dat
132102
## Docker
133103
134104
```console
135-
docker run \
136-
-v $PWD/examples/config.yml:/config.yml \
137-
quay.io/prometheuscommunity/json-exporter \
138-
--config.file=/config.yml
105+
$ docker run -v $PWD/examples/config.yml:/config.yml quay.io/prometheuscommunity/json-exporter --config.file=/config.yml
139106
```
140107

examples/animal-data.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[
2+
{
3+
"noun": "lion",
4+
"population": 123,
5+
"predator": true
6+
},
7+
{
8+
"noun": "deer",
9+
"population": 456,
10+
"predator": false
11+
},
12+
{
13+
"noun": "pigeon",
14+
"population": 789,
15+
"predator": false
16+
}
17+
]

examples/config.yml

+36-20
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,49 @@
11
---
22
modules:
33
default:
4+
headers:
5+
X-Dummy: my-test-header
46
metrics:
57
- name: example_global_value
6-
path: "{ .counter }"
8+
path: '{ .counter }'
79
help: Example of a top-level global value scrape in the json
810
labels:
911
environment: beta # static label
10-
location: "planet-{.location}" # dynamic label
12+
location: 'planet-{.location}' # dynamic label
1113
- name: example_timestamped_value
14+
type: object
1215
path: '{ .values[?(@.state == "INACTIVE")] }'
13-
epochTimestamp: "{ .timestamp }"
16+
epochTimestamp: '{ .timestamp }'
1417
help: Example of a timestamped value scrape in the json
1518
labels:
1619
environment: beta # static label
20+
values:
21+
count: '{.count}' # dynamic value
1722
- name: example_value
1823
type: object
1924
help: Example of sub-level value scrapes from a json
2025
path: '{.values[?(@.state == "ACTIVE")]}'
2126
labels:
2227
environment: beta # static label
23-
id: '{.id}' # dynamic label
28+
id: '{.id}' # dynamic label
2429
values:
25-
active: 1 # static value
30+
active: 1 # static value
2631
count: '{.count}' # dynamic value
2732
boolean: '{.some_boolean}'
28-
headers:
29-
X-Dummy: my-test-header
3033

31-
# If 'body' is set, it will be sent by the exporter as the body content in the scrape request. The HTTP method will also be set as 'POST' in this case.
32-
# body:
33-
# content: |
34-
# {"time_diff": "1m25s", "anotherVar": "some value"}
35-
36-
# The body content can also be a Go Template (https://golang.org/pkg/text/template), with all the functions from the Sprig library (https://masterminds.github.io/sprig/) available. All the query parameters sent by prometheus in the scrape query to the exporter, are available in the template.
37-
# body:
38-
# content: |
39-
# {"time_diff": "{{ duration `95` }}","anotherVar": "{{ .myVal | first }}"}
40-
# templatize: true
34+
animals:
35+
metrics:
36+
- name: animal
37+
type: object
38+
help: Example of top-level lists in a separate module
39+
path: '{ [*] }'
40+
labels:
41+
name: '{ .noun }'
42+
predator: '{ .predator }'
43+
values:
44+
population: '{ .population }'
4145

42-
# For full http client config parameters, ref: https://pkg.go.dev/github.com/prometheus/common/config?tab=doc#HTTPClientConfig
46+
## HTTP connection configurations can be set in 'modules.<module_name>.http_client_config' field. For full http client config parameters, ref: https://pkg.go.dev/github.com/prometheus/common/config?tab=doc#HTTPClientConfig
4347
#
4448
# http_client_config:
4549
# tls_config:
@@ -49,5 +53,17 @@ modules:
4953
# #password: veryverysecret
5054
# password_file: /tmp/mysecret.txt
5155

52-
# Accepted status codes for this probe. Defaults to 2xx.
53-
# valid_status_codes: [ <int>, ... | default = 2xx ]
56+
## List of accepted status codes for this probe can be set in 'modules.<module_name>.valid_status_codes' field. Defaults to 2xx.
57+
# valid_status_codes: [ <int>, ... | default = 2xx ]
58+
59+
## If 'modueles.<module_name>.body' field is set, it will be sent by the exporter as the body content in the scrape request. The HTTP method will also be set as 'POST' in this case.
60+
# body:
61+
# content: |
62+
# {"time_diff": "1m25s", "anotherVar": "some value"}
63+
64+
## The body content can also be a Go Template (https://golang.org/pkg/text/template), with all the functions from the Sprig library (https://masterminds.github.io/sprig/) available. All the query parameters sent by prometheus in the scrape query to the exporter, are available in the template.
65+
# body:
66+
# content: |
67+
# {"time_diff": "{{ duration `95` }}","anotherVar": "{{ .myVal | first }}"}
68+
# templatize: true
69+

0 commit comments

Comments
 (0)