-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add visualization tool to debug image sources
- Loading branch information
1 parent
3d770a2
commit 4cdb986
Showing
7 changed files
with
250 additions
and
8 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
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,58 @@ | ||
package visualize | ||
|
||
import ( | ||
"net/netip" | ||
"sync" | ||
|
||
"github.com/opencontainers/go-digest" | ||
) | ||
|
||
type EventKind string | ||
|
||
const ( | ||
EventKindSuccess EventKind = "success" | ||
EventKindPending EventKind = "pending" | ||
EventKindFail EventKind = "fail" | ||
) | ||
|
||
type Event struct { | ||
ID string | ||
Peer netip.AddrPort | ||
Kind EventKind | ||
Digest digest.Digest | ||
} | ||
|
||
type EventStore interface { | ||
Record(id string, peer netip.AddrPort, kind EventKind) | ||
Events() []Event | ||
} | ||
|
||
type MemoryStore struct { | ||
eventsMx sync.RWMutex | ||
events []Event | ||
} | ||
|
||
func NewMemoryStore() *MemoryStore { | ||
return &MemoryStore{ | ||
events: []Event{}, | ||
} | ||
} | ||
|
||
func (m *MemoryStore) Record(id string, peer netip.AddrPort, kind EventKind) { | ||
m.eventsMx.Lock() | ||
defer m.eventsMx.Unlock() | ||
|
||
event := Event{ | ||
ID: id, | ||
Peer: peer, | ||
Kind: kind, | ||
} | ||
m.events = append(m.events, event) | ||
} | ||
|
||
func (m *MemoryStore) Events() []Event { | ||
m.eventsMx.RLock() | ||
defer m.eventsMx.RUnlock() | ||
|
||
return m.events | ||
} |
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,114 @@ | ||
package visualize | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/emicklei/dot" | ||
|
||
"github.com/spegel-org/spegel/internal/mux" | ||
) | ||
|
||
type Server struct { | ||
eventStore EventStore | ||
} | ||
|
||
func NewServer(eventStore EventStore) *Server { | ||
return &Server{ | ||
eventStore: eventStore, | ||
} | ||
} | ||
|
||
func (s *Server) Server(addr string) *http.Server { | ||
srv := &http.Server{ | ||
Addr: addr, | ||
Handler: mux.NewServeMux(s.handle), | ||
} | ||
return srv | ||
} | ||
|
||
func (s *Server) handle(rw mux.ResponseWriter, req *http.Request) { | ||
switch req.URL.Path { | ||
case "/": | ||
s.indexHandler(rw, req) | ||
case "/graph": | ||
s.getGraph(rw, req) | ||
default: | ||
rw.WriteHeader(http.StatusNotFound) | ||
} | ||
} | ||
|
||
func (s *Server) indexHandler(rw mux.ResponseWriter, _ *http.Request) { | ||
index := ` | ||
<html> | ||
<head> | ||
<title>Spegel</title> | ||
<style> | ||
.content { | ||
width: 1110px; | ||
margin: auto; | ||
} | ||
#graph { | ||
width: 1110px; | ||
} | ||
</style> | ||
<script src="https://unpkg.com/htmx.org@1.9.12"></script> | ||
<script src="https://unpkg.com/@viz-js/viz@3.5.0/lib/viz-standalone.js"></script> | ||
</head> | ||
<body> | ||
<div class="content"> | ||
<h1>Spegel</h1> | ||
<div id="graph-container" hx-get="/graph" hx-trigger="load, every 5s" hx-swap="none" | ||
hx-on::after-request="updateGraph(event.detail.xhr.responseText)"> | ||
</div> | ||
<script> | ||
function updateGraph(dot) { | ||
Viz.instance().then(function (viz) { | ||
let container = document.getElementById("graph-container"); | ||
let newNode = viz.renderSVGElement(dot); | ||
newNode.id = "graph" | ||
let oldNode = document.getElementById("graph"); | ||
if (oldNode == null) { | ||
container.appendChild(newNode) | ||
return | ||
} | ||
container.replaceChild(newNode, oldNode) | ||
}); | ||
} | ||
</script> | ||
</div> | ||
</div> | ||
</body> | ||
</html> | ||
` | ||
rw.Write([]byte(index)) | ||
} | ||
|
||
// NOTE: image could be discoverd by peeking at the manifest content? | ||
// NOTE: In the future we could show all nodes in the cluster? | ||
// NOTE: Should be able to both see incoming and outgoing requests | ||
// NOTE: Hash result to avoid generating graph over again | ||
func (s *Server) getGraph(rw mux.ResponseWriter, _ *http.Request) { | ||
events := s.eventStore.Events() | ||
g := dot.NewGraph(dot.Directed) | ||
// g.Attr("layout", "circo") | ||
g.Attr("root", "host") | ||
g.Attr("layout", "sfdp") | ||
g.Attr("beautify", true) | ||
host := g.Node("host").Attr("shape", "box") | ||
for _, event := range events { | ||
color := "green" | ||
if event.Kind == EventKindFail { | ||
color = "red" | ||
} | ||
|
||
n := g.Node(event.Peer.Addr().String()).Attr("shape", "circle") | ||
g.Edge(host, n, event.ID).Attr("color", color) | ||
} | ||
rw.Write([]byte(g.String())) | ||
} |
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