From 5fa069b99bc903d713add0295c7e0a55d34ae573 Mon Sep 17 00:00:00 2001 From: Ian Milligan Date: Tue, 16 Jun 2020 20:48:03 -0700 Subject: [PATCH] Initialize View Start Time During View Registration (#1215) * Initialize view start time during view registration * Remove unnecessary start time initialization from reportUsage * Remove unnecessary start time check in toMetric --- stats/view/worker.go | 19 ++++++------------- stats/view/worker_commands.go | 2 +- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/stats/view/worker.go b/stats/view/worker.go index 51be9e278..ab8bfd46d 100644 --- a/stats/view/worker.go +++ b/stats/view/worker.go @@ -277,7 +277,7 @@ func (w *worker) start() { case cmd := <-w.c: cmd.handleCommand(w) case <-w.timer.C: - w.reportUsage(time.Now()) + w.reportUsage() case <-w.quit: w.timer.Stop() close(w.c) @@ -324,6 +324,7 @@ func (w *worker) tryRegisterView(v *View) (*viewInternal, error) { return x, nil } w.views[vi.view.Name] = vi + w.startTimes[vi] = time.Now() ref := w.getMeasureRef(vi.view.Measure.Name()) ref.views[vi] = struct{}{} return vi, nil @@ -333,20 +334,17 @@ func (w *worker) unregisterView(v *viewInternal) { w.mu.Lock() defer w.mu.Unlock() delete(w.views, v.view.Name) + delete(w.startTimes, v) if measure := w.measures[v.view.Measure.Name()]; measure != nil { delete(measure.views, v) } } -func (w *worker) reportView(v *viewInternal, now time.Time) { +func (w *worker) reportView(v *viewInternal) { if !v.isSubscribed() { return } rows := v.collectedRows() - _, ok := w.startTimes[v] - if !ok { - w.startTimes[v] = now - } viewData := &Data{ View: v.view, Start: w.startTimes[v], @@ -360,11 +358,11 @@ func (w *worker) reportView(v *viewInternal, now time.Time) { } } -func (w *worker) reportUsage(now time.Time) { +func (w *worker) reportUsage() { w.mu.Lock() defer w.mu.Unlock() for _, v := range w.views { - w.reportView(v, now) + w.reportView(v) } } @@ -373,11 +371,6 @@ func (w *worker) toMetric(v *viewInternal, now time.Time) *metricdata.Metric { return nil } - _, ok := w.startTimes[v] - if !ok { - w.startTimes[v] = now - } - var startTime time.Time if v.metricDescriptor.Type == metricdata.TypeGaugeInt64 || v.metricDescriptor.Type == metricdata.TypeGaugeFloat64 { diff --git a/stats/view/worker_commands.go b/stats/view/worker_commands.go index e4a2fbdea..9ac4cc059 100644 --- a/stats/view/worker_commands.go +++ b/stats/view/worker_commands.go @@ -95,7 +95,7 @@ func (cmd *unregisterFromViewReq) handleCommand(w *worker) { } // Report pending data for this view before removing it. - w.reportView(vi, time.Now()) + w.reportView(vi) vi.unsubscribe() if !vi.isSubscribed() {