Plot REST Endpoints: grafana-infinity-datasource – DZone – Uplaza

In the case of observability Grafana is the go-to software for visualization. A Grafana dashboard consists of assorted types of visualizations that are normally backed by a database.
This isn’t at all times the case. Typically as an alternative of pushing the info from the database as is, you may wish to refine the info. This can’t at all times be achieved by the functionalities the DB offers. For instance, you may wish to fetch outcomes from a proprietary API. That is the place the grafana-infinity-datasource plugin kicks in. With grafana-infinity-datasource, you may create visualizations primarily based on JSON, XML, CSV, and many others. You possibly can subject an HTTP request to a REST API and plot the acquired knowledge.

Tutorial

Let’s assume we’ve an eShop utility. We’ll create a easy Python API utilizing FastAPI to handle the objects of the eShop and the acquisition quantity.

By this API, we’ll add objects and purchase-volume entries.

from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import Checklist
from datetime import datetime
 
app = FastAPI()
 
class Merchandise(BaseModel):
    id: int
    title: str
    description: str = None
    value: float
 
class Buy(BaseModel):
    value: float
    time: datetime
 
objects = []
purchases = []
 
@app.put up("/items/", response_model=Merchandise)
def create_item(merchandise: Merchandise):
    objects.append(merchandise)
    return merchandise
 
@app.get("/items/", response_model=Checklist[Item])
def read_items():
    return objects
 
@app.get("/items/{item_id}", response_model=Merchandise)
def read_item(item_id: int):
    for merchandise in objects:
        if merchandise.id == item_id:
            return merchandise
    increase HTTPException(status_code=404, element="Item not found")
 
@app.delete("/items/{item_id}", response_model=Merchandise)
def delete_item(item_id: int):
    for idx, merchandise in enumerate(objects):
        if merchandise.id == item_id:
            return objects.pop(idx)
    increase HTTPException(status_code=404, element="Item not found")
 
@app.put up("/purchases/", response_model=Buy)
def create_purchase(buy: Buy):
    purchases.append(buy)
    return buy
 
@app.get("/purchases/", response_model=Checklist[Purchase])
def read_purchases():
    return purchases

We additionally want FastAPI to be added to the necessities.txt:

We’ll host the applying by Docker; thus, we’ll create a Dockerfile:

FROM python:3.11-slim
 
WORKDIR /app
 
COPY necessities.txt .
 
RUN pip set up --no-cache-dir -r necessities.txt
 
COPY most important.py most important.py
 
EXPOSE 8000
 
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]

We should always proceed to the Grafana visualizations. Primarily, we’ve two totally different sources of knowledge.
The mannequin Merchandise will probably be visualized in a desk and the mannequin buy will probably be visualized by a time collection graph.

I shall use Docker Compose to provision Grafana in addition to the Python utility:

model: '3.8'
 
companies:
  app:
    construct: .
    ports:
      - 8000:8000
  grafana:
    picture: grafana/grafana:newest
    ports:
      - "3000:3000"
    volumes:
      - ./grafana:/var/lib/grafana
    surroundings:
      - GF_SECURITY_ADMIN_USER=take a look at
      - GF_SECURITY_ADMIN_PASSWORD=infinity
      - GF_INSTALL_PLUGINS=yesoreyeram-infinity-datasource

Primarily by the surroundings variable on Docker, I allow the the infinity-datasource plugin.

We will get our situations up and working by issuing the next:

Docker Compose V2 is on the market with many good options.

We will now populate the applying with some knowledge:

$ curl -X POST "http://127.0.0.1:8000/purchases/" -H "Content-Type: application/json" -d '{"time": "2024-07-15T12:40:56","price":2.5}'
$ curl -X POST "http://127.0.0.1:8000/purchases/" -H "Content-Type: application/json" -d '{"time": "2024-07-15T12:41:56","price":4.0}'
$ curl -X POST "http://127.0.0.1:8000/purchases/" -H "Content-Type: application/json" -d '{"time": "2024-07-15T12:42:56","price":1.5}'
$ curl -X POST "http://127.0.0.1:8000/purchases/" -H "Content-Type: application/json" -d '{"time": "2024-07-15T12:43:56","price":3.5}'
 
$ curl -X POST "http://127.0.0.1:8000/items/" -H "Content-Type: application/json" -d '{"id": 1, "name": "Item 1", "description": "This is item 1", "price": 10.5, "tax": 0.5}'

Shifting onward, create a dashboard on Grafana.

One visualization for objects:

One visualization for buy quantity:

As you may see in each circumstances, I used the http://app:8000 endpoint which is our utility, and the DNS that the Compose utility can resolve.

That’s it! We plotted our knowledge from a REST API utilizing Grafana.

Share This Article
Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Exit mobile version