英文:
Kubernetes Controller Logging From a Context
问题
我正在使用Operator SDK为Kubernetes编写一个Operator,并且对日志记录有一个问题(我对Go还比较新手)。
我在控制器中设置了一个日志记录器,并将UUID作为Trace ID附加到其中:
func (r *MyReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
myLog := log.FromContext(ctx).WithValues("traceID", uuid.NewUUID())
myCtx := log.IntoContext(ctx, myLog)
然而,我有一个同时设置的库,通过结构体传递给控制器:
type MyReconciler struct {
MyBase
MyClient MyGreatClient
}
并且客户端的每个方法都将ctx作为参数。
由于我想要在ctx中使用日志记录器,因为它包含我的Trace ID,我需要在客户端的每个方法中添加这行代码吗?还是有更好的方法可以做到这一点?
func (mgc *MyGreatClient) SomethingExists(ctx context.Context, something string) bool {
myGreatClientLog := log.FromContext(ctx).WithName("MyGreatClient")
myGreatClientLog.Info("Checking Something Exists", "Something", something)
有没有更好的方法来实现我想要做的事情?似乎sigs.k8s.io/controller-runtime/pkg/log
(类型为logr.Logger
)不支持像zap那样仅记录上下文。
myappcontex.Logger(ctx).Info("did something awesome")
对于以惯用方式完成这个任务的任何帮助都将不胜感激。
英文:
I am writing an Operator for Kubernetes using the Operator SDK and have a question about logging (I am still rather new to Go).
I have setup a logger in my controller that I have attached a UUID to as a Trace ID
func (r *MyReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
myLog = log.FromContext(ctx).WithValues("traceID", uuid.NewUUID())
myCtx := log.IntoContext(ctx, myLog)
However I have a Library that is setup at the same time as the controllers and passed in via the struct.
type MyReconciler struct {
MyBase
MyClient MyGreatClient
}
And each method on the client take the ctx as a parameter.
As I want to use the logger in the ctx as it contains my trace ID do I need to add this line to every method in the client or is there a better way to do it?
func (mgc *MyGreatClient) SomethingExists(ctx context.Context, something string) bool {
myGreatClientLog = log.FromContext(ctx).WithName("MyGreatClient")
myGreatClientLog.Info("Checking Something Exists", "Something", something)
Is there a better way to achieve what I am wanting to do? It doesn't appear that "sigs.k8s.io/controller-runtime/pkg/log"
(of type logr.Logger
) supports just logging a context like zap.
myappcontex.Logger(ctx).Info("did something awesome")
Any help on doing this the idiomatic way is appreciated
答案1
得分: 2
我不确定这个答案,我也想知道为什么日志和日志接收器被构建得如此复杂(参考https://dave.cheney.net/2015/11/05/lets-talk-about-logging,我在logr https://pkg.go.dev/github.com/go-logr/logr@v0.3.0 中找到了这个链接!)。
这是我在生成的kubebuilder
操作符控制器中记录日志的方式:
log.Log.Info("Pod Image is set", "PodImageName", testOperator.Spec.PodImage)
输出结果为:
1.6611775636957748e+09 INFO Pod Image is set {"PodImageName": "alexcpn/run_server:1.2"}
还有这个:
log.FromContext(ctx).Info("Pod Image is ", "PodImageName", testOperator.Spec.PodImage)
输出结果为:
1.6611801111484244e+09 INFO Pod Image is {"controller": "testoperartor", "controllerGroup": "grpcapp.mytest.io", "controllerKind": "Testoperartor", "testoperartor": {"name":"testoperartor-sample","namespace":"default"}, "namespace": "default", "name": "testoperartor-sample", "reconcileID": "ffa3a957-c14f-4ec9-8cf9-767c38fc26ee", "PodImageName": "alexcpn/run_server:1.2"}
该控制器使用了Golang Logr。
controller-runtime
中的所有日志记录都是结构化的,使用了一个名为logr的包定义的一组接口(https://pkg.go.dev/github.com/go-logr/logr)。子包zap提供了基于Zap(go.uber.org/zap)的logr的设置帮助程序。
我可以看到它在主函数中设置了Zap日志记录:
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
英文:
Not at all sure of this answer and I too wonder why logging and logging sinks are built so complex (refer https://dave.cheney.net/2015/11/05/lets-talk-about-logging which I found reffered in logr https://pkg.go.dev/github.com/go-logr/logr@v0.3.0 !);
This is how I logged in a generated kubebuilder
operator controller
log.Log.Info("Pod Image is set", "PodImageName", testOperator.Spec.PodImage)
Ouput-
1.6611775636957748e+09 INFO Pod Image is set {"PodImageName": "alexcpn/run_server:1.2"}
and with this
log.FromContext(ctx).Info("Pod Image is ", "PodImageName", testOperator.Spec.PodImage)
Ouput is
1.6611801111484244e+09 INFO Pod Image is {"controller": "testoperartor", "controllerGroup": "grpcapp.mytest.io", "controllerKind": "Testoperartor", "testoperartor": {"name":"testoperartor-sample","namespace":"default"}, "namespace": "default", "name": "testoperartor-sample", "reconcileID": "ffa3a957-c14f-4ec9-8cf9-767c38fc26ee", "PodImageName": "alexcpn/run_server:1.2"}
The controller uses Golang Logr
All logging in controller-runtime is structured, using a set of interfaces defined by a package called logr (https://pkg.go.dev/github.com/go-logr/logr). The sub-package zap provides helpers for setting up logr backed by Zap (go.uber.org/zap)
https://pkg.go.dev/sigs.k8s.io/controller-runtime/pkg/log#DelegatingLogSink
And I can see that it sets Zap logging in main
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论