Files
weave-scope/probe/docker/tagger.go
Peter Bourgon 3dd59c8b9b Fixes to NodeMetadata
NewNodeMetadata -> MakeNodeMetadata. It doesn't return a pointer, so
Make is more idiomatic.

Invoke MakeNodeMetadata when necessary. The zero value for a
NodeMetadata is no longer valid.

Split MakeNodeMetadata to two constructors. MakeNodeMetadata when you
don't have anything to prepopulate; MakeNodeMetadataWith when you do.

Also, a fix to the tests in app. We unmarshal a RenderableNode struct,
which has a JSON-ignored NodeMetadata field. The zero value is invalid,
so we need to fix that before performing comparisons.
2015-07-30 17:20:44 +02:00

89 lines
1.7 KiB
Go

package docker
import (
"strconv"
"github.com/weaveworks/scope/probe/process"
"github.com/weaveworks/scope/report"
)
// Node metadata keys.
const (
ContainerID = "docker_container_id"
Domain = "domain" // TODO this is ambiguous, be more specific
Name = "name" // TODO this is ambiguous, be more specific
)
// These vars are exported for testing.
var (
NewProcessTreeStub = process.NewTree
)
// Tagger is a tagger that tags Docker container information to process
// nodes that have a PID.
type Tagger struct {
registry Registry
procWalker process.Walker
}
// NewTagger returns a usable Tagger.
func NewTagger(registry Registry, procWalker process.Walker) *Tagger {
return &Tagger{
registry: registry,
procWalker: procWalker,
}
}
// Tag implements Tagger.
func (t *Tagger) Tag(r report.Report) (report.Report, error) {
tree, err := NewProcessTreeStub(t.procWalker)
if err != nil {
return report.MakeReport(), err
}
t.tag(tree, &r.Process)
return r, nil
}
func (t *Tagger) tag(tree process.Tree, topology *report.Topology) {
for nodeID, nodeMetadata := range topology.NodeMetadatas {
pidStr, ok := nodeMetadata.Metadata[process.PID]
if !ok {
continue
}
pid, err := strconv.ParseUint(pidStr, 10, 64)
if err != nil {
continue
}
var (
c Container
candidate = int(pid)
)
t.registry.LockedPIDLookup(func(lookup func(int) Container) {
for {
c = lookup(candidate)
if c != nil {
break
}
candidate, err = tree.GetParent(candidate)
if err != nil {
break
}
}
})
if c == nil {
continue
}
md := report.MakeNodeMetadataWith(map[string]string{
ContainerID: c.ID(),
})
topology.NodeMetadatas[nodeID].Merge(md)
}
}