# Monitoring Quick Start

Quick recipes for setting up TiDB monitoring with Prometheus and Grafana.

> Make sure you're in the `tiop/dashboards` folder before running these commands.

## Deploy Prometheus & Grafana

Install the [kube-prometheus-stack](https://github.com/prometheus-community/helm-charts/tree/main/charts/kube-prometheus-stack):

```bash
tiop helm install monitoring oci://ghcr.io/prometheus-community/charts/kube-prometheus-stack --namespace monitoring --create-namespace
```


Then apply the monitoring resources:

```bash
tiop kubectl apply -f ./resources
```

This sets up:
- A Prometheus instance to collect TiDB metrics
- A NodePort service to access Grafana (Delete it if you have an ingress or don't want to export grafana.)

> [!TIP] 
> Release name `monitoring` and namespace are hardcoded in `./resources`. If you have existing monitoring, check the [official docs](https://docs.pingcap.com/tidb-in-kubernetes/v2.0/monitor-a-tidb-cluster/#monitor-the-tidb-cluster) instead.

> [!NOTE]
> Get admin password by:
> 
> ```bash
> tiop kubectl get secrets monitoring-grafana -o jsonpath="{.data.admin-password}" -n monitoring | base64 -d ; echo
> ```


## Add Dashboards

Download the dashboards:

```bash
mkdir origin
cd origin
next_gen=1 ../get-grafana-dashboards.sh
cd ..
```

Convert and apply them:

```bash
# Adjust namespace and datasource_name if needed
namespace=monitoring
datasource_name='tidb-prom'

python3 ./batch-convert-dashboards.py --namespace $namespace --datasource-value $datasource_name --tag 'tidb'
tiop kubectl apply -f './configmaps' --server-side
```

Done! 🎉

When using `minikube`, get the dashboard URL by:

```bash
tiop minikube service prometheus-stack-grafana-nodeport --url -n monitoring
```

If a native kubernetes cluster was used, access grafana at `http://localhost:30000`.