From c9266b799c270c2dd0813470b12e77dcc0095c07 Mon Sep 17 00:00:00 2001 From: "Y.Matsuda" Date: Sat, 6 Apr 2024 23:07:56 +0900 Subject: [PATCH] data flush support --- tuiexporter/internal/telemetry/cache.go | 6 ++++ tuiexporter/internal/telemetry/store.go | 14 +++++++++ tuiexporter/internal/telemetry/store_test.go | 33 ++++++++++++++++++++ tuiexporter/internal/tui/component/page.go | 11 +++++-- tuiexporter/internal/tui/tui.go | 2 +- 5 files changed, 63 insertions(+), 3 deletions(-) diff --git a/tuiexporter/internal/telemetry/cache.go b/tuiexporter/internal/telemetry/cache.go index 7fe9220..75419dc 100644 --- a/tuiexporter/internal/telemetry/cache.go +++ b/tuiexporter/internal/telemetry/cache.go @@ -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{} +} diff --git a/tuiexporter/internal/telemetry/store.go b/tuiexporter/internal/telemetry/store.go index 9511762..d3d35a6 100644 --- a/tuiexporter/internal/telemetry/store.go +++ b/tuiexporter/internal/telemetry/store.go @@ -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() +} diff --git a/tuiexporter/internal/telemetry/store_test.go b/tuiexporter/internal/telemetry/store_test.go index 32f862a..38f768e 100644 --- a/tuiexporter/internal/telemetry/store_test.go +++ b/tuiexporter/internal/telemetry/store_test.go @@ -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)) +} diff --git a/tuiexporter/internal/tui/component/page.go b/tuiexporter/internal/tui/component/page.go index eabe969..0de3506 100644 --- a/tuiexporter/internal/tui/component/page.go +++ b/tuiexporter/internal/tui/component/page.go @@ -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 @@ -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) diff --git a/tuiexporter/internal/tui/tui.go b/tuiexporter/internal/tui/tui.go index a50b7dc..4e8824c 100644 --- a/tuiexporter/internal/tui/tui.go +++ b/tuiexporter/internal/tui/tui.go @@ -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