Skip to content

WIP: structured error, funcr.PseudoStruct#361

Open
pohly wants to merge 2 commits intokubernetes:mainfrom
pohly:structured-error-pseudo-struct
Open

WIP: structured error, funcr.PseudoStruct#361
pohly wants to merge 2 commits intokubernetes:mainfrom
pohly:structured-error-pseudo-struct

Conversation

@pohly
Copy link

@pohly pohly commented Dec 6, 2022

What this PR does / why we need it:

Nicer formatting of error details.

Which issue(s) this PR fixes:
Fixes #357

Special notes for your reviewer:

This is a variant of the idea with the existing funcr.PseudoType.

Release note:

MarshalLog may return funcr.PseudoType (aka funcr/types.PseudoType) and those key/value pairs then will get logged like a struct with those fields. When an error implements MarshalLog, it gets logged both as `err=<Error()>` and as `errDetails=<MarshalLog()>`.

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Dec 6, 2022
@k8s-ci-robot k8s-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Dec 6, 2022
@dashpole
Copy link

/assign @harshanarayana
/triage accepted

@k8s-ci-robot k8s-ci-robot added triage/accepted Indicates an issue or PR is ready to be actively worked on. and removed needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Dec 15, 2022
@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jan 20, 2023
@pohly pohly changed the title structured error, funcr.PseudoStruct WIP: structured error, funcr.PseudoStruct Feb 3, 2023
@k8s-ci-robot k8s-ci-robot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Feb 3, 2023
@k8s-triage-robot
Copy link

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

This bot triages 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 PR is closed

You can:

  • Mark this PR as fresh with /remove-lifecycle stale
  • Close this 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 May 4, 2023
@k8s-triage-robot
Copy link

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

This bot triages 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 PR is closed

You can:

  • Mark this PR as fresh with /remove-lifecycle rotten
  • Close this 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 Jun 3, 2023
@pohly
Copy link
Author

pohly commented Jun 3, 2023

/remove-lifecycle rotten

Waiting for slog in Go 1.21.

@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 Jun 3, 2023
@pohly
Copy link
Author

pohly commented Jan 10, 2024

We could do this now, but I'd like to hear from @tallclair first whether this feature is really needed and going to be used.

@k8s-triage-robot
Copy link

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

This bot triages 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 PR is closed

You can:

  • Mark this PR as fresh with /remove-lifecycle stale
  • Close this 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 Apr 9, 2024
@k8s-triage-robot
Copy link

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

This bot triages 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 PR is closed

You can:

  • Mark this PR as fresh with /remove-lifecycle rotten
  • Close this 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 May 9, 2024
@k8s-triage-robot
Copy link

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

This bot triages 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 PR is closed

You can:

  • Reopen this PR with /reopen
  • Mark this PR as fresh with /remove-lifecycle rotten
  • Offer to help out with Issue Triage

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

/close

@k8s-ci-robot
Copy link

@k8s-triage-robot: Closed this PR.

Details

In response to this:

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

This bot triages 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 PR is closed

You can:

  • Reopen this PR with /reopen
  • Mark this PR as fresh with /remove-lifecycle rotten
  • Offer to help out with Issue Triage

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

/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-sigs/prow repository.

This special type can be used as value in WithValues/Info/Error or as result of
MarshalLog. It gets rendered like a struct, with curly brackets around the
content.

While at it, the import aliases get updated:
- link to docs
- add PseudoStruct (new)
- add Marshaler (an omission that currently forces several Kubernetes files
  to reference logr directly)
@pohly
Copy link
Author

pohly commented Jan 8, 2026

/reopen

Still relevant for #357

@k8s-ci-robot k8s-ci-robot reopened this Jan 8, 2026
@k8s-ci-robot
Copy link

@pohly: Reopened this PR.

Details

In response to this:

/reopen

Still relevant for #357

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-sigs/prow repository.

@k8s-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: pohly

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jan 8, 2026
@pohly pohly force-pushed the structured-error-pseudo-struct branch from 2ca1701 to 57e1146 Compare January 8, 2026 13:14
@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jan 8, 2026
@pohly pohly force-pushed the structured-error-pseudo-struct branch from 57e1146 to 7df2671 Compare January 23, 2026 12:10
@pohly
Copy link
Author

pohly commented Jan 23, 2026

/remove-lifecycle rotten

Updated based on the discussion in go-logr/zapr#56.

/hold

The zapr PR should get merged first, then used in this PR for the examples, which will depend on updating test/zapr.go. Currently it documents the behavior without support in zapr for ErrorDetails.

/assign @tallclair

You asked for this in #357.

