Opinionated InfluxDB client
Project description
Grafane
A very opinionated InfluxDB client inspired by Grafana's query builder.
Setup
Installation
poetry add grafane
Configuration
Grafane supports multi-database setups with a Django-style configuration system.
Recommended: Create a settings module in your project:
# myproject/settings.py
INFLUXDB_SETTINGS = {
'default': {
'host': 'localhost',
'port': 8086,
'database': 'metrics',
'username': 'admin',
'password': 'secret',
'metrics': [],
},
}
Then configure Grafane:
import grafane
grafane.configure('myproject.settings')
Alternatively, set the environment variable:
export GRAFANE_SETTINGS_MODULE=myproject.settings
Environment Variables (Default Configuration)
If no settings module is configured, Grafane uses these environment variables for a single default database:
| Variable | Default | Description |
|---|---|---|
INFLUXDB_HOST |
0.0.0.0 |
InfluxDB host |
INFLUXDB_PORT |
8086 |
InfluxDB port |
INFLUXDB_DB |
metrics |
Database name |
INFLUXDB_USER |
admin |
Username |
INFLUXDB_USER_PASSWORD |
admin123 |
Password |
TESTING |
0 |
If set, appends -testing to metric names |
For multi-database setups, use a settings module instead (see Configuration above).
Quick Start
import grafane
# Configure with your settings module (optional if using env vars)
grafane.configure('myproject.settings')
# Create a client for a metric
c = grafane.Grafane(metric='temperature')
# Write
c.report({'value': 23.5}, {'room': 'living'})
# Read
results = c.select(fields='value').filter_by('room', '=', 'living').execute_query()
Multi-Database Setup
Configure multiple InfluxDB databases in your settings module:
# myproject/settings.py
INFLUXDB_SETTINGS = {
'default': {
'host': 'localhost',
'port': 8086,
'database': 'metrics',
'username': 'admin',
'password': 'secret',
'metrics': [], # Empty = fallback for unmatched metrics
},
'analytics': {
'host': 'analytics.example.com',
'port': 8086,
'database': 'analytics',
'username': 'analytics_user',
'password': 'analytics_pass',
'metrics': ['page_views', 'sessions', 'events'],
},
'monitoring': {
'host': 'monitoring.example.com',
'port': 8086,
'database': 'monitoring',
'username': 'monitoring_user',
'password': 'monitoring_pass',
'metrics': ['cpu', 'memory', 'disk'],
},
}
Database Configuration Keys
| Key | Type | Required | Description |
|---|---|---|---|
host |
str | Yes | InfluxDB host |
port |
int | Yes | InfluxDB port |
database |
str | Yes | Database name |
username |
str | Yes | Username |
password |
str | Yes | Password |
metrics |
list | No | Metrics routed to this database (empty = fallback) |
InfluxDB v2 Support
Note: Python 3.9 and 3.10 support were dropped to prepare for the InfluxDB v2 client. Grafane 1.0.0 still supports Python 3.9+.
Grafane supports InfluxDB v2 with the same API. Install the v2 client:
pip install grafane[v2]
# or
poetry add grafane --extras v2
Configure v2 databases in your settings module:
# myproject/settings.py
INFLUXDB_SETTINGS = {
'default': {
'version': 2,
'url': 'http://localhost:8086',
'token': 'my-api-token',
'org': 'my-org',
'bucket': 'my-bucket',
'metrics': [],
},
}
v2 Configuration Keys:
| Key | Type | Required | Description |
|---|---|---|---|
version |
int | Yes | Set to 2 for InfluxDB v2 |
url |
str | Yes | InfluxDB v2 URL |
token |
str | Yes | API token |
org |
str | Yes | Organization |
bucket |
str | Yes | Bucket name |
metrics |
list | No | Metrics routed to this bucket (empty = fallback) |
Mixed v1/v2 Setup:
You can configure both v1 and v2 databases in the same settings:
INFLUXDB_SETTINGS = {
'legacy': {
'host': 'localhost',
'port': 8086,
'database': 'metrics_v1',
'username': 'admin',
'password': 'secret',
'metrics': ['cpu', 'memory'],
},
'modern': {
'version': 2,
'url': 'http://localhost:8086',
'token': 'my-token',
'org': 'my-org',
'bucket': 'metrics_v2',
'metrics': ['events', 'traces'],
},
}
Metric Routing
Grafane automatically routes metrics to the correct database based on the metrics list:
from grafane import Grafane
# Routes to 'analytics' (has 'page_views' in metrics list)
c = Grafane('page_views')
# Routes to 'monitoring' (has 'cpu' in metrics list)
c = Grafane('cpu')
# Routes to 'default' (fallback - empty metrics list)
c = Grafane('unknown_metric')
# Explicit database selection (bypasses routing)
c = Grafane('any_metric', db='analytics')
Routing rules:
- If
db=parameter is provided, use that database - If metric is in exactly one database's
metricslist, use that database - If metric is in multiple databases'
metricslists, raises error (usedb=to resolve) - If metric is not found, use the fallback database (database with empty
metricslist) - If no fallback exists, raises error
Write
report()
c.report(fields, tags, timestamp=False)
report_points()
c.report_points([
{'fields': {'value': 1.2}, 'tags': {'tag1': 'a'}},
{'fields': {'value': 1.8}, 'tags': {'tag1': 'b'}},
])
If no timestamp is provided, defaults to datetime.now(pytz.utc).
Read
Chainable Query API
All query methods return self and can be chained:
results = (
c.select(fields=['value', 'value2'], aggregation='mean')
.filter_by('tag1', '=', 'value1')
.time_block('1h')
.fill_with('none')
.execute_query()
)
select()
# Single field
c.select(fields='value')
# Multiple fields
c.select(fields=['value', 'value2'])
# With aggregation
c.select(fields='value', aggregation='sum')
# Multiple fields with different aggregations
c.select(fields=['value', 'value2'], aggregation=['sum', 'mean'])
filter_by()
Filter by tag with an operator:
c.filter_by('tag1', '=', 'value1')
Supported operators: =, !=, <, >, <=, >=, =~ (regex)
filter_value_in()
Match tags against multiple values:
c.filter_value_in('tag1', ['value1', 'value2'])
filter_time_range()
from datetime import datetime
time_range = (datetime(2024, 1, 1), datetime(2024, 1, 31))
c.filter_time_range(time_range)
Accepts tuple or list of datetime objects. Order doesn't matter.
time_block()
Group results by time intervals:
c.select(fields='value', aggregation='mean').time_block('1h')
fill_with()
Fill empty time blocks:
c.fill_with('none')
Options: none, null, 0, previous, linear
group_by()
Group by tag values (requires aggregation):
c.select(fields='value', aggregation='sum').group_by('tag1')
Iteration, Length, and Boolean
# Iterate directly
for row in c.select(fields='value'):
print(row)
# Check result count
count = len(c.select(fields='value'))
# Boolean check
if c.select(fields='value').filter_by('tag1', '=', 'x'):
print("Has results")
Drop Measurement
c = Grafane(metric='test')
c.drop_measurement()
Development
Docker Setup
Start services:
ahoy docker up
Services:
- metrics (InfluxDB 1.8): http://localhost:8086
- grafana: http://localhost:3000 (passwordless, admin access)
Jupyter Notebooks
ahoy notebooks run
Notebooks are stored in .notebooks/ directory.
Environment Files
Copy .env.copy to .env:
cp .env.copy .env
Contents:
INFLUXDB_DATA_ENGINE=tsm1
INFLUXDB_DB=metrics
INFLUXDB_USER=admin
INFLUXDB_PASSWORD=admin123
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file grafane-2.0.1.tar.gz.
File metadata
- Download URL: grafane-2.0.1.tar.gz
- Upload date:
- Size: 29.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.13.11 Darwin/25.3.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
997923fc4a3f2c01cb27370ff08429217656506ba84a6eefa53574b0ca2508ef
|
|
| MD5 |
16a30e3f04ccacd319e3f5793d8fc5f9
|
|
| BLAKE2b-256 |
d4fb41f3b198b1880c92be1565d98141876b29b4c2ab2395099edba496f01b3a
|
File details
Details for the file grafane-2.0.1-py3-none-any.whl.
File metadata
- Download URL: grafane-2.0.1-py3-none-any.whl
- Upload date:
- Size: 33.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.13.11 Darwin/25.3.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9fc3701037394c41fdbfa6ded5169d4853985198c84ff6863f68b182563a7431
|
|
| MD5 |
1929ca6bdfe4872c7137bd98ed840acb
|
|
| BLAKE2b-256 |
f6f4777f8ff45c84037cc4eb39a005a16e2b2f8cd497c05f68815007e7a0b1d9
|