-
Notifications
You must be signed in to change notification settings - Fork 40.1k
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 HandleErrorInContext and HandleCrashInContext 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. golangci-lint now warns about using the old functions as a hint. Because it contains a test that checks the output and that check no longer passed, k8s.io/apiserver/pkg/server/filters gets updated to use HandleErrorInContext.
- Loading branch information
Showing
11 changed files
with
193 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
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
71 changes: 71 additions & 0 deletions
71
staging/src/k8s.io/apimachinery/pkg/util/runtime/runtime_stack_test.go
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 ExampleHandleErrorInContext() { | ||
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. | ||
// | ||
// | ||
// | ||
// | ||
// | ||
// | ||
HandleErrorInContext(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
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
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
Oops, something went wrong.