Skip to content

Commit

Permalink
changed type to log level, add validate method and enforce a type on …
Browse files Browse the repository at this point in the history
…withLogLevel method
  • Loading branch information
vitorhugoro1 committed Jun 17, 2024
1 parent 56871d5 commit a0dc6da
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 35 deletions.
2 changes: 1 addition & 1 deletion cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func main() {
}

if logLevel != "" {
var level auto.Level
var level auto.LogLevel

err := level.UnmarshalText([]byte(logLevel))
if err != nil {
Expand Down
25 changes: 17 additions & 8 deletions instrumentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ 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(logLevel Level) logr.Logger {
level, err := zap.ParseAtomicLevel(logLevel.String())
if err != nil {
func newLogger(logLevel LogLevel) logr.Logger {
level, logErr := zap.ParseAtomicLevel(logLevel.String())
if logErr != nil {
level, _ = zap.ParseAtomicLevel(LevelInfo.String())
}

Expand All @@ -94,6 +94,10 @@ func newLogger(logLevel Level) logr.Logger {
logger = zapr.NewLogger(zapLog)
}

if logErr != nil {
logger.V(2).Info("error to parse log level, changed to LevelInfo by default")
}

return logger
}

Expand Down Expand Up @@ -187,7 +191,7 @@ type instConfig struct {
additionalResAttrs []attribute.KeyValue
globalImpl bool
loadIndicator chan struct{}
logLevel Level
logLevel LogLevel
}

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

if c.logLevel == "" {
if c.logLevel == LevelUndefined {
c.logLevel = LevelInfo
}

Expand Down Expand Up @@ -399,7 +403,12 @@ func WithEnv() InstrumentationOption {
}
if l, ok := lookupEnv(envLogLevelKey); ok {
var e error
c.logLevel, e = ParseLevel(l)
level, e := ParseLevel(l)

if err == nil {
c.logLevel = level
}

err = errors.Join(err, e)
}
return c, err
Expand Down Expand Up @@ -512,9 +521,9 @@ func WithLoadedIndicator(indicator chan struct{}) InstrumentationOption {

// WithLogLevel returns an [InstrumentationOption] that will configure
// an [Instrumentation] with the logger level visibility defined as inputed.
func WithLogLevel(level Level) InstrumentationOption {
func WithLogLevel(level LogLevel) InstrumentationOption {
return fnOpt(func(ctx context.Context, c instConfig) (instConfig, error) {
if err := level.UnmarshalText([]byte(level.String())); err != nil {
if err := level.validate(); err != nil {
return c, err
}

Expand Down
55 changes: 30 additions & 25 deletions level.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,47 +21,52 @@ import (
)

// Level defines the log level which instrumentation uses.
type Level string
type LogLevel string

const (
// Log Level [debug] Print all logs generate by instrumentation.
LevelDebug Level = "debug"
// Log Level [info].Print most of logs where these is running status of instrumentation.
LevelInfo Level = "info"
// Log Level [warn].Print errors and logs which need attention on instrumentation.
LevelWarn Level = "warn"
// Log Level [error].Print errors generated by instrumentation.
LevelError Level = "error"
// LevelUndefined is an unset log level, it should not be used.
LevelUndefined LogLevel = ""
// LevelDebug sets the logging level to log all messages.
LevelDebug LogLevel = "debug"
// LevelInfo sets the logging level to log only informational, warning, and error messages.
LevelInfo LogLevel = "info"
// LevelWarn sets the logging level to log only warning and error messages.
LevelWarn LogLevel = "warn"
// LevelError sets the logging level to log only error messages.
LevelError LogLevel = "error"
)

func (l Level) String() string {
// String returns the string encoding of the Level l.
func (l LogLevel) String() string {
switch l {
case LevelDebug:
return "debug"
case LevelInfo:
return "info"
case LevelWarn:
return "warn"
case LevelError:
return "error"
case LevelDebug, LevelInfo, LevelWarn, LevelError, LevelUndefined:
return string(l)
default:
return fmt.Sprintf("Level(%s)", string(l))
}
}

func (l *Level) UnmarshalText(text []byte) error {
func (l *LogLevel) UnmarshalText(text []byte) error {
if ok := l.unmarshalText(bytes.ToLower(text)); ok {
return nil
}

return l.validate()
}

func (l *LogLevel) validate() error {
if l == nil {
return errors.New("can't unmarshal nil values")
return errors.New("can't parse nil values")
}

if !l.unmarshalText(bytes.ToLower(text)) {
return fmt.Errorf("")
if l.unmarshalText(bytes.ToLower([]byte(l.String()))) == false {
return errors.New("log level value is not accepted")
}

return nil
}

func (l *Level) unmarshalText(text []byte) bool {
func (l *LogLevel) unmarshalText(text []byte) bool {
switch string(text) {
case "debug":
*l = LevelDebug
Expand All @@ -78,8 +83,8 @@ func (l *Level) unmarshalText(text []byte) bool {
return true
}

func ParseLevel(text string) (Level, error) {
var level Level
func ParseLevel(text string) (LogLevel, error) {
var level LogLevel

err := level.UnmarshalText([]byte(text))

Expand Down
17 changes: 16 additions & 1 deletion level_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,14 @@ import (
func TestLevel(t *testing.T) {
testCases := []struct {
name string
level Level
level LogLevel
str string
}{
{
name: "LevelUndefined",
level: LevelUndefined,
str: "",
},
{
name: "LevelDebug",
level: LevelDebug,
Expand Down Expand Up @@ -54,3 +59,13 @@ func TestLevel(t *testing.T) {
})
}
}

func TestValidate(t *testing.T) {
t.Run("can validate wrong log levels", func(t *testing.T) {
var l LogLevel

l.unmarshalText([]byte("notexist"))

assert.Equal(t, "log level value is not accepted", l.validate().Error(), "log level is not nil")
})
}

0 comments on commit a0dc6da

Please sign in to comment.