Skip to content

Commit

Permalink
refactor logs
Browse files Browse the repository at this point in the history
  • Loading branch information
CMGS committed Jul 6, 2022
1 parent 0d93575 commit e343332
Show file tree
Hide file tree
Showing 19 changed files with 91 additions and 91 deletions.
12 changes: 6 additions & 6 deletions cluster/calcium/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ func (c *Calcium) BuildImage(ctx context.Context, opts *types.BuildOptions) (ch
logger := log.WithField("Calcium", "BuildImage").WithField("opts", opts)
// Disable build API if scm not set
if c.source == nil {
return nil, logger.Err(ctx, errors.WithStack(types.ErrSCMNotSet))
return nil, logger.ErrWithTracing(ctx, errors.WithStack(types.ErrSCMNotSet))
}
// select nodes
node, err := c.selectBuildNode(ctx)
if err != nil {
return nil, logger.Err(ctx, err)
return nil, logger.ErrWithTracing(ctx, err)
}

log.Infof(ctx, "[BuildImage] Building image at pod %s node %s", node.Podname, node.Name)
Expand All @@ -45,13 +45,13 @@ func (c *Calcium) BuildImage(ctx context.Context, opts *types.BuildOptions) (ch
case types.BuildFromExist:
refs, node, resp, err = c.buildFromExist(ctx, opts)
default:
return nil, logger.Err(ctx, errors.WithStack(errors.New("unknown build type")))
return nil, logger.ErrWithTracing(ctx, errors.WithStack(errors.New("unknown build type")))
}
if err != nil {
return nil, logger.Err(ctx, err)
return nil, logger.ErrWithTracing(ctx, err)
}
ch, err = c.pushImageAndClean(ctx, resp, node, refs)
return ch, logger.Err(ctx, err)
return ch, logger.ErrWithTracing(ctx, err)
}

