Skip to content

Commit

Permalink
Reduce reliance on servicecfg.Config logrus logger
Browse files Browse the repository at this point in the history
Converts most items to use servicecfg.Config.Logger instead of
servicecfg.Config.Log. The deprecated logrus logger unfortunately
cannot be removed yet as it's still used in e. Additionally all
logging within the config package was converted to use slog.
  • Loading branch information
rosstimothy committed Jan 7, 2025
1 parent 6c5b737 commit 2ad2f6a
Show file tree
Hide file tree
Showing 30 changed files with 289 additions and 333 deletions.
2 changes: 1 addition & 1 deletion e2e/aws/fixtures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func newInstanceConfig(t *testing.T) helpers.InstanceConfig {
NodeName: host,
Priv: priv,
Pub: pub,
Log: utils.NewLoggerForTests(),
Logger: utils.NewSlogLoggerForTests(),
}
}

Expand Down
10 changes: 5 additions & 5 deletions integration/appaccess/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func SetupWithOptions(t *testing.T, opts AppTestOptions) *Pack {
tr := utils.NewTracer(utils.ThisFunction()).Start()
defer tr.Stop()

log := utils.NewLoggerForTests()
log := utils.NewSlogLoggerForTests()

// Insecure development mode needs to be set because the web proxy uses a
// self-signed certificate during tests.
Expand Down Expand Up @@ -323,7 +323,7 @@ func SetupWithOptions(t *testing.T, opts AppTestOptions) *Pack {
NodeName: helpers.Host,
Priv: privateKey,
Pub: publicKey,
Log: log,
Logger: log,
}
if opts.RootClusterListeners != nil {
rootCfg.Listeners = opts.RootClusterListeners(t, &rootCfg.Fds)
Expand All @@ -338,7 +338,7 @@ func SetupWithOptions(t *testing.T, opts AppTestOptions) *Pack {
NodeName: helpers.Host,
Priv: privateKey,
Pub: publicKey,
Log: log,
Logger: log,
}
if opts.LeafClusterListeners != nil {
leafCfg.Listeners = opts.LeafClusterListeners(t, &leafCfg.Fds)
Expand All @@ -347,7 +347,7 @@ func SetupWithOptions(t *testing.T, opts AppTestOptions) *Pack {

rcConf := servicecfg.MakeDefaultConfig()
rcConf.Console = nil
rcConf.Log = log
rcConf.Logger = log
rcConf.DataDir = t.TempDir()
rcConf.Auth.Enabled = true
rcConf.Auth.Preference.SetSecondFactor("off")
Expand All @@ -365,7 +365,7 @@ func SetupWithOptions(t *testing.T, opts AppTestOptions) *Pack {

lcConf := servicecfg.MakeDefaultConfig()
lcConf.Console = nil
lcConf.Log = log
lcConf.Logger = log
lcConf.DataDir = t.TempDir()
lcConf.Auth.Enabled = true
lcConf.Auth.Preference.SetSecondFactor("off")
Expand Down
7 changes: 2 additions & 5 deletions integration/appaccess/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -698,15 +698,13 @@ func (p *Pack) waitForLogout(appCookies []*http.Cookie) (int, error) {
}

func (p *Pack) startRootAppServers(t *testing.T, count int, opts AppTestOptions) []*service.TeleportProcess {
log := utils.NewLoggerForTests()

configs := make([]*servicecfg.Config, count)

for i := 0; i < count; i++ {
raConf := servicecfg.MakeDefaultConfig()
raConf.Clock = opts.Clock
raConf.Console = nil
raConf.Log = log
raConf.Logger = utils.NewSlogLoggerForTests()
raConf.DataDir = t.TempDir()
raConf.SetToken("static-token-value")
raConf.SetAuthServerAddress(utils.NetAddr{
Expand Down Expand Up @@ -870,14 +868,13 @@ func waitForAppServer(t *testing.T, tunnel reversetunnelclient.Server, name stri
}

func (p *Pack) startLeafAppServers(t *testing.T, count int, opts AppTestOptions) []*service.TeleportProcess {
log := utils.NewLoggerForTests()
configs := make([]*servicecfg.Config, count)

for i := 0; i < count; i++ {
laConf := servicecfg.MakeDefaultConfig()
laConf.Clock = opts.Clock
laConf.Console = nil
laConf.Log = log
laConf.Logger = utils.NewSlogLoggerForTests()
laConf.DataDir = t.TempDir()
laConf.SetToken("static-token-value")
laConf.SetAuthServerAddress(utils.NetAddr{
Expand Down
2 changes: 1 addition & 1 deletion integration/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestClientWithExpiredCredentialsAndDetailedErrorMessage(t *testing.T) {
ClusterName: "root.example.com",
HostID: uuid.New().String(),
NodeName: Loopback,
Log: utils.NewLoggerForTests(),
Logger: utils.NewSlogLoggerForTests(),
}
cfg.Listeners = helpers.SingleProxyPortSetup(t, &cfg.Fds)
rc := helpers.NewInstance(t, cfg)
Expand Down
12 changes: 6 additions & 6 deletions integration/db/fixture.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ func SetupDatabaseTest(t *testing.T, options ...TestOptionFunc) *DatabasePack {
tracer := utils.NewTracer(utils.ThisFunction()).Start()
t.Cleanup(func() { tracer.Stop() })
lib.SetInsecureDevMode(true)
log := utils.NewLoggerForTests()
log := utils.NewSlogLoggerForTests()

// Generate keypair.
privateKey, publicKey, err := testauthority.New().GenerateKeyPair()
Expand All @@ -272,7 +272,7 @@ func SetupDatabaseTest(t *testing.T, options ...TestOptionFunc) *DatabasePack {
NodeName: opts.nodeName,
Priv: privateKey,
Pub: publicKey,
Log: log,
Logger: log,
}
rootCfg.Listeners = opts.listenerSetup(t, &rootCfg.Fds)
p.Root.Cluster = helpers.NewInstance(t, rootCfg)
Expand All @@ -284,7 +284,7 @@ func SetupDatabaseTest(t *testing.T, options ...TestOptionFunc) *DatabasePack {
NodeName: opts.nodeName,
Priv: privateKey,
Pub: publicKey,
Log: log,
Logger: log,
}
leafCfg.Listeners = opts.listenerSetup(t, &leafCfg.Fds)
p.Leaf.Cluster = helpers.NewInstance(t, leafCfg)
Expand Down Expand Up @@ -395,15 +395,15 @@ func (p *DatabasePack) WaitForLeaf(t *testing.T) {
servers, err := accessPoint.GetDatabaseServers(ctx, apidefaults.Namespace)
if err != nil {
// Use root logger as we need a configured logger instance and the root cluster have one.
p.Root.Cluster.Log.WithError(err).Debugf("Leaf cluster access point is unavailable.")
p.Root.Cluster.Log.DebugContext(ctx, "Leaf cluster access point is unavailable", "error", err)
continue
}
if !containsDB(servers, p.Leaf.MysqlService.Name) {
p.Root.Cluster.Log.WithError(err).Debugf("Leaf db service %q is unavailable.", p.Leaf.MysqlService.Name)
p.Root.Cluster.Log.DebugContext(ctx, "Leaf db service is unavailable", "error", err, "db_service", p.Leaf.MysqlService.Name)
continue
}
if !containsDB(servers, p.Leaf.PostgresService.Name) {
p.Root.Cluster.Log.WithError(err).Debugf("Leaf db service %q is unavailable.", p.Leaf.PostgresService.Name)
p.Root.Cluster.Log.DebugContext(ctx, "Leaf db service is unavailable", "error", err, "db_service", p.Leaf.PostgresService.Name)
continue
}
return
Expand Down
21 changes: 7 additions & 14 deletions integration/ec2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ package integration
import (
"context"
"fmt"
"io"
"log/slog"
"os"
"testing"
"time"
Expand All @@ -31,7 +31,6 @@ import (
"github.com/aws/aws-sdk-go-v2/service/sts"
"github.com/gravitational/trace"
"github.com/jonboulle/clockwork"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand All @@ -52,15 +51,9 @@ import (
"github.com/gravitational/teleport/lib/service/servicecfg"
"github.com/gravitational/teleport/lib/services"
"github.com/gravitational/teleport/lib/utils"
logutils "github.com/gravitational/teleport/lib/utils/log"
)

func newSilentLogger() utils.Logger {
logger := utils.NewLoggerForTests()
logger.SetLevel(logrus.PanicLevel)
logger.SetOutput(io.Discard)
return logger
}

func newNodeConfig(t *testing.T, tokenName string, joinMethod types.JoinMethod) *servicecfg.Config {
config := servicecfg.MakeDefaultConfig()
config.Version = defaults.TeleportConfigVersionV3
Expand All @@ -71,7 +64,7 @@ func newNodeConfig(t *testing.T, tokenName string, joinMethod types.JoinMethod)
config.Auth.Enabled = false
config.Proxy.Enabled = false
config.DataDir = t.TempDir()
config.Log = newSilentLogger()
config.Logger = slog.New(logutils.DiscardHandler{})
config.CircuitBreakerConfig = breaker.NoopBreakerConfig()
config.InstanceMetadataClient = cloudimds.NewDisabledIMDSClient()
return config
Expand All @@ -92,7 +85,7 @@ func newProxyConfig(t *testing.T, authAddr utils.NetAddr, tokenName string, join

config.DataDir = t.TempDir()
config.SetAuthServerAddress(authAddr)
config.Log = newSilentLogger()
config.Logger = slog.New(logutils.DiscardHandler{})
config.CircuitBreakerConfig = breaker.NoopBreakerConfig()
config.InstanceMetadataClient = cloudimds.NewDisabledIMDSClient()
return config
Expand Down Expand Up @@ -126,7 +119,7 @@ func newAuthConfig(t *testing.T, clock clockwork.Clock) *servicecfg.Config {
config.Proxy.Enabled = false
config.SSH.Enabled = false
config.Clock = clock
config.Log = newSilentLogger()
config.Logger = slog.New(logutils.DiscardHandler{})
config.CircuitBreakerConfig = breaker.NoopBreakerConfig()
config.InstanceMetadataClient = cloudimds.NewDisabledIMDSClient()
return config
Expand Down Expand Up @@ -348,7 +341,7 @@ func TestEC2Labels(t *testing.T) {
},
}
tconf := servicecfg.MakeDefaultConfig()
tconf.Log = newSilentLogger()
tconf.Logger = slog.New(logutils.DiscardHandler{})
tconf.DataDir = t.TempDir()
tconf.Auth.Enabled = true
tconf.Proxy.Enabled = true
Expand Down Expand Up @@ -473,7 +466,7 @@ func TestEC2Hostname(t *testing.T) {
},
}
tconf := servicecfg.MakeDefaultConfig()
tconf.Log = newSilentLogger()
tconf.Logger = slog.New(logutils.DiscardHandler{})
tconf.DataDir = t.TempDir()
tconf.Auth.Enabled = true
tconf.Proxy.Enabled = true
Expand Down
6 changes: 3 additions & 3 deletions integration/helpers/fixture.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package helpers

import (
"log/slog"
"os"
"os/user"
"testing"
Expand All @@ -28,7 +29,6 @@ import (

"github.com/gravitational/teleport/lib/auth/testauthority"
"github.com/gravitational/teleport/lib/service/servicecfg"
"github.com/gravitational/teleport/lib/utils"
)

const (
Expand All @@ -44,7 +44,7 @@ type Fixture struct {
Pub []byte

// Log defines the test-specific logger
Log utils.Logger
Log *slog.Logger
}

func NewFixture(t *testing.T) *Fixture {
Expand Down Expand Up @@ -112,7 +112,7 @@ func (s *Fixture) DefaultInstanceConfig(t *testing.T) InstanceConfig {
NodeName: Host,
Priv: s.Priv,
Pub: s.Pub,
Log: s.Log,
Logger: s.Log,
}
cfg.Listeners = StandardListenerSetup(t, &cfg.Fds)
return cfg
Expand Down
8 changes: 4 additions & 4 deletions integration/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ func MakeTestServers(t *testing.T) (auth *service.TeleportProcess, proxy *servic
cfg.SSH.Enabled = false
cfg.Auth.Enabled = true
cfg.Proxy.Enabled = false
cfg.Log = utils.NewLoggerForTests()
cfg.Logger = utils.NewSlogLoggerForTests()

auth, err = service.NewTeleport(cfg)
require.NoError(t, err)
Expand Down Expand Up @@ -417,7 +417,7 @@ func MakeTestServers(t *testing.T) (auth *service.TeleportProcess, proxy *servic
cfg.Proxy.WebAddr,
}
cfg.Proxy.DisableWebInterface = true
cfg.Log = utils.NewLoggerForTests()
cfg.Logger = utils.NewSlogLoggerForTests()

proxy, err = service.NewTeleport(cfg)
require.NoError(t, err)
Expand Down Expand Up @@ -454,7 +454,7 @@ func MakeTestDatabaseServer(t *testing.T, proxyAddr utils.NetAddr, token string,
cfg.Databases.Enabled = true
cfg.Databases.Databases = dbs
cfg.Databases.ResourceMatchers = resMatchers
cfg.Log = utils.NewLoggerForTests()
cfg.Logger = utils.NewSlogLoggerForTests()

db, err := service.NewTeleport(cfg)
require.NoError(t, err)
Expand Down Expand Up @@ -487,7 +487,7 @@ func MakeAgentServer(t *testing.T, cfg *servicecfg.Config, proxyAddr utils.NetAd
cfg.Auth.Enabled = false
cfg.Proxy.Enabled = false
cfg.Databases.Enabled = false
cfg.Log = utils.NewLoggerForTests()
cfg.Logger = utils.NewSlogLoggerForTests()

agent, err := service.NewTeleport(cfg)
require.NoError(t, err)
Expand Down
Loading

0 comments on commit 2ad2f6a

Please sign in to comment.