Skip to content

Commit fc771f4

Browse files
authored
Merge pull request #32452 from pohly/contextual-logging
features: add ContextualLogging
2 parents ec69a0b + 4a57c11 commit fc771f4

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

content/en/docs/concepts/cluster-administration/system-logs.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,55 @@ I1025 00:15:15.525108 1 example.go:116] "Example" data="This is text with
110110
second line.}
111111
```
112112

113+
### Contextual Logging
114+
115+
{{< feature-state for_k8s_version="v1.24" state="alpha" >}}
116+
117+
Contextual logging builds on top of structured logging. It is primarily about
118+
how developers use logging calls: code based on that concept is more flexible
119+
and supports additional use cases as described in the [Contextual Logging
120+
KEP](https://github.com/kubernetes/enhancements/tree/master/keps/sig-instrumentation/3077-contextual-logging).
121+
122+
If developers use additional functions like `WithValues` or `WithName` in
123+
their components, then log entries contain additional information that gets
124+
passed into functions by their caller.
125+
126+
Currently this is gated behind the `StructuredLogging` feature gate and
127+
disabled by default. The infrastructure for this was added in 1.24 without
128+
modifying components. The
129+
[`component-base/logs/example`](https://github.com/kubernetes/kubernetes/blob/v1.24.0-beta.0/staging/src/k8s.io/component-base/logs/example/cmd/logger.go)
130+
command demonstrates how to use the new logging calls and how a component
131+
behaves that supports contextual logging.
132+
133+
```console
134+
$ cd $GOPATH/src/k8s.io/kubernetes/staging/src/k8s.io/component-base/logs/example/cmd/
135+
$ go run . --help
136+
...
137+
--feature-gates mapStringBool A set of key=value pairs that describe feature gates for alpha/experimental features. Options are:
138+
AllAlpha=true|false (ALPHA - default=false)
139+
AllBeta=true|false (BETA - default=false)
140+
ContextualLogging=true|false (ALPHA - default=false)
141+
$ go run . --feature-gates ContextualLogging=true
142+
...
143+
I0404 18:00:02.916429 451895 logger.go:94] "example/myname: runtime" foo="bar" duration="1m0s"
144+
I0404 18:00:02.916447 451895 logger.go:95] "example: another runtime" foo="bar" duration="1m0s"
145+
```
146+
147+
The `example` prefix and `foo="bar"` were added by the caller of the function
148+
which logs the `runtime` message and `duration="1m0s"` value, without having to
149+
modify that function.
150+
151+
With contextual logging disable, `WithValues` and `WithName` do nothing and log
152+
calls go through the global klog logger. Therefore this additional information
153+
is not in the log output anymore:
154+
155+
```console
156+
$ go run . --feature-gates ContextualLogging=false
157+
...
158+
I0404 18:03:31.171945 452150 logger.go:94] "runtime" duration="1m0s"
159+
I0404 18:03:31.171962 452150 logger.go:95] "another runtime" duration="1m0s"
160+
```
161+
113162
### JSON log format
114163

115164
{{< feature-state for_k8s_version="v1.19" state="alpha" >}}
@@ -197,5 +246,6 @@ The `logrotate` tool rotates logs daily, or once the log size is greater than 10
197246

198247
* Read about the [Kubernetes Logging Architecture](/docs/concepts/cluster-administration/logging/)
199248
* Read about [Structured Logging](https://github.com/kubernetes/enhancements/tree/master/keps/sig-instrumentation/1602-structured-logging)
249+
* Read about [Contextual Logging](https://github.com/kubernetes/enhancements/tree/master/keps/sig-instrumentation/3077-contextual-logging)
200250
* Read about [deprecation of klog flags](https://github.com/kubernetes/enhancements/tree/master/keps/sig-instrumentation/2845-deprecate-klog-specific-flags-in-k8s-components)
201251
* Read about the [Conventions for logging severity](https://github.com/kubernetes/community/blob/master/contributors/devel/sig-instrumentation/logging.md)

content/en/docs/reference/command-line-tools-reference/feature-gates.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ different Kubernetes components.
8787
| `CSIMigrationPortworx` | `false` | Alpha | 1.23 | |
8888
| `csiMigrationRBD` | `false` | Alpha | 1.23 | |
8989
| `CSIVolumeHealth` | `false` | Alpha | 1.21 | |
90+
| `ContextualLogging` | `false` | Alpha | 1.24 | |
9091
| `ControllerManagerLeaderMigration` | `false` | Alpha | 1.21 | 1.21 |
9192
| `ControllerManagerLeaderMigration` | `true` | Beta | 1.22 | |
9293
| `CustomCPUCFSQuotaPeriod` | `false` | Alpha | 1.12 | |
@@ -772,6 +773,8 @@ Each feature gate is designed for enabling/disabling a specific feature:
772773
for fsGroups when mounting a volume in a Pod. See
773774
[Configure volume permission and ownership change policy for Pods](/docs/tasks/configure-pod-container/security-context/#configure-volume-permission-and-ownership-change-policy-for-pods)
774775
for more details.
776+
- `ContextualLogging`: When you enable this feature gate, Kubernetes components that support
777+
contextual logging add extra detail to log output.
775778
- `ControllerManagerLeaderMigration`: Enables leader migration for
776779
`kube-controller-manager` and `cloud-controller-manager`.
777780
- `CronJobControllerV2`: Use an alternative implementation of the

0 commit comments

Comments
 (0)