|
4 | 4 |
|
5 | 5 | Example program for submitting a query to a data source.
|
6 | 6 |
|
7 |
| -While the ``--query`` option is obligatory, the ``--store`` option is not. |
| 7 | +While the ``--query`` option is obligatory, the ``--attr`` option is not. |
8 | 8 | It will be used to designate the ``database`` name, with e.g. InfluxDB,
|
9 |
| -or the ``metric`` name, with e.g. Prometheus. |
| 9 | +or the ``queryType`` name, with e.g. Loki. |
10 | 10 |
|
11 | 11 | Synopsis
|
12 | 12 | ========
|
13 | 13 | ::
|
14 |
| -
|
15 | 14 | # Query the InfluxDB 1.x data source on `play.grafana.org`.
|
16 | 15 | python examples/datasource-smartquery.py --uid=000000002 --query="SHOW RETENTION POLICIES on _internal"
|
17 |
| - python examples/datasource-smartquery.py --uid=000000002 --store=site --query="SHOW FIELD KEYS" |
| 16 | + python examples/datasource-smartquery.py --uid=000000002 --attr "database:site" --query="SHOW FIELD KEYS" |
| 17 | + python examples/datasource-smartquery.py --uid 000000002 --query "SELECT sum(\"value\") FROM \"logins.count\" WHERE time >= now() - 5m and time <= now() GROUP BY time(10s) fill(null)" |
| 18 | +
|
| 19 | + #Query the InfluxDB+flux on https://play.grafana.org |
| 20 | + # fix me : query invalid ! |
| 21 | + python examples/datasource-smartquery.py \ |
| 22 | + --uid M3k6ZPrnz \ |
| 23 | + --query "from(bucket: \"example-bucket\")\\n|> range(start: -1h)|> filter(fn: (r) => r._measurement == \"example-measurement\" and r._field == \"example-field\")" |
| 24 | +
|
| 25 | + // Query the Grafite |
| 26 | + python examples/datasource-smartquery.py --uid 000000001 --query "aliasByNode(apps.fakesite.web_server_02.counters.requests.count,2)" |
18 | 27 |
|
19 | 28 | # Query the Prometheus data source on `play.grafana.org`.
|
20 |
| - # FIXME: Does not work yet. |
21 |
| - python \ |
22 |
| - examples/datasource-smartquery.py --uid=000000008 --store=node_boot_time \ |
| 29 | + python examples/datasource-smartquery.py \ |
| 30 | + --uid=000000008 \ |
23 | 31 | --query='time() - node_boot_time_seconds{job="node", instance=~"demo.do.prometheus.io:.*"}'
|
24 | 32 |
|
| 33 | + # Query the Loki data source on `play.grafana.org`. |
| 34 | + python examples/datasource-smartquery.py \ |
| 35 | + --uid=NX9d1VH7k \ |
| 36 | + --query "bytes_over_time({filename=~\".+/json_access.+\", job=~\".*\", instance=~\".*\"}[5m])" \ |
| 37 | + --attr "queryType:instant" |
| 38 | + python examples/datasource-smartquery.py \ |
| 39 | + --uid=NX9d1VH7k \ |
| 40 | + --query "sum by (status) (count_over_time({filename=~\".+/json_access.+\", job=~\".*\", instance=~\".*\"} | json | __error__=\"\" [5m]))" \ |
| 41 | + --attr "legendFormat:HTTP Status {{status}}" |
| 42 | +
|
25 | 43 | """
|
26 | 44 | import json
|
27 | 45 | import logging
|
|
39 | 57 | def run(grafana: GrafanaApi):
|
40 | 58 | parser = OptionParser()
|
41 | 59 | parser.add_option("--uid", dest="uid", help="Data source UID")
|
42 |
| - parser.add_option("--store", dest="store", help="Database or metric to be addressed") |
| 60 | + # replace by --attr "database:<store>" or --attr "metric:<metric>" |
| 61 | + # parser.add_option("--store", dest="store", help="Database or metric to be addressed") |
43 | 62 | parser.add_option("--query", dest="query", help="Query expression")
|
| 63 | + parser.add_option("--attr", dest="attrs", action="append", help="key:value pair attribute to send into query") |
44 | 64 | (options, args) = parser.parse_args()
|
45 | 65 | if not options.uid:
|
46 | 66 | parser.error("Option --uid required")
|
47 | 67 | if not options.query:
|
48 | 68 | parser.error("Option --query required")
|
49 | 69 |
|
| 70 | + attributes = { |
| 71 | + } |
| 72 | + if options.attrs is not None and len(options.attrs) > 0 : |
| 73 | + for attr in options.attrs: |
| 74 | + (key, val) = attr.split(":") |
| 75 | + attributes[key] = val |
| 76 | + |
50 | 77 | # Query the data source by UID.
|
51 | 78 | response = grafana.datasource.smartquery(
|
52 |
| - DatasourceIdentifier(uid=options.uid), expression=options.query, store=options.store |
| 79 | + DatasourceIdentifier(uid=options.uid), |
| 80 | + options.query, |
| 81 | + attrs=attributes, |
53 | 82 | )
|
54 | 83 |
|
55 | 84 | # Display the outcome in JSON format.
|
|
0 commit comments