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

Commit

Permalink
Initialize View Start Time During View Registration (#1215)
Browse files Browse the repository at this point in the history
* Initialize view start time during view registration

* Remove unnecessary start time initialization from reportUsage

* Remove unnecessary start time check in toMetric
  • Loading branch information
ian-mi authored Jun 17, 2020
1 parent 1901b56 commit 5fa069b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 14 deletions.
19 changes: 6 additions & 13 deletions stats/view/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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],
Expand All @@ -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)
}
}

Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion stats/view/worker_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 5fa069b

Please sign in to comment.