Skip to main content
Like AOF? Give us a star!
If you find AOF useful, please star us on GitHub. It helps us reach more developers and grow the community.

Grafana Tools

AOF provides native Grafana integration tools for querying metrics, managing dashboards, working with alerts, and creating annotations.

Feature Flag Required: These tools require the observability feature flag to be enabled during compilation.

Prerequisites

  • Grafana instance (cloud or self-hosted)
  • Grafana API key or Service Account token
  • Bearer token authentication enabled
  • Appropriate permissions for desired operations

Authentication

All Grafana tools use Bearer token authentication. You can create an API key or Service Account token from your Grafana instance:

  1. Go to Configuration → API Keys (or Service Accounts)
  2. Create a new key with appropriate permissions
  3. Copy the token for use in agent configurations

Available Tools

ToolDescriptionUse Cases
grafana_queryQuery data sources through GrafanaMetrics analysis, trend detection
grafana_dashboard_getRetrieve dashboard by UIDDashboard inspection, backup
grafana_dashboard_listSearch and list dashboardsDiscovery, inventory
grafana_alert_listList alert rulesAlert monitoring, status checks
grafana_alert_silenceCreate alert silencesMaintenance windows, incident management
grafana_annotation_createCreate annotationsDeployment tracking, event marking

grafana_query

Query data sources through Grafana's unified query API. Supports Prometheus, Loki, and other data sources configured in your Grafana instance.

Parameters:

