Skip to content

Commit

Permalink
feat: use otelzap
Browse files Browse the repository at this point in the history
  • Loading branch information
ernado committed Jan 6, 2025
1 parent adb665d commit 2eb964a
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 45 deletions.
19 changes: 11 additions & 8 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,14 @@ func Run(f func(ctx context.Context, lg *zap.Logger, m *Metrics) error, op ...Op
// Apply options.
opts := options{
zapConfig: zap.NewProductionConfig(),
zapTee: true,
ctx: context.Background(),
resourceFn: Resource,
}
if v, err := strconv.ParseBool(os.Getenv("OTEL_ZAP_TEE")); err == nil {
// Override default.
opts.zapTee = v
}
for _, o := range op {
o.apply(&opts)
}
Expand Down Expand Up @@ -72,18 +77,16 @@ func Run(f func(ctx context.Context, lg *zap.Logger, m *Metrics) error, op ...Op
panic(fmt.Sprintf("failed to get resource: %v", err))
}

// Setup logs.
if ctx, err = autologs.Setup(ctx, res); err != nil {
panic(fmt.Sprintf("failed to setup logs: %v", err))
}
// Update root logger after autologs setup.
lg = zctx.From(ctx)

m, err := newMetrics(ctx, lg.Named("metrics"), res, opts.meterOptions, opts.tracerOptions, opts.loggerOptions)
if err != nil {
panic(err)
}

// Setup logs.
if ctx, err = autologs.Setup(ctx, m.LoggerProvider(), opts.zapTee); err != nil {
panic(fmt.Sprintf("failed to setup logs: %v", err))
}

{
// Automatically setting GOMAXPROCS.
set := true // enabled by default
Expand Down Expand Up @@ -117,7 +120,7 @@ func Run(f func(ctx context.Context, lg *zap.Logger, m *Metrics) error, op ...Op
rerr = fmt.Errorf("shutting down (panic): %v", ec)
}
}()
if err := f(ctx, lg, m); err != nil {
if err := f(ctx, zctx.From(ctx), m); err != nil {
if errors.Is(err, ctx.Err()) {
// Parent context got cancelled, error is expected.
lg.Debug("Graceful shutdown")
Expand Down
8 changes: 8 additions & 0 deletions app/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
type options struct {
zapConfig zap.Config
zapOptions []zap.Option
zapTee bool
ctx context.Context

meterOptions []autometer.Option
Expand All @@ -33,6 +34,13 @@ type Option interface {
apply(o *options)
}

// WithZapTee sets option to tee zap logs to stderr.
func WithZapTee(teeToStderr bool) Option {
return optionFunc(func(o *options) {
o.zapTee = teeToStderr
})
}

// WithZapConfig sets the default zap config for the application.
func WithZapConfig(cfg zap.Config) Option {
return optionFunc(func(o *options) {
Expand Down
56 changes: 19 additions & 37 deletions autologs/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,33 @@ package autologs

import (
"context"
"os"
"strings"

"github.com/go-faster/errors"
"go.opentelemetry.io/collector/pdata/plog/plogotlp"
"go.opentelemetry.io/otel/sdk/resource"
"go.opentelemetry.io/contrib/bridges/otelzap"
"go.opentelemetry.io/otel/log"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"

"github.com/go-faster/sdk/zapotel"
"github.com/go-faster/sdk/zctx"
)

// Setup OTLP log exporter if configured.
func Setup(ctx context.Context, res *resource.Resource) (context.Context, error) {
if os.Getenv("OTEL_LOGS_EXPORTER") != "otlp" {
return ctx, nil
}

endpoint := os.Getenv("OTEL_EXPORTER_OTLP_LOGS_ENDPOINT")
if endpoint == "" {
endpoint = os.Getenv("OTEL_EXPORTER_OTLP_ENDPOINT")
}
if endpoint == "" {
endpoint = "localhost:4317"
}

endpoint = strings.TrimPrefix(endpoint, "http://")
conn, err := grpc.NewClient(endpoint,
grpc.WithTransportCredentials(insecure.NewCredentials()),
// Setup OpenTelemetry to zap logger bridge.
func Setup(ctx context.Context, loggerProvider log.LoggerProvider, teeCore bool) (context.Context, error) {
lg := zctx.From(ctx)
otelCore := otelzap.NewCore("github.com/go-faster/sdk/app",
otelzap.WithLoggerProvider(loggerProvider),
)
if err != nil {
return ctx, errors.Wrap(err, "create grpc client")
wrapCore := func(core zapcore.Core) zapcore.Core {
return otelCore // log only to bridge
}

lg := zctx.From(ctx)
otelCore := zapotel.New(lg.Level(), res, plogotlp.NewGRPCClient(conn))
// Update logger down the stack.
lg.Info("Setting up OTLP log exporter")
lg = lg.WithOptions(
zap.WrapCore(func(core zapcore.Core) zapcore.Core {
if teeCore {
wrapCore = func(core zapcore.Core) zapcore.Core {
// Log both to bridge and original core.
return zapcore.NewTee(core, otelCore)
}),
)
return zctx.Base(ctx, lg), nil
}
}
return zctx.Base(ctx,
lg.WithOptions(
zap.WrapCore(wrapCore),
),
), nil
}
1 change: 1 addition & 0 deletions cmd/sdk-example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func main() {
return nil
},
// Configure custom zap config.
app.WithZapTee(false),
app.WithZapConfig(zap.NewDevelopmentConfig()),
app.WithZapOptions(
// Custom zap logger options.
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/prometheus/client_golang v1.20.5
github.com/stretchr/testify v1.10.0
go.opentelemetry.io/collector/pdata v1.22.0
go.opentelemetry.io/contrib/bridges/otelzap v0.8.0
go.opentelemetry.io/contrib/instrumentation/runtime v0.58.0
go.opentelemetry.io/contrib/propagators/autoprop v0.58.0
go.opentelemetry.io/otel v1.33.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJyS
go.opentelemetry.io/auto/sdk v1.1.0/go.mod h1:3wSPjt5PWp2RhlCcmmOial7AvC4DQqZb7a7wCow3W8A=
go.opentelemetry.io/collector/pdata v1.22.0 h1:3yhjL46NLdTMoP8rkkcE9B0pzjf2973crn0KKhX5UrI=
go.opentelemetry.io/collector/pdata v1.22.0/go.mod h1:nLLf6uDg8Kn5g3WNZwGyu8+kf77SwOqQvMTb5AXEbEY=
go.opentelemetry.io/contrib/bridges/otelzap v0.8.0 h1:4jqXEd0FGULFBy1bF1ledBePc0Ssu8YVddTgr8BXDTc=
go.opentelemetry.io/contrib/bridges/otelzap v0.8.0/go.mod h1:nrDogEQCtEOQ4jAiN4uHIE0BqicDF9bMyepgK1pIbP4=
go.opentelemetry.io/contrib/instrumentation/runtime v0.58.0 h1:GrcF8ABgnBHQFgp4zu5/jTSqLkoJ9uiDz2e7eKkjq+w=
go.opentelemetry.io/contrib/instrumentation/runtime v0.58.0/go.mod h1:+kxR5prZLoFAJVXJWZKWO2e4PY2dYyXIRNklBuOyzpM=
go.opentelemetry.io/contrib/propagators/autoprop v0.58.0 h1:pL1MMoBcG/ol6fVsjE1bbOO9A8GMQiN+T73hnmaXDoU=
Expand Down
2 changes: 2 additions & 0 deletions zapotel/zapotel.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// Package zapotel provides OpenTelemetry logs exporter zap core implementation.
//
// Deprecated. Use go.opentelemetry.io/contrib/bridges/otelzap.
package zapotel

import (
Expand Down

0 comments on commit 2eb964a

Please sign in to comment.