Skip to content

Commit 9a540dd

Browse files
committed
feature/dev-tools
1 parent 755913d commit 9a540dd

File tree

13 files changed

+4108
-6
lines changed

13 files changed

+4108
-6
lines changed

cardano-db-sync/app/cardano-db-sync.hs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
{-# LANGUAGE CPP #-}
12
{-# LANGUAGE OverloadedStrings #-}
23
{-# LANGUAGE NoImplicitPrelude #-}
34

45
import Cardano.Db (MigrationDir (..), PGPassSource (PGPassDefaultEnv, PGPassEnv), gitRev)
56
import Cardano.DbSync (runDbSyncNode)
7+
8+
#ifdef GHC_DEBUG_ENABLED
9+
import GHC.Debug.Stub (withGhcDebug)
10+
#endif
611
import Cardano.DbSync.Config
712
import Cardano.DbSync.Config.Types
813
import Cardano.DbSync.Metrics (withMetricSetters)
@@ -21,8 +26,16 @@ import Prelude (error)
2126
---------------------------------------------------------------------------------------------------
2227
-- Main entry point into the app
2328
---------------------------------------------------------------------------------------------------
29+
2430
main :: IO ()
25-
main = do
31+
#ifdef GHC_DEBUG_ENABLED
32+
main = withGhcDebug dbSyncMain
33+
#else
34+
main = dbSyncMain
35+
#endif
36+
37+
dbSyncMain :: IO ()
38+
dbSyncMain = do
2639
cmd <- Opt.execParser opts
2740
case cmd of
2841
CmdVersion -> runVersionCommand

cardano-db-sync/cardano-db-sync.cabal

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ build-type: Custom
1717
extra-source-files: CHANGELOG.md
1818
schema/*.sql
1919

20+
flag enable-ghc-debug
21+
description: Enable ghc-debug profiling support for heap analysis.
22+
Build with: cabal build --flag enable-ghc-debug cardano-db-sync
23+
Zero overhead when disabled (production builds).
24+
default: False
25+
manual: True
26+
2027
custom-setup
2128
setup-depends:
2229
base
@@ -252,6 +259,12 @@ executable cardano-db-sync
252259
, optparse-applicative
253260
, text
254261

262+
if flag(enable-ghc-debug)
263+
build-depends: ghc-debug-stub
264+
cpp-options: -DGHC_DEBUG_ENABLED
265+
ghc-options: -finfo-table-map
266+
-fdistinct-constructor-tables
267+
255268
executable http-get-json-metadata
256269
default-language: Haskell2010
257270
main-is: http-get-json-metadata.hs

config/pgpass-mainnet-macos

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/tmp:5432:cexplorer:*:*

dev-tools/README.md

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# Cardano DB Sync - Developer Tools
2+
3+
Local development tools for monitoring and profiling cardano-db-sync.
4+
5+
## Overview
6+
7+
Two complementary tools for understanding cardano-db-sync performance:
8+
9+
### 🔍 [Monitoring](monitoring/README.md)
10+
Real-time metrics with Prometheus/Grafana
11+
- PostgreSQL metrics (queries, cache, connections)
12+
- System metrics (CPU, memory, disk I/O)
13+
- Time-series visualization
14+
15+
### 🧠 [Profiling](profiling/README.md)
16+
Memory profiling with ghc-debug
17+
- Interactive heap exploration
18+
- Memory leak detection
19+
- Retainer chain analysis
20+
21+
## Installation
22+
23+
### Monitoring
24+
25+
```bash
26+
# macOS
27+
brew install tmux prometheus postgres_exporter node_exporter grafana
28+
29+
# Linux (apt)
30+
sudo apt-get install tmux prometheus postgres-exporter prometheus-node-exporter grafana
31+
32+
# Linux (yum)
33+
sudo yum install tmux prometheus postgres_exporter node_exporter grafana
34+
```
35+
36+
### Profiling
37+
38+
```bash
39+
# Install ghc-debug-brick
40+
git clone https://gitlab.haskell.org/ghc/ghc-debug.git
41+
cd ghc-debug/brick
42+
cabal install ghc-debug-brick
43+
44+
# Instrument cardano-db-sync (see profiling/README.md for details)
45+
```
46+
47+
## Running
48+
49+
### Start Monitoring
50+
51+
```bash
52+
cd dev-tools/monitoring
53+
./scripts/start-monitoring.sh
54+
```
55+
56+
Access at:
57+
- Prometheus: http://localhost:9090
58+
- Grafana: http://localhost:3000 (if started separately)
59+
60+
### Start Profiling
61+
62+
```bash
63+
cd dev-tools/profiling
64+
./scripts/start-profiling.sh
65+
66+
# In another terminal (after several hours):
67+
ghc-debug-brick /tmp/cardano-db-sync.ghc-debug
68+
```
69+
70+
### Run Both Together
71+
72+
```bash
73+
# Terminal 1: Monitoring
74+
cd dev-tools/monitoring && ./scripts/start-monitoring.sh
75+
76+
# Terminal 2: Profiling
77+
cd dev-tools/profiling && ./scripts/start-profiling.sh
78+
```
79+
80+
## Directory Structure
81+
82+
```
83+
dev-tools/
84+
├── README.md # This file
85+
86+
├── monitoring/ # Prometheus/Grafana monitoring
87+
│ ├── README.md # Full monitoring documentation
88+
│ ├── scripts/
89+
│ │ └── start-monitoring.sh
90+
│ ├── config/
91+
│ │ └── prometheus.yml
92+
│ ├── data/ # Prometheus data (gitignored)
93+
│ └── docs/
94+
│ └── METRICS.md # Available metrics
95+
96+
└── profiling/ # ghc-debug profiling
97+
├── README.md # Full profiling documentation
98+
├── scripts/
99+
│ ├── start-profiling.sh
100+
│ └── analyze-memory.sh
101+
├── snapshots/ # Heap snapshots (gitignored)
102+
├── logs/ # Memory logs (gitignored)
103+
└── reports/ # Analysis reports (commit these)
104+
```
105+
106+
## Documentation
107+
108+
- [Monitoring Setup Guide](monitoring/README.md)
109+
- [Profiling Setup Guide](profiling/README.md)
110+
- [Available Metrics Reference](monitoring/docs/METRICS.md)

dev-tools/monitoring/README.md

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# Cardano DB Sync - Monitoring
2+
3+
Local development monitoring using Prometheus and Grafana for PostgreSQL and system metrics.
4+
5+
## Quick Setup
6+
7+
### 1. Install Tools
8+
9+
```bash
10+
# macOS
11+
brew install prometheus postgres_exporter node_exporter grafana
12+
13+
# Linux (apt)
14+
sudo apt-get install prometheus postgres-exporter prometheus-node-exporter grafana
15+
16+
# Linux (yum)
17+
sudo yum install prometheus postgres_exporter node_exporter grafana
18+
```
19+
20+
### 2. Setup PostgreSQL Monitoring User
21+
22+
```sql
23+
-- Connect to your database
24+
psql -U postgres -d cexplorer
25+
26+
-- Create monitoring user
27+
CREATE USER postgres_exporter WITH PASSWORD 'secure_password';
28+
GRANT pg_monitor TO postgres_exporter;
29+
GRANT CONNECT ON DATABASE cexplorer TO postgres_exporter;
30+
```
31+
32+
### 3. Start Monitoring
33+
34+
```bash
35+
cd dev-tools/monitoring
36+
./scripts/start-monitoring.sh
37+
```
38+
39+
This launches a tmux session with Prometheus and exporters.
40+
41+
### 4. Start Grafana
42+
43+
```bash
44+
# macOS
45+
brew services start grafana
46+
47+
# Linux
48+
sudo systemctl start grafana-server
49+
```
50+
51+
## Access Dashboards
52+
53+
- **Prometheus**: http://localhost:9090
54+
- **Grafana**: http://localhost:3000 (default: admin/admin)
55+
56+
## Grafana Setup
57+
58+
1. Open http://localhost:3000
59+
2. Add Prometheus data source:
60+
- Configuration → Data Sources → Add data source
61+
- Select Prometheus
62+
- URL: `http://localhost:9090`
63+
- Save & Test
64+
3. Import dashboard:
65+
- Dashboards → Import
66+
- Upload `config/grafana-db-sync.json`
67+
- Select Prometheus data source
68+
69+
## Key Metrics
70+
71+
- **System**: CPU, memory usage (node_exporter)
72+
- **PostgreSQL**: Connections, transaction rates, cache hit ratio
73+
- **Sync Progress**: Block height, epoch duration
74+
75+
## Troubleshooting
76+
77+
**postgres_exporter fails to connect**:
78+
- Verify monitoring user exists and has permissions
79+
- Set `DATA_SOURCE_NAME` with correct credentials in `scripts/start-monitoring.sh`
80+
81+
**Prometheus shows "DOWN" targets**:
82+
- Check exporters are running: `ps aux | grep exporter`
83+
- Test endpoints: `curl http://localhost:9187/metrics` and `curl http://localhost:9100/metrics`
84+
85+
**Port already in use**:
86+
```bash
87+
# Kill processes using ports
88+
lsof -ti:9090 | xargs kill # Prometheus
89+
lsof -ti:9100 | xargs kill # node_exporter
90+
lsof -ti:9187 | xargs kill # postgres_exporter
91+
```
92+
93+
## Stop Services
94+
95+
```bash
96+
# Stop monitoring
97+
tmux kill-session -t cardano-monitoring
98+
99+
# Stop Grafana
100+
brew services stop grafana # macOS
101+
sudo systemctl stop grafana-server # Linux
102+
```
103+
104+
## Files
105+
106+
- `scripts/start-monitoring.sh` - Launch monitoring suite
107+
- `config/prometheus.yml` - Prometheus configuration
108+
- `config/grafana-db-sync.json` - Grafana dashboard
109+
- `docs/METRICS.md` - Complete metrics reference
110+
111+
## See Also
112+
113+
- [Prometheus Documentation](https://prometheus.io/docs/)
114+
- [Grafana Documentation](https://grafana.com/docs/)

0 commit comments

Comments
 (0)