Skip to content

Commit

Permalink
changed level to auto package and use Level as parameter on WithLogLevel
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorhugoro1 committed Jun 17, 2024
1 parent 9f7d2a4 commit 56871d5
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 24 deletions.
10 changes: 9 additions & 1 deletion cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,15 @@ func main() {
}

if logLevel != "" {
instOptions = append(instOptions, auto.WithLogLevel(logLevel))
var level auto.Level

err := level.UnmarshalText([]byte(logLevel))
if err != nil {
logger.Error(err, "failed to parse log level")
return
}

instOptions = append(instOptions, auto.WithLogLevel(level))
}

inst, err := auto.NewInstrumentation(ctx, instOptions...)
Expand Down
18 changes: 8 additions & 10 deletions instrumentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import (
semconv "go.opentelemetry.io/otel/semconv/v1.21.0"

"go.opentelemetry.io/auto/internal/pkg/instrumentation"
internalLog "go.opentelemetry.io/auto/internal/pkg/log"
"go.opentelemetry.io/auto/internal/pkg/opentelemetry"
"go.opentelemetry.io/auto/internal/pkg/process"
)
Expand Down Expand Up @@ -75,10 +74,10 @@ 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 internalLog.Level) logr.Logger {
func newLogger(logLevel Level) logr.Logger {
level, err := zap.ParseAtomicLevel(logLevel.String())
if err != nil {
level, _ = zap.ParseAtomicLevel(internalLog.LevelInfo.String())
level, _ = zap.ParseAtomicLevel(LevelInfo.String())
}

config := zap.NewProductionConfig()
Expand Down Expand Up @@ -188,7 +187,7 @@ type instConfig struct {
additionalResAttrs []attribute.KeyValue
globalImpl bool
loadIndicator chan struct{}
logLevel internalLog.Level
logLevel Level
}

func newInstConfig(ctx context.Context, opts []InstrumentationOption) (instConfig, error) {
Expand Down Expand Up @@ -220,7 +219,7 @@ func newInstConfig(ctx context.Context, opts []InstrumentationOption) (instConfi
}

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

return c, err
Expand Down Expand Up @@ -400,7 +399,7 @@ func WithEnv() InstrumentationOption {
}
if l, ok := lookupEnv(envLogLevelKey); ok {
var e error
c.logLevel, e = internalLog.ParseLevel(l)
c.logLevel, e = ParseLevel(l)
err = errors.Join(err, e)
}
return c, err
Expand Down Expand Up @@ -513,14 +512,13 @@ 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 string) InstrumentationOption {
func WithLogLevel(level Level) InstrumentationOption {
return fnOpt(func(ctx context.Context, c instConfig) (instConfig, error) {
l, err := internalLog.ParseLevel(level)
if err != nil {
if err := level.UnmarshalText([]byte(level.String())); err != nil {
return c, err
}

c.logLevel = l
c.logLevel = level

return c, nil
})
Expand Down
12 changes: 8 additions & 4 deletions instrumentation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import (

"go.opentelemetry.io/otel/attribute"
semconv "go.opentelemetry.io/otel/semconv/v1.21.0"

"go.opentelemetry.io/auto/internal/pkg/log"
)

func TestWithServiceName(t *testing.T) {
Expand Down Expand Up @@ -91,7 +89,7 @@ func TestWithEnv(t *testing.T) {

c, err := newInstConfig(context.Background(), []InstrumentationOption{WithEnv()})
require.NoError(t, err)
assert.Equal(t, log.LevelDebug, c.logLevel)
assert.Equal(t, LevelDebug, c.logLevel)

const wrong = "invalid"

Expand Down Expand Up @@ -195,7 +193,13 @@ func TestWithLogLevel(t *testing.T) {

require.NoError(t, err)

assert.Equal(t, log.LevelError, c.logLevel)
assert.Equal(t, LevelError, c.logLevel)

c, err = newInstConfig(context.Background(), []InstrumentationOption{WithLogLevel(LevelInfo)})

require.NoError(t, err)

assert.Equal(t, LevelInfo, c.logLevel)
})

t.Run("Will Validate Input", func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/log/level.go → level.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package log
package auto

import (
"bytes"
Expand Down
14 changes: 6 additions & 8 deletions internal/pkg/log/level_test.go → level_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,38 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package log_test
package auto

import (
"testing"

"github.com/stretchr/testify/assert"

"go.opentelemetry.io/auto/internal/pkg/log"
)

func TestLevel(t *testing.T) {
testCases := []struct {
name string
level log.Level
level Level
str string
}{
{
name: "LevelDebug",
level: log.LevelDebug,
level: LevelDebug,
str: "debug",
},
{
name: "LevelInfo",
level: log.LevelInfo,
level: LevelInfo,
str: "info",
},
{
name: "LevelWarn",
level: log.LevelWarn,
level: LevelWarn,
str: "warn",
},
{
name: "LevelError",
level: log.LevelError,
level: LevelError,
str: "error",
},
}
Expand Down

0 comments on commit 56871d5

Please sign in to comment.