1
0
Fork 0

New logger for the Traefik logs

This commit is contained in:
Ludovic Fernandez 2022-11-21 18:36:05 +01:00 committed by GitHub
parent 27c02b5a56
commit 56f7515ecd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
297 changed files with 2337 additions and 1934 deletions

View file

@ -10,9 +10,10 @@ import (
"strconv"
"strings"
"github.com/rs/zerolog/log"
"github.com/traefik/traefik/v2/pkg/config/dynamic"
"github.com/traefik/traefik/v2/pkg/config/label"
"github.com/traefik/traefik/v2/pkg/log"
"github.com/traefik/traefik/v2/pkg/logs"
"github.com/traefik/traefik/v2/pkg/provider"
"github.com/traefik/traefik/v2/pkg/provider/constraints"
)
@ -22,18 +23,18 @@ func (p *Provider) buildConfig(ctx context.Context, items []item) *dynamic.Confi
for _, i := range items {
svcName := provider.Normalize(i.Node + "-" + i.Name + "-" + i.ID)
ctxSvc := log.With(ctx, log.Str(log.ServiceName, svcName))
logger := log.Ctx(ctx).With().Str(logs.ServiceName, svcName).Logger()
ctxSvc := logger.WithContext(ctx)
if !p.keepItem(ctxSvc, i) {
continue
}
logger := log.FromContext(ctx)
labels := tagsToLabels(i.Tags, p.Prefix)
config, err := label.DecodeConfiguration(labels)
if err != nil {
logger.Errorf("Failed to decode configuration: %v", err)
logger.Error().Err(err).Msg("Failed to decode configuration")
continue
}
@ -42,7 +43,7 @@ func (p *Provider) buildConfig(ctx context.Context, items []item) *dynamic.Confi
if len(config.TCP.Routers) > 0 || len(config.TCP.Services) > 0 {
tcpOrUDP = true
if err := p.buildTCPConfig(i, config.TCP); err != nil {
logger.Errorf("Failed to build TCP service configuration: %v", err)
logger.Error().Err(err).Msg("Failed to build TCP service configuration")
continue
}
provider.BuildTCPRouterConfiguration(ctxSvc, config.TCP)
@ -51,7 +52,7 @@ func (p *Provider) buildConfig(ctx context.Context, items []item) *dynamic.Confi
if len(config.UDP.Routers) > 0 || len(config.UDP.Services) > 0 {
tcpOrUDP = true
if err := p.buildUDPConfig(i, config.UDP); err != nil {
logger.Errorf("Failed to build UDP service configuration: %v", err)
logger.Error().Err(err).Msg("Failed to build UDP service configuration")
continue
}
provider.BuildUDPRouterConfiguration(ctxSvc, config.UDP)
@ -67,7 +68,7 @@ func (p *Provider) buildConfig(ctx context.Context, items []item) *dynamic.Confi
// configure http service
if err := p.buildServiceConfig(i, config.HTTP); err != nil {
logger.Errorf("Failed to build HTTP service configuration: %v", err)
logger.Error().Err(err).Msg("Failed to build HTTP service configuration")
continue
}
@ -148,20 +149,20 @@ func (p *Provider) buildServiceConfig(i item, configuration *dynamic.HTTPConfigu
// TODO: check whether it is mandatory to filter again.
func (p *Provider) keepItem(ctx context.Context, i item) bool {
logger := log.FromContext(ctx)
logger := log.Ctx(ctx)
if !i.ExtraConf.Enable {
logger.Debug("Filtering disabled item")
logger.Debug().Msg("Filtering disabled item")
return false
}
matches, err := constraints.MatchTags(i.Tags, p.Constraints)
if err != nil {
logger.Errorf("Error matching constraint expressions: %v", err)
logger.Error().Err(err).Msg("Error matching constraint expressions")
return false
}
if !matches {
logger.Debugf("Filtering out item due to constraints: %q", p.Constraints)
logger.Debug().Msgf("Filtering out item due to constraints: %q", p.Constraints)
return false
}

View file

@ -10,10 +10,11 @@ import (
"github.com/cenkalti/backoff/v4"
"github.com/hashicorp/nomad/api"
"github.com/rs/zerolog/log"
ptypes "github.com/traefik/paerser/types"
"github.com/traefik/traefik/v2/pkg/config/dynamic"
"github.com/traefik/traefik/v2/pkg/job"
"github.com/traefik/traefik/v2/pkg/log"
"github.com/traefik/traefik/v2/pkg/logs"
"github.com/traefik/traefik/v2/pkg/provider"
"github.com/traefik/traefik/v2/pkg/provider/constraints"
"github.com/traefik/traefik/v2/pkg/safe"
@ -59,7 +60,7 @@ type ProviderBuilder struct {
// BuildProviders builds Nomad provider instances for the given namespaces configuration.
func (p *ProviderBuilder) BuildProviders() []*Provider {
if p.Namespace != "" {
log.WithoutContext().Warnf("Namespace option is deprecated, please use the Namespaces option instead.")
log.Warn().Msg("Namespace option is deprecated, please use the Namespaces option instead.")
}
if len(p.Namespaces) == 0 {
@ -154,8 +155,8 @@ func (p *Provider) Provide(configurationChan chan<- dynamic.Message, pool *safe.
}
pool.GoCtx(func(routineCtx context.Context) {
ctxLog := log.With(routineCtx, log.Str(log.ProviderName, p.name))
logger := log.FromContext(ctxLog)
logger := log.Ctx(routineCtx).With().Str(logs.ProviderName, p.name).Logger()
ctxLog := logger.WithContext(routineCtx)
operation := func() error {
ctx, cancel := context.WithCancel(ctxLog)
@ -186,7 +187,7 @@ func (p *Provider) Provide(configurationChan chan<- dynamic.Message, pool *safe.
}
failure := func(err error, d time.Duration) {
logger.Errorf("Provider connection error %+v, retrying in %s", err, d)
logger.Error().Err(err).Msgf("Provider connection error, retrying in %s", d)
}
if retryErr := backoff.RetryNotify(
@ -194,7 +195,7 @@ func (p *Provider) Provide(configurationChan chan<- dynamic.Message, pool *safe.
backoff.WithContext(job.NewBackOff(backoff.NewExponentialBackOff()), ctxLog),
failure,
); retryErr != nil {
logger.Errorf("Cannot connect to Nomad server %+v", retryErr)
logger.Error().Err(retryErr).Msg("Cannot connect to Nomad server")
}
})
@ -273,22 +274,22 @@ func (p *Provider) getNomadServiceData(ctx context.Context) ([]item, error) {
for _, stub := range stubs {
for _, service := range stub.Services {
logger := log.FromContext(log.With(ctx, log.Str("serviceName", service.ServiceName)))
logger := log.Ctx(ctx).With().Str("serviceName", service.ServiceName).Logger()
extraConf := p.getExtraConf(service.Tags)
if !extraConf.Enable {
logger.Debug("Filter Nomad service that is not enabled")
logger.Debug().Msg("Filter Nomad service that is not enabled")
continue
}
matches, err := constraints.MatchTags(service.Tags, p.Constraints)
if err != nil {
logger.Errorf("Error matching constraint expressions: %v", err)
logger.Error().Err(err).Msg("Error matching constraint expressions")
continue
}
if !matches {
logger.Debugf("Filter Nomad service not matching constraints: %q", p.Constraints)
logger.Debug().Msgf("Filter Nomad service not matching constraints: %q", p.Constraints)
continue
}