ParameterTypeRequiredDescription
endpointstringYesGrafana server URL (e.g., https://grafana.example.com)
datasource_uidstringYesData source UID from Grafana
querystringYesQuery in data source's native language (PromQL, LogQL, etc.)
api_keystringYesGrafana API key or service account token
fromstringNoStart time (RFC3339 or Unix timestamp in milliseconds)
tostringNoEnd time (RFC3339 or Unix timestamp in milliseconds)
max_data_pointsintegerNoMaximum number of data points to return (default: 1000)
interval_msintegerNoInterval in milliseconds between data points
org_idintegerNoOrganization ID for multi-org setups

Example Agent Configuration:

apiVersion: aof.dev/v1
kind: Agent
metadata:
name: grafana-metrics-agent
spec:
model: google:gemini-2.5-flash
instructions: |
You are a Grafana metrics analyst. Query Prometheus data and provide insights.
tools:
- grafana_query
env:
GRAFANA_ENDPOINT: "https://grafana.example.com"
GRAFANA_API_KEY: "${GRAFANA_TOKEN}"
GRAFANA_DATASOURCE_UID: "prometheus-uid"

Example Tool Call:

{
"name": "grafana_query",
"input": {
"endpoint": "https://grafana.example.com",
"datasource_uid": "prometheus-main",
"query": "avg(rate(http_requests_total[5m]))",
"from": "now-1h",
"to": "now",
"api_key": "glsa_xxxxxxxxxxxx"
}
}

Response:

{
"results": {
"A": {
"frames": [
{
"schema": {...},
"data": {...}
}
]
}
},
"datasource_uid": "prometheus-main",
"query": "avg(rate(http_requests_total[5m]))"
}

Common Use Cases:

  • Query Prometheus metrics for troubleshooting
  • Analyze time-series data patterns
  • Fetch Loki logs for specific time ranges
  • Monitor application performance metrics

grafana_dashboard_get

Retrieve a complete dashboard definition by UID. Returns full dashboard JSON including panels, variables, and settings.

Parameters:

ParameterTypeRequiredDescription
endpointstringYesGrafana server URL
dashboard_uidstringYesDashboard UID (found in dashboard URL)
api_keystringYesGrafana API key
org_idintegerNoOrganization ID

Example:

{
"name": "grafana_dashboard_get",
"input": {
"endpoint": "https://grafana.example.com",
"dashboard_uid": "abc123def",
"api_key": "glsa_xxxxxxxxxxxx"
}
}

Response:

{
"dashboard": {
"id": 123,
"uid": "abc123def",
"title": "Production Metrics",
"panels": [...],
"templating": {...}
},
"meta": {
"isStarred": false,
"url": "/d/abc123def/production-metrics"
}
}

Common Use Cases:

  • Backup dashboard configurations
  • Inspect panel queries and settings
  • Clone dashboards to other environments
  • Audit dashboard definitions

grafana_dashboard_list

Search and list dashboards with optional filters. Useful for discovery and inventory management.

Parameters:

ParameterTypeRequiredDescription
endpointstringYesGrafana server URL
api_keystringYesGrafana API key
querystringNoSearch query string (matches title)
tagsarray[string]NoFilter by tags
folder_idsarray[integer]NoFilter by folder IDs
limitintegerNoMaximum results (default: 100)
org_idintegerNoOrganization ID

Example:

{
"name": "grafana_dashboard_list",
"input": {
"endpoint": "https://grafana.example.com",
"api_key": "glsa_xxxxxxxxxxxx",
"tags": ["production", "kubernetes"],
"limit": 50
}
}

Response:

{
"dashboards": [
{
"id": 123,
"uid": "abc123",
"title": "Kubernetes Cluster",
"uri": "db/kubernetes-cluster",
"url": "/d/abc123/kubernetes-cluster",
"tags": ["production", "kubernetes"]
}
],
"count": 1
}

Common Use Cases:

  • Find dashboards by tag or name
  • Generate dashboard inventory
  • Locate specific monitoring views
  • Organize dashboard discovery

grafana_alert_list

List alert rules and their current states. Supports filtering by dashboard, state, and folder.

Parameters:

ParameterTypeRequiredDescription
endpointstringYesGrafana server URL
api_keystringYesGrafana API key
dashboard_uidstringNoFilter by dashboard UID
panel_idintegerNoFilter by panel ID
statestringNoFilter by state: alerting, ok, no_data, paused
folder_idintegerNoFilter by folder ID
org_idintegerNoOrganization ID

Example:

{
"name": "grafana_alert_list",
"input": {
"endpoint": "https://grafana.example.com",
"api_key": "glsa_xxxxxxxxxxxx",
"state": "alerting"
}
}

Response:

{
"alerts": [
{
"id": 1,
"name": "High CPU Usage",
"state": "alerting",
"dashboardUid": "abc123",
"panelId": 2,
"newStateDate": "2024-12-23T10:30:00Z"
}
],
"count": 1
}

Common Use Cases:

  • Monitor active alerts
  • Check alert status before deployments
  • Filter alerts by environment or service
  • Alert health checks

grafana_alert_silence

Create an alert silence to suppress notifications during maintenance windows or known issues.

Parameters:

ParameterTypeRequiredDescription
endpointstringYesGrafana server URL
api_keystringYesGrafana API key
matchersarray[object]YesLabel matchers for silence (see below)
ends_atstringYesEnd time (RFC3339 format)
commentstringYesReason for silence
starts_atstringNoStart time (default: now)
created_bystringNoUsername
org_idintegerNoOrganization ID

Matcher Object:

{
"name": "alertname", // Label name
"value": "HighCPU", // Label value
"isRegex": false // Whether value is regex
}

Example:

{
"name": "grafana_alert_silence",
"input": {
"endpoint": "https://grafana.example.com",
"api_key": "glsa_xxxxxxxxxxxx",
"matchers": [
{
"name": "alertname",
"value": "HighCPU",
"isRegex": false
},
{
"name": "environment",
"value": "production",
"isRegex": false
}
],
"ends_at": "2024-12-23T12:00:00Z",
"comment": "Planned maintenance window for database upgrade",
"created_by": "ops-team"
}
}

Response:

{
"silence_id": "abc123-def456",
"starts_at": "2024-12-23T10:00:00Z",
"ends_at": "2024-12-23T12:00:00Z"
}

Common Use Cases:

  • Silence alerts during deployments
  • Suppress known issues temporarily
  • Maintenance window alert management
  • Reduce alert noise during incidents

grafana_annotation_create

Create annotations on dashboards to mark significant events like deployments, incidents, or configuration changes.

Parameters:

ParameterTypeRequiredDescription
endpointstringYesGrafana server URL
api_keystringYesGrafana API key
textstringYesAnnotation text/description
timeintegerNoEvent time in Unix milliseconds (default: now)
time_endintegerNoEnd time for region annotations
tagsarray[string]NoTags for annotation
dashboard_uidstringNoAssociate with specific dashboard
panel_idintegerNoAssociate with specific panel
org_idintegerNoOrganization ID

Example:

{
"name": "grafana_annotation_create",
"input": {
"endpoint": "https://grafana.example.com",
"api_key": "glsa_xxxxxxxxxxxx",
"text": "Deployed v1.2.3 to production",
"tags": ["deployment", "production", "v1.2.3"],
"dashboard_uid": "prod-overview"
}
}

Response:

{
"annotation_id": 456,
"message": "Annotation created"
}

Common Use Cases:

  • Mark deployment events on metrics
  • Annotate incident start/end times
  • Document configuration changes
  • Track release timelines

Multi-Organization Support

For Grafana instances with multiple organizations, include the org_id parameter:

spec:
tools:
- grafana_query
env:
GRAFANA_ORG_ID: "2"

The organization ID will be sent as the X-Grafana-Org-Id header.


Environment Variables

For secure credential management, use environment variables:

apiVersion: aof.dev/v1
kind: Agent
metadata:
name: grafana-agent
spec:
model: google:gemini-2.5-flash
instructions: |
Query Grafana for metrics and create annotations.
tools:
- grafana_query
- grafana_annotation_create
env:
# Injected from secrets or local environment
GRAFANA_ENDPOINT: "${GRAFANA_URL}"
GRAFANA_API_KEY: "${GRAFANA_TOKEN}"
DATASOURCE_UID: "prometheus-prod"

Error Handling

Tools return structured error responses for common issues:

StatusErrorMeaning
401Authentication failedInvalid API key or expired token
404Resource not foundDashboard UID or datasource doesn't exist
429Rate limitedToo many requests, retry later
500+Server errorGrafana internal error

Example Error Response:

{
"error": "Authentication failed. Check API key and permissions."
}

Best Practices

  1. Use Service Account Tokens: Prefer service accounts over API keys for better security and audit trails
  2. Scope Permissions: Grant minimal permissions needed (viewer for queries, editor for annotations)
  3. Cache Datasource UIDs: Look up UIDs once and store in environment variables
  4. Time Ranges: Use relative times (now-1h) for dynamic queries
  5. Rate Limiting: Respect Grafana API rate limits, especially for automated queries
  6. Error Handling: Check for authentication and resource errors before retrying

Complete Example: Production Monitoring Agent

apiVersion: aof.dev/v1
kind: Agent
metadata:
name: production-monitor
namespace: monitoring
spec:
model: google:gemini-2.5-flash
instructions: |
You monitor production metrics in Grafana. When you detect anomalies:
1. Query relevant metrics using grafana_query
2. Check alert status with grafana_alert_list
3. Create annotations for significant events
4. Provide analysis and recommendations

tools:
- grafana_query
- grafana_alert_list
- grafana_annotation_create

env:
GRAFANA_ENDPOINT: "${GRAFANA_URL}"
GRAFANA_API_KEY: "${GRAFANA_TOKEN}"
PROMETHEUS_UID: "prometheus-prod"

triggers:
- type: cron
schedule: "*/5 * * * *" # Every 5 minutes
message: "Check production metrics and alert status"