Skip to content

Commit c38fb53

Browse files
AbhiPrasadanonrig
authored andcommitted
fix rounding issues with timestamp
1 parent e62bf48 commit c38fb53

File tree

2 files changed

+4
-5
lines changed

2 files changed

+4
-5
lines changed

packages/core/src/baseclient.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
407407
* @inheritDoc
408408
*/
409409
public captureAggregateMetrics(metricBucketItems: Array<MetricBucketItem>): void {
410+
DEBUG_BUILD && logger.log(`Flushing aggregated metrics, number of metrics: ${metricBucketItems.length}`);
410411
const metricsEnvelope = createMetricEnvelope(
411412
metricBucketItems,
412413
this._dsn,

packages/core/src/metrics/aggregator.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)