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: add trace topology view #197

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
4 changes: 3 additions & 1 deletion go.work
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
go 1.22.7
go 1.23

toolchain go1.23.4

use (
.
Expand Down
60 changes: 52 additions & 8 deletions go.work.sum

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion tuiexporter/go.mod
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
module github.com/ymtdzzz/otel-tui/tuiexporter

go 1.22.7
go 1.23

toolchain go1.23.4

require (
github.com/gdamore/tcell/v2 v2.8.1
github.com/icza/gox v0.2.0
github.com/navidys/tvxwidgets v0.9.0
github.com/rivo/tview v0.0.0-20240616192244-23476fa0bab2
github.com/stretchr/testify v1.10.0
github.com/ymtdzzz/mermaid-ascii v0.0.0-20250116141729-eafe4748a419
go.opentelemetry.io/collector/component v0.117.0
go.opentelemetry.io/collector/component/componentstatus v0.117.0
go.opentelemetry.io/collector/component/componenttest v0.117.0
Expand All @@ -19,11 +22,13 @@ require (
require (
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/elliotchance/orderedmap/v2 v2.2.0 // indirect
github.com/gdamore/encoding v1.0.1 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gookit/color v1.5.4 // indirect
github.com/hashicorp/go-version v1.7.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
Expand All @@ -33,6 +38,8 @@ require (
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/rogpeppe/go-internal v1.13.1 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect
go.opentelemetry.io/collector/config/configretry v1.23.0 // indirect
go.opentelemetry.io/collector/config/configtelemetry v0.117.0 // indirect
go.opentelemetry.io/collector/consumer v1.23.0 // indirect
Expand Down
13 changes: 13 additions & 0 deletions tuiexporter/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/elliotchance/orderedmap/v2 v2.2.0 h1:7/2iwO98kYT4XkOjA9mBEIwvi4KpGB4cyHeOFOnj4Vk=
github.com/elliotchance/orderedmap/v2 v2.2.0/go.mod h1:85lZyVbpGaGvHvnKa7Qhx7zncAdBIBq6u56Hb1PRU5Q=
github.com/gdamore/encoding v1.0.1 h1:YzKZckdBL6jVt2Gc+5p82qhrGiqMdG/eNs6Wy0u3Uhw=
github.com/gdamore/encoding v1.0.1/go.mod h1:0Z0cMFinngz9kS1QfMjCP8TY7em3bZYeeklsSDPivEo=
github.com/gdamore/tcell/v2 v2.8.1 h1:KPNxyqclpWpWQlPLx6Xui1pMk8S+7+R37h3g07997NU=
Expand All @@ -26,6 +28,8 @@ github.com/google/pprof v0.0.0-20240827171923-fa2c70bbbfe5 h1:5iH8iuqE5apketRbSF
github.com/google/pprof v0.0.0-20240827171923-fa2c70bbbfe5/go.mod h1:vavhavw2zAxS5dIdcRluK6cSGGPlZynqzFM8NdvU144=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gookit/color v1.5.4 h1:FZmqs7XOyGgCAxmWyPslpiok1k05wmY3SJTytgvYFs0=
github.com/gookit/color v1.5.4/go.mod h1:pZJOeOS8DM43rXbp4AZo1n9zCU2qjpcRko0b6/QJi9w=
github.com/hashicorp/go-version v1.7.0 h1:5tqGy27NaOTB8yJKUZELlFAS/LTKJkrmONwQKeRZfjY=
github.com/hashicorp/go-version v1.7.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/icza/gox v0.2.0 h1:+0N8PCt9/QSx+k0dqe/wdlXJNR/haaPsPwrTJTNDeyk=
Expand Down Expand Up @@ -64,10 +68,17 @@ github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 h1:QldyIu/L63oPpyvQmHgvgickp1Yw510KJOqX7H24mg8=
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs=
github.com/ymtdzzz/mermaid-ascii v0.0.0-20250116141729-eafe4748a419 h1:oVe1YcVRyoRGNeAjkost3Z3il83lCQaNwxwGGAw0L8g=
github.com/ymtdzzz/mermaid-ascii v0.0.0-20250116141729-eafe4748a419/go.mod h1:gDx5dss6A/czppfsA5jQ0J5jxYpfhojLVgiF5ArbIE8=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
Expand Down Expand Up @@ -183,6 +194,7 @@ golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down Expand Up @@ -234,5 +246,6 @@ google.golang.org/protobuf v1.36.2/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojt
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
5 changes: 5 additions & 0 deletions tuiexporter/internal/telemetry/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ func (c *TraceCache) GetSpanByID(spanID string) (*SpanData, bool) {
return span, ok
}

func (c *TraceCache) GetSpanDependencies() []DependencyLink {
// TODO: make it private function?
return c.spanid2span.GetDependencies()
}

func (c *TraceCache) flush() {
c.spanid2span = SpanDataMap{}
c.traceid2spans = TraceSpanDataMap{}
Expand Down
72 changes: 72 additions & 0 deletions tuiexporter/internal/telemetry/dependency.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package telemetry

import (
"fmt"
"log"

Check failure on line 5 in tuiexporter/internal/telemetry/dependency.go

View workflow job for this annotation

GitHub Actions / Run test

"log" imported and not used

Check failure on line 5 in tuiexporter/internal/telemetry/dependency.go

View workflow job for this annotation

GitHub Actions / Run test for exporter

"log" imported and not used

Check failure on line 5 in tuiexporter/internal/telemetry/dependency.go

View workflow job for this annotation

GitHub Actions / Run lint

"log" imported and not used

Check failure on line 5 in tuiexporter/internal/telemetry/dependency.go

View workflow job for this annotation

GitHub Actions / Run lint for exporter

"log" imported and not used
"strings"

"github.com/ymtdzzz/mermaid-ascii/pkg/drawer"
)

// DependencyLink shows dependencies between services
type DependencyLink struct {
Parent string
Child string
CallCount uint64
}

func (m SpanDataMap) GetDependencies() []DependencyLink {
// TODO: should we take an exclusive lock?
deps := map[string]*DependencyLink{}
for _, span := range m {
parentspan, ok := m[span.Span.ParentSpanID().String()]
if !ok {
continue
}
parentsn, ok := parentspan.ResourceSpan.Resource().Attributes().Get("service.name")
if !ok {
continue
}
sn, ok := span.ResourceSpan.Resource().Attributes().Get("service.name")
if !ok {
continue
}
if parentsn == sn {
continue
}
depkey := parentsn.AsString() + "&&&" + sn.AsString()
if _, ok := deps[depkey]; !ok {
deps[depkey] = &DependencyLink{
Parent: parentsn.AsString(),
Child: sn.AsString(),
CallCount: 1,
}
} else {
deps[depkey].CallCount++
}
}
res := make([]DependencyLink, 0, len(deps))
for _, dep := range deps {
res = append(res, *dep)
}
return res
}

func GetDependencyGraph(deps []DependencyLink) (string, error) {
props, err := drawer.MermaidFileToMap(getMermaidFromDependencies(deps), "cli")
if err != nil {
return "", err
}
return drawer.DrawMap(props), nil
}

func getMermaidFromDependencies(deps []DependencyLink) string {
var builder strings.Builder
builder.WriteString("graph TD\n")

for _, dep := range deps {
builder.WriteString(fmt.Sprintf(" %s -->|%d| %s\n", dep.Parent, dep.CallCount, dep.Child))
}

return builder.String()
}
15 changes: 15 additions & 0 deletions tuiexporter/internal/tui/component/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package component
import (
"fmt"
"log"
"os"
"strings"

"github.com/gdamore/tcell/v2"
Expand Down Expand Up @@ -196,6 +197,20 @@ func (p *TUIPages) createTracePage(store *telemetry.Store) *tview.Flex {
store.Flush()
p.clearPanes()
return nil
} else if event.Key() == tcell.KeyCtrlZ {
f, err := os.Create("diagram.txt")
if err != nil {
log.Print(err)
return nil
}
defer f.Close()
graph, err := telemetry.GetDependencyGraph(store.GetTraceCache().GetSpanDependencies())
if err != nil {
log.Print(err)
return nil
}
_, _ = f.Write([]byte(graph))
return nil
} else if event.Key() == tcell.KeyCtrlS {
if sortType == telemetry.SORT_TYPE_NONE {
sortType = telemetry.SORT_TYPE_LATENCY_DESC
Expand Down
Loading