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
This commit is contained in:
Bryan Boreham
2018-05-31 11:20:03 +00:00
parent 96f51c47af
commit 56137211b5
3 changed files with 23 additions and 47 deletions

View File

@@ -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()

View File

@@ -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)
}

View File

@@ -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)
}