@@ -24,8 +24,6 @@ export class MetricsAggregator implements MetricsAggregatorBase {
2424 // that we store in memory.
2525 private _bucketsTotalWeight ;
2626
27- // TODO(@anonrig): Use `setTimeout` instead of `setInterval` to be more accurate
28- // with the flush interval.
2927 private readonly _interval : ReturnType < typeof setInterval > ;
3028
3129 // SDKs are required to shift the flush interval by random() * rollup_in_seconds.
@@ -44,7 +42,7 @@ export class MetricsAggregator implements MetricsAggregatorBase {
4442 this . _buckets = new Map ( ) ;
4543 this . _bucketsTotalWeight = 0 ;
4644 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 ) ;
4846 this . _forceFlush = false ;
4947 }
5048
@@ -132,12 +130,12 @@ export class MetricsAggregator implements MetricsAggregatorBase {
132130 this . _buckets . clear ( ) ;
133131 return ;
134132 }
135- const cutoffSeconds = timestampInSeconds ( ) - DEFAULT_FLUSH_INTERVAL - this . _flushShift ;
133+ const cutoffSeconds = Math . floor ( timestampInSeconds ( ) ) - DEFAULT_FLUSH_INTERVAL / 1000 - this . _flushShift ;
136134 // TODO(@anonrig): Optimization opportunity.
137135 // Convert this map to an array and store key in the bucketItem.
138136 const flushedBuckets : MetricBucket = new Map ( ) ;
139137 for ( const [ key , bucket ] of this . _buckets ) {
140- if ( bucket . timestamp < cutoffSeconds ) {
138+ if ( bucket . timestamp <= cutoffSeconds ) {
141139 flushedBuckets . set ( key , bucket ) ;
142140 this . _bucketsTotalWeight -= bucket . metric . weight ;
143141 }
0 commit comments