@k8s-ci-robot k8s-ci-robot added do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. and removed lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. labels Jan 23, 2026
// Other backends might not support this, so all relevant information
// should be in the error string.
type ErrorDetailer interface {
ErrorDetails() any
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could become a type alias for logr.ErrorDetailer. It probably makes sense to decide whether we want such a type there before merging this and the zapr PR.

I'll prepare a logr PR for discussion.

/cc @thockin

{"caller":"test/output.go:<LINE>","msg":"duplicates","trace":"101112131415161718191a1b1c1d1e1f","span":"0102030405060708","a":1,"v":0}
{"caller":"test/output.go:<LINE>","msg":"duplicates","trace":"101112131415161718191a1b1c1d1e1f","span":"0102030405060708","a":1,"c":3,"trace":"101112131415161718191a1b1c1d1e1f","span":"2122232425262728","d":4,"v":0}
`,
// Without support for funcr.PseudoStruct, zapr falls back to rendering a list.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With go-logr/zapr#56, zapr works as desired:

diff --git a/test/zapr.go b/test/zapr.go
index be5de7e..9b5d71c 100644
--- a/test/zapr.go
+++ b/test/zapr.go
@@ -283,36 +283,34 @@ I output.go:<LINE>] "duplicates" trace="101112131415161718191a1b1c1d1e1f" a=1 c=
 {"caller":"test/output.go:<LINE>","msg":"duplicates","trace":"101112131415161718191a1b1c1d1e1f","span":"0102030405060708","a":1,"v":0}
 {"caller":"test/output.go:<LINE>","msg":"duplicates","trace":"101112131415161718191a1b1c1d1e1f","span":"0102030405060708","a":1,"c":3,"trace":"101112131415161718191a1b1c1d1e1f","span":"2122232425262728","d":4,"v":0}
 `,
-		// Without support for funcr.PseudoStruct, zapr falls back to rendering a list.
 		`I output.go:<LINE>] "keys and values" parent={ boolsub=true intsub=1 recursive={ sub="level2" } multiLine=<
 	abc
 	def
  > }
-`: `{"caller":"test/output.go:<LINE>","msg":"keys and values","v":0,"parent":["boolsub",true,"intsub",1,"recursive",["sub","level2"],"multiLine","abc\ndef"]}
+`: `{"caller":"test/output.go:<LINE>","msg":"keys and values","v":0,"parent":{"boolsub":true,"intsub":1,"recursive":{"sub":"level2"},"multiLine":"abc\ndef"}}
 `,
 
-		// Errors are rendered without details by zapr.
-		// This is okay, they were meant to be optional.
+		// Errors are rendered with details by zapr, including support for PseudoStruct.
 		`I output.go:<LINE>] "structured error" someErr="fake error" someErrDetails={ x=1 y=<
 	multi-line
 	string
  > }
-`: `{"caller":"test/output.go:<LINE>","msg":"structured error","v":0,"someErr":"fake error"}
+`: `{"caller":"test/output.go:<LINE>","msg":"structured error","v":0,"someErr":"fake error","someErrDetails":{"x":1,"y":"multi-line\nstring"}}
 `,
 
 		`I output.go:<LINE>] "my structured error" someErr="fake error" someErrDetails={"SomeInt":1,"SomeString":"multi-line\nstring"}
-`: `{"caller":"test/output.go:<LINE>","msg":"my structured error","v":0,"someErr":"fake error"}
+`: `{"caller":"test/output.go:<LINE>","msg":"my structured error","v":0,"someErr":"fake error","someErrDetails":{"SomeInt":1,"SomeString":"multi-line\nstring"}}
 `,
 
 		`E output.go:<LINE>] "structured error" err="fake error" errDetails={ x=1 y=<
 	multi-line
 	string
  > }
-`: `{"caller":"test/output.go:<LINE>","msg":"structured error","err":"fake error"}
+`: `{"caller":"test/output.go:<LINE>","msg":"structured error","err":"fake error","errDetails":{"x":1,"y":"multi-line\nstring"}}
 `,
 
 		`E output.go:<LINE>] "my structured error" err="fake error" errDetails={"SomeInt":1,"SomeString":"multi-line\nstring"}
-`: `{"caller":"test/output.go:<LINE>","msg":"my structured error","err":"fake error"}
+`: `{"caller":"test/output.go:<LINE>","msg":"my structured error","err":"fake error","errDetails":{"SomeInt":1,"SomeString":"multi-line\nstring"}}
 `,
 	}
 }

def
> }
`,
},
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO:

  • ErrorDetails implementation which crashes (should be caught by backends).

@pohly pohly force-pushed the structured-error-pseudo-struct branch from 7df2671 to f7651cc Compare January 23, 2026 16:13
A "structured error" is an error type that implements both Error and
ErrorDetails. It gets logged like a normal error with "err=<Error()>", but then
another "errDetails=<ErrorDetails()>" gets added to log additional information
that may be stored in the error.

The result of ErrorDetails is logged like any other value. Beware that rendering
of structs, in particular multi-line strings in structs, is not very readable
because it all gets handled by json.Marshal. This also means that unexported
fields are not logged.

To get nicer output of multiple values, a PseudoStruct can be returned. Those
values then are formatted one-by-one by klog, which means that multi-line
strings are readable.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. triage/accepted Indicates an issue or PR is ready to be actively worked on.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[RFC] Structured Error type

6 participants