@@ -99,24 +99,31 @@ function validateAndProcessSampleRate(metric: Metric, client: Client): boolean {
9999 *
100100 * @param metric - The metric containing the sample_rate.
101101 * @param scope - The scope containing the sampleRand.
102- * @returns true if the metric should be sampled, false if it should be dropped .
102+ * @returns An object with sampled (boolean) and samplingPerformed (boolean) flags .
103103 */
104- function shouldSampleMetric ( metric : Metric , scope : Scope ) : boolean {
104+ function shouldSampleMetric ( metric : Metric , scope : Scope ) : { sampled : boolean ; samplingPerformed : boolean } {
105105 if ( metric . sample_rate === undefined ) {
106- return true ;
106+ return { sampled : true , samplingPerformed : false } ;
107107 }
108- const sampleRand = scope . getPropagationContext ( ) . sampleRand ;
109- return sampleRand < metric . sample_rate ;
108+
109+ const propagationContext = scope . getPropagationContext ( ) ;
110+ if ( ! propagationContext || propagationContext . sampleRand === undefined ) {
111+ return { sampled : true , samplingPerformed : false } ;
112+ }
113+
114+ const sampleRand = propagationContext . sampleRand ;
115+ return { sampled : sampleRand < metric . sample_rate , samplingPerformed : true } ;
110116}
111117
112118/**
113- * Adds the sample_rate attribute to the metric attributes if needed .
119+ * Adds the sample_rate attribute to the metric attributes if sampling was actually performed .
114120 *
115121 * @param metric - The metric containing the sample_rate.
116122 * @param attributes - The attributes object to modify.
123+ * @param samplingPerformed - Whether sampling was actually performed.
117124 */
118- function addSampleRateAttribute ( metric : Metric , attributes : Record < string , unknown > ) : void {
119- if ( metric . sample_rate !== undefined && metric . sample_rate !== 1.0 ) {
125+ function addSampleRateAttribute ( metric : Metric , attributes : Record < string , unknown > , samplingPerformed : boolean ) : void {
126+ if ( metric . sample_rate !== undefined && metric . sample_rate !== 1.0 && samplingPerformed ) {
120127 setMetricAttribute ( attributes , 'sentry.client_sample_rate' , metric . sample_rate ) ;
121128 }
122129}
@@ -127,14 +134,15 @@ function addSampleRateAttribute(metric: Metric, attributes: Record<string, unkno
127134 * @param beforeMetric - The original metric.
128135 * @param currentScope - The current scope.
129136 * @param client - The client.
137+ * @param samplingPerformed - Whether sampling was actually performed.
130138 * @returns The processed metric attributes.
131139 */
132- function processMetricAttributes ( beforeMetric : Metric , currentScope : Scope , client : Client ) : Record < string , unknown > {
140+ function processMetricAttributes ( beforeMetric : Metric , currentScope : Scope , client : Client , samplingPerformed : boolean ) : Record < string , unknown > {
133141 const processedMetricAttributes = {
134142 ...beforeMetric . attributes ,
135143 } ;
136144
137- addSampleRateAttribute ( beforeMetric , processedMetricAttributes ) ;
145+ addSampleRateAttribute ( beforeMetric , processedMetricAttributes , samplingPerformed ) ;
138146
139147 const {
140148 user : { id, email, username } ,
@@ -234,12 +242,13 @@ export function _INTERNAL_captureMetric(beforeMetric: Metric, options?: Internal
234242 return ;
235243 }
236244
237- if ( ! shouldSampleMetric ( beforeMetric , currentScope ) ) {
245+ const { sampled, samplingPerformed } = shouldSampleMetric ( beforeMetric , currentScope ) ;
246+ if ( ! sampled ) {
238247 return ;
239248 }
240249
241250 const [ , traceContext ] = _getTraceInfoFromScope ( client , currentScope ) ;
242- const processedMetricAttributes = processMetricAttributes ( beforeMetric , currentScope , client ) ;
251+ const processedMetricAttributes = processMetricAttributes ( beforeMetric , currentScope , client , samplingPerformed ) ;
243252
244253 const metric : Metric = {
245254 ...beforeMetric ,
0 commit comments