Skip to content

Commit 30942bf

Browse files
authored
Merge pull request #7015 from killianmuldoon/docs/add-log-exploration-ideas
📖 Add example queries to developer logging guidelines
2 parents 18e45bf + e04196e commit 30942bf

File tree

2 files changed

+53
-16
lines changed

2 files changed

+53
-16
lines changed

REVIEWING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ This makes any change that can impact the Cluster API contract critical and usua
123123
While developing controllers in Cluster API a key requirement is to add logging to observe the system and
124124
to help troubleshooting issues.
125125

126-
- For CAPI controllers see TODO.
126+
- For CAPI controllers see [Cluster API logging conventions](https://cluster-api.sigs.k8s.io/developer/logging.html).
127127
- For clusterctl see [clusterctl logging conventions](https://github.com/kubernetes-sigs/cluster-api/blob/main/cmd/clusterctl/log/doc.go).
128128

129129
### Testing

docs/book/src/developer/logging.md

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -130,23 +130,25 @@ thorny parts of code. Over time, based on feedback from SRE/developers, more log
130130

131131
## Developing and testing logs
132132

133-
We are continuously improving our [Tilt](tilt.md) setup allowing Cluster API developers to use logs and improve them as part of
134-
their development process.
135-
136-
Developers can deploy an entire log observability suite by simply setting the
137-
`deploy_observability` value in your [tilt-setting.yaml](tilt.md#create-a-tilt-settings-file).
133+
Our [Tilt](tilt.md) setup offers a batteries-included log suite based on [Promtail](https://grafana.com/docs/loki/latest/clients/promtail/), [Loki](https://grafana.com/docs/loki/latest/fundamentals/overview/) and [Grafana](https://grafana.com/docs/grafana/latest/explore/logs-integration/).
138134

139-
```yaml
140-
deploy_observability:
141-
- promtail
142-
- loki
143-
- grafana
144-
```
135+
We are working to continuously improving this experience, allowing Cluster API developers to use logs and improve them as part of their development process.
145136

146-
In order to test structured logging it is required to override some controller flags, and similar approach can be used
147-
for increasing log verbosity, e.g.
137+
For the best experience exploring the logs using Tilt:
138+
1. Set `--logging-format=json`.
139+
2. Set a high log verbosity, e.g. `v=5`.
140+
3. Enable promtail, loki, and grafana under deploy_observability.
148141

142+
A minimal example of a tilt-settings.yaml file that deploys a ready-to-use logging suite looks like:
149143
```yaml
144+
deploy_observability:
145+
- promtail
146+
- loki
147+
- grafana
148+
enable_providers:
149+
- docker
150+
- kubeadm-bootstrap
151+
- kubeadm-control-plane
150152
extra_args:
151153
core:
152154
- "--logging-format=json"
@@ -161,9 +163,43 @@ extra_args:
161163
- "--v=5"
162164
- "--logging-format=json"
163165
```
166+
The above options can be combined with other settings from our [Tilt](tilt.md) setup. Once Tilt is is up and running with these settings users will be able to browse logs using the Grafana Explore UI.
167+
168+
This will normally be available on `localhost:3001`. To explore logs from Loki, open the Explore interface for the DataSource 'Loki'. [This link](http://localhost:3001/explore?datasource%22:%22Loki%22) should work as a shortcut with the default Tilt settings.
164169

165-
Over time, we would like to further improve this with the help of the community; feel free to open issues or propose
166-
ideas!
170+
### Example queries
171+
172+
In the Log browser the following queries can be used to browse logs by controller, and by specific Cluster API objects. For example:
173+
```
174+
{app="capi-controller-manager"} | json
175+
```
176+
Will return logs from the `capi-controller-manager` which are parsed in json. Passing the query through the json parser allows filtering by key-value pairs that are part of nested json objects. For example `.cluster.name` becomes `cluster_name`.
177+
178+
```
179+
{app="capi-controller-manager"} | json | cluster_name="my-cluster"
180+
```
181+
Will return logs from the `capi-controller-manager` that are associated with the Cluster `my-cluster`.
182+
183+
```
184+
{app="capi-controller-manager"} | json | cluster_name="my-cluster" reconcileID="6f6ad971-bdb6-4fa3-b803-xxxxxxxxxxxx"
185+
```
186+
187+
Will return logs from the `capi-controller-manager`, associated with the Cluster `my-cluster` and the Reconcile ID `6f6ad971-bdb6-4fa3-b803-xxxxxxxxxxxx`. Each reconcile loop will have a unique Reconcile ID.
188+
189+
```
190+
{app="capi-controller-manager"} | json | cluster_name="my-cluster" reconcileID="6f6ad971-bdb6-4fa3-b803-ef81c5c8f9d0" controller="cluster" | line_format "{{ .msg }}"
191+
```
192+
Will return logs from the `capi-controller-manager`, associated with the Cluster `my-cluster` and the Reconcile ID `6f6ad971-bdb6-4fa3-b803-xxxxxxxxxxxx` it further selects only those logs which come from the Cluster controller. It will then format the logs so only the message is displayed.
193+
194+
```
195+
{app=~"capd-controller-manager|capi-kubeadm-bootstrap-controller-manager|capi-kubeadm-control-plane-controller-manager"} | json | cluster_name="my-cluster" machine_name="my-cluster-linux-worker-1" | line_format "{{.controller}} {{.msg}}"
196+
```
197+
198+
Will return the logs from four CAPI providers - the Core provider, Kubeadm Control Plane provider, Kubeadm Bootstrap provider and the Docker infrastructure provider. It filters by the cluster name and the machine name and then formats the log lines to show just the source controller and the message. This allows us to correlate logs and see actions taken by each of these four providers related to the machine `my-cluster-linux-worker-1`.
199+
200+
For more information on formatting and filtering logs using Grafana and Loki see:
201+
- [json parsing](https://grafana.com/docs/loki/latest/clients/promtail/stages/json/)
202+
- [log queries](https://grafana.com/docs/loki/latest/logql/log_queries/)
167203
168204
## What about providers
169205
Cluster API providers are developed by independent teams, and each team is free to define their own processes and
@@ -174,3 +210,4 @@ we encourage providers to adopt and contribute to the guidelines defined in this
174210
175211
It is also worth noting that the foundational elements of the approach described in this document are easy to achieve
176212
by leveraging default Kubernetes tooling for logging.
213+

0 commit comments

Comments
 (0)