Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
WIP: simplify tracer
Browse files Browse the repository at this point in the history
Signed-off-by: Chelsea Mafrica <[email protected]>
  • Loading branch information
Chelsea Mafrica committed Oct 6, 2020
1 parent b0a0f13 commit 680c0a6
Showing 1 changed file with 44 additions and 89 deletions.
133 changes: 44 additions & 89 deletions containerd-shim-v2/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,8 @@ func New(ctx context.Context, id string, publisher events.Publisher) (cdshim.Shi
return nil, err
}
// create span
span := trace(ctx, tracer, "New")
if span != opentracing.Span(nil) {
defer span.Finish()
}
span := trace(ctx, "New")
defer span.Finish()
}

ctx, cancel := context.WithCancel(ctx)
Expand Down Expand Up @@ -188,10 +186,8 @@ func (s *service) StartShim(ctx context.Context, id, containerdBinary, container
// Stop tracing because a new tracer will be created when New is called again after StartShim
defer katautils.StopTracing(s.ctx)

span := trace(s.ctx, s.tracer, "StartShim")
if span != opentracing.Span(nil) {
defer span.Finish()
}
span := trace(s.ctx, "StartShim")
defer span.Finish()

bundlePath, err := os.Getwd()
if err != nil {
Expand Down Expand Up @@ -306,22 +302,15 @@ func getTopic(e interface{}) string {
return cdruntime.TaskUnknownTopic
}

func trace(ctx context.Context, tracer opentracing.Tracer, name string) opentracing.Span {
if tracer != opentracing.Tracer(nil) {
span, _ := opentracing.StartSpanFromContext(ctx, name)
span.SetTag("subsystem", "runtime")
return span
}
err := "TRACING: tracer nil (" + name + ")"
logrus.Error(err)
return opentracing.Span(nil)
func trace(ctx context.Context, name string) opentracing.Span {
span, _ := opentracing.StartSpanFromContext(ctx, name)
span.SetTag("subsystem", "runtime")
return span
}

func (s *service) Cleanup(ctx context.Context) (_ *taskAPI.DeleteResponse, err error) {
span := trace(s.ctx, s.tracer, "Cleanup")
if span != opentracing.Span(nil) {
defer span.Finish()
}
span := trace(s.ctx, "Cleanup")
defer span.Finish()

//Since the binary cleanup will return the DeleteResponse from stdout to
//containerd, thus we must make sure there is no any outputs in stdout except
Expand Down Expand Up @@ -381,10 +370,8 @@ func (s *service) Create(ctx context.Context, r *taskAPI.CreateTaskRequest) (_ *
// create span
// Note: runtime config is overwritten in create(), which might be contributing
// to opentracing web ui reporting errors in createSandbox
span := trace(s.ctx, s.tracer, "Create")
if span != opentracing.Span(nil) {
defer span.Finish()
}
span := trace(s.ctx, "Create")
defer span.Finish()

defer func() {
err = toGRPC(err)
Expand Down Expand Up @@ -432,10 +419,8 @@ func (s *service) Start(ctx context.Context, r *taskAPI.StartRequest) (_ *taskAP
s.mu.Lock()
defer s.mu.Unlock()

span := trace(s.ctx, s.tracer, "Start")
if span != opentracing.Span(nil) {
defer span.Finish()
}
span := trace(s.ctx, "Start")
defer span.Finish()

c, err := s.getContainer(r.ID)
if err != nil {
Expand Down Expand Up @@ -484,10 +469,8 @@ func (s *service) Delete(ctx context.Context, r *taskAPI.DeleteRequest) (_ *task
defer s.mu.Unlock()

defer katautils.StopTracing(s.ctx)
span := trace(s.ctx, s.tracer, "Delete")
if span != opentracing.Span(nil) {
defer span.Finish()
}
span := trace(s.ctx, "Delete")
defer span.Finish()

c, err := s.getContainer(r.ID)
if err != nil {
Expand Down Expand Up @@ -536,10 +519,8 @@ func (s *service) Exec(ctx context.Context, r *taskAPI.ExecProcessRequest) (_ *p
s.mu.Lock()
defer s.mu.Unlock()

span := trace(s.ctx, s.tracer, "Exec")
if span != opentracing.Span(nil) {
defer span.Finish()
}
span := trace(s.ctx, "Exec")
defer span.Finish()

c, err := s.getContainer(r.ID)
if err != nil {
Expand Down Expand Up @@ -574,10 +555,8 @@ func (s *service) ResizePty(ctx context.Context, r *taskAPI.ResizePtyRequest) (_
s.mu.Lock()
defer s.mu.Unlock()

span := trace(s.ctx, s.tracer, "ResizePty")
if span != opentracing.Span(nil) {
defer span.Finish()
}
span := trace(s.ctx, "ResizePty")
defer span.Finish()

c, err := s.getContainer(r.ID)
if err != nil {
Expand Down Expand Up @@ -613,10 +592,8 @@ func (s *service) State(ctx context.Context, r *taskAPI.StateRequest) (_ *taskAP
s.mu.Lock()
defer s.mu.Unlock()

span := trace(s.ctx, s.tracer, "State")
if span != opentracing.Span(nil) {
defer span.Finish()
}
span := trace(s.ctx, "State")
defer span.Finish()

c, err := s.getContainer(r.ID)
if err != nil {
Expand Down Expand Up @@ -665,10 +642,8 @@ func (s *service) Pause(ctx context.Context, r *taskAPI.PauseRequest) (_ *ptypes
s.mu.Lock()
defer s.mu.Unlock()

span := trace(s.ctx, s.tracer, "Pause")
if span != opentracing.Span(nil) {
defer span.Finish()
}
span := trace(s.ctx, "Pause")
defer span.Finish()

c, err := s.getContainer(r.ID)
if err != nil {
Expand Down Expand Up @@ -704,10 +679,8 @@ func (s *service) Resume(ctx context.Context, r *taskAPI.ResumeRequest) (_ *ptyp
s.mu.Lock()
defer s.mu.Unlock()

span := trace(s.ctx, s.tracer, "Resume")
if span != opentracing.Span(nil) {
defer span.Finish()
}
span := trace(s.ctx, "Resume")
defer span.Finish()

c, err := s.getContainer(r.ID)
if err != nil {
Expand Down Expand Up @@ -741,10 +714,8 @@ func (s *service) Kill(ctx context.Context, r *taskAPI.KillRequest) (_ *ptypes.E
s.mu.Lock()
defer s.mu.Unlock()

span := trace(s.ctx, s.tracer, "Kill")
if span != opentracing.Span(nil) {
defer span.Finish()
}
span := trace(s.ctx, "Kill")
defer span.Finish()

signum := syscall.Signal(r.Signal)

Expand Down Expand Up @@ -800,10 +771,8 @@ func (s *service) Pids(ctx context.Context, r *taskAPI.PidsRequest) (_ *taskAPI.
s.mu.Lock()
defer s.mu.Unlock()

span := trace(s.ctx, s.tracer, "Pids")
if span != opentracing.Span(nil) {
defer span.Finish()
}
span := trace(s.ctx, "Pids")
defer span.Finish()

pInfo := task.ProcessInfo{
Pid: s.pid,
Expand All @@ -824,10 +793,8 @@ func (s *service) CloseIO(ctx context.Context, r *taskAPI.CloseIORequest) (_ *pt
s.mu.Lock()
defer s.mu.Unlock()

span := trace(s.ctx, s.tracer, "CloseIO")
if span != opentracing.Span(nil) {
defer span.Finish()
}
span := trace(s.ctx, "CloseIO")
defer span.Finish()

c, err := s.getContainer(r.ID)
if err != nil {
Expand Down Expand Up @@ -865,10 +832,8 @@ func (s *service) Checkpoint(ctx context.Context, r *taskAPI.CheckpointTaskReque
s.mu.Lock()
defer s.mu.Unlock()

span := trace(s.ctx, s.tracer, "Checkpoint")
if span != opentracing.Span(nil) {
defer span.Finish()
}
span := trace(s.ctx, "Checkpoint")
defer span.Finish()

return nil, errdefs.ToGRPCf(errdefs.ErrNotImplemented, "service Checkpoint")
}
Expand All @@ -882,10 +847,8 @@ func (s *service) Connect(ctx context.Context, r *taskAPI.ConnectRequest) (_ *ta
s.mu.Lock()
defer s.mu.Unlock()

span := trace(s.ctx, s.tracer, "Connect")
if span != opentracing.Span(nil) {
defer span.Finish()
}
span := trace(s.ctx, "Connect")
defer span.Finish()

return &taskAPI.ConnectResponse{
ShimPid: s.pid,
Expand All @@ -901,10 +864,8 @@ func (s *service) Shutdown(ctx context.Context, r *taskAPI.ShutdownRequest) (_ *

s.mu.Lock()

span := trace(s.ctx, s.tracer, "Shutdown")
if span != opentracing.Span(nil) {
defer span.Finish()
}
span := trace(s.ctx, "Shutdown")
defer span.Finish()

if len(s.containers) != 0 {
s.mu.Unlock()
Expand All @@ -931,10 +892,8 @@ func (s *service) Stats(ctx context.Context, r *taskAPI.StatsRequest) (_ *taskAP
s.mu.Lock()
defer s.mu.Unlock()

span := trace(s.ctx, s.tracer, "Stats")
if span != opentracing.Span(nil) {
defer span.Finish()
}
span := trace(s.ctx, "Stats")
defer span.Finish()

c, err := s.getContainer(r.ID)
if err != nil {
Expand All @@ -960,10 +919,8 @@ func (s *service) Update(ctx context.Context, r *taskAPI.UpdateTaskRequest) (_ *
s.mu.Lock()
defer s.mu.Unlock()

span := trace(s.ctx, s.tracer, "Update")
if span != opentracing.Span(nil) {
defer span.Finish()
}
span := trace(s.ctx, "Update")
defer span.Finish()

var resources *specs.LinuxResources
v, err := typeurl.UnmarshalAny(r.Resources)
Expand Down Expand Up @@ -991,10 +948,8 @@ func (s *service) Wait(ctx context.Context, r *taskAPI.WaitRequest) (_ *taskAPI.
err = toGRPC(err)
}()

span := trace(s.ctx, s.tracer, "Wait")
if span != opentracing.Span(nil) {
defer span.Finish()
}
span := trace(s.ctx, "Wait")
defer span.Finish()

s.mu.Lock()
c, err := s.getContainer(r.ID)
Expand Down

0 comments on commit 680c0a6

Please sign in to comment.