-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mengyang He
committed
Apr 25, 2024
1 parent
e6af90e
commit 20c32df
Showing
4 changed files
with
138 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package tracing | ||
|
||
import ( | ||
"log" | ||
"sync" | ||
) | ||
|
||
// BackTraceTracer can record tasks incomplete tasks | ||
type BackTraceTracer struct { | ||
tracingTasks map[string]Task | ||
lock sync.Mutex | ||
} | ||
|
||
// NewBackTraceTracer creates a new BackTraceTracer | ||
func NewBackTraceTracer() *BackTraceTracer { | ||
t := &BackTraceTracer{ | ||
tracingTasks: make(map[string]Task), | ||
} | ||
|
||
return t | ||
} | ||
|
||
func (t *BackTraceTracer) StartTask(task Task) { | ||
t.lock.Lock() | ||
defer t.lock.Unlock() | ||
|
||
t.tracingTasks[task.ID] = task | ||
} | ||
|
||
func (t *BackTraceTracer) StepTask(task Task) { | ||
// Do Nothing | ||
} | ||
|
||
func (t *BackTraceTracer) EndTask(task Task) { | ||
t.lock.Lock() | ||
defer t.lock.Unlock() | ||
|
||
delete(t.tracingTasks, task.ID) | ||
} | ||
|
||
func (t *BackTraceTracer) DumpBackTrace(task Task) { | ||
log.Printf("Task %s", task.ID) | ||
|
||
if task.ParentID == "" { | ||
return | ||
} | ||
|
||
t.DumpBackTrace(t.tracingTasks[task.ParentID]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package tracing | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("BackTraceTracer", func() { | ||
var ( | ||
t *BackTraceTracer | ||
) | ||
|
||
BeforeEach(func() { | ||
t = NewBackTraceTracer() | ||
}) | ||
|
||
It("should trace a single task", func() { | ||
t.StartTask(Task{ID: "1"}) | ||
|
||
Expect(len(t.tracingTasks)).To(Equal(1)) | ||
Expect(t.tracingTasks["1"].ParentID).To(Equal("")) | ||
}) | ||
|
||
It("should trace two tasks", func() { | ||
t.StartTask(Task{ID: "1"}) | ||
t.StartTask(Task{ID: "2", ParentID: "1"}) | ||
|
||
Expect(len(t.tracingTasks)).To(Equal(2)) | ||
Expect(t.tracingTasks["1"].ParentID).To(Equal("")) | ||
Expect(t.tracingTasks["2"].ParentID).To(Equal("1")) | ||
}) | ||
|
||
It("should trace three tasks", func() { | ||
t.StartTask(Task{ID: "1"}) | ||
t.StartTask(Task{ID: "2", ParentID: "1"}) | ||
t.StartTask(Task{ID: "3", ParentID: "2"}) | ||
|
||
Expect(len(t.tracingTasks)).To(Equal(3)) | ||
Expect(t.tracingTasks["1"].ParentID).To(Equal("")) | ||
Expect(t.tracingTasks["2"].ParentID).To(Equal("1")) | ||
Expect(t.tracingTasks["3"].ParentID).To(Equal("2")) | ||
}) | ||
|
||
It("should end a single task", func() { | ||
t.StartTask(Task{ID: "1"}) | ||
|
||
t.EndTask(Task{ID: "1"}) | ||
|
||
Expect(len(t.tracingTasks)).To(Equal(0)) | ||
}) | ||
|
||
It("should end two tasks", func() { | ||
t.StartTask(Task{ID: "1"}) | ||
t.StartTask(Task{ID: "2", ParentID: "1"}) | ||
t.StartTask(Task{ID: "3", ParentID: "2"}) | ||
|
||
t.EndTask(Task{ID: "3", ParentID: "2"}) | ||
t.EndTask(Task{ID: "2", ParentID: "1"}) | ||
|
||
Expect(len(t.tracingTasks)).To(Equal(1)) | ||
Expect(t.tracingTasks["1"].ParentID).To(Equal("")) | ||
}) | ||
|
||
It("should print single tasks", func() { | ||
t.StartTask(Task{ID: "1"}) | ||
|
||
t.DumpBackTrace(Task{ID: "1"}) | ||
}) | ||
|
||
It("should print three tasks", func() { | ||
t.StartTask(Task{ID: "1"}) | ||
t.StartTask(Task{ID: "2", ParentID: "1"}) | ||
t.StartTask(Task{ID: "3", ParentID: "2"}) | ||
|
||
t.DumpBackTrace(Task{ID: "3", ParentID: "2"}) | ||
}) | ||
}) |