-
Notifications
You must be signed in to change notification settings - Fork 545
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
apimachinery runtime: support contextual logging
In contrast to the original HandleError and HandleCrash, the new HandleErrorWithContext and HandleCrashWithContext functions properly do contextual logging, so if a problem occurs while e.g. dealing with a certain request and WithValues was used for that request, then the error log entry will also contain information about it. The output changes from unstructured to structured, which might be a breaking change for users who grep for panics. Care was taken to format panics as similar as possible to the original output. For errors, a message string gets added. There was none before, which made it impossible to find all error output coming from HandleError. Keeping HandleError and HandleCrash around without deprecating while changing the signature of callbacks is a compromise between not breaking existing code and not adding too many special cases that need to be supported. There is some code which uses PanicHandlers or ErrorHandlers, but less than code that uses the Handle* calls. In Kubernetes, we want to replace the calls. logcheck warns about them in code which is supposed to be contextual. The steps towards that are: - add TODO remarks as reminder (this commit) - locally remove " TODO(pohly): " to enable the check with `//logcheck:context`, merge fixes for linter warnings - once there are none, remove the TODO to enable the check permanently Kubernetes-commit: 5a130d2b71e5d70cfff15087f4d521c6b68fb01e
- Loading branch information
1 parent
7778646
commit 126f5ce
Showing
4 changed files
with
190 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
Copyright 2014 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package runtime | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"errors" | ||
"flag" | ||
"fmt" | ||
"regexp" | ||
|
||
"k8s.io/klog/v2" | ||
) | ||
|
||
//nolint:logcheck // Several functions are normally not okay in a package. | ||
func ExampleHandleErrorWithContext() { | ||
state := klog.CaptureState() | ||
defer state.Restore() | ||
var fs flag.FlagSet | ||
klog.InitFlags(&fs) | ||
for flag, value := range map[string]string{ | ||
"one_output": "true", | ||
"logtostderr": "false", | ||
} { | ||
if err := fs.Set(flag, value); err != nil { | ||
fmt.Printf("Unexpected error configuring klog: %v", err) | ||
return | ||
} | ||
} | ||
var buffer bytes.Buffer | ||
klog.SetOutput(&buffer) | ||
|
||
logger := klog.Background() | ||
logger = klog.LoggerWithValues(logger, "request", 42) | ||
ctx := klog.NewContext(context.Background(), logger) | ||
|
||
// The line number of the next call must be at line 60. Here are some | ||
// blank lines that can be removed to keep the line unchanged. | ||
// | ||
// | ||
// | ||
// | ||
// | ||
// | ||
HandleErrorWithContext(ctx, errors.New("fake error"), "test") | ||
|
||
klog.Flush() | ||
// Strip varying header. Code location should be constant and something | ||
// that needs to be tested. | ||
output := buffer.String() | ||
output = regexp.MustCompile(`^.* ([^[:space:]]*.go:[[:digit:]]*)\] `).ReplaceAllString(output, `xxx $1] `) | ||
fmt.Print(output) | ||
|
||
// Output: | ||
// xxx runtime_stack_test.go:60] "test" err="fake error" logger="UnhandledError" request=42 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters