Skip to content

Commit 79e31c6

Browse files
feat(io): add Prometheus Pushgateway support
1 parent 4b60f9f commit 79e31c6

8 files changed

Lines changed: 494 additions & 25 deletions

File tree

docs/usage/writers.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
| `pubsub` | PubSubWriter | `project` |
2222
| `mongo` | MongoDbWriter | `connection_string`, `db` |
2323
| `firestore` | FirestoreWriter | `project`, `db` |
24+
| `pushgateway` | PushgatewayWriter | `endpoint`, `namespace`, `job` |
2425

2526

2627
## Installation
@@ -53,6 +54,7 @@ To install specific writers use:
5354
* `pip install garf-io[pubsub]` for PubSub support
5455
* `pip install garf-io[mongo]` for MongoDB support
5556
* `pip install garf-io[firestore]` for Firestore support
57+
* `pip install garf-io[pushgateway]` for Prometheus Pushgateway support
5658

5759

5860
## Usage
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
!!! important
2+
To save data to Pushgateway install `garf-io` with Pushgateway support
3+
4+
```bash
5+
pip install garf-io[pushgateway]
6+
```
7+
8+
9+
`pushgateway` writer allows you to publish `GarfReport` to a Pushgateway endpoint.
10+
11+
/// tab | cli
12+
```bash
13+
garf query.sql --source API_SOURCE \
14+
--output pushgateway
15+
```
16+
///
17+
18+
/// tab | python
19+
```python
20+
from garf.core import report
21+
from garf.io.writers import pushgateway_writer
22+
23+
sample_report = report.GarfReport(results=[[1]], column_names=['one'])
24+
25+
writer = pushgateway_writer.PushgatewayWriter()
26+
writer.write(sample_report, 'grouping_key')
27+
```
28+
///
29+
30+
## Parameters
31+
### endpoint
32+
33+
By default writer pushes data to `http://localhost:9091/`.
34+
You can overwrite it with `endpoint` parameter.
35+
36+
/// tab | cli
37+
```bash hl_lines="3"
38+
garf query.sql --source API_SOURCE \
39+
--output pushgateway \
40+
--pushgateway.endpoint=http://pushgateway:9091
41+
```
42+
///
43+
44+
/// tab | python
45+
```python hl_lines="7"
46+
from garf.core import report
47+
from garf.io.writers import pushgateway_writer
48+
49+
sample_report = report.GarfReport(results=[[1]], column_names=['one'])
50+
51+
writer = pushgateway_writer.PushgatewayWriter(endpoint="http://pushgateway:9091")
52+
writer.write(sample_report, 'grouping_key')
53+
```
54+
///
55+
56+
### namespace
57+
58+
Every metric pushes to Pushgateway is prefix with `garf_` prefix.
59+
You can overwrite it with `namespace` parameter.
60+
61+
/// tab | cli
62+
```bash hl_lines="3"
63+
garf query.sql --source API_SOURCE \
64+
--output pushgateway \
65+
--pushgateway.namespace=my_garf
66+
```
67+
///
68+
69+
/// tab | python
70+
```python hl_lines="7"
71+
from garf.core import report
72+
from garf.io.writers import pushgateway_writer
73+
74+
sample_report = report.GarfReport(results=[[1]], column_names=['one'])
75+
76+
writer = pushgateway_writer.PushgatewayWriter(namespace="my_garf")
77+
writer.write(sample_report, 'grouping_key')
78+
```
79+
///
80+
81+
### job
82+
83+
By default all metrics is pushed into `garf` job.
84+
You can overwrite it with `job` parameter.
85+
86+
/// tab | cli
87+
```bash hl_lines="3"
88+
garf query.sql --source API_SOURCE \
89+
--output pushgateway \
90+
--pushgateway.job=garf_job
91+
```
92+
///
93+
94+
/// tab | python
95+
```python hl_lines="7"
96+
from garf.core import report
97+
from garf.io.writers import pushgateway_writer
98+
99+
sample_report = report.GarfReport(results=[[1]], column_names=['one'])
100+
101+
writer = pushgateway_writer.PushgatewayWriter(job="garf_job")
102+
writer.write(sample_report, 'grouping_key')
103+
```
104+
///
105+
106+
### expose_metrics_with_zero_values
107+
108+
By default only metrics with non-zero values are pushed to Pushgateway.
109+
You can overwrite it with `expose_metrics_with_zero_values` parameter.
110+
111+
/// tab | cli
112+
```bash hl_lines="3"
113+
garf query.sql --source API_SOURCE \
114+
--output pushgateway \
115+
--pushgateway.expose_metrics_with_zero_values
116+
```
117+
///
118+
119+
/// tab | python
120+
```python hl_lines="7"
121+
from garf.core import report
122+
from garf.io.writers import pushgateway_writer
123+
124+
sample_report = report.GarfReport(results=[[1]], column_names=['one'])
125+
126+
writer = pushgateway_writer.PushgatewayWriter(expose_metrics_with_zero_values=True)
127+
writer.write(sample_report, 'grouping_key')
128+
```
129+
///
130+
131+
132+
## Query syntax
133+
134+
In order to expose metrics to Pushgateway you need to ensure that either field name
135+
or alias should contain `metric` in it.
136+
So given the query
137+
138+
```sql
139+
SELECT
140+
dimension.name AS name,
141+
metric.name AS field1,
142+
name AS metric_field2
143+
FROM resource
144+
```
145+
146+
two metrics will be pushed to Pushgateway - `<namespace>_<job>_field1` and `<namespace>_<job>_field2`.

