@@ -10,17 +10,17 @@ import type { MetricType } from './types';
1010 */
1111type MetricSummaryStorage = Map < string , [ string , MetricSummary ] > ;
1212
13- let SPAN_METRIC_SUMMARY : WeakMap < Span , MetricSummaryStorage > | undefined ;
13+ const METRICS_SPAN_FIELD = '_sentryMetrics' ;
1414
15- function getMetricStorageForSpan ( span : Span ) : MetricSummaryStorage | undefined {
16- return SPAN_METRIC_SUMMARY ? SPAN_METRIC_SUMMARY . get ( span ) : undefined ;
17- }
15+ type SpanWithPotentialMetrics = Span & {
16+ [ METRICS_SPAN_FIELD ] ?: MetricSummaryStorage ;
17+ } ;
1818
1919/**
2020 * Fetches the metric summary if it exists for the passed span
2121 */
2222export function getMetricSummaryJsonForSpan ( span : Span ) : Record < string , Array < MetricSummary > > | undefined {
23- const storage = getMetricStorageForSpan ( span ) ;
23+ const storage = ( span as SpanWithPotentialMetrics ) [ METRICS_SPAN_FIELD ] ;
2424
2525 if ( ! storage ) {
2626 return undefined ;
@@ -50,7 +50,10 @@ export function updateMetricSummaryOnSpan(
5050 tags : Record < string , Primitive > ,
5151 bucketKey : string ,
5252) : void {
53- const storage = getMetricStorageForSpan ( span ) || new Map < string , [ string , MetricSummary ] > ( ) ;
53+ const existingStorage = ( span as SpanWithPotentialMetrics ) [ METRICS_SPAN_FIELD ] ;
54+ const storage =
55+ existingStorage ||
56+ ( ( span as SpanWithPotentialMetrics ) [ METRICS_SPAN_FIELD ] = new Map < string , [ string , MetricSummary ] > ( ) ) ;
5457
5558 const exportKey = `${ metricType } :${ sanitizedName } @${ unit } ` ;
5659 const bucketItem = storage . get ( bucketKey ) ;
@@ -79,10 +82,4 @@ export function updateMetricSummaryOnSpan(
7982 } ,
8083 ] ) ;
8184 }
82-
83- if ( ! SPAN_METRIC_SUMMARY ) {
84- SPAN_METRIC_SUMMARY = new WeakMap ( ) ;
85- }
86-
87- SPAN_METRIC_SUMMARY . set ( span , storage ) ;
8885}
0 commit comments