Skip to content

Commit

Permalink
Use slog instead of logr
Browse files Browse the repository at this point in the history
  • Loading branch information
MrAlias committed Sep 11, 2024
1 parent dbd8e85 commit 74f17c6
Show file tree
Hide file tree
Showing 37 changed files with 210 additions and 732 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,32 @@ OpenTelemetry Go Automatic Instrumentation adheres to [Semantic Versioning](http
- Add gRPC status code attribute for client spans (`rpc.grpc.status_code`). ([#1044](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/1044))
- Support `google.golang.org/grpc` `1.68.0-dev`. ([#1044](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/1044))
- Support `go.opentelemetry.io/otel@v1.30.0`. ([#1044](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/1044))
- The `WithLogger` `InstrumentationOption` is added as a replacement for `WithLogLevel`.
An `slog.Logger` can now be configured by the user any way they want and then passed to the `Instrumentation` for its logging with this option.
By default, if this is not provided, the default `slog` `Logger` will be used. ([#1080](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/1080))

### Changed

- The `WithSampler` option function now accepts the new `Sampler` interface instead of `trace.Sampler`. ([#982](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/982))
- The `WithEnv` `InstrumentationOption` no longer supports the `OTEL_LOG_LEVEL` environment variable.
This is now configured by the user via the logger they pass using the added `WithLogger` option. ([#1080](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/1080))

### Fixed

- Fix dirty shutdown caused by panic. ([#980](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/980))
- Flush pending span exports on shutdown. ([#1028](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/1028))

### Removed

- `WithLogLevel` is removed.
Use `WithLogger` instead. ([#1080](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/1080))
- The unused `LogLevelDebug` constant is removed. ([#1080](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/1080))
- The unused `LogLevelInfo` constant is removed. ([#1080](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/1080))
- The unused `LogLevelWarn` constant is removed. ([#1080](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/1080))
- The unused `LogLevelError` constant is removed. ([#1080](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/1080))
- The unused `LogLevel` type is removed. ([#1080](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/1080))
- The unused `ParseLogLevel` function is removed. ([#1080](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/1080))

## [v0.14.0-alpha] - 2024-07-15

### Added
Expand Down
201 changes: 0 additions & 201 deletions LICENSES/github.com/go-logr/zapr/LICENSE

This file was deleted.

19 changes: 0 additions & 19 deletions LICENSES/go.uber.org/multierr/LICENSE.txt

This file was deleted.

19 changes: 0 additions & 19 deletions LICENSES/go.uber.org/zap/LICENSE

This file was deleted.

67 changes: 37 additions & 30 deletions cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,28 @@ import (
"errors"
"flag"
"fmt"
"log"
"log/slog"
"os"
"os/signal"
"path/filepath"
"syscall"

"github.com/go-logr/logr"
"github.com/go-logr/stdr"
"github.com/go-logr/zapr"
"go.uber.org/zap"

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

const help = `
OpenTelemetry auto-instrumentation for Go applications using eBPF
const help = `Usage of %s:
-global-impl
Record telemetry from the OpenTelemetry default global implementation
-log-level string
logging level ("debug", "info", "warn", "error")
Runs the OpenTelemetry auto-instrumentation for Go applications using eBPF.
Environment variable configuration:
- OTEL_GO_AUTO_TARGET_EXE: sets the target binary
- OTEL_LOG_LEVEL: sets the log level (flag takes precedence)
- OTEL_SERVICE_NAME (or OTEL_RESOURCE_ATTRIBUTES): sets the service name
- OTEL_TRACES_EXPORTER: sets the trace exporter
Expand All @@ -37,19 +39,34 @@ package's documentation for information on supported values and registration of
custom exporters.
`

// envLogLevelKey is the key for the environment variable value containing the
// log level.
const envLogLevelKey = "OTEL_LOG_LEVEL"

func usage() {
fmt.Fprintf(os.Stderr, "%s", help)
program := filepath.Base(os.Args[0])
fmt.Fprintf(os.Stderr, help, program)
}

func newLogger() logr.Logger {
zapLog, err := zap.NewProduction()
func newLogger(lvlStr string) *slog.Logger {
levelVar := new(slog.LevelVar) // Default value of info.
opts := &slog.HandlerOptions{AddSource: true, Level: levelVar}
h := slog.NewJSONHandler(os.Stderr, opts)
logger := slog.New(h)

var logger logr.Logger
if err != nil {
// Fallback to stdr logger.
logger = stdr.New(log.New(os.Stderr, "", log.LstdFlags))
if lvlStr == "" {
lvlStr = os.Getenv(envLogLevelKey)
}

if lvlStr == "" {
return logger
}

var level slog.Level
if err := level.UnmarshalText([]byte(lvlStr)); err != nil {
logger.Error("failed to parse log level", "error", err, "log-level", lvlStr)
} else {
logger = zapr.NewLogger(zapLog)
levelVar.Set(level)
}

return logger
Expand All @@ -60,12 +77,12 @@ func main() {
var logLevel string

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

flag.Usage = usage
flag.Parse()

logger := newLogger().WithName("go.opentelemetry.io/auto")
logger := newLogger(logLevel)

// Trap Ctrl+C and SIGTERM and call cancel on the context.
ctx, cancel := context.WithCancel(context.Background())
Expand Down Expand Up @@ -95,19 +112,9 @@ func main() {
instOptions = append(instOptions, auto.WithGlobal())
}

if logLevel != "" {
level, err := auto.ParseLogLevel(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...)
if err != nil {
logger.Error(err, "failed to create instrumentation")
logger.Error("failed to create instrumentation", "error", err)
return
}

Expand All @@ -122,6 +129,6 @@ func main() {

logger.Info("starting instrumentation...")
if err = inst.Run(ctx); err != nil && !errors.Is(err, process.ErrInterrupted) {
logger.Error(err, "instrumentation crashed")
logger.Error("instrumentation crashed", "error", err)
}
}
Loading

0 comments on commit 74f17c6

Please sign in to comment.