This plugin allows Grafana to connect to Cloudflare D1 databases as a data source, enabling you to query and visualize your D1 data within Grafana dashboards.
- Connect to Cloudflare D1: Configure your D1 Account ID, Database ID, and API Token to access your database.
- Execute SQL Queries: Write standard SQL queries in the Grafana query editor to fetch data from your D1 tables.
- Visualize Data: Leverage Grafana's rich visualization capabilities (tables, charts, etc.) with your D1 data.
- Basic Type Inference: Attempts to infer data types (numbers, strings, booleans, timestamps in RFC3339Nano format) from query results.
- Grafana 9.x or later (Please update if a different version is targeted by the SDK used).
- A Cloudflare account with a D1 database.
- A Cloudflare API Token with permissions to read the D1 database.
-
Install the Plugin:
- From Release (Recommended for users): Download the latest release
zip
file from the Releases page (replace with actual link once you have one). Unzip it into your Grafana plugin directory (e.g.,/var/lib/grafana/plugins
on Linux, ordata/plugins
relative to your Grafana install). - Manual Build (For developers): See the Development section.
- Restart your Grafana server after installing the plugin.
- From Release (Recommended for users): Download the latest release
-
Gather Cloudflare D1 Credentials:
- Account ID:
- Log in to your Cloudflare dashboard.
- Navigate to "Workers & Pages" on the left sidebar.
- Your Account ID is displayed in the "Account details" section on the right sidebar. You can click "Click to copy".
- Alternatively, refer to the official Cloudflare documentation to find your Account ID.
- Database ID:
- In the Cloudflare dashboard, navigate to "Workers & Pages" -> "D1".
- Select your desired D1 database from the list.
- The Database ID (UUID) is a long alphanumeric string and will be displayed on the database's overview page, typically labeled as "ID".
- For more details on D1, you can visit the Cloudflare D1 documentation.
- API Token:
- In the Cloudflare dashboard, go to "My Profile" (top right icon) -> "API Tokens".
- Click "Create Token".
- It is recommended to use a "Custom token". Click "Get started" under Custom token.
- Give your token a descriptive name (e.g., "Grafana D1 ReadOnly Access").
- Under "Permissions", configure the following:
- Select
Account
for the resource group. - Select
D1
for the specific resource. - Select
Read
for the permission level. This grants read access to all D1 databases within the account(s) this token is scoped to.
- Select
- Under "Account Resources", you can typically leave it as
Include
->All accounts
if your user has access to multiple accounts and you want the token to work for any of them where D1 is used. TheD1 Read
permission itself is tied to the D1 service within an account. - You can optionally restrict the token by IP address or TTL.
- Click "Continue to summary".
- Review the permissions and click "Create Token".
- Important: Copy the generated API token immediately. You will not be able to see it again.
- For more details on creating API tokens, refer to the Cloudflare API token creation guide.
- Account ID:
-
Add Data Source in Grafana:
- In Grafana, go to "Connections" (or "Configuration" in older versions) -> "Data sources".
- Click "Add new data source".
- Search for "Cloudflare D1" (or the name you set in
plugin.json
) and select it. - Enter the following details:
- Name: A descriptive name for this data source instance (e.g., "My D1 Prod DB").
- Account ID: Your Cloudflare Account ID.
- Database ID: Your Cloudflare D1 Database ID.
- API Token: Your Cloudflare API Token (this is a secret and will be encrypted).
- Click "Save & test". You should see a message like "Successfully connected to Cloudflare D1 and executed test query."
- Create a Panel: Go to a dashboard and add a new panel.
- Select Data Source: Choose the Cloudflare D1 data source you configured.
- Write SQL Query: In the query editor, enter your SQL query.
SELECT column1, column2, timestamp_column FROM your_table WHERE some_condition = \'value\' ORDER BY timestamp_column DESC LIMIT 100;
- Visualize: Choose a visualization (e.g., Table, Time series) and configure it.
- Column Ordering: Columns in the Grafana table/results are currently sorted alphabetically by column name, not by the order in your
SELECT
statement. This is due to how data is processed from the D1 API. - Timestamp Handling: The plugin attempts to detect timestamp columns if they are strings formatted according to RFC3339Nano (e.g.,
2023-10-26T07:30:00.123456789Z
). Other timestamp formats might be treated as plain strings or numbers. For time-series visualizations, ensure your timestamp column is correctly identified. - Type Inference: Data types are inferred from the first row of the result set. If the first row has a
NULL
value for a column, that column may default to a string type.
-
Prerequisites:
-
Clone the Repository:
git clone https://github.com/olipayne/grafana-cloudflare-d1-datasource.git cd grafana-cloudflare-d1-datasource
-
Install Dependencies:
- Frontend:
npm install
- Backend:
go mod tidy
(should already be tidy)
- Frontend:
-
Build Plugin:
- Build frontend (watches for changes):
npm run dev
- Build backend (replace
darwinARM64
with your OS/architecture, e.g.,linuxAMD64
,windowsAMD64
):Common targets:mage -v build:darwinARM64
build:linuxAMD64
,build:linuxARM64
,build:darwinAMD64
,build:darwinARM64
,build:windowsAMD64
. Runmage -l
to see all available build targets. - To build all backend binaries:
mage -v buildAll
- Build frontend (watches for changes):
-
Run Grafana with Plugin Locally:
- Ensure
npm run dev
is running in one terminal. - Ensure your backend binary (e.g.,
dist/gpx_cloudflare_d1_datasource_linux_arm64
if your Docker runs Linux/ARM64) is built and matches the architecture expected by the Docker container (seeDockerfile
TARGETARCH
- Ensure