Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: data flush support #25

Merged
merged 1 commit into from
Apr 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions tuiexporter/internal/telemetry/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,9 @@ func (c *TraceCache) GetSpanByID(spanID string) (*SpanData, bool) {
span, ok := c.spanid2span[spanID]
return span, ok
}

func (c *TraceCache) flush() {
c.spanid2span = SpanDataMap{}
c.traceid2spans = TraceSpanDataMap{}
c.tracesvc2spans = TraceServiceSpanDataMap{}
}
14 changes: 14 additions & 0 deletions tuiexporter/internal/telemetry/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,17 @@ func (s *Store) AddSpan(traces *ptrace.Traces) {

s.updateFilterService()
}

// Flush clears the store including the cache
func (s *Store) Flush() {
s.mut.Lock()
defer func() {
s.updatedAt = time.Now()
s.mut.Unlock()
}()

s.svcspans = SvcSpans{}
s.svcspansFiltered = SvcSpans{}
s.cache.flush()
s.updatedAt = time.Now()
}
33 changes: 33 additions & 0 deletions tuiexporter/internal/telemetry/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,36 @@ func TestStoreAddSpanWithRotation(t *testing.T) {
assert.Equal(t, testdata2.Spans[0], gotsds["test-service-1"][0].Span) // trace 2, span-1-1-1
}
}

func TestStoreFlush(t *testing.T) {
// traceid: 1
// └- resource: test-service-1
// | └- scope: test-scope-1-1
// | | └- span: span-1-1-1
// | | └- span: span-1-1-2
// | └- scope: test-scope-1-2
// | └- span: span-1-2-3
// └- resource: test-service-2
// └- scope: test-scope-2-1
// └- span: span-2-1-1
// traceid: 2
// └- resource: test-service-1
// └- scope: test-scope-1-1
// └- span: span-1-1-1
store := NewStore()
store.maxServiceSpanCount = 1
payload1, _ := test.GenerateOTLPPayload(t, 1, 2, []int{2, 1}, [][]int{{2, 1}, {1}})
payload2, _ := test.GenerateOTLPPayload(t, 2, 1, []int{1}, [][]int{{1}})
store.AddSpan(&payload1)
store.AddSpan(&payload2)

before := store.updatedAt
store.Flush()

assert.True(t, before.Before(store.updatedAt))
assert.Equal(t, 0, len(store.svcspans))
assert.Equal(t, 0, len(store.svcspansFiltered))
assert.Equal(t, 0, len(store.cache.spanid2span))
assert.Equal(t, 0, len(store.cache.traceid2spans))
assert.Equal(t, 0, len(store.cache.tracesvc2spans))
}
11 changes: 9 additions & 2 deletions tuiexporter/internal/tui/component/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,20 @@ func (p *TUIPages) createTracePage(store *telemetry.Store) *tview.Flex {
}
// don't return nil here, because we want to pass the event to the search input
}

if event.Key() == tcell.KeyCtrlL {
store.Flush()
return nil
}

return event
})
page = attatchCommandList(page, KeyMaps{
*tcell.NewEventKey(tcell.KeyCtrlL, ' ', tcell.ModNone): "Toggle Log",
*tcell.NewEventKey(tcell.KeyF12, ' ', tcell.ModNone): "Toggle Log",
*tcell.NewEventKey(tcell.KeyRune, '/', tcell.ModNone): "Search traces",
*tcell.NewEventKey(tcell.KeyEsc, ' ', tcell.ModNone): "(search) Cancel",
*tcell.NewEventKey(tcell.KeyEnter, ' ', tcell.ModNone): "(search) Confirm",
*tcell.NewEventKey(tcell.KeyCtrlL, ' ', tcell.ModNone): "Clear all data",
})

return page
Expand Down Expand Up @@ -191,7 +198,7 @@ func (p *TUIPages) refreshTimeline(store *telemetry.Store, row int) {
timeline.AddItem(tl, 0, 1, true)
}

keymaps[*tcell.NewEventKey(tcell.KeyCtrlL, ' ', tcell.ModNone)] = "Toggle Log"
keymaps[*tcell.NewEventKey(tcell.KeyF12, ' ', tcell.ModNone)] = "Toggle Log"
keymaps[*tcell.NewEventKey(tcell.KeyEsc, ' ', tcell.ModNone)] = "Back to Traces"
timeline = attatchCommandList(timeline, keymaps)

Expand Down
2 changes: 1 addition & 1 deletion tuiexporter/internal/tui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func NewTUIApp(store *telemetry.Store) *TUIApp {
app.SetRoot(pages, true)

app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == tcell.KeyCtrlL {
if event.Key() == tcell.KeyF12 {
tpages.ToggleLog()

return nil
Expand Down
Loading