# Send metrics and logs to Monitoring

The OpenTelemetry collector add-on puts node and workload metrics into Monitoring and container logs into the cluster's Logs, with nothing to set up.

Replace _my-cluster_ with your cluster's name or id.

The `opentelemetry-collector` add-on is installed on every new cluster unless you turn
**Metrics and logs into Monitoring** off when you create it (or list other add-ons in the create
request). It runs the OpenTelemetry Collector on every node and one more for the cluster's object
state, and sends what they collect to Deutsche Cloud with their service-account tokens: no key is
stored in the cluster.

A cluster created without it (or before it was a default) is never given it silently: its
**Add-ons** tab offers **Collect metrics and logs**: choose it to install the add-on with the
defaults.

## What you get

**Metrics**, in Monitoring under the namespace `Kubernetes/<cluster name>`, once a minute:

| Metric                                                                                        | Dimensions                           |
| --------------------------------------------------------------------------------------------- | ------------------------------------ |
| `k8s.node.cpu.usage` (cores), `k8s.node.memory.working_set` (bytes)                           | `node`                               |
| `k8s.node.filesystem.usage`, `k8s.node.filesystem.capacity`                                   | `node`                               |
| `k8s.node.network.io`                                                                         | `node`, `direction`                  |
| `k8s.node.condition_ready` (1 ready, 0 not)                                                   | `node`                               |
| `k8s.pod.cpu.usage`, `k8s.pod.memory.working_set`, `k8s.container.restarts`                   | `namespace`, `workload`              |
| `k8s.pod.network.io`                                                                          | `namespace`, `workload`, `direction` |
| `k8s.deployment.available`, `k8s.deployment.desired` (and StatefulSet, DaemonSet equivalents) | `namespace`, `workload`              |

`workload` is `deployment/web`, `statefulset/db`, `daemonset/agent`, `cronjob/report`, `job/x`, or
`pod/<name>` for a pod nobody owns. Every pod of a workload lands on the same series: its
**Average** and **Maximum** are per pod, and a rollout makes no new series. Chart them in
**Monitoring › Custom metrics** (the cluster's **Monitoring** tab links there) and put alarms on
them like on any metric.

**Logs**: every container's lines, in the cluster's **Logs** as type **Workloads**, searchable
for as long as your logs retention. At most 10,000 lines a minute per cluster are kept; lines
above that are dropped and the collector is told how many.

```sh
dcloud eks clusters logs my-cluster --type workloads --namespace shop --contains error
```

## Install, change or remove

You need `eks:CreateAddon` (to change: `eks:UpdateAddon`; to remove: `eks:DeleteAddon`) on the
cluster, and in the console also `eks:DescribeCluster` to open it. Reading the metrics takes
`monitoring:GetMetricData`; reading the logs, `eks:GetClusterLogs`.

**Console**
1. Open **Kubernetes**, open the cluster, then choose **Add-ons**.
2. Choose **Add an add-on**, then **Metrics and logs**; or **Change** on its row to switch metrics
   or logs off, or to leave namespaces' logs out.
3. Choose **Install** (or **Apply**).

**CLI**
```sh
dcloud eks addons create my-cluster --name opentelemetry-collector
dcloud eks addons update my-cluster opentelemetry-collector --config 'logs=false'
```

**API**
```http
POST /eks/api/clusters/my-cluster/addons
{ "name": "opentelemetry-collector", "config": { "excludeLogNamespaces": ["kube-system"] } }
```

**Terraform**
```hcl
resource "dcloud_eks_addon" "telemetry" {
  cluster_id = dcloud_eks_cluster.main.id
  name       = "opentelemetry-collector"
  config     = jsonencode({ logs = true, metrics = true })
}
```

## What it runs

- `otel-agent`, a DaemonSet on every node (tolerates every taint): reads the kubelet's stats over
  TLS the cluster CA verifies, and the container log files under `/var/log/pods`. Log files belong
  to root, so it runs as root with no capability, and its namespace `opentelemetry-collector`
  allows privileged pods. With `logs: false` it runs as a normal user and mounts nothing from the
  node.
- `otel-cluster`, one replica, reading the API (Deployments, pods, nodes) for the object metrics.
- Its own logs are never collected. A collector that restarts starts its log files at their end:
  lines written while it was down are not sent.
