From 59af5d28adf679af1a537cd03781577b4f518f70 Mon Sep 17 00:00:00 2001 From: Alfonso Acosta Date: Tue, 29 Mar 2016 16:44:25 +0000 Subject: [PATCH] Review feedback: merge host controls with reporter --- probe/host/controls.go | 19 +++++-------------- probe/host/reporter.go | 14 ++++++++++++-- probe/host/reporter_test.go | 2 +- prog/probe.go | 7 +++---- 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/probe/host/controls.go b/probe/host/controls.go index c7d8d878ca..0ecad9f59e 100644 --- a/probe/host/controls.go +++ b/probe/host/controls.go @@ -15,24 +15,15 @@ const ( ExecHost = "host_exec" ) -// Controls handles controls for a hosts. -type Controls struct { - pipes controls.PipeClient +func (r *Reporter) registerControls() { + controls.Register(ExecHost, r.execHost) } -// NewControls creates new host controls. -func NewControls(pipes controls.PipeClient) *Controls { - c := &Controls{pipes: pipes} - controls.Register(ExecHost, c.execHost) - return c -} - -// Stop stops the host controls. -func (*Controls) Stop() { +func (*Reporter) deregisterControls() { controls.Rm(ExecHost) } -func (c *Controls) execHost(req xfer.Request) xfer.Response { +func (r *Reporter) execHost(req xfer.Request) xfer.Response { cmd := exec.Command(hostShellCmd[0], hostShellCmd[1:]...) cmd.Env = []string{"TERM=xterm"} ptyPipe, err := pty.Start(cmd) @@ -40,7 +31,7 @@ func (c *Controls) execHost(req xfer.Request) xfer.Response { return xfer.ResponseError(err) } - id, pipe, err := controls.NewPipeFromEnds(nil, ptyPipe, c.pipes, req.AppID) + id, pipe, err := controls.NewPipeFromEnds(nil, ptyPipe, r.pipes, req.AppID) if err != nil { return xfer.ResponseError(err) } diff --git a/probe/host/reporter.go b/probe/host/reporter.go index eced090eb0..23b7c48e46 100644 --- a/probe/host/reporter.go +++ b/probe/host/reporter.go @@ -6,6 +6,7 @@ import ( "time" "github.com/weaveworks/scope/common/mtime" + "github.com/weaveworks/scope/probe/controls" "github.com/weaveworks/scope/report" ) @@ -37,16 +38,20 @@ type Reporter struct { hostID string hostName string probeID string + pipes controls.PipeClient } // NewReporter returns a Reporter which produces a report containing host // topology for this host. -func NewReporter(hostID, hostName, probeID string) *Reporter { - return &Reporter{ +func NewReporter(hostID, hostName, probeID string, pipes controls.PipeClient) *Reporter { + r := &Reporter{ hostID: hostID, hostName: hostName, probeID: probeID, + pipes: pipes, } + r.registerControls() + return r } // Name of this reporter, for metrics gathering @@ -119,3 +124,8 @@ func (r *Reporter) Report() (report.Report, error) { return rep, nil } + +// Stop stops the reporter. +func (r *Reporter) Stop() { + r.deregisterControls() +} diff --git a/probe/host/reporter_test.go b/probe/host/reporter_test.go index 86abd89fe9..3933c1e276 100644 --- a/probe/host/reporter_test.go +++ b/probe/host/reporter_test.go @@ -58,7 +58,7 @@ func TestReporter(t *testing.T) { host.GetMemoryUsageBytes = func() (float64, float64) { return 60.0, 100.0 } host.GetLocalNetworks = func() ([]*net.IPNet, error) { return []*net.IPNet{ipnet}, nil } - rpt, err := host.NewReporter(hostID, hostname, probeID).Report() + rpt, err := host.NewReporter(hostID, hostname, probeID, nil).Report() if err != nil { t.Fatal(err) } diff --git a/prog/probe.go b/prog/probe.go index 79408f9b4d..7ae166747f 100644 --- a/prog/probe.go +++ b/prog/probe.go @@ -129,9 +129,6 @@ func probeMain() { }) defer clients.Stop() - hostControls := host.NewControls(clients) - defer hostControls.Stop() - resolver := appclient.NewResolver(targets, net.LookupIP, clients.Set) defer resolver.Stop() @@ -143,9 +140,11 @@ func probeMain() { p := probe.New(*spyInterval, *publishInterval, clients) p.AddTicker(processCache) + hostReporter := host.NewReporter(hostID, hostName, probeID, clients) + defer hostReporter.Stop() p.AddReporter( endpointReporter, - host.NewReporter(hostID, hostName, probeID), + hostReporter, process.NewReporter(processCache, hostID, process.GetDeltaTotalJiffies), ) p.AddTagger(probe.NewTopologyTagger(), host.NewTagger(hostID))