Openstatus
www.openstatus.dev
1# Export Blog Post Metrics Script
2
3This script exports monitor metrics data from OpenStatus for use in blog posts and documentation.
4
5## Overview
6
7The script fetches monitor data directly from the database and Tinybird analytics, then exports it to a JSON file that can be used for visualizations in blog posts.
8
9**Features:**
10- Fetches metrics from both regular regions and private locations
11- Automatically combines public regions with private location data
12- Supports both HTTP and TCP monitors
13
14## Configuration
15
16Edit the constants at the top of `export-blog-post-metrics.ts`:
17
18```typescript
19const MONITOR_ID = "1"; // The ID of the monitor to export
20const PERIOD = "7d"; // Time period: "1d", "7d", or "14d"
21const INTERVAL = 60; // Interval in minutes for data points
22const TYPE = "http"; // Fallback monitor type: "http" or "tcp" (auto-detected from monitor)
23const OUTPUT_FILE = "blog-post-metrics.json"; // Output filename
24```
25
26**Note:** The script automatically detects the monitor type from the database, but you can set a fallback with the `TYPE` constant.
27
28## Prerequisites
29
301. Make sure you have the `TINY_BIRD_API_KEY` environment variable set in your `.env` file
312. The database should be accessible (local or remote)
323. Install dependencies: `pnpm install`
33
34## Usage
35
36> [!IMPORTANT]
37> Go to the `/tinybird/src/client.ts` file and make sure tb is **not using the NoopClient**.
38
39From the `apps/dashboard` directory:
40
41```bash
42# Using the npm script
43pnpm export-metrics
44
45# Or directly with bun
46bun src/scripts/export-blog-post-metrics.ts
47```
48
49## Output Format
50
51The script generates a JSON file with the following structure:
52
53```json
54{
55 "regions": ["ams", "fra", "lhr", ...],
56 "data": {
57 "regions": ["ams", "fra", "lhr", ...],
58 "data": [
59 {
60 "timestamp": "2025-08-18T16:00:00.000Z",
61 "ams": 207,
62 "fra": 142,
63 "lhr": 327,
64 ...
65 }
66 ]
67 },
68 "metricsByRegions": [
69 {
70 "region": "ams",
71 "count": 1000,
72 "ok": 995,
73 "p50Latency": 150,
74 "p75Latency": 200,
75 "p90Latency": 250,
76 "p95Latency": 300,
77 "p99Latency": 400
78 }
79 ]
80}
81```
82
83## Data Fields
84
85- **regions**: Array of region codes and private location names for the monitor
86- **data.data**: Timeline data with latency values per region/location at each timestamp
87- **metricsByRegions**: Summary statistics per region/location including:
88 - `count`: Total number of checks
89 - `ok`: Number of successful checks
90 - `p50Latency`, `p75Latency`, `p90Latency`, `p95Latency`, `p99Latency`: Latency percentiles in milliseconds
91
92**Note:** The script automatically includes both public Fly.io regions and any private locations connected to the monitor.
93
94## Example: Moving to Web Assets
95
96To use the exported data in the web app (like the existing `hono-cold.json`):
97
98```bash
99# After running the script
100cp blog-post-metrics.json ../web/public/assets/posts/your-blog-post/data.json
101```
102
103## Troubleshooting
104
105**Error: "TINY_BIRD_API_KEY environment variable is required"**
106- Make sure you have the `TINY_BIRD_API_KEY` set in your `.env` file
107
108**Error: "Monitor with ID X not found"**
109- Verify the monitor ID exists in your database
110- Check that you're connected to the correct database
111
112**No data returned**
113- Ensure the monitor has been running and collecting data for the specified period
114- Try a different time period (e.g., "7d" instead of "1d")
115