@@ -24,8 +24,6 @@ export class MetricsAggregator implements MetricsAggregatorBase {
24
24
// that we store in memory.
25
25
private _bucketsTotalWeight ;
26
26
27
- // TODO(@anonrig): Use `setTimeout` instead of `setInterval` to be more accurate
28
- // with the flush interval.
29
27
private readonly _interval : ReturnType < typeof setInterval > ;
30
28
31
29
// SDKs are required to shift the flush interval by random() * rollup_in_seconds.
@@ -44,7 +42,7 @@ export class MetricsAggregator implements MetricsAggregatorBase {
44
42
this . _buckets = new Map ( ) ;
45
43
this . _bucketsTotalWeight = 0 ;
46
44
this . _interval = setInterval ( ( ) => this . _flush ( ) , DEFAULT_FLUSH_INTERVAL ) ;
47
- this . _flushShift = Math . random ( ) * DEFAULT_FLUSH_INTERVAL ;
45
+ this . _flushShift = Math . floor ( ( Math . random ( ) * DEFAULT_FLUSH_INTERVAL ) / 1000 ) ;
48
46
this . _forceFlush = false ;
49
47
}
50
48
@@ -132,12 +130,12 @@ export class MetricsAggregator implements MetricsAggregatorBase {
132
130
this . _buckets . clear ( ) ;
133
131
return ;
134
132
}
135
- const cutoffSeconds = timestampInSeconds ( ) - DEFAULT_FLUSH_INTERVAL - this . _flushShift ;
133
+ const cutoffSeconds = Math . floor ( timestampInSeconds ( ) ) - DEFAULT_FLUSH_INTERVAL / 1000 - this . _flushShift ;
136
134
// TODO(@anonrig): Optimization opportunity.
137
135
// Convert this map to an array and store key in the bucketItem.
138
136
const flushedBuckets : MetricBucket = new Map ( ) ;
139
137
for ( const [ key , bucket ] of this . _buckets ) {
140
- if ( bucket . timestamp < cutoffSeconds ) {
138
+ if ( bucket . timestamp <= cutoffSeconds ) {
141
139
flushedBuckets . set ( key , bucket ) ;
142
140
this . _bucketsTotalWeight -= bucket . metric . weight ;
143
141
}
0 commit comments