|
| 1 | +import type { |
| 2 | + Client, |
| 3 | + ClientOptions, |
| 4 | + MeasurementUnit, |
| 5 | + MetricsAggregator as MetricsAggregatorBase, |
| 6 | + Primitive, |
| 7 | +} from '@sentry/types'; |
| 8 | +import { timestampInSeconds } from '@sentry/utils'; |
| 9 | +import { DEFAULT_FLUSH_INTERVAL } from './constants'; |
| 10 | +import type { MetricBucket, MetricType } from './types'; |
| 11 | + |
| 12 | +/** |
| 13 | + * A metrics aggregator that aggregates metrics in memory and flushes them periodically. |
| 14 | + */ |
| 15 | +export class MetricsAggregator implements MetricsAggregatorBase { |
| 16 | + private _buckets: MetricBucket; |
| 17 | + private _bucketsTotalWeight; |
| 18 | + private readonly _interval: ReturnType<typeof setInterval>; |
| 19 | + private readonly _flushShift: number; |
| 20 | + private _forceFlush: boolean; |
| 21 | + |
| 22 | + public constructor(private readonly _client: Client<ClientOptions>) { |
| 23 | + this._buckets = new Map(); |
| 24 | + this._bucketsTotalWeight = 0; |
| 25 | + this._interval = setInterval(() => this._flush(), DEFAULT_FLUSH_INTERVAL); |
| 26 | + |
| 27 | + // SDKs are required to shift the flush interval by random() * rollup_in_seconds. |
| 28 | + // That shift is determined once per startup to create jittering. |
| 29 | + this._flushShift = Math.random() * DEFAULT_FLUSH_INTERVAL; |
| 30 | + |
| 31 | + this._forceFlush = false; |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * @inheritDoc |
| 36 | + */ |
| 37 | + public add( |
| 38 | + metricType: MetricType, |
| 39 | + unsanitizedName: string, |
| 40 | + value: number | string, |
| 41 | + unit: MeasurementUnit = 'none', |
| 42 | + unsanitizedTags: Record<string, Primitive> = {}, |
| 43 | + maybeFloatTimestamp = timestampInSeconds(), |
| 44 | + ): void { |
| 45 | + // Do nothing |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Flushes the current metrics to the transport via the transport. |
| 50 | + */ |
| 51 | + public flush(): void { |
| 52 | + this._forceFlush = true; |
| 53 | + this._flush(); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Shuts down metrics aggregator and clears all metrics. |
| 58 | + */ |
| 59 | + public close(): void { |
| 60 | + this._forceFlush = true; |
| 61 | + clearInterval(this._interval); |
| 62 | + this._flush(); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Returns a string representation of the aggregator. |
| 67 | + */ |
| 68 | + public toString(): string { |
| 69 | + return ''; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Flushes the buckets according to the internal state of the aggregator. |
| 74 | + * If it is a force flush, which happens on shutdown, it will flush all buckets. |
| 75 | + * Otherwise, it will only flush buckets that are older than the flush interval, |
| 76 | + * and according to the flush shift. |
| 77 | + * |
| 78 | + * This function mutates `_forceFlush` and `_bucketsTotalWeight` properties. |
| 79 | + */ |
| 80 | + private _flush(): void { |
| 81 | + // This path eliminates the need for checking for timestamps since we're forcing a flush. |
| 82 | + // Remember to reset the flag, or it will always flush all metrics. |
| 83 | + if (this._forceFlush) { |
| 84 | + this._forceFlush = false; |
| 85 | + this._bucketsTotalWeight = 0; |
| 86 | + this._captureMetrics(this._buckets); |
| 87 | + this._buckets.clear(); |
| 88 | + return; |
| 89 | + } |
| 90 | + const cutoffSeconds = timestampInSeconds() - DEFAULT_FLUSH_INTERVAL - this._flushShift; |
| 91 | + // TODO(@anonrig): Optimization opportunity. |
| 92 | + // Convert this map to an array and store key in the bucketItem. |
| 93 | + const flushedBuckets: MetricBucket = new Map(); |
| 94 | + for (const [key, bucket] of this._buckets) { |
| 95 | + if (bucket.timestamp < cutoffSeconds) { |
| 96 | + flushedBuckets.set(key, bucket); |
| 97 | + this._bucketsTotalWeight -= bucket.metric.weight; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + for (const [key] of flushedBuckets) { |
| 102 | + this._buckets.delete(key); |
| 103 | + } |
| 104 | + |
| 105 | + this._captureMetrics(flushedBuckets); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Only captures a subset of the buckets passed to this function. |
| 110 | + * @param flushedBuckets |
| 111 | + */ |
| 112 | + private _captureMetrics(flushedBuckets: MetricBucket): void { |
| 113 | + if (flushedBuckets.size > 0 && this._client.captureAggregateMetrics) { |
| 114 | + // TODO(@anonrig): This copy operation can be avoided if we store the key in the bucketItem. |
| 115 | + const buckets = Array.from(flushedBuckets).map(([, bucketItem]) => bucketItem); |
| 116 | + this._client.captureAggregateMetrics(buckets); |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments