Skip to content

Commit

Permalink
Convert lib/integrations to use slog (#50484)
Browse files Browse the repository at this point in the history
  • Loading branch information
rosstimothy authored Dec 21, 2024
1 parent 659db2b commit 8890e3e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
ststypes "github.com/aws/aws-sdk-go-v2/service/sts/types"
"github.com/google/uuid"
"github.com/jonboulle/clockwork"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -75,8 +74,6 @@ func (f *fakeSTSClient) AssumeRoleWithWebIdentity(ctx context.Context, params *s
}

func TestCredentialsCache(t *testing.T) {
logrus.SetLevel(logrus.DebugLevel)

ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)

Expand Down
20 changes: 11 additions & 9 deletions lib/integrations/diagnostics/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package diagnostics

import (
"context"
"log/slog"
"os"
"path/filepath"
"runtime"
Expand All @@ -26,14 +28,14 @@ import (
"time"

"github.com/gravitational/trace"
"github.com/sirupsen/logrus"
)

// Profile captures various Go pprof profiles and writes
// them to the profivided directory. All profiles are prefixed
// with the same epoch time so that profiles can easily be associated
// as being captured from the same call.
func Profile(dir string) error {
ctx := context.Background()
if err := os.MkdirAll(dir, 0o755); err != nil {
return trace.Wrap(err, "creating profile directory %v", dir)
}
Expand Down Expand Up @@ -69,37 +71,37 @@ func Profile(dir string) error {
}
defer blockFile.Close()

logrus.Debugf("capturing trace profile to %s", traceFile.Name())
slog.DebugContext(ctx, "capturing trace profile", "file", traceFile.Name())

if err := runtimetrace.Start(traceFile); err != nil {
return trace.Wrap(err, "capturing trace profile")
}

logrus.Debugf("capturing cpu profile to %s", cpuFile.Name())
slog.DebugContext(ctx, "capturing cpu profile", "file", cpuFile.Name())

if err := pprof.StartCPUProfile(cpuFile); err != nil {
return trace.Wrap(err, "capturing cpu profile")
}

defer func() {
logrus.Debugf("capturing goroutine profile to %s", cpuFile.Name())
slog.DebugContext(ctx, "capturing goroutine profile", "file", cpuFile.Name())

if err := pprof.Lookup("goroutine").WriteTo(goroutineFile, 0); err != nil {
logrus.WithError(err).Warn("failed to capture goroutine profile")
slog.WarnContext(ctx, "failed to capture goroutine profile", "error", err)
}

logrus.Debugf("capturing block profile to %s", cpuFile.Name())
slog.DebugContext(ctx, "capturing block profile", "file", cpuFile.Name())

if err := pprof.Lookup("block").WriteTo(blockFile, 0); err != nil {
logrus.WithError(err).Warn("failed to capture block profile")
slog.WarnContext(ctx, "failed to capture block profile", "error", err)
}

runtime.GC()

logrus.Debugf("capturing heap profile to %s", cpuFile.Name())
slog.DebugContext(ctx, "capturing heap profile", "file", cpuFile.Name())

if err := pprof.WriteHeapProfile(heapFile); err != nil {
logrus.WithError(err).Warn("failed to capture heap profile")
slog.WarnContext(ctx, "failed to capture heap profile", "error", err)
}

pprof.StopCPUProfile()
Expand Down
2 changes: 0 additions & 2 deletions lib/integrations/externalauditstorage/configurator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/google/uuid"
"github.com/jonboulle/clockwork"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -179,7 +178,6 @@ func TestConfiguratorIsUsed(t *testing.T) {
}

func TestCredentialsCache(t *testing.T) {
logrus.SetLevel(logrus.DebugLevel)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

Expand Down
19 changes: 14 additions & 5 deletions lib/integrations/externalauditstorage/error_counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (

"github.com/gravitational/trace"
"github.com/jonboulle/clockwork"
"github.com/sirupsen/logrus"

"github.com/gravitational/teleport"
auditlogpb "github.com/gravitational/teleport/api/gen/proto/go/teleport/auditlog/v1"
Expand All @@ -38,6 +37,7 @@ import (
apievents "github.com/gravitational/teleport/api/types/events"
"github.com/gravitational/teleport/lib/events"
"github.com/gravitational/teleport/lib/session"
logutils "github.com/gravitational/teleport/lib/utils/log"
)

const (
Expand All @@ -53,7 +53,7 @@ const (
syncInterval = 30 * time.Second
)

var log = logrus.WithField(teleport.ComponentKey, "ExternalAuditStorage")
var log = logutils.NewPackageLogger(teleport.ComponentKey, "ExternalAuditStorage")

// ClusterAlertService abstracts a service providing Upsert and Delete
// operations for cluster alerts.
Expand Down Expand Up @@ -189,16 +189,25 @@ func (c *ErrorCounter) sync(ctx context.Context) {
types.WithAlertLabel(types.AlertOnLogin, "yes"),
types.WithAlertLabel(types.AlertVerbPermit, "external_audit_storage:create"))
if err != nil {
log.Infof("ErrorCounter failed to create cluster alert %s: %s", newAlert.name, err)
log.InfoContext(ctx, "ErrorCounter failed to create cluster alert",
"alert_name", newAlert.name,
"error", err,
)
continue
}
if err := c.alertService.UpsertClusterAlert(ctx, alert); err != nil {
log.Infof("ErrorCounter failed to upsert cluster alert %s: %s", newAlert.name, err)
log.InfoContext(ctx, "ErrorCounter failed to upsert cluster alert",
"alert_name", newAlert.name,
"error", err,
)
}
}
for _, alertToClear := range allAlertActions.clearAlerts {
if err := c.alertService.DeleteClusterAlert(ctx, alertToClear); err != nil && !trace.IsNotFound(err) {
log.Infof("ErrorCounter failed to delete cluster alert %s: %s", alertToClear, err)
log.InfoContext(ctx, "ErrorCounter failed to delete cluster alert",
"alert_name", alertToClear,
"error", err,
)
}
}
}
Expand Down

0 comments on commit 8890e3e

Please sign in to comment.