jzelinskie / cobrautil

A collection of utility functions when using Cobra.
https://github.com/jzelinskie/cobrautil
Apache License 2.0
5 stars 10 forks source link

github.com/jzelinskie/cobrautil/v2/cobraotel.New panics if BuilInfo returns nil #55

Open yoks opened 5 months ago

yoks commented 5 months ago

This part panics if BuildInfo returns nil and serviceName is provided:

func New(serviceName string, opts ...Option) *Builder {
    bi, ok := debug.ReadBuildInfo()
    if !ok && serviceName == "" {
        panic("no service name provided and failed to read from debug info")
    }

    b := &Builder{
        flagPrefix:  "otel",
        serviceName: stringz.DefaultEmpty(serviceName, bi.Main.Path),
        preRunLevel: 0,
        logger:      logr.Discard(),
    }
    for _, configure := range opts {
        configure(b)
    }
    return b
}

It skips if check and blows on eval of bi.Main.Path.

jzelinskie commented 5 months ago

What are you doing to reproduce debug.ReadBuildInfo() failing? This panic is because parsing the Go module info really shouldn't fail and it's being used as the fallback for when you don't provide a service name.

yoks commented 5 months ago

Building it with Bazel. It does not inject BuildInfo as it breaks hermeticity of the builds (i will have diffrerent results on different machines), you can reproduce some of it using stamping. But it requires additonal configs, or patches (is what we doing).

I propose as you already can insert servicename, why not ignore BuildInfo in this case?