From 56137211b58fbb279d5489e950095c77639207bc Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Thu, 31 May 2018 11:20:03 +0000 Subject: [PATCH] probe: Eliminate appclient.reportPublisher Simplification: move the 'noControls' functionality into the probe, as we don't need a whole struct to do that. The ReportPublisher interface also moves into probe where it belongs: "the consumer should define the interface" - Dave Cheney --- probe/appclient/multi_client.go | 9 ++++++- probe/appclient/report_publisher.go | 42 ----------------------------- probe/probe.go | 19 ++++++++++--- 3 files changed, 23 insertions(+), 47 deletions(-) delete mode 100644 probe/appclient/report_publisher.go diff --git a/probe/appclient/multi_client.go b/probe/appclient/multi_client.go index e93b33674..668d234d1 100644 --- a/probe/appclient/multi_client.go +++ b/probe/appclient/multi_client.go @@ -2,6 +2,7 @@ package appclient import ( "bytes" + "compress/gzip" "errors" "fmt" "net/url" @@ -42,7 +43,7 @@ type MultiAppClient interface { PipeConnection(appID, pipeID string, pipe xfer.Pipe) error PipeClose(appID, pipeID string) error Stop() - ReportPublisher + Publish(r report.Report) error } // NewMultiAppClient creates a new MultiAppClient. @@ -155,6 +156,12 @@ func (c *multiClient) Stop() { // Publish implements Publisher by publishing the reader to all of the // underlying publishers sequentially. To do that, it needs to drain the // reader, and recreate new readers for each publisher. Note that it will +func serializeReport(r report.Report) (*bytes.Buffer, error) { + buf := &bytes.Buffer{} + err := r.WriteBinary(buf, gzip.DefaultCompression) + return buf, err +} + // publish to one endpoint for each unique ID. Failed publishes don't count. func (c *multiClient) Publish(r report.Report) error { c.mtx.Lock() diff --git a/probe/appclient/report_publisher.go b/probe/appclient/report_publisher.go deleted file mode 100644 index 8d373f710..000000000 --- a/probe/appclient/report_publisher.go +++ /dev/null @@ -1,42 +0,0 @@ -package appclient - -import ( - "bytes" - "compress/gzip" - - "github.com/weaveworks/scope/report" -) - -// ReportPublisher publishes reports, probably to a remote collector. -type ReportPublisher interface { - Publish(r report.Report) error -} - -type reportPublisher struct { - publisher ReportPublisher - noControls bool -} - -// NewReportPublisher creates a new report publisher -func NewReportPublisher(publisher ReportPublisher, noControls bool) ReportPublisher { - return &reportPublisher{ - publisher: publisher, - noControls: noControls, - } -} - -func serializeReport(r report.Report) (*bytes.Buffer, error) { - buf := &bytes.Buffer{} - err := r.WriteBinary(buf, gzip.DefaultCompression) - return buf, err -} - -// Publish sanitises a report, then passes it to a publisher -func (p *reportPublisher) Publish(r report.Report) error { - if p.noControls { - r.WalkTopologies(func(t *report.Topology) { - t.Controls = report.Controls{} - }) - } - return p.publisher.Publish(r) -} diff --git a/probe/probe.go b/probe/probe.go index d9c07bd9a..cb7286a8d 100644 --- a/probe/probe.go +++ b/probe/probe.go @@ -7,7 +7,6 @@ import ( log "github.com/Sirupsen/logrus" "github.com/armon/go-metrics" - "github.com/weaveworks/scope/probe/appclient" "github.com/weaveworks/scope/report" ) @@ -15,10 +14,16 @@ const ( reportBufferSize = 16 ) +// ReportPublisher publishes reports, probably to a remote collector. +type ReportPublisher interface { + Publish(r report.Report) error +} + // Probe sits there, generating and publishing reports. type Probe struct { spyInterval, publishInterval time.Duration - publisher appclient.ReportPublisher + publisher ReportPublisher + noControls bool tickers []Ticker reporters []Reporter @@ -67,13 +72,14 @@ type Ticker interface { // New makes a new Probe. func New( spyInterval, publishInterval time.Duration, - publisher appclient.ReportPublisher, + publisher ReportPublisher, noControls bool, ) *Probe { result := &Probe{ spyInterval: spyInterval, publishInterval: publishInterval, - publisher: appclient.NewReportPublisher(publisher, noControls), + publisher: publisher, + noControls: noControls, quit: make(chan struct{}), spiedReports: make(chan report.Report, reportBufferSize), shortcutReports: make(chan report.Report, reportBufferSize), @@ -200,6 +206,11 @@ ForLoop: } } + if p.noControls { + rpt.WalkTopologies(func(t *report.Topology) { + t.Controls = report.Controls{} + }) + } if err := p.publisher.Publish(rpt); err != nil { log.Infof("publish: %v", err) }