@@ -10,17 +10,17 @@ import type { MetricType } from './types';
10
10
*/
11
11
type MetricSummaryStorage = Map < string , [ string , MetricSummary ] > ;
12
12
13
- let SPAN_METRIC_SUMMARY : WeakMap < Span , MetricSummaryStorage > | undefined ;
13
+ const METRICS_SPAN_FIELD = '_sentryMetrics' ;
14
14
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
+ } ;
18
18
19
19
/**
20
20
* Fetches the metric summary if it exists for the passed span
21
21
*/
22
22
export function getMetricSummaryJsonForSpan ( span : Span ) : Record < string , Array < MetricSummary > > | undefined {
23
- const storage = getMetricStorageForSpan ( span ) ;
23
+ const storage = ( span as SpanWithPotentialMetrics ) [ METRICS_SPAN_FIELD ] ;
24
24
25
25
if ( ! storage ) {
26
26
return undefined ;
@@ -50,7 +50,10 @@ export function updateMetricSummaryOnSpan(
50
50
tags : Record < string , Primitive > ,
51
51
bucketKey : string ,
52
52
) : 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 ] > ( ) ) ;
54
57
55
58
const exportKey = `${ metricType } :${ sanitizedName } @${ unit } ` ;
56
59
const bucketItem = storage . get ( bucketKey ) ;
@@ -79,10 +82,4 @@ export function updateMetricSummaryOnSpan(
79
82
} ,
80
83
] ) ;
81
84
}
82
-
83
- if ( ! SPAN_METRIC_SUMMARY ) {
84
- SPAN_METRIC_SUMMARY = new WeakMap ( ) ;
85
- }
86
-
87
- SPAN_METRIC_SUMMARY . set ( span , storage ) ;
88
85
}
0 commit comments