Over at MOVE-UP we’re already using Grafana to display sensor data like altitude and speed. However, storing into and fetching it again from a database takes some seconds while we’d rather have that information live.

That’s where Grafana Live comes in, which allows you to stream data in (soft) real-time. For example, let’s say we have the data in a Python script:

import random
import time

alt = random.uniform(0, 1000)
speed = random.uniform(0, 100)

for _ in range(500):
    alt += random.uniform(-5, 5)
    speed += random.uniform(-1, 1)
    time.sleep(0.1)

How do we get it into Grafana?

meme of a guy sweating to decide between two buttons labeled curl -X POST and npm template package + tutorial

The official documentation suggests creating a full-blown TypeScript + Go plugin or setup Telegraf. None of that is necessary, you can just use the new API endpoint /api/live/push/:streamId directly:

import os
import random
import requests
import time

url = "http://localhost:3000/api/live/push/test_stream"
headers = {"Authorization": "Bearer " + os.environ["AUTH"]}

alt = random.uniform(0, 1000)
speed = random.uniform(0, 100)

for _ in range(500):
    alt += random.uniform(-5, 5)
    speed += random.uniform(-1, 1)
    response = requests.post(url, headers=headers, data=f"balloon speed={speed},alt={alt}")
    if response.status_code != 200:
        print(f"Error: {response.status_code} - {response.text}")

    time.sleep(0.1)

Authentication is done with an API token.

Thanks to Grafana89e9 and Smaragda Benetou for pointing this out with code examples.

Webmentions

No webmentions were found.

Comments