Grafana is the open-source observability platform. Its HTTP API lets you create dashboards, alerts, and data sources entirely through code.
Dashboard API: Create Dashboards via Code
const dashboard = {
dashboard: {
title: "Scraping Metrics",
tags: ["scraping", "monitoring"],
panels: [
{
id: 1,
type: "timeseries",
title: "Scrapes per Hour",
gridPos: { h: 8, w: 12, x: 0, y: 0 },
targets: [{
expr: 'rate(scrapes_total[1h])',
legendFormat: '{{source}}',
}],
},
{
id: 2,
type: "stat",
title: "Total Products Scraped",
gridPos: { h: 4, w: 6, x: 12, y: 0 },
targets: [{ expr: 'scrapes_total' }],
options: { colorMode: "value", graphMode: "area" },
},
{
id: 3,
type: "table",
title: "Recent Errors",
gridPos: { h: 8, w: 12, x: 12, y: 4 },
targets: [{
expr: 'scrape_errors_total',
format: 'table',
}],
},
],
},
overwrite: true,
};
const response = await fetch("http://localhost:3000/api/dashboards/db", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${GRAFANA_API_KEY}`,
},
body: JSON.stringify(dashboard),
});
Alert Rules API
const alertRule = {
folderUID: "scraping-alerts",
ruleGroup: "scraping",
rules: [{
grafana_alert: {
title: "High Error Rate",
condition: "C",
data: [
{ refId: "A", model: { expr: 'rate(scrape_errors_total[5m])' } },
{ refId: "B", model: { type: "reduce", reducer: "last" } },
{ refId: "C", model: { type: "threshold", conditions: [{ evaluator: { type: "gt", params: [0.1] } }] } },
],
no_data_state: "OK",
exec_err_state: "Error",
},
for: "5m",
annotations: { summary: "Scraping error rate above 10%" },
labels: { severity: "warning" },
}],
};
await fetch("http://localhost:3000/api/v1/provisioning/alert-rules", {
method: "POST",
headers: { "Content-Type": "application/json", Authorization: `Bearer ${GRAFANA_API_KEY}` },
body: JSON.stringify(alertRule),
});
Data Source API
// Add Prometheus data source
await fetch("http://localhost:3000/api/datasources", {
method: "POST",
headers: { "Content-Type": "application/json", Authorization: `Bearer ${GRAFANA_API_KEY}` },
body: JSON.stringify({
name: "Prometheus",
type: "prometheus",
url: "http://prometheus:9090",
access: "proxy",
isDefault: true,
}),
});
Query API: Run Queries Programmatically
const queryResult = await fetch("http://localhost:3000/api/ds/query", {
method: "POST",
headers: { "Content-Type": "application/json", Authorization: `Bearer ${GRAFANA_API_KEY}` },
body: JSON.stringify({
queries: [{
refId: "A",
datasource: { type: "prometheus" },
expr: 'scrapes_total{source="amazon"}',
range: true,
intervalMs: 60000,
maxDataPoints: 1000,
}],
from: "now-24h",
to: "now",
}),
}).then(r => r.json());
Monitor scraping pipelines? My Apify tools + Grafana = full observability.
Custom monitoring? Email spinov001@gmail.com
Top comments (0)