Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
fujiwara committed Aug 16, 2024
1 parent b374888 commit a8ac224
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 38 deletions.
58 changes: 31 additions & 27 deletions tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type Tracer struct {
option *RunOption
}

func (t *Tracer) AddEvent(ts *time.Time, source, message string) {
func (t *Tracer) AddEvent(ts time.Time, source, message string) {
t.timeline.Add(newEvent(ts, source, message))
}

Expand All @@ -63,36 +63,40 @@ type Timeline struct {
}

func (tl *Timeline) Add(event *TimeLineEvent) {
if event.Timestamp.IsZero() { // ignore zero time event
return
}
tl.mu.Lock()
defer tl.mu.Unlock()
tl.events = append(tl.events, event)
}

func (tl *Timeline) Print(w io.Writer) {
func (tl *Timeline) Print(w io.Writer) (int, error) {
tl.mu.Lock()
defer tl.mu.Unlock()

tls := make([]*TimeLineEvent, 0, len(tl.events))
for _, e := range tl.events {
if e.Timestamp == nil {
continue
}
tls = append(tls, e)
}
tls = append(tls, tl.events...)
sort.SliceStable(tls, func(i, j int) bool {
return (*tls[i].Timestamp).Before(*tls[j].Timestamp)
return tls[i].Timestamp.Before(tls[j].Timestamp)
})
n := 0
for _, e := range tls {
s := e.String()
if !tl.seen[s] {
fmt.Fprint(w, e.String())
l, err := fmt.Fprint(w, e.String())
if err != nil {
return n, err
}
n += l
tl.seen[s] = true
}
}
return n, nil
}

type TimeLineEvent struct {
Timestamp *time.Time
Timestamp time.Time
Source string
Message string
}
Expand Down Expand Up @@ -122,7 +126,7 @@ func NewWithConfig(config aws.Config) (*Tracer, error) {
}, nil
}

func newEvent(ts *time.Time, src, msg string) *TimeLineEvent {
func newEvent(ts time.Time, src, msg string) *TimeLineEvent {
return &TimeLineEvent{
Timestamp: ts,
Source: src,
Expand Down Expand Up @@ -243,18 +247,18 @@ func (t *Tracer) traceTask(ctx context.Context, cluster string, taskID string) (
t.fetchServiceEvents(ctx, cluster, taskGroup[1])
}

t.AddEvent(task.CreatedAt, "TASK", "Created")
t.AddEvent(task.ConnectivityAt, "TASK", "Connected")
t.AddEvent(task.StartedAt, "TASK", "Started")
t.AddEvent(task.PullStartedAt, "TASK", "Pull started")
t.AddEvent(task.PullStoppedAt, "TASK", "Pull stopped")
t.AddEvent(task.StoppedAt, "TASK", "Stopped")
t.AddEvent(task.StoppingAt, "TASK", "Stopping")
t.AddEvent(aws.ToTime(task.CreatedAt), "TASK", "Created")
t.AddEvent(aws.ToTime(task.ConnectivityAt), "TASK", "Connected")
t.AddEvent(aws.ToTime(task.StartedAt), "TASK", "Started")
t.AddEvent(aws.ToTime(task.PullStartedAt), "TASK", "Pull started")
t.AddEvent(aws.ToTime(task.PullStoppedAt), "TASK", "Pull stopped")
t.AddEvent(aws.ToTime(task.StoppedAt), "TASK", "Stopped")
t.AddEvent(aws.ToTime(task.StoppingAt), "TASK", "Stopping")
if task.StoppedReason != nil {
t.AddEvent(task.StoppingAt, "TASK", "StoppedReason:"+*task.StoppedReason)
t.AddEvent(aws.ToTime(task.StoppingAt), "TASK", "StoppedReason:"+aws.ToString(task.StoppedReason))
}
t.AddEvent(task.StoppingAt, "TASK", "StoppedCode:"+string(task.StopCode))
t.AddEvent(task.ExecutionStoppedAt, "TASK", "Execution stopped")
t.AddEvent(aws.ToTime(task.StoppingAt), "TASK", "StoppedCode:"+string(task.StopCode))
t.AddEvent(aws.ToTime(task.ExecutionStoppedAt), "TASK", "Execution stopped")

for _, c := range task.Containers {
containerName := *c.Name
Expand All @@ -265,10 +269,10 @@ func (t *Tracer) traceTask(ctx context.Context, cluster string, taskID string) (
if c.Reason != nil {
msg += fmt.Sprintf(" (reason: %s)", *c.Reason)
}
t.AddEvent(&t.now, "CONTAINER:"+containerName, msg)
t.AddEvent(t.now, "CONTAINER:"+containerName, msg)
}

t.AddEvent(&t.now, "TASK", "LastStatus:"+aws.ToString(task.LastStatus))
t.AddEvent(t.now, "TASK", "LastStatus:"+aws.ToString(task.LastStatus))

return &task, nil
}
Expand Down Expand Up @@ -325,9 +329,9 @@ func (t *Tracer) fetchServiceEvents(ctx context.Context, cluster, service string
return fmt.Errorf("no services found: %w", err)
}
for _, e := range res.Services[0].Events {
ts := *e.CreatedAt
ts := aws.ToTime(e.CreatedAt)
if ts.After(t.headBegin) && ts.Before(t.headEnd) || ts.After(t.tailBegin) && ts.Before(t.tailEnd) {
t.AddEvent(e.CreatedAt, "SERVICE", *e.Message)
t.AddEvent(ts, "SERVICE", aws.ToString(e.Message))
}
}
return nil
Expand Down Expand Up @@ -362,7 +366,7 @@ func (t *Tracer) fetchLogs(ctx context.Context, containerName, group, stream str
fetched++
for _, e := range res.Events {
ts := msecToTime(aws.ToInt64(e.Timestamp))
t.AddEvent(&ts, "CONTAINER:"+containerName, aws.ToString(e.Message))
t.AddEvent(ts, "CONTAINER:"+containerName, aws.ToString(e.Message))
}
if aws.ToString(nextToken) == aws.ToString(res.NextForwardToken) {
break
Expand Down
30 changes: 19 additions & 11 deletions tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,44 @@ import (
"testing"
"time"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/fujiwara/tracer"
)

func ptr[T any](v T) *T {
return &v
}

var (
testEvents = []tracer.TimeLineEvent{
{
Timestamp: ptr(time.Date(2021, 1, 2, 3, 4, 5, 123_999_000, time.UTC)),
Timestamp: time.Date(2021, 1, 2, 3, 4, 5, 123_999_000, time.UTC),
Message: "test message 1",
Source: "test_source 1",
},
{
Timestamp: ptr(time.Date(2021, 1, 2, 3, 4, 5, 123_999_999, time.UTC)),
Timestamp: time.Date(2021, 1, 2, 3, 4, 5, 123_999_999, time.UTC),
Message: "test message 5",
Source: "test_source 5",
},
{
Timestamp: ptr(time.Date(2021, 1, 2, 3, 4, 6, 123_999_000, time.UTC)),
Timestamp: time.Date(2021, 1, 2, 3, 4, 6, 123_999_000, time.UTC),
Message: "test message 2",
Source: "test_source 2",
},
{
// same timestamp to test sort stable
Timestamp: ptr(time.Date(2021, 1, 2, 3, 4, 5, 123_999_000, time.UTC)),
Timestamp: time.Date(2021, 1, 2, 3, 4, 5, 123_999_000, time.UTC),
Message: "test message 3",
Source: "test_source 3",
},
{
// duplicate event
Timestamp: ptr(time.Date(2021, 1, 2, 3, 4, 5, 123_999_000, time.UTC)),
Timestamp: time.Date(2021, 1, 2, 3, 4, 5, 123_999_000, time.UTC),
Message: "test message 3",
Source: "test_source 3",
},
{
Timestamp: aws.ToTime(nil),
Message: "test message ignored",
Source: "test_source ignored",
},
}
expectedOutput = `2021-01-02T03:04:05.123Z test_source 1 test message 1
2021-01-02T03:04:05.123Z test_source 3 test message 3
Expand All @@ -53,7 +55,7 @@ func TestTimeLineEvent(t *testing.T) {
t.Setenv("TZ", "UTC")
now := time.Date(2021, 1, 2, 3, 4, 5, 123_999_000, time.UTC)
ev := tracer.TimeLineEvent{
Timestamp: &now,
Timestamp: now,
Message: "test message",
Source: "test_source",
}
Expand All @@ -70,7 +72,13 @@ func TestTimeLine(t *testing.T) {
tl.Add(&ev)
}
b := new(strings.Builder)
tl.Print(b)
n, err := tl.Print(b)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if n != len(expectedOutput) {
t.Errorf("unexpected length: %d", n)
}
if b.String() != expectedOutput {
t.Errorf("unexpected output: %s", b.String())
}
Expand Down

0 comments on commit a8ac224

Please sign in to comment.