> ## Documentation Index
> Fetch the complete documentation index at: https://solana.com/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> This page is part of the Solana developer documentation at https://solana.com/developers/docs. Append `.md` to any docs URL, or send `Accept: text/markdown`, to get the page as Markdown.
> Index of all pages: https://solana.com/llms.txt. Per-section indexes such as https://solana.com/developers/docs/payments/llms.txt are smaller; prefer them when working on one task.
> Coding agents: install the official Solana skill for current APIs and conventions with `npx skills add https://github.com/solana-foundation/solana-dev-skill` (also served at https://solana.com/SKILL.md).
> Code in the cookbook and task pages comes from tested example files. Prefer it over writing API calls from memory.

# Monitoring & Metrics

> Monitor your Kora node with Prometheus and Grafana

_Last Updated: 2025-08-22_

The
[Kora Metrics Crate](https://github.com/solana-foundation/kora/tree/v2.0.5/crates/lib/src/metrics)
provides comprehensive metrics collection and monitoring for the Kora RPC
server.

Kora exposes a `/metrics` endpoint that provides real-time performance data in
Prometheus format. Set `port` in `[metrics]` to the RPC port to serve metrics
on the RPC server, or to another port to run a separate metrics listener. The
examples on this page use a separate listener on `8081`, with the RPC server on
`8080`.

## Configuration

Metrics are configured in the `[metrics]` section of your `kora.toml`. The
`[metrics]` section configures metrics collection and monitoring. This section
is optional and by default, metrics are disabled.

```toml
[metrics]
enabled = true
endpoint = "/metrics"
port = 8081  # Separate listener; use the RPC port (8080 by default) to serve metrics there
scrape_interval = 60

[metrics.fee_payer_balance]
enabled = true
expiry_seconds = 30
```

| Option            | Description                                                            | Required | Type    |
| ----------------- | ---------------------------------------------------------------------- | -------- | ------- |
| `enabled`         | Enable metrics collection                                              | ✅       | boolean |
| `endpoint`        | Custom metrics endpoint path                                           | ✅       | string  |
| `port`            | Metrics port: the RPC port to serve metrics there, or a separate port | ✅       | number  |
| `scrape_interval` | Frequency of Prometheus scrape (seconds)                               | ✅       | number  |

When `port` equals the RPC port, Kora logs
`Metrics endpoint enabled at /metrics on RPC server` and answers `/metrics` on
the RPC port. With a separate port, only that port answers `/metrics`.

### Fee Payer Balance Tracking

The `[metrics.fee_payer_balance]` section configures automatic monitoring of
your fee payer's SOL balance:

Leave the section out to keep balance tracking off. If you include it, set both
keys:

| Option           | Description                             | Required | Type    |
| ---------------- | --------------------------------------- | -------- | ------- |
| `enabled`        | Enable fee payer balance tracking       | ✅       | boolean |
| `expiry_seconds` | Background tracking interval in seconds | ✅       | number  |

When enabled, Kora automatically tracks your fee payer's SOL balance and exposes
it via the `signer_balance_lamports` Prometheus gauge, labeled with
`signer_name` and `signer_pubkey`. This helps with
capacity planning and low-balance alerting.

## Quick Start

**Access metrics:**

```bash
curl http://localhost:8081/metrics
```

## What You'll See

The metrics show how your RPC server is performing:

```
# Total requests by method and HTTP status
kora_http_requests_total{method="signTransaction",status="200"} 42
kora_http_requests_total{method="signTransaction",status="401"} 3

# Request duration (in seconds) by method, as a Prometheus histogram
kora_http_request_duration_seconds_bucket{method="signTransaction",le="0.05"} 40
kora_http_request_duration_seconds_bucket{method="signTransaction",le="+Inf"} 45
kora_http_request_duration_seconds_sum{method="signTransaction"} 1.9
kora_http_request_duration_seconds_count{method="signTransaction"} 45

# Signer balances (for multi-signer setups)
signer_balance_lamports{signer_name="primary_signer",signer_pubkey="4gBe...xyz"} 500000000
signer_balance_lamports{signer_name="backup_signer",signer_pubkey="7XyZ...abc"} 300000000
```

If you haven't called the RPC server yet, you will not see any metrics. You can
run a simple test by calling the `getConfig` method:

```bash
curl -X POST http://localhost:8080 \
  -H "Content-Type: application/json" \
  -d '{"jsonrpc": "2.0", "method": "getConfig", "id": 1}'
```

and then check the metrics:

```bash
curl http://localhost:8081/metrics
```

## Key Metrics Explained

1. **`kora_http_requests_total`** - How many requests you've handled
   - `method`: Which RPC method was called
   - `status`: HTTP status code of the response
   - JSON-RPC errors, such as a rejected transaction or a missing parameter,
     are returned with HTTP 200, so they're counted under `status="200"`.
     Requests that fail authentication are counted under `status="401"`.
     Unknown or disabled methods get HTTP 405 before metrics are collected,
     so they aren't counted.
   - Use this to track usage patterns and authentication failures

2. **`kora_http_request_duration_seconds`** - How fast your requests are
   - A Prometheus histogram with bucket, sum, and count series; compute p50,
     p95, and p99 response times with `histogram_quantile()`
   - Use this to identify slow endpoints

3. **`signer_balance_lamports`** - Current SOL balance of each signer
   - Shows balance in lamports (1 SOL = 1,000,000,000 lamports) for each signer
   - Labels: `signer_name` (human-readable name) and `signer_pubkey` (public
     key)
   - Updated automatically in the background when enabled
   - Use this for capacity planning and low-balance alerts across all signers

## Using the Data

### Option 1: Quick Health Check

```bash
# See all metrics
curl http://localhost:8081/metrics

# Check specific method performance
curl http://localhost:8081/metrics | grep signTransaction
```

### Option 2: Prometheus + Grafana (Recommended)

For graphs and alerts, run the full monitoring stack:

```bash
# from kora root directory
make run-metrics   # on main or beta tags: just run-metrics
```

Then visit:

- Prometheus: http://localhost:9090 (query metrics)
- Grafana Pre-built Kora dashboard: http://localhost:3000
  - Default login: admin/admin (or use the `GF_SECURITY_ADMIN_PASSWORD` and
    `GF_SECURITY_ADMIN_USER` credentials from your `.env` file)

### Option 3: Your Own Monitoring

Point any Prometheus-compatible tool at `http://your-server:8081/metrics`:

- Datadog
- New Relic
- CloudWatch
- VictoriaMetrics

## Example Queries (Prometheus)

```sh
# Requests per second by method
rate(kora_http_requests_total[1m])

# 95th percentile response time
histogram_quantile(0.95, sum by (le) (rate(kora_http_request_duration_seconds_bucket[5m])))

# Authentication failures per second (JSON-RPC errors are counted as status 200)
rate(kora_http_requests_total{status="401"}[5m])

# Balance of specific signer by name
signer_balance_lamports{signer_name="primary_signer"} / 1000000000

# Balance of specific signer by public key
signer_balance_lamports{signer_pubkey="4gBe...xyz"} / 1000000000

# Total balance across all signers
sum(signer_balance_lamports) / 1000000000

# Minimum balance across all signers (useful for alerts)
min(signer_balance_lamports) / 1000000000
```

## Multi-Signer Monitoring

When using multiple signers, you can monitor each signer individually or track
aggregate metrics:

### Individual Signer Metrics

```bash
# Check balance of specific signer
curl http://localhost:8081/metrics | grep 'signer_balance_lamports{signer_name="primary_signer"}'

# View all signer balances
curl http://localhost:8081/metrics | grep signer_balance_lamports
```

### Prometheus Queries for Multi-Signer Setups

```sh
# Alert if any signer has low balance (< 0.05 SOL)
min(signer_balance_lamports) < 50000000

# Monitor balance distribution across signers
signer_balance_lamports / on() group_left() sum(signer_balance_lamports)

# Track signer with lowest balance
min_over_time(signer_balance_lamports[1h])

# Count number of healthy signers (> 0.01 SOL)
count(signer_balance_lamports > 10000000)

# Average balance across all signers
avg(signer_balance_lamports) / 1000000000
```

## Security Note

The `/metrics` endpoint is public by default. In production, consider:

- Putting it behind a firewall
- Using a separate metrics port
- Adding authentication via reverse proxy

## How Metrics Collection Works

1. **HTTP Middleware Layer** - Intercepts all requests and collects:
   - Request count by JSON-RPC method and status
   - Request duration by method

2. **Metrics Endpoint** - `/metrics` endpoint exposed automatically when feature
   is enabled
   - Handled by `MetricsHandlerLayer`
   - Returns Prometheus-formatted metrics

3. **Prometheus Scraping** - Configured to scrape Kora every 60 seconds (see
   [`crates/lib/src/metrics/prometheus.yml`](https://github.com/solana-foundation/kora/blob/v2.0.5/crates/lib/src/metrics/prometheus.yml))
