Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contextual structured logging #239

Closed
paullaffitte opened this issue May 20, 2021 · 12 comments
Closed

Contextual structured logging #239

paullaffitte opened this issue May 20, 2021 · 12 comments
Assignees
Labels
kind/feature Categorizes issue or PR as related to a new feature. wg/structured-logging Categorizes an issue or PR as relevant to WG Structured Logging.

Comments

@paullaffitte
Copy link

paullaffitte commented May 20, 2021

/kind feature

Describe the solution you'd like
It would be nice to be able to use a context for structured logging. Something like described in this article. Here is a short example from the article:

ctx := context.Background()
log.Infof(ctx, "test")    // Message: “test”
ctx = log.WithLogTag(ctx, "n", 1)
log.Infof(ctx, "test")    // Message: “[n1] test”
ctx = log.WithLogTag(ctx, "s", 2)
log.Infof(ctx, "test")    // Message: “[n1,s2] test”
ctx = log.WithLogTag(ctx, "bootstrap", nil)
log.Infof(ctx, "test")    // Message: “[n1,s2,bootstrap] test”

Something similar could be implemented in order to populate structured logs, example inspired from kubenetes introduction to structured logs:

ctx := context.Background()
ctx = klog.WithLogTag(ctx, "pod", klog.KObj(pod))
ctx = klog.WithLogTag(ctx, "status", status)
klog.WithContext(ctx).InfoS("Pod status updated")

Output:

I1025 00:15:15.525108       1 controller_utils.go:116] "Pod status updated" pod="kube-system/kubedns" status="ready"

Anything else you would like to add:

The form klog.WithContext(ctx).InfoS("") would help to avoid breaking changes over klog.InfoS(ctx, ""). Moreover, introducing new functions like klog.ContextualInfoS risks to bloat the api. These are the reasons which make me think that introducing a single new function klog.WithContext would be rather a good solution.

This article provides some good reasons to use context in logging.

@k8s-ci-robot k8s-ci-robot added the kind/feature Categorizes issue or PR as related to a new feature. label May 20, 2021
@serathius
Copy link

/wg structured-logging

@k8s-ci-robot k8s-ci-robot added the wg/structured-logging Categorizes an issue or PR as relevant to WG Structured Logging. label May 26, 2021
@serathius
Copy link

serathius commented May 26, 2021

Thanks for creating issue, contextual logging is a great idea!
It should be something in scope of newly created Structured Logging Working Group.

I have added it to WG Roadmap
/assign

As for api design, I think wait for Kubernetes to migrate to https://github.com/go-logr/logr as it already supports contextual logging. Example:

func external() {
  ctx := logr.NewContext(context.Background(), baseLogger)
  internal(ctx)
}

func internal(ctx context.Context) {
  lg := logr.FromContext(ctx).WithValues("pod", klog.KObj(pod))
  lg.InfoS("Pod status updated")
}

@yuzhiquan
Copy link
Member

yuzhiquan commented Jul 5, 2021

/cc
And i meet this context logging need in my current work, interesting feature.

@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough contributors to adequately respond to all issues and PRs.

This bot triages issues and PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue or PR as fresh with /remove-lifecycle stale
  • Mark this issue or PR as rotten with /lifecycle rotten
  • Close this issue or PR with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Oct 20, 2021
@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages issues and PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue or PR as fresh with /remove-lifecycle rotten
  • Close this issue or PR with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle rotten

@k8s-ci-robot k8s-ci-robot added lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. and removed lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. labels Nov 19, 2021
@serathius
Copy link

/remove-lifecycle rotten
/cc @pohly

@k8s-ci-robot k8s-ci-robot removed the lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. label Nov 22, 2021
@pohly
Copy link

pohly commented Nov 22, 2021

I'm working on a KEP for this. The logr API already supports this, we "just" need to figure out how to use that in Kubernetes.

/assign

@pohly
Copy link

pohly commented Dec 17, 2021

Here's the KEP: kubernetes/enhancements#3078

@pohly
Copy link

pohly commented Feb 4, 2022

/assign

The KEP was approved. I'll work on this.

@pohly
Copy link

pohly commented Feb 4, 2022

It's worth pointing out that the solution will not be exactly as in the description. Have a look at the KEP for the proposed API.

In contrast to what is currently described in the KEP, we'll probably just add FromContext to klog itself and not introduce a separate klogr.

@pohly
Copy link

pohly commented Apr 8, 2022

Support for contextual logging as I described it in the KEP (logger is part of the context, not "logger uses context") has been implemented in klog and Kubernetes 1.24. I consider that the solution for this issue.

/close

@k8s-ci-robot
Copy link

@pohly: Closing this issue.

In response to this:

Support for contextual logging as I described it in the KEP (logger is part of the context, not "logger uses context") has been implemented in klog and Kubernetes 1.24. I consider that the solution for this issue.

/close

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

WG Structured Logging - Enhancement work automation moved this from Inbox to Done Apr 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/feature Categorizes issue or PR as related to a new feature. wg/structured-logging Categorizes an issue or PR as relevant to WG Structured Logging.
Development

No branches or pull requests

6 participants