Skip to content

Commit

Permalink
Merge pull request #366 from weaveworks/331-process-container-names
Browse files Browse the repository at this point in the history
Add container names to process minor label if possible.
  • Loading branch information
tomwilkie committed Aug 19, 2015
2 parents 0cc3dd0 + fbe010a commit 2b7b0c3
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 7 deletions.
16 changes: 10 additions & 6 deletions app/api_topology_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,25 @@ func TestAll(t *testing.T) {
}
}

func TestAPITopologyApplications(t *testing.T) {
func TestAPITopologyContainers(t *testing.T) {
ts := httptest.NewServer(Router(StaticReport{}))
defer ts.Close()
is404(t, ts, "/api/topology/applications/foobar")
{
body := getRawJSON(t, ts, "/api/topology/applications")
body := getRawJSON(t, ts, "/api/topology/containers")
var topo APITopology
if err := json.Unmarshal(body, &topo); err != nil {
t.Fatal(err)
}

if want, have := render.OnlyConnected(expected.RenderedProcesses), fixNodeMetadatas(topo.Nodes); !reflect.DeepEqual(want, have) {
if want, have := expected.RenderedContainers, fixNodeMetadatas(topo.Nodes); !reflect.DeepEqual(want, have) {
t.Error(test.Diff(want, have))
}
}
}

func TestAPITopologyApplications(t *testing.T) {
ts := httptest.NewServer(Router(StaticReport{}))
defer ts.Close()
is404(t, ts, "/api/topology/applications/foobar")
{
body := getRawJSON(t, ts, "/api/topology/applications/"+expected.ServerProcessID)
var node APINode
Expand All @@ -85,7 +89,7 @@ func TestAPITopologyApplications(t *testing.T) {
}
equals(t, expected.ServerProcessID, node.Node.ID)
equals(t, "apache", node.Node.LabelMajor)
equals(t, fmt.Sprintf("%s (%s)", test.ServerHostID, test.ServerPID), node.Node.LabelMinor)
equals(t, fmt.Sprintf("%s (server:%s)", test.ServerHostID, test.ServerPID), node.Node.LabelMinor)
equals(t, false, node.Node.Pseudo)
// Let's not unit-test the specific content of the detail tables
}
Expand Down
2 changes: 1 addition & 1 deletion app/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ var topologyRegistry = map[string]topologyView{
"applications": {
human: "Applications",
parent: "",
renderer: render.FilterUnconnected{Renderer: render.ProcessRenderer},
renderer: render.FilterUnconnected{Renderer: render.ProcessWithContainerNameRenderer{}},
},
"applications-by-name": {
human: "by name",
Expand Down
43 changes: 43 additions & 0 deletions render/topologies.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package render

import (
"fmt"

"github.com/weaveworks/scope/probe/docker"
"github.com/weaveworks/scope/probe/process"
"github.com/weaveworks/scope/report"
)

Expand All @@ -25,6 +29,45 @@ var ProcessRenderer = MakeReduce(
},
)

// ProcessWithContainerNameRenderer is a Renderer which produces a process
// graph enriched with container names where appropriate
type ProcessWithContainerNameRenderer struct{}

// Render produces a process graph where the minor labels contain the
// container name, if found.
func (r ProcessWithContainerNameRenderer) Render(rpt report.Report) RenderableNodes {
processes := ProcessRenderer.Render(rpt)
containers := LeafMap{
Selector: report.SelectContainer,
Mapper: MapContainerIdentity,
Pseudo: PanicPseudoNode,
}.Render(rpt)

for id, p := range processes {
pid, ok := p.NodeMetadata.Metadata[process.PID]
if !ok {
continue
}
containerID, ok := p.NodeMetadata.Metadata[docker.ContainerID]
if !ok {
continue
}
container, ok := containers[containerID]
if !ok {
continue
}
p.LabelMinor = fmt.Sprintf("%s (%s:%s)", report.ExtractHostID(p.NodeMetadata), container.LabelMajor, pid)
processes[id] = p
}

return processes
}

// EdgeMetadata produces an EdgeMetadata for a given edge.
func (r ProcessWithContainerNameRenderer) EdgeMetadata(rpt report.Report, localID, remoteID string) report.EdgeMetadata {
return ProcessRenderer.EdgeMetadata(rpt, localID, remoteID)
}

// ProcessRenderer is a Renderer which produces a renderable process
// name graph by munging the progess graph.
var ProcessNameRenderer = Map{
Expand Down

0 comments on commit 2b7b0c3

Please sign in to comment.