# Monitoring and alerting

Connect the Backend to the monitoring of the company and set the alerts that show a problem before users see it.

The Backend (The server program of Platforma. It keeps the projects, runs the Blocks and starts jobs.) gives a health check, metrics and logs, so you can connect it to the monitoring that the company already has: Prometheus, the monitoring of the cloud, or systemd. This page tells where each signal is, which alerts to set, and how to find the cost of the installation in the cloud bill.

## Health of the Backend

The main check is `/health` on the metrics port, `9090` by default. It answers `200` when the Backend is ready and `503` when it is not ready. When TLS is on, this port serves HTTPS only.

#### Variant: Kubernetes cluster

The probes of the pod use this check: the readiness probe asks `/health`, and the liveness probe asks `/health?service=GRPC`, so Kubernetes (A system that runs programs on a group of machines (a cluster). Platforma can run in a Kubernetes cluster.) restarts the Backend when its gRPC service stops.

```sh
kubectl --namespace <namespace> get deployment <release>
kubectl --namespace <namespace> get events --field-selector type=Warning
```

#### Variant: Single Server

The metrics port listens on `127.0.0.1`. On the server:

```sh
curl --insecure https://127.0.0.1:9090/health
systemctl status platforma
systemctl show platforma --property=NRestarts
```

Without TLS, use `http://`. The unit has `Restart=on-failure`: systemd starts the Backend again after a failure, so count the restarts.

From outside, the gRPC health check on the entry point (The way in to the Backend from outside the cluster, a load balancer or an Ingress with a DNS name. It usually ends TLS and sends the traffic to the Backend pod.) (port `6345` in Single Server mode (The mode in which the Backend and all its jobs run on one Linux machine, the server, with no Kubernetes cluster.), `443` through the entry point of the cluster) shows that clients can reach the Backend. It does not need a sign-in (To show the Backend who you are, with a sign-in provider or a single-user token. The verb is "sign in"; the noun and the adjective are "sign-in".). See [Verify the installation](/install-verify/).

## Metrics

A collector scrapes `/metrics` on the same port, in the Prometheus format. For the address and the most useful metrics, see [Metrics](/reference-metrics/).

## Logs

#### Variant: Kubernetes cluster

The Backend writes its full log to the logs disk, `/var/log/platforma` in the pod. `kubectl logs` shows only the short start log. When the logs disk is off, the full log goes to the output of the pod.

#### Variant: Single Server

The full log is in `/data/platforma/log/`. Errors before the Backend opens its log are in the journal: `journalctl -u platforma`.

Errors have `"level":"error"`. To collect logs for support, see [Get help](/maintain-get-help/).

## Alerts to set

