Skip to content

Commit

Permalink
log: use slog for logging
Browse files Browse the repository at this point in the history
Use slog for logging from the stdlib to get rid of external logging
dependencies.
  • Loading branch information
travisshivers committed Nov 12, 2024
1 parent ae9733f commit 5a3138b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
5 changes: 3 additions & 2 deletions example/basic/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
"context"
"errors"
"fmt"
"log/slog"
"os"
"time"

otelb "barney.ci/go-otel"
"github.com/go-logr/logr"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
Expand All @@ -23,9 +23,10 @@ func setup(ctx context.Context) {
os.Setenv("JAEGER_ENABLED", "true")

tp, closer, err := otelb.OtelSetup(
ctx,
"barney.ci/go-otel/example/basic",
// logger first so that any errors can be reported.
otelb.WithLogger(logr.FromContextOrDiscard(ctx)),
otelb.WithLogger(slog.Default()),

otelb.WithEnvGate(),
otelb.WithShutdownTimeout(time.Minute),
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ go 1.22
toolchain go1.23.1

require (
github.com/go-logr/logr v1.4.2
github.com/go-logr/stdr v1.2.2
github.com/segmentio/kafka-go v0.4.47
go.opentelemetry.io/otel v1.32.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.32.0
Expand All @@ -18,6 +16,8 @@ require (

require (
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.23.0 // indirect
github.com/klauspost/compress v1.15.9 // indirect
Expand Down
23 changes: 10 additions & 13 deletions setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import (
"context"
"fmt"
"io"
"log"
"log/slog"
"net"
"os"
"time"

"github.com/go-logr/logr"
"github.com/go-logr/stdr"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
Expand All @@ -26,7 +24,7 @@ type setupConfig struct {
name string
envGate bool
shutdownTimeout time.Duration
logger logr.Logger
logger *slog.Logger
exporter trace.SpanExporter
sampler trace.Sampler
propagator propagation.TextMapPropagator
Expand Down Expand Up @@ -94,9 +92,8 @@ func WithGeneralPropagatorSetup() setupOptionFunc {

// WithLogger configures the given logger to be used for printing errors
// or info at runtime emitted by the tracer implementation. If unset,
// a default value of github.com/go-logr/stdr.New(log.Default()) will
// be used.
func WithLogger(logger logr.Logger) setupOptionFunc {
// a default value of slog.Default() will be used.
func WithLogger(logger *slog.Logger) setupOptionFunc {
return setupOptionFunc(func(opts *setupConfig) {
opts.logger = logger
})
Expand Down Expand Up @@ -169,7 +166,7 @@ func getIPAddress() (string, error) {
//
// It's a good idea to pass WithLogger first, so errors
// raised by subsequent options will be sent to that callback.
func OtelSetup(name string, with ...setupOptionFunc) (
func OtelSetup(ctx context.Context, name string, with ...setupOptionFunc) (
tp *trace.TracerProvider, closer closerFunc, err error,
) {
// Always return working no-ops instead of nils
Expand All @@ -187,11 +184,11 @@ func OtelSetup(name string, with ...setupOptionFunc) (
name: name,
sampler: trace.ParentBased(trace.AlwaysSample()),
exporter: nullExporter{},
logger: stdr.New(log.Default()),
logger: slog.Default(),
}
defer func() {
if r := recover(); r != nil {
opts.logger.Error(fmt.Errorf("%s", r), "panic occurred in OtelSetup")
opts.logger.ErrorContext(ctx, "panic occurred in OtelSetup", "error", r)
}
}()
for _, fn := range with {
Expand All @@ -213,13 +210,13 @@ func OtelSetup(name string, with ...setupOptionFunc) (
}

if ip, err := getIPAddress(); err != nil {
opts.logger.Error(fmt.Errorf("getIPAddress: %w", err), "failed to find host IP address")
opts.logger.ErrorContext(ctx, "failed to find host IP address", "error", err)
} else {
attrs = append(attrs, semconv.NetHostIPKey.String(ip))
}

if host, err := os.Hostname(); err != nil {
opts.logger.Error(fmt.Errorf("Hostname: %w", err), "os.Hostname() failed")
opts.logger.ErrorContext(ctx, "os.Hostname() failed", "error", err)
} else {
attrs = append(attrs, semconv.HostNameKey.String(host))
}
Expand All @@ -242,7 +239,7 @@ func OtelSetup(name string, with ...setupOptionFunc) (
}
err := tp.Shutdown(ctx)
if err != nil {
opts.logger.Error(err, "otel shutdown error")
opts.logger.ErrorContext(ctx, "otel shutdown error", "error", err)
}
return err
})
Expand Down

0 comments on commit 5a3138b

Please sign in to comment.