Skip to content

Commit b25fb56

Browse files
authored
Merge pull request #232 from kzys/log-fields
Set log fields consistently
2 parents 2578f3d + fcada9a commit b25fb56

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

runtime/service.go

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ func (s *service) StartShim(shimCtx context.Context, containerID, containerdBina
284284

285285
logrus.SetOutput(logFifo)
286286

287-
log := log.G(shimCtx).WithField("id", containerID)
287+
log := log.G(shimCtx).WithField("task_id", containerID)
288288
log.Debug("StartShim")
289289

290290
// If we are running a shim start routine, we can safely assume our current working
@@ -316,14 +316,18 @@ func (s *service) StartShim(shimCtx context.Context, containerID, containerdBina
316316

317317
s.vmID = uuid.String()
318318

319+
// This request is handled by a short-lived shim process to find its control socket.
320+
// A long-running shim process won't have the request. So, setting s.logger doesn't affect others.
321+
log = log.WithField("vmID", s.vmID)
322+
319323
// If the client didn't specify a VMID, this is a single-task VM and should thus exit after this
320324
// task is deleted
321325
containerCount = 1
322326
exitAfterAllTasksDeleted = true
323327

324-
log.Infof("will start a single-task VM %s since no VMID has been provided", s.vmID)
328+
log.Info("will start a single-task VM since no VMID has been provided")
325329
} else {
326-
log.Infof("will start a persistent VM %s", s.vmID)
330+
log.Info("will start a persistent VM")
327331
}
328332

329333
client, err := ttrpcutil.NewClient(containerdAddress + ".ttrpc")
@@ -661,7 +665,7 @@ func (s *service) buildVMConfiguration(req *proto.CreateVMRequest) (*firecracker
661665
}
662666

663667
func (s *service) Create(requestCtx context.Context, request *taskAPI.CreateTaskRequest) (*taskAPI.CreateTaskResponse, error) {
664-
logger := s.logger.WithField("containerID", request.ID)
668+
logger := s.logger.WithField("task_id", request.ID)
665669
defer logPanicAndDie(logger)
666670

667671
err := s.waitVMReady()
@@ -767,7 +771,7 @@ func (s *service) Create(requestCtx context.Context, request *taskAPI.CreateTask
767771
func (s *service) Start(requestCtx context.Context, req *taskAPI.StartRequest) (*taskAPI.StartResponse, error) {
768772
defer logPanicAndDie(log.G(requestCtx))
769773

770-
log.G(requestCtx).WithFields(logrus.Fields{"id": req.ID, "exec_id": req.ExecID}).Debug("start")
774+
log.G(requestCtx).WithFields(logrus.Fields{"task_id": req.ID, "exec_id": req.ExecID}).Debug("start")
771775
resp, err := s.agentClient.Start(requestCtx, req)
772776
if err != nil {
773777
return nil, err
@@ -778,7 +782,7 @@ func (s *service) Start(requestCtx context.Context, req *taskAPI.StartRequest) (
778782

779783
func (s *service) Delete(requestCtx context.Context, req *taskAPI.DeleteRequest) (*taskAPI.DeleteResponse, error) {
780784
defer logPanicAndDie(log.G(requestCtx))
781-
logger := log.G(requestCtx).WithFields(logrus.Fields{"id": req.ID, "exec_id": req.ExecID})
785+
logger := log.G(requestCtx).WithFields(logrus.Fields{"task_id": req.ID, "exec_id": req.ExecID})
782786

783787
logger.Debug("delete")
784788

@@ -814,7 +818,7 @@ func (s *service) Delete(requestCtx context.Context, req *taskAPI.DeleteRequest)
814818
// Exec an additional process inside the container
815819
func (s *service) Exec(requestCtx context.Context, req *taskAPI.ExecProcessRequest) (*ptypes.Empty, error) {
816820
defer logPanicAndDie(log.G(requestCtx))
817-
logger := s.logger.WithField("TaskID", req.ID).WithField("ExecID", req.ExecID)
821+
logger := s.logger.WithField("task_id", req.ID).WithField("exec_id", req.ExecID)
818822
logger.Debug("exec")
819823

820824
// no OCI config bytes or DriveID to provide for Exec, just leave those fields empty
@@ -876,7 +880,7 @@ func (s *service) Exec(requestCtx context.Context, req *taskAPI.ExecProcessReque
876880
func (s *service) ResizePty(requestCtx context.Context, req *taskAPI.ResizePtyRequest) (*ptypes.Empty, error) {
877881
defer logPanicAndDie(log.G(requestCtx))
878882

879-
log.G(requestCtx).WithFields(logrus.Fields{"id": req.ID, "exec_id": req.ExecID}).Debug("resize_pty")
883+
log.G(requestCtx).WithFields(logrus.Fields{"task_id": req.ID, "exec_id": req.ExecID}).Debug("resize_pty")
880884
resp, err := s.agentClient.ResizePty(requestCtx, req)
881885
if err != nil {
882886
return nil, err
@@ -889,7 +893,7 @@ func (s *service) ResizePty(requestCtx context.Context, req *taskAPI.ResizePtyRe
889893
func (s *service) State(requestCtx context.Context, req *taskAPI.StateRequest) (*taskAPI.StateResponse, error) {
890894
defer logPanicAndDie(log.G(requestCtx))
891895

892-
log.G(requestCtx).WithFields(logrus.Fields{"id": req.ID, "exec_id": req.ExecID}).Debug("state")
896+
log.G(requestCtx).WithFields(logrus.Fields{"task_id": req.ID, "exec_id": req.ExecID}).Debug("state")
893897
resp, err := s.agentClient.State(requestCtx, req)
894898
if err != nil {
895899
return nil, err
@@ -902,7 +906,7 @@ func (s *service) State(requestCtx context.Context, req *taskAPI.StateRequest) (
902906
func (s *service) Pause(requestCtx context.Context, req *taskAPI.PauseRequest) (*ptypes.Empty, error) {
903907
defer logPanicAndDie(log.G(requestCtx))
904908

905-
log.G(requestCtx).WithField("id", req.ID).Debug("pause")
909+
log.G(requestCtx).WithField("task_id", req.ID).Debug("pause")
906910
resp, err := s.agentClient.Pause(requestCtx, req)
907911
if err != nil {
908912
return nil, err
@@ -915,7 +919,7 @@ func (s *service) Pause(requestCtx context.Context, req *taskAPI.PauseRequest) (
915919
func (s *service) Resume(requestCtx context.Context, req *taskAPI.ResumeRequest) (*ptypes.Empty, error) {
916920
defer logPanicAndDie(log.G(requestCtx))
917921

918-
log.G(requestCtx).WithField("id", req.ID).Debug("resume")
922+
log.G(requestCtx).WithField("task_id", req.ID).Debug("resume")
919923
resp, err := s.agentClient.Resume(requestCtx, req)
920924
if err != nil {
921925
return nil, err
@@ -928,7 +932,7 @@ func (s *service) Resume(requestCtx context.Context, req *taskAPI.ResumeRequest)
928932
func (s *service) Kill(requestCtx context.Context, req *taskAPI.KillRequest) (*ptypes.Empty, error) {
929933
defer logPanicAndDie(log.G(requestCtx))
930934

931-
log.G(requestCtx).WithFields(logrus.Fields{"id": req.ID, "exec_id": req.ExecID}).Debug("kill")
935+
log.G(requestCtx).WithFields(logrus.Fields{"task_id": req.ID, "exec_id": req.ExecID}).Debug("kill")
932936
resp, err := s.agentClient.Kill(requestCtx, req)
933937
if err != nil {
934938
return nil, err
@@ -940,7 +944,7 @@ func (s *service) Kill(requestCtx context.Context, req *taskAPI.KillRequest) (*p
940944
func (s *service) Pids(requestCtx context.Context, req *taskAPI.PidsRequest) (*taskAPI.PidsResponse, error) {
941945
defer logPanicAndDie(log.G(requestCtx))
942946

943-
log.G(requestCtx).WithField("id", req.ID).Debug("pids")
947+
log.G(requestCtx).WithField("task_id", req.ID).Debug("pids")
944948
resp, err := s.agentClient.Pids(requestCtx, req)
945949
if err != nil {
946950
return nil, err
@@ -953,7 +957,7 @@ func (s *service) Pids(requestCtx context.Context, req *taskAPI.PidsRequest) (*t
953957
func (s *service) CloseIO(requestCtx context.Context, req *taskAPI.CloseIORequest) (*ptypes.Empty, error) {
954958
defer logPanicAndDie(log.G(requestCtx))
955959

956-
log.G(requestCtx).WithFields(logrus.Fields{"id": req.ID, "exec_id": req.ExecID}).Debug("close_io")
960+
log.G(requestCtx).WithFields(logrus.Fields{"task_id": req.ID, "exec_id": req.ExecID}).Debug("close_io")
957961
resp, err := s.agentClient.CloseIO(requestCtx, req)
958962
if err != nil {
959963
return nil, err
@@ -966,7 +970,7 @@ func (s *service) CloseIO(requestCtx context.Context, req *taskAPI.CloseIOReques
966970
func (s *service) Checkpoint(requestCtx context.Context, req *taskAPI.CheckpointTaskRequest) (*ptypes.Empty, error) {
967971
defer logPanicAndDie(log.G(requestCtx))
968972

969-
log.G(requestCtx).WithFields(logrus.Fields{"id": req.ID, "path": req.Path}).Info("checkpoint")
973+
log.G(requestCtx).WithFields(logrus.Fields{"task_id": req.ID, "path": req.Path}).Info("checkpoint")
970974
resp, err := s.agentClient.Checkpoint(requestCtx, req)
971975
if err != nil {
972976
return nil, err
@@ -979,7 +983,7 @@ func (s *service) Checkpoint(requestCtx context.Context, req *taskAPI.Checkpoint
979983
func (s *service) Connect(requestCtx context.Context, req *taskAPI.ConnectRequest) (*taskAPI.ConnectResponse, error) {
980984
defer logPanicAndDie(log.G(requestCtx))
981985

982-
log.G(requestCtx).WithField("id", req.ID).Debug("connect")
986+
log.G(requestCtx).WithField("task_id", req.ID).Debug("connect")
983987
resp, err := s.agentClient.Connect(requestCtx, req)
984988
if err != nil {
985989
return nil, err
@@ -999,7 +1003,7 @@ func (s *service) Connect(requestCtx context.Context, req *taskAPI.ConnectReques
9991003
// Shutdown is not directly exposed to containerd clients.
10001004
func (s *service) Shutdown(requestCtx context.Context, req *taskAPI.ShutdownRequest) (*ptypes.Empty, error) {
10011005
defer logPanicAndDie(log.G(requestCtx))
1002-
s.logger.WithFields(logrus.Fields{"id": req.ID, "now": req.Now}).Debug("shutdown")
1006+
s.logger.WithFields(logrus.Fields{"task_id": req.ID, "now": req.Now}).Debug("shutdown")
10031007

10041008
shouldShutdown := req.Now || s.exitAfterAllTasksDeleted && s.taskManager.ShutdownIfEmpty()
10051009
if !shouldShutdown {
@@ -1040,7 +1044,7 @@ func (s *service) Shutdown(requestCtx context.Context, req *taskAPI.ShutdownRequ
10401044

10411045
func (s *service) Stats(requestCtx context.Context, req *taskAPI.StatsRequest) (*taskAPI.StatsResponse, error) {
10421046
defer logPanicAndDie(log.G(requestCtx))
1043-
log.G(requestCtx).WithField("id", req.ID).Debug("stats")
1047+
log.G(requestCtx).WithField("task_id", req.ID).Debug("stats")
10441048

10451049
resp, err := s.agentClient.Stats(requestCtx, req)
10461050
if err != nil {
@@ -1053,7 +1057,7 @@ func (s *service) Stats(requestCtx context.Context, req *taskAPI.StatsRequest) (
10531057
// Update a running container
10541058
func (s *service) Update(requestCtx context.Context, req *taskAPI.UpdateTaskRequest) (*ptypes.Empty, error) {
10551059
defer logPanicAndDie(log.G(requestCtx))
1056-
log.G(requestCtx).WithField("id", req.ID).Debug("update")
1060+
log.G(requestCtx).WithField("task_id", req.ID).Debug("update")
10571061

10581062
resp, err := s.agentClient.Update(requestCtx, req)
10591063
if err != nil {
@@ -1066,7 +1070,7 @@ func (s *service) Update(requestCtx context.Context, req *taskAPI.UpdateTaskRequ
10661070
// Wait for a process to exit
10671071
func (s *service) Wait(requestCtx context.Context, req *taskAPI.WaitRequest) (*taskAPI.WaitResponse, error) {
10681072
defer logPanicAndDie(log.G(requestCtx))
1069-
log.G(requestCtx).WithFields(logrus.Fields{"id": req.ID, "exec_id": req.ExecID}).Debug("wait")
1073+
log.G(requestCtx).WithFields(logrus.Fields{"task_id": req.ID, "exec_id": req.ExecID}).Debug("wait")
10701074

10711075
resp, err := s.agentClient.Wait(requestCtx, req)
10721076
if err != nil {

0 commit comments

Comments
 (0)