Skip to content

Commit

Permalink
Merge pull request #3148 from thaJeztah/loggggggs
Browse files Browse the repository at this point in the history
reduce direct imports of logrus
  • Loading branch information
dperny authored Aug 7, 2023
2 parents b98a45d + cd9912a commit 9afd813
Show file tree
Hide file tree
Showing 41 changed files with 174 additions and 199 deletions.
4 changes: 1 addition & 3 deletions agent/csi/volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"sync"
"time"

"github.com/sirupsen/logrus"

"github.com/docker/docker/pkg/plugingetter"

"github.com/moby/swarmkit/v2/agent/csi/plugin"
Expand Down Expand Up @@ -65,7 +63,7 @@ func (r *volumes) retryVolumes() {
for {
vid, attempt := r.pendingVolumes.Wait()

dctx := log.WithFields(ctx, logrus.Fields{
dctx := log.WithFields(ctx, log.Fields{
"volume.id": vid,
"attempt": fmt.Sprintf("%d", attempt),
})
Expand Down
6 changes: 2 additions & 4 deletions agent/exec/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/moby/swarmkit/v2/log"
"github.com/moby/swarmkit/v2/protobuf/ptypes"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

// Controller controls execution of a task.
Expand Down Expand Up @@ -348,11 +347,10 @@ func Do(ctx context.Context, task *api.Task, ctlr Controller) (*api.TaskStatus,

func logStateChange(ctx context.Context, desired, previous, next api.TaskState) {
if previous != next {
fields := logrus.Fields{
log.G(ctx).WithFields(log.Fields{
"state.transition": fmt.Sprintf("%v->%v", previous, next),
"state.desired": desired,
}
log.G(ctx).WithFields(fields).Debug("state changed")
}).Debug("state changed")
}
}

Expand Down
5 changes: 2 additions & 3 deletions agent/exec/dockerapi/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/moby/swarmkit/v2/api"
"github.com/moby/swarmkit/v2/log"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/time/rate"
)

Expand Down Expand Up @@ -84,13 +83,13 @@ func (c *containerAdapter) pullImage(ctx context.Context) error {
// if we have progress details, we have everything we need
if progress, ok := m["progressDetail"].(map[string]interface{}); ok {
// first, log the image and status
l = l.WithFields(logrus.Fields{
l = l.WithFields(log.Fields{
"image": c.container.image(),
"status": m["status"],
})
// then, if we have progress, log the progress
if progress["current"] != nil && progress["total"] != nil {
l = l.WithFields(logrus.Fields{
l = l.WithFields(log.Fields{
"current": progress["current"],
"total": progress["total"],
})
Expand Down
15 changes: 7 additions & 8 deletions agent/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/moby/swarmkit/v2/api"
"github.com/moby/swarmkit/v2/connectionbroker"
"github.com/moby/swarmkit/v2/log"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand Down Expand Up @@ -180,7 +179,7 @@ func (s *session) heartbeat(ctx context.Context) error {
heartbeat := time.NewTimer(1) // send out a heartbeat right away
defer heartbeat.Stop()

fields := logrus.Fields{
fields := log.Fields{
"sessionID": s.sessionID,
"method": "(*session).heartbeat",
}
Expand Down Expand Up @@ -243,8 +242,8 @@ func (s *session) handleSessionMessage(ctx context.Context, msg *api.SessionMess
}

func (s *session) logSubscriptions(ctx context.Context) error {
log := log.G(ctx).WithFields(logrus.Fields{"method": "(*session).logSubscriptions"})
log.Debugf("")
logger := log.G(ctx).WithFields(log.Fields{"method": "(*session).logSubscriptions"})
logger.Debugf("")

client := api.NewLogBrokerClient(s.conn.ClientConn)
subscriptions, err := client.ListenSubscriptions(ctx, &api.ListenSubscriptionsRequest{})
Expand All @@ -257,7 +256,7 @@ func (s *session) logSubscriptions(ctx context.Context) error {
resp, err := subscriptions.Recv()
st, _ := status.FromError(err)
if st.Code() == codes.Unimplemented {
log.Warning("manager does not support log subscriptions")
logger.Warning("manager does not support log subscriptions")
// Don't return, because returning would bounce the session
select {
case <-s.closed:
Expand All @@ -281,8 +280,8 @@ func (s *session) logSubscriptions(ctx context.Context) error {
}

func (s *session) watch(ctx context.Context) error {
log := log.G(ctx).WithFields(logrus.Fields{"method": "(*session).watch"})
log.Debugf("")
logger := log.G(ctx).WithFields(log.Fields{"method": "(*session).watch"})
logger.Debugf("")
var (
resp *api.AssignmentsMessage
assignmentWatch api.Dispatcher_AssignmentsClient
Expand Down Expand Up @@ -313,7 +312,7 @@ func (s *session) watch(ctx context.Context) error {
}
tasksFallback = true
assignmentWatch = nil
log.WithError(err).Infof("falling back to Tasks")
logger.WithError(err).Infof("falling back to Tasks")
}
}

Expand Down
6 changes: 4 additions & 2 deletions agent/task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import (

"github.com/moby/swarmkit/v2/agent/exec"
"github.com/moby/swarmkit/v2/api"
"github.com/sirupsen/logrus"
"github.com/moby/swarmkit/v2/log"
"github.com/stretchr/testify/assert"
)

const debugLevel = 5

func init() {
logrus.SetLevel(logrus.DebugLevel)
log.L.Logger.SetLevel(debugLevel)
}

func TestTaskManager(t *testing.T) {
Expand Down
23 changes: 11 additions & 12 deletions agent/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/moby/swarmkit/v2/api"
"github.com/moby/swarmkit/v2/log"
"github.com/moby/swarmkit/v2/watch"
"github.com/sirupsen/logrus"
bolt "go.etcd.io/bbolt"
)

Expand Down Expand Up @@ -135,7 +134,7 @@ func (w *worker) Assign(ctx context.Context, assignments []*api.AssignmentChange
return ErrClosed
}

log.G(ctx).WithFields(logrus.Fields{
log.G(ctx).WithFields(log.Fields{
"len(assignments)": len(assignments),
}).Debug("(*worker).Assign")

Expand Down Expand Up @@ -174,7 +173,7 @@ func (w *worker) Update(ctx context.Context, assignments []*api.AssignmentChange
return ErrClosed
}

log.G(ctx).WithFields(logrus.Fields{
log.G(ctx).WithFields(log.Fields{
"len(assignments)": len(assignments),
}).Debug("(*worker).Update")

Expand Down Expand Up @@ -212,7 +211,7 @@ func reconcileTaskState(ctx context.Context, w *worker, assignments []*api.Assig
}
}

log.G(ctx).WithFields(logrus.Fields{
log.G(ctx).WithFields(log.Fields{
"len(updatedTasks)": len(updatedTasks),
"len(removedTasks)": len(removedTasks),
}).Debug("(*worker).reconcileTaskState")
Expand All @@ -227,10 +226,10 @@ func reconcileTaskState(ctx context.Context, w *worker, assignments []*api.Assig
assigned := map[string]struct{}{}

for _, task := range updatedTasks {
log.G(ctx).WithFields(
logrus.Fields{
"task.id": task.ID,
"task.desiredstate": task.DesiredState}).Debug("assigned")
log.G(ctx).WithFields(log.Fields{
"task.id": task.ID,
"task.desiredstate": task.DesiredState,
}).Debug("assigned")
if err := PutTask(tx, task); err != nil {
return err
}
Expand Down Expand Up @@ -359,7 +358,7 @@ func reconcileSecrets(ctx context.Context, w *worker, assignments []*api.Assignm

secrets := secretsProvider.Secrets()

log.G(ctx).WithFields(logrus.Fields{
log.G(ctx).WithFields(log.Fields{
"len(updatedSecrets)": len(updatedSecrets),
"len(removedSecrets)": len(removedSecrets),
}).Debug("(*worker).reconcileSecrets")
Expand Down Expand Up @@ -402,7 +401,7 @@ func reconcileConfigs(ctx context.Context, w *worker, assignments []*api.Assignm

configs := configsProvider.Configs()

log.G(ctx).WithFields(logrus.Fields{
log.G(ctx).WithFields(log.Fields{
"len(updatedConfigs)": len(updatedConfigs),
"len(removedConfigs)": len(removedConfigs),
}).Debug("(*worker).reconcileConfigs")
Expand Down Expand Up @@ -448,7 +447,7 @@ func reconcileVolumes(ctx context.Context, w *worker, assignments []*api.Assignm

volumes := volumesProvider.Volumes()

log.G(ctx).WithFields(logrus.Fields{
log.G(ctx).WithFields(log.Fields{
"len(updatedVolumes)": len(updatedVolumes),
"len(removedVolumes)": len(removedVolumes),
}).Debug("(*worker).reconcileVolumes")
Expand Down Expand Up @@ -536,7 +535,7 @@ func (w *worker) taskManager(ctx context.Context, tx *bolt.Tx, task *api.Task) (
}

func (w *worker) newTaskManager(ctx context.Context, tx *bolt.Tx, task *api.Task) (*taskManager, error) {
ctx = log.WithLogger(ctx, log.G(ctx).WithFields(logrus.Fields{
ctx = log.WithLogger(ctx, log.G(ctx).WithFields(log.Fields{
"task.id": task.ID,
"service.id": task.ServiceID,
}))
Expand Down
9 changes: 4 additions & 5 deletions agent/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/moby/swarmkit/v2/api"
"github.com/moby/swarmkit/v2/log"
"github.com/moby/swarmkit/v2/testutils"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
bolt "go.etcd.io/bbolt"
)
Expand All @@ -20,7 +19,7 @@ type testPublisherProvider struct {

func (tpp *testPublisherProvider) Publisher(ctx context.Context, subscriptionID string) (exec.LogPublisher, func(), error) {
return exec.LogPublisherFunc(func(ctx context.Context, message api.LogMessage) error {
log.G(ctx).WithFields(logrus.Fields{
log.G(ctx).WithFields(log.Fields{
"subscription": subscriptionID,
"task.id": message.Context.TaskID,
"node.id": message.Context.NodeID,
Expand Down Expand Up @@ -61,7 +60,7 @@ func TestWorkerAssign(t *testing.T) {
worker := newWorker(db, executor, &testPublisherProvider{})
reporter := newFakeReporter(
statusReporterFunc(func(ctx context.Context, taskID string, status *api.TaskStatus) error {
log.G(ctx).WithFields(logrus.Fields{"task.id": taskID, "status": status}).Info("status update received")
log.G(ctx).WithFields(log.Fields{"task.id": taskID, "status": status}).Info("status update received")
return nil
}),
volumeReporterFunc(func(ctx context.Context, volumeID string) error {
Expand Down Expand Up @@ -282,7 +281,7 @@ func TestWorkerWait(t *testing.T) {
worker := newWorker(db, executor, &testPublisherProvider{})
reporter := newFakeReporter(
statusReporterFunc(func(ctx context.Context, taskID string, status *api.TaskStatus) error {
log.G(ctx).WithFields(logrus.Fields{"task.id": taskID, "status": status}).Info("status update received")
log.G(ctx).WithFields(log.Fields{"task.id": taskID, "status": status}).Info("status update received")
return nil
}),
volumeReporterFunc(func(ctx context.Context, volumeID string) error {
Expand Down Expand Up @@ -433,7 +432,7 @@ func TestWorkerUpdate(t *testing.T) {
worker := newWorker(db, executor, &testPublisherProvider{})
reporter := newFakeReporter(
statusReporterFunc(func(ctx context.Context, taskID string, status *api.TaskStatus) error {
log.G(ctx).WithFields(logrus.Fields{"task.id": taskID, "status": status}).Info("status update received")
log.G(ctx).WithFields(log.Fields{"task.id": taskID, "status": status}).Info("status update received")
return nil
}),
volumeReporterFunc(func(ctx context.Context, volumeID string) error {
Expand Down
4 changes: 1 addition & 3 deletions ca/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"crypto/x509/pkix"
"strings"

"github.com/sirupsen/logrus"

"github.com/moby/swarmkit/v2/api"
"github.com/moby/swarmkit/v2/log"
"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -43,7 +41,7 @@ func LogTLSState(ctx context.Context, tlsState *tls.ConnectionState) {
verifiedChain = append(verifiedChain, strings.Join(subjects, ","))
}

log.G(ctx).WithFields(logrus.Fields{
log.G(ctx).WithFields(log.Fields{
"peer.peerCert": peerCerts,
// "peer.verifiedChain": verifiedChain},
}).Debugf("")
Expand Down
13 changes: 6 additions & 7 deletions ca/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/moby/swarmkit/v2/watch"
"github.com/opencontainers/go-digest"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"google.golang.org/grpc/credentials"
)

Expand Down Expand Up @@ -463,7 +462,7 @@ func LoadSecurityConfig(ctx context.Context, rootCA RootCA, krw *KeyReadWriter,
PublicKey: issuer.RawSubjectPublicKeyInfo,
})
if err == nil {
log.G(ctx).WithFields(logrus.Fields{
log.G(ctx).WithFields(log.Fields{
"node.id": secConfig.ClientTLSCreds.NodeID(),
"node.role": secConfig.ClientTLSCreds.Role(),
}).Debug("loaded node credentials")
Expand Down Expand Up @@ -524,12 +523,12 @@ func (rootCA RootCA) CreateSecurityConfig(ctx context.Context, krw *KeyReadWrite
return nil, nil, err
}
case nil:
log.G(ctx).WithFields(logrus.Fields{
log.G(ctx).WithFields(log.Fields{
"node.id": cn,
"node.role": proposedRole,
}).Debug("issued new TLS certificate")
default:
log.G(ctx).WithFields(logrus.Fields{
log.G(ctx).WithFields(log.Fields{
"node.id": cn,
"node.role": proposedRole,
}).WithError(err).Errorf("failed to issue and save new certificate")
Expand All @@ -538,7 +537,7 @@ func (rootCA RootCA) CreateSecurityConfig(ctx context.Context, krw *KeyReadWrite

secConfig, cleanup, err := NewSecurityConfig(&rootCA, krw, tlsKeyPair, issuerInfo)
if err == nil {
log.G(ctx).WithFields(logrus.Fields{
log.G(ctx).WithFields(log.Fields{
"node.id": secConfig.ClientTLSCreds.NodeID(),
"node.role": secConfig.ClientTLSCreds.Role(),
}).Debugf("new node credentials generated: %s", krw.Target())
Expand Down Expand Up @@ -579,7 +578,7 @@ func RenewTLSConfigNow(ctx context.Context, s *SecurityConfig, connBroker *conne
defer s.renewalMu.Unlock()

ctx = log.WithModule(ctx, "tls")
log := log.G(ctx).WithFields(logrus.Fields{
logger := log.G(ctx).WithFields(log.Fields{
"node.id": s.ClientTLSCreds.NodeID(),
"node.role": s.ClientTLSCreds.Role(),
})
Expand All @@ -602,7 +601,7 @@ func RenewTLSConfigNow(ctx context.Context, s *SecurityConfig, connBroker *conne
}
}
if err != nil {
log.WithError(err).Errorf("failed to renew the certificate")
logger.WithError(err).Errorf("failed to renew the certificate")
return err
}

Expand Down
5 changes: 2 additions & 3 deletions ca/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/moby/swarmkit/v2/manager/state/store"
"github.com/moby/swarmkit/v2/testutils"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -322,7 +321,7 @@ func TestLoadSecurityConfigIntermediates(t *testing.T) {
rootCA, err := ca.NewRootCA(cautils.ECDSACertChain[2], nil, nil, ca.DefaultNodeCertExpiration, nil)
require.NoError(t, err)

ctx := log.WithLogger(context.Background(), log.L.WithFields(logrus.Fields{
ctx := log.WithLogger(context.Background(), log.L.WithFields(log.Fields{
"testname": t.Name(),
"testHasExternalCA": false,
}))
Expand Down Expand Up @@ -360,7 +359,7 @@ func TestLoadSecurityConfigKeyFormat(t *testing.T) {
rootCA, err := ca.NewRootCA(cautils.ECDSACertChain[1], nil, nil, ca.DefaultNodeCertExpiration, nil)
require.NoError(t, err)

ctx := log.WithLogger(context.Background(), log.L.WithFields(logrus.Fields{
ctx := log.WithLogger(context.Background(), log.L.WithFields(log.Fields{
"testname": t.Name(),
"testHasExternalCA": false,
}))
Expand Down
3 changes: 1 addition & 2 deletions ca/external.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/cloudflare/cfssl/signer"
"github.com/moby/swarmkit/v2/log"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/net/context/ctxhttp"
)

Expand Down Expand Up @@ -203,7 +202,7 @@ func makeExternalSignRequest(ctx context.Context, client *http.Client, url strin

var apiResponse api.Response
if err := json.Unmarshal(body, &apiResponse); err != nil {
logrus.Debugf("unable to JSON-parse CFSSL API response body: %s", string(body))
log.G(ctx).Debugf("unable to JSON-parse CFSSL API response body: %s", string(body))
return nil, recoverableErr{err: errors.Wrap(err, "unable to parse JSON response")}
}

Expand Down
Loading

0 comments on commit 9afd813

Please sign in to comment.