Visualizing Influx metrics with Grafana

Maciej Szafraniec

Grafana is a data visualization and monitoring tool allowing you to show metrics stored for example in InfluxDB as charts. It gives you possibility to create dashboards to link charts or histograms into connected groups. Thanks to built-in InfluxDB support it automatically handles provided aggregation functions. Query editor supports also tag or field completion.

Here are few steps you must do to visualize data:

InfluxDB measurement

You must start with data to show on the graph. As an example, let’s use following measurement called “simple_measurement” in “test” database:

select * from „simple_measurement”

It has 4 tags and 2 fields:

Grafana data source

Next step after creating data to show is to define it in Grafana as a data source. In Grafana app go to Configuration->Data Sources and find ‘Add data source’ button:

Then you have to choose InfluxDB type, put a name and provide InfluxDB details and credentials:

And finally provide a database name and credentials:

Click  button to verify if information put is correct.

Grafana dashboard

Final step is to create a dashboard with a graph panel. After clicking on „Home” button  there is a „New dashboard” button which will navigate you to dashboard creator where you can add a panel with a proper chart – Graph:

Than you must define correct data source and queries in graph editor to show specific data – value_1 and value_2 (fields):

What finally give you chart like this:

This is just a basic graph showing all records from DB without any narrowing down. When you want to have a specific chart with a data limited you have to use ‘where’ clause in graph editor and select tag and tag value (e.g. customer=’jlabs’ or version=’1.0’):

Steps provided above are simple but inefficient when there is a bigger number of dashboards or graphs to create. Grafana also provides API to manage its content – adding or modifying dashboards, adding panels and so on. It is available under:

http://<grafana_url>:<grafana_port>/api/dasboards/db

where you can POST a request with a JSON body containing definition of a dashboard. To achieve results like from manual steps you have to create JSON like:

json = {
    "dashboard": {
        "id": None,
        "sharedCrosshair": True,
        "title": 'simple_measurement_2',
        "timezone": "browser",
        "panels": [{
            "datasource": "simple_measurement",
            "fill": 1,
            "gridPos": {
                "h": 9,
                "w": 12,
                "x": 0,
                "y": 0},
            "id": 1,
            "span": 12,
            "targets": [{
                "alias": "value_1_other",
                "groupBy": [{
                    "params": ["$__interval"],
                    "type": "time"
                }, {
                    "params": ["null"],
                    "type": "fill"}],
                "hide": False,
                "measurement": "simple_measurement",
                "orderByTime": "ASC",
                "policy": "default",
                "refId": "A",
                "resultFormat": "time_series",
                "select": [[{
                    "params": ["value_1"],
                    "type": "field"
                }, {
                    "params": [],
                    "type": "mean"}]],
                "tags": [{
                    "key": "customer",
                    "operator": "=",
                    "value": "other"}]}],
            "title": "simple_measurement",
            "type": "graph",
            "xaxis": {"mode": "time", "show": True},
            "yaxes": [{"show": True, "logBase": 1, "format": "ms"},
                      {"show": True, "logBase": 1, "format": "short"}]}],
        "schemaVersion": 6,
        "version": 2},
    "overwrite": True
}

POST response will contain so called dashboard uid which is needed to manage or delete this dashboard. To delete dashboard send DELETE request to:

http://<grafana_url>:<grafana_port>/api/dasboards/uid/<dashboard_uid>

Poznaj mageek of j‑labs i daj się zadziwić, jak może wyglądać praca z j‑People!

Skontaktuj się z nami