containers / virtcontainers

A Go package for building hardware virtualized container runtimes
Apache License 2.0
139 stars 43 forks source link

Produce enriched errors #656

Closed jodh-intel closed 6 years ago

jodh-intel commented 6 years ago

Improve the generated error objects.

jodh-intel commented 6 years ago

This is proving to be difficult: not the new API code, but how to use it.

Rationale for structured errors

The API

A slightly modified version of the simple API shown on #657...

package e

func Log(entry *logrus.Entry) error

... works fine:

Description current call new call
Handle existing error return err return e.Log(virtLog.WithError(err))
Create static error return errrors.New(...) return e.Log(virtLog.WithField("error", "..."))
Create formatted error return fmt.Errorf(...) return e.Log(virtLog.WithField("error", fmt.Sprintf(...))

That's a little verbose, but bearable (I think).

The complication

The problem is now more about where the calls to e.Log() are placed. There are hundreds of code locations that return errors, but it doesn't make sense to change all of them to use e.Log(): if we do that, we'll end up with errors containing multiple sets of fields. The code handles this fine, but we'll essentially get "too much" detail when all we actually want is the full stacktrace for the location where the error was first detected.

In summary:

The solution therefore would seem to be to only create structured errors at the lowest possible layers:

Layers

virtcontainers is structured at the file level, something like the following:

Level files
high api.go, pkg/oci.go
pod.go, container.go
hypervisor.go, agent.go, shim.go, proxy.go
medium qemu*.go, agent_*.go, shim_*.go, proxy_*.go
asset.go, bridge.go, cni.go, cnm.go, device.go, filesystem.go, hook.go, mount.go, network.go, syscall.go, utils.go
low pkg/*/*.go (excluding pkg/oci.go)

Application approaches

Although the code can be updated manually to introduce the structured error API call:

The best approach might be to find/write a tool to identify such code locations and error if there is no call to e.Log().

What might help is if we split the main package into sub-packages as suggested on #631.

See also: #667.

jodh-intel commented 6 years ago

Any thoughts on this folks?

sboeuf commented 6 years ago

@jodh-intel could you copy this issue over to github.com/kata-containers/runtime so that it can be discussed with everybody ?

jodh-intel commented 6 years ago

Done. I guess we could close this one now?

sboeuf commented 6 years ago

Yes let's close it !