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

Initialize View Start Time During View Registration #1215

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

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]
ian-mi marked this conversation as resolved.
Show resolved Hide resolved
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