|
| 1 | +import type { MeasurementUnit, Span } from '@sentry/types'; |
| 2 | +import type { MetricSummary } from '@sentry/types'; |
| 3 | +import type { Primitive } from '@sentry/types'; |
| 4 | +import { dropUndefinedKeys } from '@sentry/utils'; |
| 5 | +import { getActiveSpan } from '../tracing'; |
| 6 | +import type { MetricType } from './types'; |
| 7 | + |
| 8 | +/** |
| 9 | + * key: bucketKey |
| 10 | + * value: [exportKey, MetricSummary] |
| 11 | + */ |
| 12 | +type MetricSummaryStorage = Map<string, [string, MetricSummary]>; |
| 13 | + |
| 14 | +let SPAN_METRIC_SUMMARY: WeakMap<Span, MetricSummaryStorage> | undefined; |
| 15 | + |
| 16 | +function getMetricStorageForSpan(span: Span): MetricSummaryStorage | undefined { |
| 17 | + return SPAN_METRIC_SUMMARY ? SPAN_METRIC_SUMMARY.get(span) : undefined; |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * Fetches the metric summary if it exists for the passed span |
| 22 | + */ |
| 23 | +export function getMetricSummaryJsonForSpan(span: Span): Record<string, Array<MetricSummary>> | undefined { |
| 24 | + const storage = getMetricStorageForSpan(span); |
| 25 | + |
| 26 | + if (!storage) { |
| 27 | + return undefined; |
| 28 | + } |
| 29 | + const output: Record<string, Array<MetricSummary>> = {}; |
| 30 | + |
| 31 | + for (const [, [exportKey, summary]] of storage) { |
| 32 | + if (!output[exportKey]) { |
| 33 | + output[exportKey] = []; |
| 34 | + } |
| 35 | + |
| 36 | + output[exportKey].push(dropUndefinedKeys(summary)); |
| 37 | + } |
| 38 | + |
| 39 | + return output; |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * Updates the metric summary on the currently active span |
| 44 | + */ |
| 45 | +export function updateMetricSummaryOnActiveSpan( |
| 46 | + metricType: MetricType, |
| 47 | + sanitizedName: string, |
| 48 | + value: number, |
| 49 | + unit: MeasurementUnit, |
| 50 | + tags: Record<string, Primitive>, |
| 51 | + bucketKey: string, |
| 52 | +): void { |
| 53 | + const span = getActiveSpan(); |
| 54 | + if (span) { |
| 55 | + const storage = getMetricStorageForSpan(span) || new Map<string, [string, MetricSummary]>(); |
| 56 | + |
| 57 | + const exportKey = `${metricType}:${sanitizedName}@${unit}`; |
| 58 | + const bucketItem = storage.get(bucketKey); |
| 59 | + |
| 60 | + if (bucketItem) { |
| 61 | + const [, summary] = bucketItem; |
| 62 | + storage.set(bucketKey, [ |
| 63 | + exportKey, |
| 64 | + { |
| 65 | + min: Math.min(summary.min, value), |
| 66 | + max: Math.max(summary.max, value), |
| 67 | + count: (summary.count += 1), |
| 68 | + sum: (summary.sum += value), |
| 69 | + tags: summary.tags, |
| 70 | + }, |
| 71 | + ]); |
| 72 | + } else { |
| 73 | + storage.set(bucketKey, [ |
| 74 | + exportKey, |
| 75 | + { |
| 76 | + min: value, |
| 77 | + max: value, |
| 78 | + count: 1, |
| 79 | + sum: value, |
| 80 | + tags, |
| 81 | + }, |
| 82 | + ]); |
| 83 | + } |
| 84 | + |
| 85 | + if (!SPAN_METRIC_SUMMARY) { |
| 86 | + SPAN_METRIC_SUMMARY = new WeakMap(); |
| 87 | + } |
| 88 | + |
| 89 | + SPAN_METRIC_SUMMARY.set(span, storage); |
| 90 | + } |
| 91 | +} |
0 commit comments