Skip to content

Commit f60f5fd

Browse files
committed
Report data for a given view when it is unregistered.
This might help with census-instrumentation#773.
1 parent 9260bbf commit f60f5fd

File tree

3 files changed

+64
-20
lines changed

3 files changed

+64
-20
lines changed

stats/view/worker.go

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -194,26 +194,30 @@ func (w *worker) tryRegisterView(v *View) (*viewInternal, error) {
194194
return vi, nil
195195
}
196196

197+
func (w *worker) reportView(v *viewInternal, now time.Time) {
198+
if !v.isSubscribed() {
199+
return
200+
}
201+
rows := v.collectedRows()
202+
_, ok := w.startTimes[v]
203+
if !ok {
204+
w.startTimes[v] = now
205+
}
206+
viewData := &Data{
207+
View: v.view,
208+
Start: w.startTimes[v],
209+
End: time.Now(),
210+
Rows: rows,
211+
}
212+
exportersMu.Lock()
213+
for e := range exporters {
214+
e.ExportView(viewData)
215+
}
216+
exportersMu.Unlock()
217+
}
218+
197219
func (w *worker) reportUsage(now time.Time) {
198220
for _, v := range w.views {
199-
if !v.isSubscribed() {
200-
continue
201-
}
202-
rows := v.collectedRows()
203-
_, ok := w.startTimes[v]
204-
if !ok {
205-
w.startTimes[v] = now
206-
}
207-
viewData := &Data{
208-
View: v.view,
209-
Start: w.startTimes[v],
210-
End: time.Now(),
211-
Rows: rows,
212-
}
213-
exportersMu.Lock()
214-
for e := range exporters {
215-
e.ExportView(viewData)
216-
}
217-
exportersMu.Unlock()
221+
w.reportView(v, now)
218222
}
219223
}

stats/view/worker_commands.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ func (cmd *unregisterFromViewReq) handleCommand(w *worker) {
8888
continue
8989
}
9090

91+
// Report pending data for this view before removing it.
92+
w.reportView(vi, time.Now())
93+
9194
vi.unsubscribe()
9295
if !vi.isSubscribed() {
9396
// this was the last subscription and view is not collecting anymore.

stats/view/worker_test.go

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,9 +362,45 @@ func TestWorkerStarttime(t *testing.T) {
362362
e.Unlock()
363363
}
364364

365+
func TestUnregisterReportsUsage(t *testing.T) {
366+
restart()
367+
ctx := context.Background()
368+
369+
m1 := stats.Int64("measure", "desc", "unit")
370+
view1 := &View{Name: "count", Measure: m1, Aggregation: Count()}
371+
m2 := stats.Int64("measure2", "desc", "unit")
372+
view2 := &View{Name: "count2", Measure: m2, Aggregation: Count()}
373+
374+
SetReportingPeriod(time.Hour)
375+
376+
if err := Register(view1, view2); err != nil {
377+
t.Fatalf("cannot register: %v", err)
378+
}
379+
380+
e := &countExporter{}
381+
RegisterExporter(e)
382+
383+
stats.Record(ctx, m1.M(1))
384+
stats.Record(ctx, m2.M(1))
385+
stats.Record(ctx, m2.M(1))
386+
387+
Unregister(view2)
388+
389+
// Unregister should only flush view2, so expect the count of 2.
390+
want := int64(2)
391+
392+
e.Lock()
393+
got := e.totalCount
394+
e.Unlock()
395+
if got != want {
396+
t.Errorf("got count data = %v; want %v", got, want)
397+
}
398+
}
399+
365400
type countExporter struct {
366401
sync.Mutex
367-
count int64
402+
count int64
403+
totalCount int64
368404
}
369405

370406
func (e *countExporter) ExportView(vd *Data) {
@@ -376,6 +412,7 @@ func (e *countExporter) ExportView(vd *Data) {
376412
e.Lock()
377413
defer e.Unlock()
378414
e.count = d.Value
415+
e.totalCount += d.Value
379416
}
380417

381418
type vdExporter struct {

0 commit comments

Comments
 (0)