| Alert                                                                                                                                                                                                                                                      | Signal                                                                                                                                                                    | What it means                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Backend not ready for some minutes                                                                                                                                                                                                                         | `/health` answers `503`, or the pod is not `Ready`                                                                                                                        | Users cannot connect                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| Restart loop                                                                                                                                                                                                                                               | The pod is in `CrashLoopBackOff`; in Single Server mode, `NRestarts` grows                                                                                                | The Backend cannot start. With `--fail-fast-on-invalid-license`, a license that is not valid also stops the start: see [License](/configuration-license/#when-the-license-is-not-valid)                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| Database disk fills up                                                                                                                                                                                                                                     | Free space of the claim `<release>-database` (Single Server mode: the disk of the data folder)                                                                            | The Backend keeps all its state there                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
| Logs disk fills up                                                                                                                                                                                                                                         | Free space of the claim `<release>-logs` (Single Server mode: the disk of the data folder)                                                                                | The Backend cannot write its log                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| Workspace (The storage where the Backend and all jobs keep the working files of running jobs. The Backend and the jobs use it at the same time. In Kubernetes, it is a network file system; in Single Server mode, it is a folder on the server.) fills up | Free space of the claim `<release>-workspace` (Single Server mode: the disk of the data folder)                                                                           | Jobs (One run of a tool on data. The Backend starts jobs on the server where it runs, or in a Kubernetes cluster.) cannot write their working files and fail. On Google Cloud, the Filestore instance of the workspace has a fixed size: the Terraform modules (The Terraform code that MiLaboratories provides to install Platforma. The cloud-specific Terraform modules make the cluster and its cloud parts in AWS or Google Cloud; the cloud-agnostic Terraform modules install Platforma into the cluster.) make 1024 GiB by default, and the installer sets the size from the deployment size. On AWS, EFS grows by itself |
| Job pods wait for a long time                                                                                                                                                                                                                              | Job pods are `Pending`, or Kueue (A Kubernetes add-on that holds jobs in a queue until the cluster has free CPU, memory and GPU for them.) does not admit their workloads | Analyses do not start. The Desktop App (The program that each user installs on their own computer to work with Platforma. It is a client of the Backend.) also shows the load of the queue on its server icon: see [A job waits or hangs](/maintain-troubleshooting-jobs/#look-at-the-load-of-the-queue)                                                                                                                                                                                                                                                                                                                          |
| Certificate is close to its expiry                                                                                                                                                                                                                         | The expiry date of the certificate of the entry point                                                                                                                     | The Desktop App cannot connect after the expiry                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| Storage errors                                                                                                                                                                                                                                             | `pl_objfs_transfers_total{result="failed"}` or `pl_objfs_calls_total{result="error"}` grows                                                                               | The Backend cannot read or write the primary storage (The bucket (Amazon S3 or Google Cloud Storage) or folder where Platforma keeps all results and uploaded files.), when it is a bucket. Jobs and file transfers fail                                                                                                                                                                                                                                                                                                                                                                                                          |
| API errors                                                                                                                                                                                                                                                 | `grpc_server_handled_total` with a `grpc_code` other than `OK` and `Canceled` grows fast                                                                                  | Requests of the Desktop App fail                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |

In Kubernetes, the free space of a volume comes from the metrics of the kubelet (`kubelet_volume_stats_available_bytes`), with the name of the claim in the label `persistentvolumeclaim`. `<release>` is the name of the Helm release of the Backend, `platforma` by default. In Single Server mode, the database, the logs and the working folders of the jobs are all in the data folder: watch the free space of its disk.

### Rules in the monitoring of the company

The alerts use the metrics of the Backend and the standard metrics of the cluster or the server. Write them as rules of the alerting tool that the company uses.

#### Variant: Kubernetes cluster

The example is a `PrometheusRule` of the Prometheus Operator (The person who installs and runs Platforma in your company, for example an IT engineer or a bioinformatician.). It needs the metrics of the kubelet (volumes) and of kube-state-metrics (the Deployment), and a collector that scrapes the Backend.

*platforma-alerts.yaml*

```yaml
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
  name: platforma
  namespace: <namespace>
spec:
  groups:
    - name: platforma
      rules:
        - alert: PlatformaBackendNotReady
          expr: kube_deployment_status_replicas_available{namespace="<namespace>", deployment="<release>"} < 1
          for: 5m
        - alert: PlatformaVolumeAlmostFull
          expr: |
            kubelet_volume_stats_available_bytes{namespace="<namespace>", persistentvolumeclaim=~"<release>-(database|logs|workspace)"}
              / kubelet_volume_stats_capacity_bytes < 0.1
          for: 15m
        - alert: PlatformaStorageErrors
          expr: increase(pl_objfs_transfers_total{result="failed"}[15m]) > 0
```

#### Variant: Single Server

Watch the state of the unit `platforma` in systemd, the answer of `/health` on `127.0.0.1:9090`, and the free space of the disk of the data folder. With Prometheus, the node exporter gives the free space (`node_filesystem_avail_bytes`) and, with its systemd collector, the state of the unit.

## Cost of the cloud resources

The cloud bill shows the cost of each resource. The tags (AWS) or labels (Google Cloud) that the installation puts on its resources let you filter the bill by installation.

#### Variant: AWS installer

The stack puts the tag `platforma` with the value of the parameter `ClusterName` on its resources: the network, the cluster, the nodes and their boot disks, the bucket and the file system. To filter the bill by this tag, activate `platforma` as a cost allocation tag in the Billing console of AWS. The bill shows the tag only for the costs after the activation.

#### Variant: Google Cloud installer

The installer labels the bucket with `platform=platforma` and `cluster=<name>-cluster`, and the database disk with `platforma-installation=<name>-cluster` and `platforma-component=database`. The Google Cloud bill also groups the costs by project: to see the full cost of the installation, install it in a project of its own.

#### Variant: Terraform modules

The modules put the same keys on all cloud resources that support them: tags on AWS, labels on Google Cloud.

| Key                      | Value                                                                                                                                                                               |
| ------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `platforma-installation` | The input `name` of the root `cloud`                                                                                                                                                |
| `platforma-component`    | What the resource is for: `network`, `cluster`, `identity`, `primary-storage`, `workspace`, `entry-point`, `registry-mirrors`, and for the volumes `database`, `logs` and `scratch` |
| `platforma-node-pool`    | On the nodes and their disks, in place of `platforma-component`: `system`, `backend`, `ui`, `batch` or `gpu`                                                                        |

Some resources come from the controllers of the cluster, not from Terraform. They also get the keys. On AWS, these are the volumes, the access points of the file system and the load balancer. On Google Cloud, these are the disks of the volumes. To add the keys of the company, such as a cost centre, use the input `tags` (AWS) or `labels` (Google Cloud) of the root `cloud`. The modules refuse the three keys of the table in these inputs.

#### Variant: AWS

Activate the keys as cost allocation tags in the Billing console of AWS. Then Cost Explorer can group the costs by installation, by component and by node pool, for the costs after the activation. On AWS, the network, the cluster and the IAM roles have the tags too.

#### Variant: Google Cloud

The labels go to the bill and to its export to BigQuery. On Google Cloud, the network, the firewall rules, the service accounts and the DNS records do not support labels.

#### Variant: Own cluster

The modules do not create cloud resources on a cluster that exists. The cost is the cost of the cluster: tag it as the company tags its other clusters.

#### Variant: Helm chart

The chart does not create cloud resources itself. The volumes come from the StorageClasses of the cluster and the load balancer from its entry point: they get the tags or labels that the cluster gives. The cost is the cost of the cluster and of the bucket.

#### Variant: Single Server

The cost is the cost of the server, its disks and the bucket, if the primary storage is a bucket. Tag them in the cloud console when the server is in a cloud.

## Next

- [Troubleshooting: connection](/maintain-troubleshooting-connection/)
- [A job waits or hangs](/maintain-troubleshooting-jobs/)
- [Get help](/maintain-get-help/)

Source: https://docs.platforma.bio/maintain-monitoring/
