Skip to content

Commit

Permalink
Convert lib/labels to slog (#43625)
Browse files Browse the repository at this point in the history
  • Loading branch information
zmb3 authored Jun 28, 2024
1 parent 42708e3 commit 89b6ae8
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 23 deletions.
2 changes: 1 addition & 1 deletion lib/kube/proxy/cluster_details.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func newClusterDetails(ctx context.Context, cfg clusterDetailsConfig) (_ *kubeDe
ctx,
&labels.DynamicConfig{
Labels: cfg.cluster.GetDynamicLabels(),
Log: cfg.log,
// TODO: pass cfg.log through after it is converted to slog
})
if err != nil {
return nil, trace.Wrap(err)
Expand Down
20 changes: 9 additions & 11 deletions lib/labels/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@
package labels

import (
"cmp"
"context"
"fmt"
"log/slog"
"maps"
"sync"
"time"

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

"github.com/gravitational/teleport"
"github.com/gravitational/teleport/api/types"
Expand Down Expand Up @@ -58,7 +59,7 @@ const (
type CloudConfig struct {
Client imds.Client
Clock clockwork.Clock
Log logrus.FieldLogger
Log *slog.Logger
namespace string
instanceMetadataHint string
}
Expand All @@ -67,12 +68,9 @@ func (conf *CloudConfig) checkAndSetDefaults() error {
if conf.Client == nil {
return trace.BadParameter("missing parameter: Client")
}
if conf.Clock == nil {
conf.Clock = clockwork.NewRealClock()
}
if conf.Log == nil {
conf.Log = logrus.WithField(teleport.ComponentKey, "cloudlabels")
}

conf.Clock = cmp.Or(conf.Clock, clockwork.NewRealClock())
conf.Log = cmp.Or(conf.Log, slog.With(teleport.ComponentKey, "cloudlabels"))
return nil
}

Expand Down Expand Up @@ -150,7 +148,7 @@ func (l *CloudImporter) Sync(ctx context.Context) error {
if trace.IsNotFound(err) || trace.IsAccessDenied(err) {
// Only show the error the first time around.
l.instanceTagsNotFoundOnce.Do(func() {
l.Log.Warning(l.instanceMetadataHint)
l.Log.WarnContext(ctx, l.instanceMetadataHint) //nolint:sloglint // message should be a constant but in this case we are creating it at runtime.
})
return nil
}
Expand All @@ -160,7 +158,7 @@ func (l *CloudImporter) Sync(ctx context.Context) error {
m := make(map[string]string)
for key, value := range tags {
if !types.IsValidLabelKey(key) {
l.Log.Debugf("Skipping cloud tag %q, not a valid label key.", key)
l.Log.DebugContext(ctx, "Skipping cloud tag due to invalid label key", "tag", key)
continue
}
m[FormatCloudLabelKey(l.namespace, key)] = value
Expand All @@ -183,7 +181,7 @@ func (l *CloudImporter) periodicUpdateLabels(ctx context.Context) {

for {
if err := l.Sync(ctx); err != nil {
l.Log.Warningf("Error fetching cloud tags: %v", err)
l.Log.WarnContext(ctx, "Failed to fetch cloud tags", "error", err)
}
select {
case <-ticker.Chan():
Expand Down
16 changes: 8 additions & 8 deletions lib/labels/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@
package labels

import (
"cmp"
"context"
"log/slog"
"os/exec"
"strings"
"sync"
"time"

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

"github.com/gravitational/teleport"
"github.com/gravitational/teleport/api/types"
"github.com/gravitational/teleport/lib/services"
)
Expand All @@ -40,15 +42,13 @@ type DynamicConfig struct {
Labels services.CommandLabels

// Log is a component logger.
Log *logrus.Entry
Log *slog.Logger
}

// CheckAndSetDefaults makes sure valid values were passed in to create
// dynamic labels.
func (c *DynamicConfig) CheckAndSetDefaults() error {
if c.Log == nil {
c.Log = logrus.NewEntry(logrus.StandardLogger())
}
c.Log = cmp.Or(c.Log, slog.With(teleport.ComponentKey, "dynamiclabels"))

// Loop over all labels and make sure the key name is valid and the interval
// is valid as well. If the interval is not valid, update the value.
Expand All @@ -65,11 +65,11 @@ func (c *DynamicConfig) CheckAndSetDefaults() error {
if label.GetPeriod() < time.Second {
label.SetPeriod(time.Second)
labels[name] = label
c.Log.Warnf("Label period can't be less than 1 second. Period for label %q was set to 1 second.", name)
c.Log.WarnContext(context.Background(), "Label period cannot be less than 1 second. Defaulting to 1 second.", "label", name)
}
}
c.Labels = labels

c.Labels = labels
return nil
}

Expand Down Expand Up @@ -152,7 +152,7 @@ func (l *Dynamic) periodicUpdateLabel(name string, label types.CommandLabel) {
func (l *Dynamic) updateLabel(name string, label types.CommandLabel) {
out, err := exec.Command(label.GetCommand()[0], label.GetCommand()[1:]...).Output()
if err != nil {
l.c.Log.Errorf("Failed to run command and update label: %v.", err)
l.c.Log.ErrorContext(context.Background(), "Failed to run command and update label", "error", err)
label.SetResult(err.Error() + " output: " + string(out))
} else {
label.SetResult(strings.TrimSpace(string(out)))
Expand Down
2 changes: 1 addition & 1 deletion lib/service/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (process *TeleportProcess) initKubernetesService(logger *slog.Logger, conn
if len(cfg.Kube.DynamicLabels) != 0 {
dynLabels, err = labels.NewDynamic(process.ExitContext(), &labels.DynamicConfig{
Labels: cfg.Kube.DynamicLabels,
Log: process.log.WithField(teleport.ComponentKey, teleport.Component(teleport.ComponentKube, process.id)),
Log: process.logger.With(teleport.ComponentKey, teleport.Component(teleport.ComponentKube, process.id)),
})
if err != nil {
return trace.Wrap(err)
Expand Down
2 changes: 1 addition & 1 deletion lib/srv/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ func (s *Server) startDynamicLabels(ctx context.Context, app types.Application)
}
dynamic, err := labels.NewDynamic(ctx, &labels.DynamicConfig{
Labels: app.GetDynamicLabels(),
Log: s.log,
// TODO: pass s.log through after it's been converted to slog
})
if err != nil {
return trace.Wrap(err)
Expand Down
2 changes: 1 addition & 1 deletion lib/srv/db/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ func (s *Server) startDynamicLabels(ctx context.Context, database types.Database
}
dynamic, err := labels.NewDynamic(ctx, &labels.DynamicConfig{
Labels: database.GetDynamicLabels(),
Log: s.log,
// TODO: pass s.log through after it's been converted to slog
})
if err != nil {
return trace.Wrap(err)
Expand Down

0 comments on commit 89b6ae8

Please sign in to comment.