libs/io/README.md

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,26 @@
55

66
`garf-io` handles reading queries and writing `GarfReport` to various local/remote storages.
77

8-
Currently it supports writing data to the following destination:
9-
10-
| identifier | Writer | Options |
11-
|------------| ---------------- | -------- |
12-
| `console` | ConsoleWriter | `page-size=10`,`format=table\|json\|jsonl`|
13-
| `csv` | CsvWriter | `destination-folder` |
14-
| `json` | JsonWriter | `destination-folder`,`format=json\|jsonl`|
15-
| `bq` | BigQueryWriter | `project`, `dataset`, `location`, `write-disposition` |
16-
| `sqldb` | SqlAlchemyWriter | `connection-string`, `if-exists=fail\|replace\|append` |
17-
| `sheets` | SheetsWriter | `share-with`, `credentials-file`, `spreadsheet-url`, `is_append=True\|False`|
18-
19-
Each of writer also support two options for dealing with arrays:
20-
21-
* `WRITER.array-handling` - arrays handling method: "strings" (default) - store arrays as strings (items combined via a separator, e.g. "item1|item2"), "arrays" - store arrays as arrays.
22-
* `WRITER.array-separator` - a separator symbol for joining arrays as strings, by default '|'.
23-
248
## Installation
259

26-
`pip install garf-io`
10+
```
11+
pip install garf-io
12+
```
13+
By default `garf-io` has only support for `console`, `csv` and `json` writers -
14+
explore what [additional writers are available](https://google.github.io/garf/usage/writers).
2715

28-
By default `garf-io` has only support for `console`, `csv` and `json` writers.\
29-
To install all writers use the following command `pip install garf-io[all]`.\
30-
To install specific writers use:
31-
* `pip install garf-io[bq]` for BigQuery support
32-
* `pip install garf-io[sheets]` for Google spreadsheets support
33-
* `pip install garf-io[sqlalchemy]` for SqlAlchemy support
3416

3517
## Usage
3618

19+
### CLI
20+
21+
```
22+
garf query.sql --source API_SOURCE \
23+
--output csv --csv.destination-folder=/tmp/
3724
```
25+
### Python
26+
27+
```python
3828
import garf.core import report
3929
from garf.io import writer
4030

@@ -47,3 +37,7 @@ concrete_writer = writer.create_writer('csv', destination_folder='/tmp/')
4737
# Write data to /tmp/sample.csv
4838
concrete_writer.write(sample_report, 'sample')
4939
```
40+
41+
## Documentation
42+
43+
[Learn more](https://google.github.io/garf/usage/writers) about using `garf` writers.

libs/io/garf/io/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@
1313
# limitations under the License.
1414
"""Write GarfReport to anywhere."""
1515

16-
__version__ = '1.3.1'
16+
__version__ = '1.3.2'

libs/io/garf/io/formatter.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ def apply_transformations(
4949
results=formatted_rows,
5050
column_names=report.column_names,
5151
results_placeholder=formatted_placeholders,
52+
query_specification=report.query_specification,
5253
)
5354

5455
def _format_rows(

0 commit comments

Comments
 (0)