Skip to content

Commit

Permalink
working: traefik v2.10.1 + zap v1.24.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Van Bouwel committed May 31, 2024
1 parent 197bf72 commit e10578a
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ Just a demo to illustrate issues experienced with zap when using inside a traffi
## Repro

1. Start whoami container which serves as backend service `podman run --name whoami -d --rm -p 8080:8888 -p 8081:80 traefik/whoami`
2. Start traefik container with plugin: `podman run -it --rm -v $PWD/traefik.yaml:/etc/traefik.yaml -v $PWD/src:/plugins-local/src --network container:whoami docker.io/traefik:v2.10.1 --configfile=/etc/traefik.yaml`
2. Start traefik container with plugin: `podman run -it --rm -v $PWD/traefik.yaml:/etc/traefik.yaml -v $PWD/src:/plugins-local/src --network container:whoami docker.io/traefik:v2.10.1 --configfile=/etc/traefik.yaml`
3. In another terminal window `curl http://localhost:8080/demo`
6 changes: 6 additions & 0 deletions src/yaegi_zap_issue_demo/go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
module github.com/pvbouwel/yaegi_zap_issue_demo

go 1.22.2

require (
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.10.0 // indirect
go.uber.org/zap v1.24.0 // indirect
)
15 changes: 15 additions & 0 deletions src/yaegi_zap_issue_demo/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60=
go.uber.org/zap v1.24.0/go.mod h1:2kMP+WWQ8aoFoedH3T2sq6iJ2yDWpHbP0f6MQbS9Gkg=
go.uber.org/zap v1.25.0 h1:4Hvk6GtkucQ790dqmj7l1eEnRdKm3k3ZUrUMS2d5+5c=
go.uber.org/zap v1.25.0/go.mod h1:JIAUzQIH94IC4fOJQm7gMmBJP5k7wQfdcnYdPoEXJYk=
45 changes: 41 additions & 4 deletions src/yaegi_zap_issue_demo/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ package yaegi_zap_issue_demo
import (
"context"
"net/http"
"time"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

type Config struct {
Expand All @@ -14,17 +18,50 @@ func CreateConfig() *Config {
}

type issuePlugin struct {
name string
next http.Handler
name string
next http.Handler
logger *zap.Logger
}

func (p *issuePlugin) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
p.logger.Info("test")
p.next.ServeHTTP(rw, req)
}

func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
logger, _ := NewLogger("INFO")
return &issuePlugin{
name: name,
next: next,
name: name,
next: next,
logger: logger,
}, nil
}

func NewLogger(logLevel string) (*zap.Logger, error) {
config := zap.Config{
Encoding: "json",
OutputPaths: []string{"stdout"},
ErrorOutputPaths: []string{"stdout"},
EncoderConfig: zapcore.EncoderConfig{
TimeKey: "time",
EncodeTime: func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.UTC().Format("2006-01-02T15:04:05Z"))
},
LevelKey: "level",
NameKey: "logger",
MessageKey: "message",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
},
}

var level zapcore.Level
err := level.UnmarshalText([]byte(logLevel))
if err != nil {
panic(err)
}
config.Level = zap.NewAtomicLevelAt(level)
return config.Build()
}

0 comments on commit e10578a

Please sign in to comment.