Skip to content

Commit

Permalink
Create a new recorder rather than reuse one for same labels (#610)
Browse files Browse the repository at this point in the history
Co-authored-by: Rahul Patel <[email protected]>
  • Loading branch information
evantorrie and rghetia authored Apr 2, 2020
1 parent 0f771bb commit 367635b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
10 changes: 5 additions & 5 deletions sdk/metric/correct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,15 +285,15 @@ func TestObserverCollection(t *testing.T) {
meter := metric.WrapMeterImpl(sdk, "test")

_ = Must(meter).RegisterFloat64Observer("float.observer", func(result metric.Float64ObserverResult) {
// TODO: The spec says the last-value wins in observer
// instruments, but it is not implemented yet, i.e., with the
// following line we get 1-1==0 instead of -1:
// result.Observe(1, meter.Labels(key.String("A", "B")))

result.Observe(1, key.String("A", "B"))
// last value wins
result.Observe(-1, key.String("A", "B"))
result.Observe(-1, key.String("C", "D"))
})
_ = Must(meter).RegisterInt64Observer("int.observer", func(result metric.Int64ObserverResult) {
result.Observe(-1, key.String("A", "B"))
result.Observe(1)
// last value wins
result.Observe(1, key.String("A", "B"))
result.Observe(1)
})
Expand Down
8 changes: 7 additions & 1 deletion sdk/metric/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,13 @@ func (a *asyncInstrument) getRecorder(kvs []core.KeyValue) export.Aggregator {

lrec, ok := a.recorders[labels.ordered]
if ok {
lrec.modifiedEpoch = a.meter.currentEpoch
if lrec.modifiedEpoch == a.meter.currentEpoch {
// last value wins for Observers, so if we see the same labels
// in the current epoch, we replace the old recorder
lrec.recorder = a.meter.batcher.AggregatorFor(&a.descriptor)
} else {
lrec.modifiedEpoch = a.meter.currentEpoch
}
a.recorders[labels.ordered] = lrec
return lrec.recorder
}
Expand Down

0 comments on commit 367635b

Please sign in to comment.