-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #162 from ymtdzzz/feature/sort_by_span_latency
Add feature of sorting by the span latency
- Loading branch information
Showing
13 changed files
with
334 additions
and
22 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
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,60 @@ | ||
package telemetry | ||
|
||
import "sort" | ||
|
||
const ( | ||
SORT_TYPE_NONE SortType = "none" | ||
SORT_TYPE_LATENCY_DESC SortType = "latency-desc" | ||
SORT_TYPE_LATENCY_ASC SortType = "latency-asc" | ||
) | ||
|
||
// SortType is sort type | ||
type SortType string | ||
|
||
func (t SortType) IsNone() bool { | ||
return t == SORT_TYPE_NONE | ||
} | ||
|
||
func (t SortType) IsDesc() bool { | ||
return t == SORT_TYPE_LATENCY_DESC | ||
} | ||
|
||
func (t SortType) GetHeaderLabel() string { | ||
switch t { | ||
case SORT_TYPE_LATENCY_DESC: | ||
return "Latency" | ||
case SORT_TYPE_LATENCY_ASC: | ||
return "Latency" | ||
} | ||
return "N/A" | ||
} | ||
|
||
func sortSvcSpans(svcSpans SvcSpans, sortType SortType) { | ||
switch sortType { | ||
case SORT_TYPE_NONE: | ||
sort.Slice(svcSpans, func(i, j int) bool { | ||
// default sort is received_at asc | ||
return svcSpans[i].ReceivedAt.Before(svcSpans[j].ReceivedAt) | ||
}) | ||
case SORT_TYPE_LATENCY_DESC: | ||
sort.Slice(svcSpans, func(i, j int) bool { | ||
istart := svcSpans[i].Span.StartTimestamp().AsTime() | ||
iend := svcSpans[i].Span.EndTimestamp().AsTime() | ||
iduration := iend.Sub(istart) | ||
jstart := svcSpans[j].Span.StartTimestamp().AsTime() | ||
jend := svcSpans[j].Span.EndTimestamp().AsTime() | ||
jduration := jend.Sub(jstart) | ||
return iduration > jduration | ||
}) | ||
case SORT_TYPE_LATENCY_ASC: | ||
sort.Slice(svcSpans, func(i, j int) bool { | ||
istart := svcSpans[i].Span.StartTimestamp().AsTime() | ||
iend := svcSpans[i].Span.EndTimestamp().AsTime() | ||
iduration := iend.Sub(istart) | ||
jstart := svcSpans[j].Span.StartTimestamp().AsTime() | ||
jend := svcSpans[j].Span.EndTimestamp().AsTime() | ||
jduration := jend.Sub(jstart) | ||
return iduration < jduration | ||
}) | ||
} | ||
} |
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,150 @@ | ||
package telemetry | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/ymtdzzz/otel-tui/tuiexporter/internal/test" | ||
) | ||
|
||
func TestSortType(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input SortType | ||
wantIsNone bool | ||
wantIsDesc bool | ||
wantHeaderLabel string | ||
}{ | ||
{ | ||
name: "SORT_TYPE_NONE", | ||
input: SORT_TYPE_NONE, | ||
wantIsNone: true, | ||
wantIsDesc: false, | ||
wantHeaderLabel: "N/A", | ||
}, | ||
{ | ||
name: "SORT_TYPE_LATENCY_DESC", | ||
input: SORT_TYPE_LATENCY_DESC, | ||
wantIsNone: false, | ||
wantIsDesc: true, | ||
wantHeaderLabel: "Latency", | ||
}, | ||
{ | ||
name: "SORT_TYPE_LATENCY_ASC", | ||
input: SORT_TYPE_LATENCY_ASC, | ||
wantIsNone: false, | ||
wantIsDesc: false, | ||
wantHeaderLabel: "Latency", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
assert.Equal(t, tt.wantIsNone, tt.input.IsNone()) | ||
assert.Equal(t, tt.wantIsDesc, tt.input.IsDesc()) | ||
assert.Equal(t, tt.wantHeaderLabel, tt.input.GetHeaderLabel()) | ||
}) | ||
} | ||
} | ||
|
||
func TestSortSvcSpans(t *testing.T) { | ||
baseSvcSpans := SvcSpans{ | ||
&SpanData{ | ||
Span: test.GenerateSpanWithDuration(t, "100ms", 100*time.Millisecond), | ||
}, | ||
&SpanData{ | ||
Span: test.GenerateSpanWithDuration(t, "75µs", 50*time.Microsecond), | ||
}, | ||
&SpanData{ | ||
Span: test.GenerateSpanWithDuration(t, "230ms", 230*time.Millisecond), | ||
}, | ||
&SpanData{ | ||
Span: test.GenerateSpanWithDuration(t, "101ms", 101*time.Millisecond), | ||
}, | ||
&SpanData{ | ||
Span: test.GenerateSpanWithDuration(t, "50ns", 50*time.Nanosecond), | ||
}, | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
sortType SortType | ||
input SvcSpans | ||
want SvcSpans | ||
}{ | ||
{ | ||
name: "SORT_TYPE_NONE", | ||
sortType: SORT_TYPE_NONE, | ||
input: append(SvcSpans{}, baseSvcSpans...), | ||
want: SvcSpans{ | ||
&SpanData{ | ||
Span: test.GenerateSpanWithDuration(t, "100ms", 100*time.Millisecond), | ||
}, | ||
&SpanData{ | ||
Span: test.GenerateSpanWithDuration(t, "75µs", 50*time.Microsecond), | ||
}, | ||
&SpanData{ | ||
Span: test.GenerateSpanWithDuration(t, "230ms", 230*time.Millisecond), | ||
}, | ||
&SpanData{ | ||
Span: test.GenerateSpanWithDuration(t, "101ms", 101*time.Millisecond), | ||
}, | ||
&SpanData{ | ||
Span: test.GenerateSpanWithDuration(t, "50ns", 50*time.Nanosecond), | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "SORT_TYPE_LATENCY_DESC", | ||
sortType: SORT_TYPE_LATENCY_DESC, | ||
input: append(SvcSpans{}, baseSvcSpans...), | ||
want: SvcSpans{ | ||
&SpanData{ | ||
Span: test.GenerateSpanWithDuration(t, "230ms", 230*time.Millisecond), | ||
}, | ||
&SpanData{ | ||
Span: test.GenerateSpanWithDuration(t, "101ms", 101*time.Millisecond), | ||
}, | ||
&SpanData{ | ||
Span: test.GenerateSpanWithDuration(t, "100ms", 100*time.Millisecond), | ||
}, | ||
&SpanData{ | ||
Span: test.GenerateSpanWithDuration(t, "75µs", 50*time.Microsecond), | ||
}, | ||
&SpanData{ | ||
Span: test.GenerateSpanWithDuration(t, "50ns", 50*time.Nanosecond), | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "SORT_TYPE_LATENCY_ASC", | ||
sortType: SORT_TYPE_LATENCY_ASC, | ||
input: append(SvcSpans{}, baseSvcSpans...), | ||
want: SvcSpans{ | ||
&SpanData{ | ||
Span: test.GenerateSpanWithDuration(t, "50ns", 50*time.Nanosecond), | ||
}, | ||
&SpanData{ | ||
Span: test.GenerateSpanWithDuration(t, "75µs", 50*time.Microsecond), | ||
}, | ||
&SpanData{ | ||
Span: test.GenerateSpanWithDuration(t, "100ms", 100*time.Millisecond), | ||
}, | ||
&SpanData{ | ||
Span: test.GenerateSpanWithDuration(t, "101ms", 101*time.Millisecond), | ||
}, | ||
&SpanData{ | ||
Span: test.GenerateSpanWithDuration(t, "230ms", 230*time.Millisecond), | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
sortSvcSpans(tt.input, tt.sortType) | ||
assert.Equal(t, tt.want, tt.input) | ||
}) | ||
} | ||
} |
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
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
Oops, something went wrong.