|
| 1 | +""" |
| 2 | +About |
| 3 | +===== |
| 4 | +
|
| 5 | +Example program demonstrating how to work with both `grafana-client`_ and |
| 6 | +`grafanalib`_. It reflects the example program `example.upload-dashboard.py`_ |
| 7 | +from `grafanalib`_ and will create and upload a dashboard. |
| 8 | +
|
| 9 | +Setup |
| 10 | +===== |
| 11 | +:: |
| 12 | +
|
| 13 | + pip install grafana-client grafanalib |
| 14 | +
|
| 15 | +Synopsis |
| 16 | +======== |
| 17 | +:: |
| 18 | +
|
| 19 | + # Invoke example program. |
| 20 | + # By default, it will assume a Grafana instance accessible |
| 21 | + # via `http://admin:admin@localhost:3000`. |
| 22 | + python examples/grafanalib-upload-dashboard.py |
| 23 | +
|
| 24 | + # Optionally target a remote Grafana instance, with authentication token. |
| 25 | + export GRAFANA_URL=https://grafana.example.org/ |
| 26 | + export GRAFANA_TOKEN=eyJrIjoiWHg...dGJpZCI6MX0= |
| 27 | +
|
| 28 | +.. _example.upload-dashboard.py: https://github.com/weaveworks/grafanalib/blob/main/grafanalib/tests/examples/example.upload-dashboard.py |
| 29 | +.. _grafana-client: https://github.com/panodata/grafana-client |
| 30 | +.. _grafanalib: https://github.com/weaveworks/grafanalib |
| 31 | +""" |
| 32 | +import json |
| 33 | +import logging |
| 34 | +from typing import Dict |
| 35 | + |
| 36 | +from grafanalib._gen import DashboardEncoder |
| 37 | +from grafanalib.core import Dashboard |
| 38 | + |
| 39 | +from grafana_client import GrafanaApi |
| 40 | +from grafana_client.util import setup_logging |
| 41 | + |
| 42 | +logger = logging.getLogger(__name__) |
| 43 | + |
| 44 | + |
| 45 | +def mkdashboard(uid: str, title: str = None, message: str = None, overwrite: bool = False) -> Dict: |
| 46 | + """ |
| 47 | + Create a dashboard create/update payload using grafanalib, suitable for |
| 48 | + submitting to the Grafana HTTP API. |
| 49 | +
|
| 50 | + Note: This is solely for demonstration purposes, a real-world |
| 51 | + implementation would stuff more details into the dashboard beforehand. |
| 52 | + """ |
| 53 | + dashboard = Dashboard(uid=uid, title=title) |
| 54 | + data = { |
| 55 | + "dashboard": encode_dashboard(dashboard), |
| 56 | + "overwrite": overwrite, |
| 57 | + "message": message, |
| 58 | + } |
| 59 | + return data |
| 60 | + |
| 61 | + |
| 62 | +def encode_dashboard(entity) -> Dict: |
| 63 | + """ |
| 64 | + Encode grafanalib `Dashboard` entity to dictionary. |
| 65 | +
|
| 66 | + TODO: Optimize without going through JSON marshalling. |
| 67 | + """ |
| 68 | + return json.loads(json.dumps(entity, sort_keys=True, cls=DashboardEncoder)) |
| 69 | + |
| 70 | + |
| 71 | +def run(grafana: GrafanaApi): |
| 72 | + """ |
| 73 | + The main body of the example program. |
| 74 | +
|
| 75 | + - Create a Grafana dashboard create/update payload. |
| 76 | + - Submit to Grafana HTTP API. |
| 77 | + - Display response. |
| 78 | + """ |
| 79 | + |
| 80 | + # Create dashboard payload. |
| 81 | + dashboard_payload = mkdashboard( |
| 82 | + uid="abifsd", |
| 83 | + title="My awesome dashboard", |
| 84 | + message="Updated by grafanalib", |
| 85 | + overwrite=True, |
| 86 | + ) |
| 87 | + |
| 88 | + # Create or update dashboard at Grafana HTTP API. |
| 89 | + response = grafana.dashboard.update_dashboard(dashboard_payload) |
| 90 | + |
| 91 | + # Display the outcome in JSON format. |
| 92 | + print(json.dumps(response, indent=2)) |
| 93 | + |
| 94 | + |
| 95 | +if __name__ == "__main__": |
| 96 | + """ |
| 97 | + Boilerplate bootloader. Create a `GrafanaApi` instance and run example. |
| 98 | + """ |
| 99 | + |
| 100 | + # Setup logging. |
| 101 | + setup_logging(level=logging.DEBUG) |
| 102 | + |
| 103 | + # Create a `GrafanaApi` instance, optionally configured with environment |
| 104 | + # variables `GRAFANA_URL` and `GRAFANA_TOKEN`. |
| 105 | + grafana_client = GrafanaApi.from_env() |
| 106 | + |
| 107 | + # Invoke example conversation. |
| 108 | + run(grafana_client) |
0 commit comments