Skip to content
This repository has been archived by the owner on Jul 31, 2023. It is now read-only.

Commit

Permalink
Update with feedback from @rghetia
Browse files Browse the repository at this point in the history
Signed-off-by: Evan Anderson <evan.k.anderson@gmail.com>
  • Loading branch information
evankanderson committed Feb 13, 2020
1 parent c4c5f2d commit e7bc6a0
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 15 deletions.
10 changes: 6 additions & 4 deletions stats/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ func init() {
}
}

// Recorder is a subset of the view.Meter interface which only includes
// the Record method, to avoid circular imports between stats and view.
// Recorder provides an interface for exporting measurement information from
// the static Record method by using the WithRecorder option.
type Recorder interface {
// Record records a set of measurements associated with the given tags and attachments.
// The second argument is a `[]Measurement`.
Record(*tag.Map, interface{}, map[string]interface{})
}

Expand Down Expand Up @@ -65,9 +67,9 @@ func WithMeasurements(measurements ...Measurement) Options {
}
}

// WithMeter records the measurements to the specified `view.Meter`, rather
// WithRecorder records the measurements to the specified `Recorder`, rather
// than to the global metrics recorder.
func WithMeter(meter Recorder) Options {
func WithRecorder(meter Recorder) Options {
return func(ro *recordOptions) {
ro.recorder = meter
}
Expand Down
6 changes: 3 additions & 3 deletions stats/record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ func cmpExemplar(got, want *metricdata.Exemplar) string {
return cmp.Diff(got, want, cmpopts.IgnoreFields(metricdata.Exemplar{}, "Timestamp"), cmpopts.IgnoreUnexported(metricdata.Exemplar{}))
}

func TestResolveOptions(t *testing.T) {
meter := view.NewWorker()
func TestRecordWithMeter(t *testing.T) {
meter := view.NewMeter()
meter.Start()
k1 := tag.MustNewKey("k1")
k2 := tag.MustNewKey("k2")
Expand Down Expand Up @@ -128,7 +128,7 @@ func TestResolveOptions(t *testing.T) {
stats.WithTags(tag.Upsert(k1, "bar"), tag.Insert(k2, "bar")),
stats.WithAttachments(attachments),
stats.WithMeasurements(m1.M(12), m1.M(6), m2.M(5)),
stats.WithMeter(meter))
stats.WithRecorder(meter))
if err != nil {
t.Fatalf("Failed to resolve data point: %v", err)
}
Expand Down
11 changes: 6 additions & 5 deletions stats/view/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
)

func init() {
defaultWorker = NewWorker().(*worker)
defaultWorker = NewMeter().(*worker)
go defaultWorker.start()
internal.DefaultRecorder = record
}
Expand Down Expand Up @@ -60,8 +60,7 @@ type worker struct {
// Note that this is an advanced use case, and the static functions in this
// module should cover the common use cases.
type Meter interface {
// Record records a set of measurements ms associated with the given tags and attachments.
Record(tags *tag.Map, ms interface{}, attachments map[string]interface{})
stats.Recorder
// Find returns a registered view associated with this name.
// If no registered view is found, nil is returned.
Find(name string) *View
Expand Down Expand Up @@ -233,8 +232,10 @@ func (w *worker) SetReportingPeriod(d time.Duration) {
<-req.c // don't return until the timer is set to the new duration.
}

// NewWorker constructs a
func NewWorker() Meter {
// NewMeter constructs a Meter instance. You should only need to use this if
// you need to separate out Measurement recordings and View aggregations within
// a single process.
func NewMeter() Meter {
return &worker{
measures: make(map[string]*measureRef),
views: make(map[string]*viewInternal),
Expand Down
6 changes: 3 additions & 3 deletions stats/view/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func Test_Worker_MultiExport(t *testing.T) {

// This test reports the same data for the default worker and a secondary
// worker, and ensures that the stats are kept independently.
worker2 := NewWorker().(*worker)
worker2 := NewMeter().(*worker)
worker2.Start()

m := stats.Float64("Test_Worker_MultiExport/MF1", "desc MF1", "unit")
Expand Down Expand Up @@ -397,7 +397,7 @@ func TestReportUsage(t *testing.T) {
func Test_SetReportingPeriodReqNeverBlocks(t *testing.T) {
t.Parallel()

worker := NewWorker().(*worker)
worker := NewMeter().(*worker)
durations := []time.Duration{-1, 0, 10, 100 * time.Millisecond}
for i, duration := range durations {
ackChan := make(chan bool, 1)
Expand Down Expand Up @@ -616,6 +616,6 @@ func (e *vdExporter) ExportView(vd *Data) {
// restart stops the current processors and creates a new one.
func restart() {
defaultWorker.Stop()
defaultWorker = NewWorker().(*worker)
defaultWorker = NewMeter().(*worker)
go defaultWorker.start()
}

0 comments on commit e7bc6a0

Please sign in to comment.