Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[otelzap] Extra fields config not used in sugared logger #135

Open
skubakdj opened this issue Jul 24, 2024 · 0 comments
Open

[otelzap] Extra fields config not used in sugared logger #135

skubakdj opened this issue Jul 24, 2024 · 0 comments

Comments

@skubakdj
Copy link

skubakdj commented Jul 24, 2024

Hey! Thank you for this awesome library!

I would like to include extra fields on my otelzap logger with every message. To do that I'm looking at the otelzap.WithExtraFields() config and also maybe using .With() in the sugared logger.

I'm finding the otelzap.WithExtraFields() config to be respected in the plain logger, but it doesn't look to be used in the sugared logger. Also, using With() in the sugared logger doesn't seem to work when chained with Ctx().

Here's some code to illustrate the above:

package main

import (
	"context"
	"log"

	"github.com/uptrace/opentelemetry-go-extra/otelzap"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
)

func main() {

	// Create new Zap logger
	var level zapcore.Level
	err := level.UnmarshalText([]byte("INFO"))
	if err != nil {
		log.Fatal(err)
	}
	config := zap.NewProductionConfig()
	config.Level = zap.NewAtomicLevelAt(level)
	zapLogger, err := config.Build(zap.AddCallerSkip(0))
	if err != nil {
		log.Fatal(err)
	}

	// Create OTEL Zap logger
	otelZapLogger := otelzap.New(
		zapLogger,
		otelzap.WithMinLevel(zapLogger.Level()),
		otelzap.WithExtraFields(zap.String("extra_key", "extra_value")),
	)

	ctx := context.Background()

	// Example scenarios
	otelZapLogger.Ctx(ctx).Info("plain + ctx, DOES have extra fields")

	otelZapLogger.Sugar().Ctx(ctx).Infof("sugared + ctx, DOES NOT have extra fields")

	otelZapLogger.Sugar().With("with_key", "with_value").Infof("sugared + with, DOES have 'with' fields, DOES NOT have extra fields")

	otelZapLogger.Sugar().With("with_key", "with_value").Ctx(ctx).Infof("sugared + with + ctx, DOES NOT have 'with' fields, DOES NOT have extra fields")

	otelZapLogger.Sugar().With("with_key", "with_value").Ctx(ctx).Desugar().Info("desugared + with + ctx, DOES NOT have 'with' fields, DOES have extra fields")
}

This produces the following output:

{"level":"info","ts":1721846112.587265,"caller":"test_logger/main.go:37","msg":"plain + ctx, DOES have extra fields","extra_key":"extra_value"}
{"level":"info","ts":1721846112.587362,"caller":"test_logger/main.go:39","msg":"sugared + ctx, DOES NOT have extra fields"}
{"level":"info","ts":1721846112.587384,"caller":"test_logger/main.go:41","msg":"sugared + with, DOES have 'with' fields, DOES NOT have extra fields","with_key":"with_value"}
{"level":"info","ts":1721846112.587411,"caller":"test_logger/main.go:43","msg":"sugared + with + ctx, DOES NOT have 'with' fields, DOES NOT have extra fields"}
{"level":"info","ts":1721846112.587422,"caller":"test_logger/main.go:45","msg":"desugared + with + ctx, DOES NOT have 'with' fields, DOES have extra fields","extra_key":"extra_value"}

Is this a bug, a limitation, or is there some other way to use the sugared logger?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant