![]() |
| (warning: you have to zoom in, or else it will be unreadable) |
In the past, I've done this with java parsers and the graphviz API.
But, for golang, I found the easiest way was to use callgraph, which actually is graphviz friendly.
When I first ran it, it gave me issues with CGO, package paths, etc. etc. Basically you need to do 3 things.
- make sure your gopath is correct when you run it. if the code doesnt compile, the paths cant be generated.
- on a mac (at least) , export the disabled CGO env var.
- rather then being clever with package exclusion, i just grepped out the edges i cared about and reconstituted a correct graphviz file after the fact. otherwise the file is too big and its useless.
So, here's how i did this for kubernetes. The instructions should work WLOG for any golang project, esp. since kubernetes is much more complex than most golang projects....
First, get callgraph
- go get golang.org/x/tools/cmd/
Now export your gopath (you fish out the correct one by running hack/build-go.sh and echo'ing the gopath before the build steps)
export GOPATH=/Users/jayunit100/ Development/gopath/src/k8s.io/ kubernetes/_output/local/go:/ Users/jayunit100/Development/ gopath/src/k8s.io/kubernetes/ Godeps/_workspace:/Users/ jayunit100/Development/gopath/
export GOPATH=/Users/jayunit100/
Finally, run callgraph. After it runs, grep out the stuff you care about or else you will get ~ an unusable half million line file. ~/Development/gopath/bin/
Since you grepped out parts of the file, correct graphviz formatting add "digraph xyz" >> {"
add "}" to the bottom.
add "}" to the bottom.
Format and view your lovely new graph.
neato e2e.dot > e2en.dot ; dot -Tpng e2en.dot > e2e.png ; open e2e.png
regarding
the hacky step in the middle where we grep certain things out... Im
sure there might be a more elegant way to deal with removing unwanted
packages. For me it seemed to be an all or nothing deal with callgraph.

No comments:
Post a Comment