mirror of
https://github.com/optim-enterprises-bv/kubernetes.git
synced 2025-11-26 19:35:10 +00:00
The benchmark depends on k8s.io/api (for v1.Container). Such a dependency is not desirable for k8s.io/component-base/logs, even if it's just for testing. The solution is to create a separate directory where such a dependency isn't a problem. The alternative, a separate package with its own go.mod file under k8s.io/component-base/logs wouldd have been more complicated to maintain (yet another go.mod file and different whitelisted dependencies).
80 lines
1.8 KiB
Go
80 lines
1.8 KiB
Go
/*
|
|
Copyright 2021 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 benchmark
|
|
|
|
import (
|
|
"flag"
|
|
"io"
|
|
|
|
"github.com/go-logr/logr"
|
|
"go.uber.org/zap/zapcore"
|
|
|
|
logsjson "k8s.io/component-base/logs/json"
|
|
"k8s.io/klog/v2"
|
|
)
|
|
|
|
func init() {
|
|
// Cause all klog output to be discarded with minimal overhead.
|
|
// We don't include time stamps and caller information.
|
|
klog.InitFlags(nil)
|
|
flag.Set("alsologtostderr", "false")
|
|
flag.Set("logtostderr", "false")
|
|
flag.Set("skip_headers", "true")
|
|
flag.Set("one_output", "true")
|
|
flag.Set("stderrthreshold", "FATAL")
|
|
klog.SetOutput(&output)
|
|
}
|
|
|
|
type bytesWritten int64
|
|
|
|
func (b *bytesWritten) Write(data []byte) (int, error) {
|
|
l := len(data)
|
|
*b += bytesWritten(l)
|
|
return l, nil
|
|
}
|
|
|
|
func (b *bytesWritten) Sync() error {
|
|
return nil
|
|
}
|
|
|
|
var output bytesWritten
|
|
var jsonLogger = newJSONLogger(&output)
|
|
|
|
func newJSONLogger(out io.Writer) logr.Logger {
|
|
encoderConfig := &zapcore.EncoderConfig{
|
|
MessageKey: "msg",
|
|
}
|
|
logger, _ := logsjson.NewJSONLogger(zapcore.AddSync(out), nil, encoderConfig)
|
|
return logger
|
|
}
|
|
|
|
func printf(item logMessage) {
|
|
if item.isError {
|
|
klog.Errorf("%s: %v %s", item.msg, item.err, item.kvs)
|
|
} else {
|
|
klog.Infof("%s: %v", item.msg, item.kvs)
|
|
}
|
|
}
|
|
|
|
func prints(item logMessage) {
|
|
if item.isError {
|
|
klog.ErrorS(item.err, item.msg, item.kvs...)
|
|
} else {
|
|
klog.InfoS(item.msg, item.kvs...)
|
|
}
|
|
}
|