func (c *Calcium) selectBuildNode(ctx context.Context) (*types.Node, error) {
Expand Down Expand Up @@ -174,7 +174,7 @@ func (c *Calcium) pushImageAndClean(ctx context.Context, resp io.ReadCloser, nod
log.Infof(ctx, "[BuildImage] Push image %s", tag)
rc, err := node.Engine.ImagePush(ctx, tag)
if err != nil {
ch <- &types.BuildImageMessage{Error: logger.Err(ctx, err).Error()}
ch <- &types.BuildImageMessage{Error: logger.ErrWithTracing(ctx, err).Error()}
continue
}

Expand Down
6 changes: 3 additions & 3 deletions cluster/calcium/calcium.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func New(config types.Config, t *testing.T) (*Calcium, error) {
// set store
store, err := store.NewStore(config, t)
if err != nil {
return nil, logger.Err(context.TODO(), errors.WithStack(err))
return nil, logger.ErrWithTracing(context.TODO(), errors.WithStack(err))
}

// set scm
Expand All @@ -64,7 +64,7 @@ func New(config types.Config, t *testing.T) (*Calcium, error) {
// set resource plugin manager
resource, err := resources.NewPluginManager(config)
if err != nil {
return nil, logger.Err(context.TODO(), errors.WithStack(err))
return nil, logger.ErrWithTracing(context.TODO(), errors.WithStack(err))
}

// load cpumem plugin
Expand Down Expand Up @@ -94,7 +94,7 @@ func New(config types.Config, t *testing.T) (*Calcium, error) {

go cal.InitMetrics()

return cal, logger.Err(nil, errors.WithStack(err)) //nolint
return cal, logger.ErrWithTracing(nil, errors.WithStack(err)) //nolint
}

// DisasterRecover .
Expand Down
2 changes: 1 addition & 1 deletion cluster/calcium/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (c *Calcium) ControlWorkload(ctx context.Context, ids []string, t string, f
}
ch <- &types.ControlWorkloadMessage{
WorkloadID: id,
Error: logger.Err(ctx, err),
Error: logger.ErrWithTracing(ctx, err),
Hook: message,
}
}
Expand Down
4 changes: 2 additions & 2 deletions cluster/calcium/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
func (c *Calcium) Copy(ctx context.Context, opts *types.CopyOptions) (chan *types.CopyMessage, error) {
logger := log.WithField("Calcium", "Copy").WithField("opts", opts)
if err := opts.Validate(); err != nil {
return nil, logger.Err(ctx, err)
return nil, logger.ErrWithTracing(ctx, err)
}

ch := make(chan *types.CopyMessage)
Expand All @@ -37,7 +37,7 @@ func (c *Calcium) Copy(ctx context.Context, opts *types.CopyOptions) (chan *type
ch <- &types.CopyMessage{
ID: id,
Path: path,
Error: logger.Err(ctx, err),
Error: logger.ErrWithTracing(ctx, err),
}
}
return
Expand Down
12 changes: 6 additions & 6 deletions cluster/calcium/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ import (
func (c *Calcium) CreateWorkload(ctx context.Context, opts *types.DeployOptions) (chan *types.CreateWorkloadMessage, error) {
logger := log.WithField("Calcium", "CreateWorkload").WithField("opts", opts)
if err := opts.Validate(); err != nil {
return nil, logger.Err(ctx, err)
return nil, logger.ErrWithTracing(ctx, err)
}

opts.ProcessIdent = utils.RandomString(16)
log.Infof(ctx, "[CreateWorkload %s] Creating workload with options:\n%s", opts.ProcessIdent, litter.Options{Compact: true}.Sdump(opts))
// Count 要大于0
if opts.Count <= 0 {
return nil, logger.Err(ctx, errors.WithStack(types.NewDetailedErr(types.ErrBadCount, opts.Count)))
return nil, logger.ErrWithTracing(ctx, errors.WithStack(types.NewDetailedErr(types.ErrBadCount, opts.Count)))
}

return c.doCreateWorkloads(ctx, opts), nil
Expand Down Expand Up @@ -95,7 +95,7 @@ func (c *Calcium) doCreateWorkloads(ctx context.Context, opts *types.DeployOptio
func(ctx context.Context) (err error) {
defer func() {
if err != nil {
ch <- &types.CreateWorkloadMessage{Error: logger.Err(ctx, err)}
ch <- &types.CreateWorkloadMessage{Error: logger.ErrWithTracing(ctx, err)}
}
}()
return c.withNodesPodLocked(ctx, opts.NodeFilter, func(ctx context.Context, nodeMap map[string]*types.Node) (err error) {
Expand Down Expand Up @@ -153,7 +153,7 @@ func (c *Calcium) doCreateWorkloads(ctx context.Context, opts *types.DeployOptio
})
return c.resource.RollbackAlloc(ctx, nodename, resourceArgsToRollback)
}); e != nil {
err = logger.Err(ctx, e)
err = logger.ErrWithTracing(ctx, e)
}
}
return err
Expand Down Expand Up @@ -224,7 +224,7 @@ func (c *Calcium) doDeployWorkloadsOnNode(ctx context.Context,
node, err := c.doGetAndPrepareNode(ctx, nodename, opts.Image)
if err != nil {
for i := 0; i < deploy; i++ {
ch <- &types.CreateWorkloadMessage{Error: logger.Err(ctx, err)}
ch <- &types.CreateWorkloadMessage{Error: logger.ErrWithTracing(ctx, err)}
}
return utils.Range(deploy), err
}
Expand All @@ -243,7 +243,7 @@ func (c *Calcium) doDeployWorkloadsOnNode(ctx context.Context,
defer func() {
if e != nil {
err = e
createMsg.Error = logger.Err(ctx, e)
createMsg.Error = logger.ErrWithTracing(ctx, e)
appendLock.Lock()
indices = append(indices, idx)
appendLock.Unlock()
Expand Down
22 changes: 11 additions & 11 deletions cluster/calcium/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ import (
func (c *Calcium) RemoveImage(ctx context.Context, opts *types.ImageOptions) (chan *types.RemoveImageMessage, error) {
logger := log.WithField("Calcium", "RemoveImage").WithField("opts", opts)
if err := opts.Validate(); err != nil {
return nil, logger.Err(ctx, err)
return nil, logger.ErrWithTracing(ctx, err)
}
opts.Normalize()

nodes, err := c.filterNodes(ctx, types.NodeFilter{Podname: opts.Podname, Includes: opts.Nodenames})
if err != nil {
return nil, logger.Err(ctx, err)
return nil, logger.ErrWithTracing(ctx, err)
}

if len(nodes) == 0 {
return nil, logger.Err(ctx, errors.WithStack(types.ErrPodNoNodes))
return nil, logger.ErrWithTracing(ctx, errors.WithStack(types.ErrPodNoNodes))
}

ch := make(chan *types.RemoveImageMessage)
Expand All @@ -47,7 +47,7 @@ func (c *Calcium) RemoveImage(ctx context.Context, opts *types.ImageOptions) (ch
Messages: []string{},
}
if removeItems, err := node.Engine.ImageRemove(ctx, image, false, true); err != nil {
m.Messages = append(m.Messages, logger.Err(ctx, err).Error())
m.Messages = append(m.Messages, logger.ErrWithTracing(ctx, err).Error())
} else {
m.Success = true
for _, item := range removeItems {
Expand Down Expand Up @@ -81,17 +81,17 @@ func (c *Calcium) RemoveImage(ctx context.Context, opts *types.ImageOptions) (ch
func (c *Calcium) CacheImage(ctx context.Context, opts *types.ImageOptions) (chan *types.CacheImageMessage, error) {
logger := log.WithField("Calcium", "CacheImage").WithField("opts", opts)
if err := opts.Validate(); err != nil {
return nil, logger.Err(ctx, err)
return nil, logger.ErrWithTracing(ctx, err)
}
opts.Normalize()

nodes, err := c.filterNodes(ctx, types.NodeFilter{Podname: opts.Podname, Includes: opts.Nodenames})
if err != nil {
return nil, logger.Err(ctx, err)
return nil, logger.ErrWithTracing(ctx, err)
}

if len(nodes) == 0 {
return nil, logger.Err(ctx, errors.WithStack(types.ErrPodNoNodes))
return nil, logger.ErrWithTracing(ctx, errors.WithStack(types.ErrPodNoNodes))
}

ch := make(chan *types.CacheImageMessage)
Expand All @@ -114,7 +114,7 @@ func (c *Calcium) CacheImage(ctx context.Context, opts *types.ImageOptions) (cha
}
if err := pullImage(ctx, node, image); err != nil {
m.Success = false
m.Message = logger.Err(ctx, err).Error()
m.Message = logger.ErrWithTracing(ctx, err).Error()
}
ch <- m
}
Expand All @@ -137,11 +137,11 @@ func (c *Calcium) ListImage(ctx context.Context, opts *types.ImageOptions) (chan

nodes, err := c.filterNodes(ctx, types.NodeFilter{Podname: opts.Podname, Includes: opts.Nodenames})
if err != nil {
return nil, logger.Err(ctx, err)
return nil, logger.ErrWithTracing(ctx, err)
}

if len(nodes) == 0 {
return nil, logger.Err(ctx, errors.WithStack(types.ErrPodNoNodes))
return nil, logger.ErrWithTracing(ctx, errors.WithStack(types.ErrPodNoNodes))
}

ch := make(chan *types.ListImageMessage)
Expand All @@ -161,7 +161,7 @@ func (c *Calcium) ListImage(ctx context.Context, opts *types.ImageOptions) (chan
Error: nil,
}
if images, err := node.Engine.ImageList(ctx, opts.Filter); err != nil {
msg.Error = logger.Err(ctx, err)
msg.Error = logger.ErrWithTracing(ctx, err)
} else {
for _, image := range images {
msg.Images = append(msg.Images, &types.Image{
Expand Down
4 changes: 2 additions & 2 deletions cluster/calcium/lambda.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (c *Calcium) RunAndWait(ctx context.Context, opts *types.DeployOptions, inC

logger := log.WithField("Calcium", "RunAndWait").WithField("opts", opts)
if err := opts.Validate(); err != nil {
return workloadIDs, nil, logger.Err(ctx, err)
return workloadIDs, nil, logger.ErrWithTracing(ctx, err)
}
opts.Lambda = true
// count = 1 && OpenStdin
Expand Down Expand Up @@ -71,7 +71,7 @@ func (c *Calcium) RunAndWait(ctx context.Context, opts *types.DeployOptions, inC
if err != nil {
return &types.AttachWorkloadMessage{
WorkloadID: message.WorkloadID,
Data: []byte(fmt.Sprintf("Create wal failed: %s, %+v", message.WorkloadID, logger.Err(ctx, err))),
Data: []byte(fmt.Sprintf("Create wal failed: %s, %+v", message.WorkloadID, logger.ErrWithTracing(ctx, err))),
StdStreamType: types.EruError,
}
}
Expand Down
4 changes: 2 additions & 2 deletions cluster/calcium/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (c *Calcium) LogStream(ctx context.Context, opts *types.LogStreamOptions) (
defer close(ch)
workload, err := c.GetWorkload(ctx, opts.ID)
if err != nil {
ch <- &types.LogStreamMessage{ID: opts.ID, Error: logger.Err(ctx, err)}
ch <- &types.LogStreamMessage{ID: opts.ID, Error: logger.ErrWithTracing(ctx, err)}
return
}

Expand All @@ -32,7 +32,7 @@ func (c *Calcium) LogStream(ctx context.Context, opts *types.LogStreamOptions) (
Stderr: true,
})
if err != nil {
ch <- &types.LogStreamMessage{ID: opts.ID, Error: logger.Err(ctx, err)}
ch <- &types.LogStreamMessage{ID: opts.ID, Error: logger.ErrWithTracing(ctx, err)}
return
}

Expand Down
14 changes: 7 additions & 7 deletions cluster/calcium/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ func (c *Calcium) ListNetworks(ctx context.Context, podname string, driver strin
networks := []*enginetypes.Network{}
ch, err := c.ListPodNodes(ctx, &types.ListNodesOptions{Podname: podname})
if err != nil {
return networks, logger.Err(ctx, errors.WithStack(err))
return networks, logger.ErrWithTracing(ctx, errors.WithStack(err))
}

nodes := []*types.Node{}
for n := range ch {
nodes = append(nodes, n)
}
if len(nodes) == 0 {
return networks, logger.Err(ctx, errors.WithStack(types.NewDetailedErr(types.ErrPodNoNodes, podname)))
return networks, logger.ErrWithTracing(ctx, errors.WithStack(types.NewDetailedErr(types.ErrPodNoNodes, podname)))
}

drivers := []string{}
Expand All @@ -38,28 +38,28 @@ func (c *Calcium) ListNetworks(ctx context.Context, podname string, driver strin
node := nodes[0]

networks, err = node.Engine.NetworkList(ctx, drivers)
return networks, logger.Err(ctx, errors.WithStack(err))
return networks, logger.ErrWithTracing(ctx, errors.WithStack(err))
}

// ConnectNetwork connect to a network
func (c *Calcium) ConnectNetwork(ctx context.Context, network, target, ipv4, ipv6 string) ([]string, error) {
logger := log.WithField("Calcium", "ConnectNetwork").WithField("network", network).WithField("target", target).WithField("ipv4", ipv4).WithField("ipv6", ipv6)
workload, err := c.GetWorkload(ctx, target)
if err != nil {
return nil, logger.Err(ctx, errors.WithStack(err))
return nil, logger.ErrWithTracing(ctx, errors.WithStack(err))
}

networks, err := workload.Engine.NetworkConnect(ctx, network, target, ipv4, ipv6)
return networks, logger.Err(ctx, errors.WithStack(err))
return networks, logger.ErrWithTracing(ctx, errors.WithStack(err))
}

// DisconnectNetwork connect to a network
func (c *Calcium) DisconnectNetwork(ctx context.Context, network, target string, force bool) error {
logger := log.WithField("Calcium", "DisconnectNetwork").WithField("network", network).WithField("target", target).WithField("force", force)
workload, err := c.GetWorkload(ctx, target)
if err != nil {
return logger.Err(ctx, errors.WithStack(err))
return logger.ErrWithTracing(ctx, errors.WithStack(err))
}

return logger.Err(ctx, errors.WithStack(workload.Engine.NetworkDisconnect(ctx, network, target, force)))
return logger.ErrWithTracing(ctx, errors.WithStack(workload.Engine.NetworkDisconnect(ctx, network, target, force)))
}
Loading

0 comments on commit e343332

Please sign in to comment.