Skip to content

Commit

Permalink
added log level through commnd line flag
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorhugoro1 committed Jun 17, 2024
1 parent 0a1ea0e commit df532a5
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 10 deletions.
5 changes: 5 additions & 0 deletions cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,11 @@ func newLogger() logr.Logger {

func main() {
var globalImpl bool
var logLevel string

flag.BoolVar(&globalImpl, "global-impl", false, "Record telemetry from the OpenTelemetry default global implementation")
flag.StringVar(&logLevel, "log-level", "info", "Define log visibility level")

flag.Usage = usage
flag.Parse()

Expand Down Expand Up @@ -99,6 +102,8 @@ func main() {
instOptions = append(instOptions, auto.WithGlobal())
}

instOptions = append(instOptions, auto.WithLogLevel(logLevel))

inst, err := auto.NewInstrumentation(ctx, instOptions...)
if err != nil {
logger.Error(err, "failed to create instrumentation")
Expand Down
40 changes: 30 additions & 10 deletions instrumentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,17 @@ type Instrumentation struct {
// binary or pid.
var errUndefinedTarget = fmt.Errorf("undefined target Go binary, consider setting the %s environment variable pointing to the target binary to instrument", envTargetExeKey)

func newLogger() logr.Logger {
zapLog, err := zap.NewProduction()
func newLogger(logLevel string) logr.Logger {
level, err := zap.ParseAtomicLevel(logLevel)
if err != nil {
level, _ = zap.ParseAtomicLevel(zap.InfoLevel.String())
}

config := zap.NewProductionConfig()

config.Level.SetLevel(level.Level())

zapLog, err := config.Build()

var logger logr.Logger
if err != nil {
Expand All @@ -92,14 +101,6 @@ func newLogger() logr.Logger {
// If conflicting or duplicate options are provided, the last one will have
// precedence and be used.
func NewInstrumentation(ctx context.Context, opts ...InstrumentationOption) (*Instrumentation, error) {
// TODO: pass this in as an option.
//
// We likely want to use slog instead of logr in the longterm. Wait until
// that package has enough Go version support and then switch to that so we
// can expose it in an option.
logger := newLogger()
logger = logger.WithName("Instrumentation")

c, err := newInstConfig(ctx, opts)
if err != nil {
return nil, err
Expand All @@ -108,6 +109,11 @@ func NewInstrumentation(ctx context.Context, opts ...InstrumentationOption) (*In
return nil, err
}

// We likely want to use slog instead of logr in the longterm. Wait until
// that package has enough Go version support
logger := newLogger(c.logLevel)
logger = logger.WithName("Instrumentation")

pa := process.NewAnalyzer(logger)
pid, err := pa.DiscoverProcessID(ctx, &c.target)
if err != nil {
Expand Down Expand Up @@ -179,6 +185,7 @@ type instConfig struct {
additionalResAttrs []attribute.KeyValue
globalImpl bool
loadIndicator chan struct{}
logLevel string
}

func newInstConfig(ctx context.Context, opts []InstrumentationOption) (instConfig, error) {
Expand Down Expand Up @@ -209,6 +216,10 @@ func newInstConfig(ctx context.Context, opts []InstrumentationOption) (instConfi
c.sampler = trace.AlwaysSample()
}

if c.logLevel == "" {
c.logLevel = "info"
}

return c, err
}

Expand Down Expand Up @@ -490,3 +501,12 @@ func WithLoadedIndicator(indicator chan struct{}) InstrumentationOption {
return c, nil
})
}

// WithLogLevel returns an [InstrumentationOption] that will configure
// an [Instrumentation] with the logger level visibility defined as inputed.
func WithLogLevel(level string) InstrumentationOption {
return fnOpt(func(ctx context.Context, c instConfig) (instConfig, error) {
c.logLevel = level
return c, nil
})
}
8 changes: 8 additions & 0 deletions instrumentation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,14 @@ func TestWithResourceAttributes(t *testing.T) {
})
}

func TestWithLogLevel(t *testing.T) {
c, err := newInstConfig(context.Background(), []InstrumentationOption{WithLogLevel("error")})

require.NoError(t, err)

assert.Equal(t, "error", c.logLevel)
}

func mockEnv(t *testing.T, env map[string]string) {
orig := lookupEnv
t.Cleanup(func() { lookupEnv = orig })
Expand Down

0 comments on commit df532a5

Please sign in to comment.